diff options
author | Simon Howard | 2009-11-21 16:36:46 +0000 |
---|---|---|
committer | Simon Howard | 2009-11-21 16:36:46 +0000 |
commit | afa0c4c7979c1508605d0e79487f299cd03adda6 (patch) | |
tree | 7df1745badf169e4490e3a56a89a28ac37977c14 /src/setup/execute.c | |
parent | 095bc1110b3c11fbf2e17bfd51bf78909fe34662 (diff) | |
parent | 2c6e7b2f10c32ca0406ca6753e7701d83e6dea8f (diff) | |
download | chocolate-doom-afa0c4c7979c1508605d0e79487f299cd03adda6.tar.gz chocolate-doom-afa0c4c7979c1508605d0e79487f299cd03adda6.tar.bz2 chocolate-doom-afa0c4c7979c1508605d0e79487f299cd03adda6.zip |
Merge from trunk.
Subversion-branch: /branches/raven-branch
Subversion-revision: 1737
Diffstat (limited to 'src/setup/execute.c')
-rw-r--r-- | src/setup/execute.c | 123 |
1 files changed, 72 insertions, 51 deletions
diff --git a/src/setup/execute.c b/src/setup/execute.c index 18a07156..be7214f5 100644 --- a/src/setup/execute.c +++ b/src/setup/execute.c @@ -159,81 +159,69 @@ static unsigned int WaitForProcessExit(HANDLE subprocess) } } -static wchar_t *GetFullExePath(const char *program) +static void ConcatWCString(wchar_t *buf, const char *value) +{ + MultiByteToWideChar(CP_OEMCP, 0, + value, strlen(value) + 1, + buf + wcslen(buf), strlen(value) + 1); +} + +// Build the command line string, a wide character string of the form: +// +// "program" "arg" + +static wchar_t *BuildCommandLine(const char *program, const char *arg) { wchar_t *result; - unsigned int path_len; char *sep; - // Find the full path to the EXE to execute, by taking the path - // to this program and concatenating the EXE name: + result = calloc(strlen(myargv[0]) + strlen(program) + strlen(arg) + 6, + sizeof(wchar_t)); + + wcscpy(result, L"\""); sep = strrchr(myargv[0], DIR_SEPARATOR); - if (sep == NULL) - { - path_len = 0; - result = calloc(strlen(program) + 1, sizeof(wchar_t)); - } - else + if (sep != NULL) { - path_len = sep - myargv[0] + 1; - - result = calloc(path_len + strlen(program) + 1, - sizeof(wchar_t)); - MultiByteToWideChar(CP_OEMCP, 0, - myargv[0], path_len, - result, path_len); - } - - MultiByteToWideChar(CP_OEMCP, 0, - program, strlen(program) + 1, - result + path_len, strlen(program) + 1); - - return result; -} + ConcatWCString(result, myargv[0]); -// Convert command line argument to wchar_t string and add surrounding -// "" quotes: + // Cut off the string after the last directory separator, + // before appending the actual program. -static wchar_t *GetPaddedWideArg(const char *arg) -{ - wchar_t *result; - unsigned int len = strlen(arg); + result[sep - myargv[0] + 2] = '\0'; + + } - // Convert the command line arg to a wide char string: + ConcatWCString(result, program); - result = calloc(len + 3, sizeof(wchar_t)); - MultiByteToWideChar(CP_OEMCP, 0, - arg, len + 1, - result + 1, len + 1); + wcscat(result, L"\" \""); - // Surrounding quotes: + ConcatWCString(result, arg); - result[0] = '"'; - result[len + 1] = '"'; - result[len + 2] = 0; + wcscat(result, L"\""); return result; } static int ExecuteCommand(const char *program, const char *arg) { + STARTUPINFOW startup_info; PROCESS_INFORMATION proc_info; - wchar_t *exe_path; - wchar_t *warg; + wchar_t *command; int result = 0; - exe_path = GetFullExePath(program); - warg = GetPaddedWideArg(arg); + command = BuildCommandLine(program, arg); // Invoke the program: memset(&proc_info, 0, sizeof(proc_info)); + memset(&startup_info, 0, sizeof(startup_info)); + startup_info.cb = sizeof(startup_info); - if (!CreateProcessW(exe_path, warg, - NULL, NULL, FALSE, 0, NULL, NULL, NULL, - &proc_info)) + if (!CreateProcessW(NULL, command, + NULL, NULL, FALSE, 0, NULL, NULL, + &startup_info, &proc_info)) { result = -1; } @@ -247,19 +235,48 @@ static int ExecuteCommand(const char *program, const char *arg) CloseHandle(proc_info.hThread); } - free(exe_path); - free(warg); + free(command); return result; } #else +// Given the specified program name, get the full path to the program, +// assuming that it is in the same directory as this program is. + +static char *GetFullExePath(const char *program) +{ + char *result; + char *sep; + unsigned int path_len; + + sep = strrchr(myargv[0], DIR_SEPARATOR); + + if (sep == NULL) + { + result = strdup(program); + } + else + { + path_len = sep - myargv[0] + 1; + + result = malloc(strlen(program) + path_len + 1); + + strncpy(result, myargv[0], path_len); + result[path_len] = '\0'; + + strcat(result, program); + } + + return result; +} + static int ExecuteCommand(const char *program, const char *arg) { pid_t childpid; int result; - const char *argv[] = { program, arg, NULL }; + const char *argv[3]; childpid = fork(); @@ -267,7 +284,11 @@ static int ExecuteCommand(const char *program, const char *arg) { // This is the child. Execute the command. - execv(argv[0], (char **) argv); + argv[0] = GetFullExePath(program); + argv[1] = arg; + argv[2] = NULL; + + execvp(argv[0], (char **) argv); exit(-1); } |