Skip to content

Commit c29f694

Browse files
author
kalibera
committed
Allow space in file name for R script (fixes 72503).
git-svn-id: https://svn.r-project.org/R/trunk@74643 00db46b3-68df-0310-9c12-caf00c1e9a41
1 parent 60ee247 commit c29f694

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

src/unix/Rscript.c

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* R : A Computer Language for Statistical Data Analysis
3-
* Copyright (C) 2006-2017 The R Core Team
3+
* Copyright (C) 2006-2018 The R Core Team
44
*
55
* This program is free software; you can redistribute it and/or modify
66
* it under the terms of the GNU General Public License as published by
@@ -132,24 +132,32 @@ int main(int argc_, char *argv_[])
132132
133133
argv_[1] is split here into individual arguments assuming any space or
134134
tab is a separator - no quoting is supported
135+
136+
This code is, however, also used with explicit invocation of Rscript
137+
where arguments are not joined and the first argument may be a file
138+
name, which is explicitly allowed to contain space. Thus, only split the
139+
first argument if it starts with "--" (a file name for Rscript cannot
140+
start with "--"; it can start with "-", but the only short option is "-e"
141+
and that is not usable with '#!' invocation).
135142
*/
136143

137144
/* compute number of arguments included in argv_[1] */
138145
char *s = argv_[1];
139146
int njoined = 0;
140147
size_t j;
141-
for(j = 0; s[j] != 0; j++)
142-
if (s[j] != ' ' && s[j] != '\t' &&
143-
(j == 0 || s[j-1] == ' ' || s[j-1] == '\t'))
144-
/* first character of an argument */
145-
njoined++;
148+
if (strncmp(s, "--", 2) == 0)
149+
for(j = 0; s[j] != 0; j++)
150+
if (s[j] != ' ' && s[j] != '\t' &&
151+
(j == 0 || s[j-1] == ' ' || s[j-1] == '\t'))
152+
/* first character of an argument */
153+
njoined++;
146154

147155
int argc;
148156
char **argv;
149157

150158
if (njoined > 1) { /* need to split argv_[1] */
151159
argc = argc_ - 1 + njoined;
152-
argv = (char **) malloc((size_t) (argc+1)*sizeof(char *));
160+
argv = (char **) malloc((size_t) (argc+1)*sizeof(char *));
153161
if (!argv) {
154162
fprintf(stderr, "malloc failure\n");
155163
exit(1);

0 commit comments

Comments
 (0)