History

cmd(3): fix command argument quoting for win32

cinap_lenrek committed 6 years ago

Commit 788f6541 parent 580a5141 file · +40 −23Browse files

Unified · Split

kern/win32.c+40−23
@@ -246,6 +246,45 @@ args(char *argv[], int n, char *p) · 245 unmodified lines
246 return i;246 return i;
247}247}
248 248
249/*
250 * Quote a single command line argument using the rules above.
251 */
252static char*
253qarg(char *s)
254{
255 char *d, *p;
256 int n, c;
257
258 n = strlen(s);
259 d = smalloc(3+2*n);
260 for(p = s; (c = *p) != 0; p++)
261 if(strchr(" \t\n\r\"", c) != nil)
262 break;
263 if(c == 0){
264 memmove(d, s, n+1);
265 return d;
266 }
267 p = d;
268 *p++ = '"';
269 for(;;){
270 for(n = 0; (c = *s++) == '\\'; n++)
271 *p++ = c;
272 if(c == 0){
273 while(n-- > 0)
274 *p++ = '\\';
275 break;
276 }
277 if(c == '"'){
278 while(n-- >= 0)
279 *p++ = '\\';
280 }
281 *p++ = c;
282 }
283 *p++ = '"';
284 *p = 0;
285 return d;
286}
287
249extern int main(int, char*[]);288extern int main(int, char*[]);
250 289
251int APIENTRY290int APIENTRY
@@ -272,28 +311,6 @@ WinMain(HINSTANCE x, HINSTANCE y, LPSTR z, int w) · 20 unmodified lines
272 return 0;311 return 0;
273}312}
274 313
275static char*
276qarg(char *s)
277{
278 char *d, *p;
279 int n, c;
280
281 n = strlen(s);
282 d = p = smalloc(3+2*n);
283 if(s[0] == '"' || (strchr(s, ' ') == nil && strchr(s, '\t') == nil)){
284 memmove(d, s, n+1);
285 return d;
286 }
287 *p++ = '"';
288 while((c = *s++) != 0){
289 if(c == '\\' || c == '"')
290 *p++ = '\\';
291 *p++ = c;
292 }
293 *p++ = '"';
294 *p = 0;
295 return d;
296}
297 314
298static wchar_t*315static wchar_t*
299wcmdline(char **argv)316wcmdline(char **argv)
@@ -315,7 +332,7 @@ wcmdline(char **argv) · 15 unmodified lines
315 if(i != 0)332 if(i != 0)
316 *w++ = L' ';333 *w++ = L' ';
317 q = qarg(argv[i]);334 q = qarg(argv[i]);
318 w += MultiByteToWideChar(CP_UTF8, 0, q, strlen(argv[i]), w, e - w);335 w += MultiByteToWideChar(CP_UTF8, 0, q, strlen(q), w, e - w);
319 free(q);336 free(q);
320 }337 }
321 *w = 0;338 *w = 0;

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t