| 1 |
#include <stdio.h> |
| 2 |
#include <stdlib.h> |
| 3 |
#include <string.h> |
| 4 |
|
| 5 |
#if defined(_MSC_VER) && _MSC_VER < 1900 |
| 6 |
# include <stdarg.h> |
| 7 |
static int snprintf(char* buffer, size_t count, char const* format, ...) |
| 8 |
{ |
| 9 |
int n; |
| 10 |
va_list argptr; |
| 11 |
va_start(argptr, format); |
| 12 |
n = _vscprintf(format, argptr); |
| 13 |
vsnprintf_s(buffer, count, _TRUNCATE, format, argptr); |
| 14 |
va_end(argptr); |
| 15 |
return n; |
| 16 |
} |
| 17 |
#endif |
| 18 |
|
| 19 |
static int launch(int argc, char const* argv[]) |
| 20 |
{ |
| 21 |
char cmd[4096]; |
| 22 |
size_t len = 0; |
| 23 |
char const* sep = ""; |
| 24 |
int i; |
| 25 |
int n; |
| 26 |
#ifdef _WIN32 |
| 27 |
n = snprintf(cmd + len, sizeof(cmd) - len, "cmd /C \""); |
| 28 |
if (n < 0) { |
| 29 |
return 1; |
| 30 |
} |
| 31 |
len += n; |
| 32 |
#endif |
| 33 |
for (i = 0; i < argc; ++i) { |
| 34 |
n = snprintf(cmd + len, sizeof(cmd) - len, "%s\"%s\"", sep, argv[i]); |
| 35 |
if (n < 0) { |
| 36 |
return 1; |
| 37 |
} |
| 38 |
len += n; |
| 39 |
if (len >= sizeof(cmd)) { |
| 40 |
fprintf(stderr, "error: command too long\n"); |
| 41 |
return 1; |
| 42 |
} |
| 43 |
sep = " "; |
| 44 |
} |
| 45 |
#ifdef _WIN32 |
| 46 |
printf("launching: %s\n", cmd + 8); |
| 47 |
n = snprintf(cmd + len, sizeof(cmd) - len, "\""); |
| 48 |
if (n < 1) { |
| 49 |
return 1; |
| 50 |
} |
| 51 |
#else |
| 52 |
printf("launching: %s\n", cmd); |
| 53 |
#endif |
| 54 |
fflush(stdout); |
| 55 |
return system(cmd); |
| 56 |
} |
| 57 |
|
| 58 |
int main(int argc, char const* argv[]) |
| 59 |
{ |
| 60 |
int ownArgs = 1; |
| 61 |
int i; |
| 62 |
for (i = 0; i < argc; ++i) { |
| 63 |
printf("test_launcher: got arg %d '%s'\n", i, argv[i]); |
| 64 |
if (ownArgs && strcmp(argv[i], "--") == 0) { |
| 65 |
ownArgs = 0; |
| 66 |
} else if (!ownArgs) { |
| 67 |
return launch(argc - i, argv + i); |
| 68 |
} |
| 69 |
} |
| 70 |
return 1; |
| 71 |
} |