diff options
author | dec05eba <dec05eba@protonmail.com> | 2019-08-02 20:08:27 +0200 |
---|---|---|
committer | dec05eba <dec05eba@protonmail.com> | 2019-08-02 20:08:27 +0200 |
commit | 65cf7681a04f2511db8c7829e9828b53a6676c88 (patch) | |
tree | 6e7db509cf2bce4a10fa6d8bc89a9eae88d893fa /src/Program.c | |
parent | 109c797d7c89223c9eed07dc322448b5c5fe3373 (diff) |
Convert to sfml, starting on manganelo and youtube
Diffstat (limited to 'src/Program.c')
-rw-r--r-- | src/Program.c | 38 |
1 files changed, 24 insertions, 14 deletions
diff --git a/src/Program.c b/src/Program.c index 526f64b..39957ed 100644 --- a/src/Program.c +++ b/src/Program.c @@ -8,7 +8,7 @@ #define READ_END 0 #define WRITE_END 1 -int exec_program(char **args, ProgramOutputCallback output_callback, void *userdata) { +int exec_program(const char **args, ProgramOutputCallback output_callback, void *userdata) { /* 1 arguments */ if(args[0] == NULL) return -1; @@ -28,24 +28,15 @@ int exec_program(char **args, ProgramOutputCallback output_callback, void *userd close(fd[READ_END]); close(fd[WRITE_END]); - execv(args[0], args); + execvp(args[0], args); return 0; } else { /* parent */ close(fd[WRITE_END]); + int result = 0; int status; - waitpid(pid, &status, 0); - if(!WIFEXITED(status)) - return -4; - - int exit_status = WEXITSTATUS(status); - if(exit_status != 0) { - fprintf(stderr, "Failed to execute program, exit status %d\n", exit_status); - return -exit_status; - } char buffer[2048]; - int result = 0; for(;;) { ssize_t bytes_read = read(fd[READ_END], buffer, sizeof(buffer)); @@ -55,14 +46,33 @@ int exec_program(char **args, ProgramOutputCallback output_callback, void *userd int err = errno; fprintf(stderr, "Failed to read from pipe to program %s, error: %s\n", args[0], strerror(err)); result = -err; - break; + goto cleanup; } if(output_callback(buffer, bytes_read, userdata) != 0) break; } + if(waitpid(pid, &status, WUNTRACED) == -1) { + perror("waitpid failed"); + result = -5; + goto cleanup; + } + + if(!WIFEXITED(status)) { + result = -4; + goto cleanup; + } + + int exit_status = WEXITSTATUS(status); + if(exit_status != 0) { + fprintf(stderr, "Failed to execute program, exit status %d\n", exit_status); + result = -exit_status; + goto cleanup; + } + + cleanup: close(fd[READ_END]); return result; } -}
\ No newline at end of file +} |