Unified · Split
| @@ -246,6 +246,45 @@ args(char *argv[], int n, char *p) · 245 unmodified lines | ||
| 246 | 246 | return i; |
| 247 | 247 | } |
| 248 | 248 | |
| 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 | + | |
| 249 | 288 | extern int main(int, char*[]); |
| 250 | 289 | |
| 251 | 290 | int APIENTRY |
| @@ -272,28 +311,6 @@ WinMain(HINSTANCE x, HINSTANCE y, LPSTR z, int w) · 20 unmodified lines | ||
| 272 | 311 | return 0; |
| 273 | 312 | } |
| 274 | 313 | |
| 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 | −} | |
| 297 | 314 | |
| 298 | 315 | static wchar_t* |
| 299 | 316 | wcmdline(char **argv) |
| @@ -315,7 +332,7 @@ wcmdline(char **argv) · 15 unmodified lines | ||
| 315 | 332 | if(i != 0) |
| 316 | 333 | *w++ = L' '; |
| 317 | 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 | 336 | free(q); |
| 320 | 337 | } |
| 321 | 338 | *w = 0; |