Skip to content

Commit

Permalink
Workaround Microsoft stupidity
Browse files Browse the repository at this point in the history
  • Loading branch information
ojwb committed Dec 23, 2024
1 parent 5ed7dfe commit 5e514a8
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/wrapsurvexport.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,27 @@ main(int argc, char **argv)
memcpy(a, argv[0], a_len);
strcpy(a + a_len, "survexpor_.exe");
const char *real_argv0 = argv[0];
// Behind the scenes it appears Microsoft's _execv() actually takes the
// argv passed and crudely glues it together into a command line string
// with spaces in between but *WITHOUT ANY ESCAPING*, and then it gets
// split back up into arguments at spaces, so an argument containing a
// space gets split into two arguments. Coupled with the default
// installation directory path containing a space (C:\Program Files) this
// doesn't work out well. Words fail me.
//
// Apparently putting quotes around the argument is necessary.
for (int i = 0; i < argc; ++i) {
const char *arg = argv[i];
if (arg[strcspn(arg, " \t\n\r\v")]) {
// Argument contains whitespace.
char *newarg = malloc(strlen(arg) + 3);
if (!newarg) return 1;
newarg[0] = '"';
strcpy(newarg + 1, arg);
strcat(newarg + 1, "\"");
argv[i] = newarg;
}
}
_execv(a, (const char * const*)argv);
printf("%s: %s\n", real_argv0, strerror(errno));
return 1;
Expand Down

0 comments on commit 5e514a8

Please sign in to comment.