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
246246 return i;
247247 }
248248
249+/*
250+ * Quote a single command line argument using the rules above.
251+ */
252+static char*
253+qarg(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+
249288 extern int main(int, char*[]);
250289
251290 int APIENTRY
@@ -272,28 +311,6 @@ WinMain(HINSTANCE x, HINSTANCE y, LPSTR z, int w) · 20 unmodified lines
272311 return 0;
273312 }
274313
275−static char*
276−qarg(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−}
297314
298315 static wchar_t*
299316 wcmdline(char **argv)
@@ -315,7 +332,7 @@ wcmdline(char **argv) · 15 unmodified lines
315332 if(i != 0)
316333 *w++ = L' ';
317334 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);
319336 free(q);
320337 }
321338 *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