diff options
author | Joseph Hunkeler <jhunkeler@gmail.com> | 2024-05-20 10:07:16 -0400 |
---|---|---|
committer | Joseph Hunkeler <jhunkeler@gmail.com> | 2024-05-20 10:11:29 -0400 |
commit | 67975a5944706e382fe1e7b00d226715c6242358 (patch) | |
tree | e93dd37df57f49d4327567479521014f380206e5 /src/system.c | |
parent | 30c9945e0da19305ebad88a2835653ff4f409313 (diff) | |
download | stasis-67975a5944706e382fe1e7b00d226715c6242358.tar.gz |
Mass attempt at windows portability
Diffstat (limited to 'src/system.c')
-rw-r--r-- | src/system.c | 80 |
1 files changed, 80 insertions, 0 deletions
diff --git a/src/system.c b/src/system.c index ca2da97..895ed8a 100644 --- a/src/system.c +++ b/src/system.c @@ -5,6 +5,85 @@ #include "system.h" #include "omc.h" +#if defined(OMC_OS_WINDOWS) +int shell(struct Process *proc, char *args) { + char cmd[OMC_BUFSIZ]; + memset(cmd, 0, sizeof(cmd)); + strcat(cmd, args); + return system(cmd); +} +#else +int shell(struct Process *proc, char *args[]) { + FILE *fp_out, *fp_err; + pid_t pid; + pid_t status; + status = 0; + errno = 0; + + pid = fork(); + if (pid == -1) { + fprintf(stderr, "fork failed\n"); + exit(1); + } else if (pid == 0) { + int retval; + if (proc != NULL) { + if (strlen(proc->standard_output)) { + fp_out = freopen(proc->standard_output, "w+", stdout); + } + + if (strlen(proc->standard_error)) { + fp_err = freopen(proc->standard_error, "w+", stderr); + } + + if (proc->redirect_stderr) { + if (fp_err) { + fclose(fp_err); + fclose(stderr); + } + dup2(fileno(stdout), fileno(stderr)); + } + } + + retval = execv(args[0], args); + fprintf(stderr, "# executing: "); + for (size_t x = 0; args[x] != NULL; x++) { + fprintf(stderr, "%s ", args[x]); + } + + if (proc != NULL && strlen(proc->standard_output)) { + fflush(fp_out); + fclose(fp_out); + fflush(stdout); + fclose(stdout); + } + if (proc != NULL && strlen(proc->standard_error)) { + fflush(fp_err); + fclose(fp_err); + fflush(stderr); + fclose(stderr); + } + exit(retval); + } else { + if (waitpid(pid, &status, WUNTRACED) > 0) { + if (WIFEXITED(status) && WEXITSTATUS(status)) { + if (WEXITSTATUS(status) == 127) { + fprintf(stderr, "execv failed\n"); + } + } else if (WIFSIGNALED(status)) { + fprintf(stderr, "signal received: %d\n", WIFSIGNALED(status)); + } + } else { + fprintf(stderr, "waitpid() failed\n"); + } + } + + + if (proc != NULL) { + proc->returncode = status; + } + return WEXITSTATUS(status); +} + int shell(struct Process *proc, char *args) { FILE *fp_out = NULL; FILE *fp_err = NULL; @@ -96,6 +175,7 @@ int shell(struct Process *proc, char *args) { guard_free(t_name); return WEXITSTATUS(status); } +#endif int shell_safe(struct Process *proc, char *args) { FILE *fp; |