diff options
author | Charles Forsyth <charles.forsyth@gmail.com> | 2017-04-15 15:36:53 +0100 |
---|---|---|
committer | Charles Forsyth <charles.forsyth@gmail.com> | 2017-04-15 15:36:53 +0100 |
commit | a87b37fb7a99f66068cb7dbe7e1aaa0343ef4ded (patch) | |
tree | 88d2257b888df2fa246d35d0b9f203b24aea5ab7 | |
parent | 9f87c1613c0385e8264a2e5eca8f503cc446fbf1 (diff) |
Windows' system() library function can malfunction with "Access Denied" when crossing 32/64 barrier, so define our own
-rw-r--r-- | utils/awk/missing95.c | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/utils/awk/missing95.c b/utils/awk/missing95.c index 64138ade..4daa8d2e 100644 --- a/utils/awk/missing95.c +++ b/utils/awk/missing95.c @@ -10,3 +10,45 @@ FILE *popen(char *s, char *m) { int pclose(FILE *f) { return _pclose(f); /* return NULL; */ } + +#include <windows.h> +#include <winbase.h> +#include <winsock.h> + +/* system doesn't work properly in some 32/64 (WoW64) combinations */ +int system(char *s) { + int status, n; + PROCESS_INFORMATION pinfo; + STARTUPINFO si; + char *cmd; + char app[256]; + static char cmdexe[] = "\\cmd.exe"; + + memset(&si, 0, sizeof(si)); + si.cb = sizeof(si); +// si.dwFlags = STARTF_USESHOWWINDOW; +// si.wShowWindow = SW_SHOW; + + n = GetSystemDirectory(app, sizeof(app)-sizeof(cmdexe)); + if(n > sizeof(app)) + return -1; + strcat_s(app, sizeof(app), cmdexe); + n = strlen(s)+20; + cmd = malloc(n); + if(cmd == NULL) + return -1; + strcpy_s(cmd, n, "cmd.exe /c"); + strcat_s(cmd, n, s); + if(!CreateProcess(app, cmd, NULL, NULL, TRUE, CREATE_DEFAULT_ERROR_MODE, NULL/* env*/, NULL /*wdir*/, &si, &pinfo)){ + fprintf(stderr, "can't create process %s %d\n", s, GetLastError()); + free(cmd); + return -1; + } + free(cmd); + if(WaitForSingleObject(pinfo.hProcess, INFINITE) == WAIT_FAILED) + return -1; + if(!GetExitCodeProcess(pinfo.hProcess, &status)) + status = 1; + //fprintf(stderr, "status %d\n", status); + return status; +} |