The R Project SVN R

Rev

Rev 84676 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 84676 Rev 85539
Line 41... Line 41...
41
 
41
 
42
#include <trioremap.h>
42
#include <trioremap.h>
43
 
43
 
44
static char RunError[501] = "";
44
static char RunError[501] = "";
45
 
45
 
-
 
46
static Rboolean hasspace(const char *s)
-
 
47
{
-
 
48
    if (!s)
-
 
49
	return FALSE;
-
 
50
    for(;*s;s++)
-
 
51
	if (isspace(*s)) return TRUE;
-
 
52
    return FALSE;
-
 
53
} 
-
 
54
 
46
/* This might be given a command line (whole = 0) or just the
55
/* This might be given a command line (whole = 0) or just the
47
   executable (whole = 1).  In the later case the path may or may not
56
   executable (whole = 1).  In the later case the path may or may not
48
   be quoted. */
57
   be quoted. 
-
 
58
 
-
 
59
   When whole = 0, the command will be quoted in the result if it contains
-
 
60
   space. */
49
static char *expandcmd(const char *cmd, int whole)
61
static char *expandcmd(const char *cmd, int whole)
50
{
62
{
51
    char c = '\0';
63
    char c = '\0';
52
    char *s = NULL, *p, *q = NULL, *f, *dest, *src, *fn = NULL;
64
    char *s = NULL, *p, *q = NULL, *f, *dest, *src, *fn = NULL;
53
    int  ext, len = strlen(cmd)+1;
65
    int  ext, len = strlen(cmd)+1;
Line 140... Line 152...
140
 
152
 
141
    res = GetShortPathName(fn, NULL, 0);
153
    res = GetShortPathName(fn, NULL, 0);
142
    if (res > 0) {
154
    if (res > 0) {
143
	/* perform the translation again with sufficient buffer size */
155
	/* perform the translation again with sufficient buffer size */
144
	// This is the return value.
156
	// This is the return value.
145
	if (!(s = (char *) malloc(res + len))) { /* over-estimate */
157
	if (!(s = (char *) malloc(res + len + 2))) {
-
 
158
	    /* the size over-estimate, +2 in case quotes will be needed */
146
	    if (fn) free(fn);
159
	    if (fn) free(fn);
147
	    strcpy(RunError, "Insufficient memory (expandcmd)");
160
	    strcpy(RunError, "Insufficient memory (expandcmd)");
148
	    return NULL;
161
	    return NULL;
149
	}
162
	}
150
	res = GetShortPathName(fn, s, res);
163
	res = GetShortPathName(fn, s, res);
Line 152... Line 165...
152
    if (res == 0) {
165
    if (res == 0) {
153
	/* Use full name if GetShortPathName fails, i.e. due to insufficient
166
	/* Use full name if GetShortPathName fails, i.e. due to insufficient
154
	   permissions for some component of the path. */
167
	   permissions for some component of the path. */
155
	if (s) free(s);
168
	if (s) free(s);
156
	// This is the return value.
169
	// This is the return value.
157
	if (!(s = (char *) malloc(d + len))) { /* over-estimate */
170
	if (!(s = (char *) malloc(d + len + 2))) { /* over-estimate */
158
	    if (fn) free(fn);
171
	    if (fn) free(fn);
159
	    strcpy(RunError, "Insufficient memory (expandcmd)");
172
	    strcpy(RunError, "Insufficient memory (expandcmd)");
160
	    return NULL;
173
	    return NULL;
161
	}
174
	}
-
 
175
	if (!whole && hasspace(fn)) 
-
 
176
	    snprintf(s, d + 3, "\"%s\"", fn);
-
 
177
	else
162
        strncpy(s, fn, d + 1);
178
	    strncpy(s, fn, d + 1);
-
 
179
    } else if (!whole && hasspace(s)) {
-
 
180
	/* GetShortPathName succeeded but it may still have returned the
-
 
181
	   long path (so with spaces) */
-
 
182
 
-
 
183
	memmove(s + 1, s, res + 1); 
-
 
184
	s[0] = '"';
-
 
185
	s[1 + res] = '"';
-
 
186
	s[1 + res + 1] = '\0';
163
    }
187
    }
164
 
188
 
165
    /* FIXME: warn if the path contains space? */
-
 
166
    if (!whole) {
189
    if (!whole) {
167
	*q = c;         /* restore character after command */
190
	*q = c;         /* restore character after command */
168
	strcat(s, q);   /* add the rest of input (usually arguments) */
191
	strcat(s, q);   /* add the rest of input (usually arguments) */
169
    }
192
    }
170
    if (fn) free(fn);
193
    if (fn) free(fn);