From 1dd53ce54c2008e3a11a636a496853cf6f9a5d65 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Fri, 24 Jan 2020 09:11:53 +0100 Subject: Convert hash map to gc, implement more instructions and call command --- src/command.c | 90 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 src/command.c (limited to 'src/command.c') diff --git a/src/command.c b/src/command.c new file mode 100644 index 0000000..c413502 --- /dev/null +++ b/src/command.c @@ -0,0 +1,90 @@ +#include "../include/command.h" +#include +#include +#include +#include +#include +#include + +#define READ_END 0 +#define WRITE_END 1 + +int tsl_command_exec(char **args, ProgramOutputCallback output_callback, void *userdata) { + int fd[2]; + pid_t pid; + + /* 1 arguments */ + if(args[0] == NULL) + return -1; + + if(pipe(fd) == -1) { + perror("Failed to open pipe"); + return -2; + } + + pid = fork(); + if(pid == -1) { + perror("Failed to fork"); + return -3; + } else if(pid == 0) { /* child */ + dup2(fd[WRITE_END], STDOUT_FILENO); + close(fd[READ_END]); + close(fd[WRITE_END]); + + execvp(args[0], args); + return 0; + } else { /* parent */ + int result = 0; + int status; + char buffer[2048]; + int exit_status; + + close(fd[WRITE_END]); + for(;;) { + ssize_t bytes_read = read(fd[READ_END], buffer, sizeof(buffer) - 1); + if(bytes_read == 0) { + break; + } else if(bytes_read == -1) { + int err = errno; + fprintf(stderr, "Failed to read from pipe to program %s, error: %s\n", args[0], strerror(err)); + result = -err; + goto cleanup; + } + + buffer[bytes_read] = '\0'; + if(output_callback && !output_callback(buffer, bytes_read, userdata)) + break; + } + + if(waitpid(pid, &status, 0) == -1) { + perror("waitpid failed"); + result = -5; + goto cleanup; + } + + if(!WIFEXITED(status)) { + result = -4; + goto cleanup; + } + + exit_status = WEXITSTATUS(status); + if(exit_status != 0) { + char **arg = args; + fprintf(stderr, "Failed to execute program ("); + + while(*arg) { + if(arg != args) + fputc(' ', stderr); + fprintf(stderr, "'%s'", *arg); + ++arg; + } + fprintf(stderr, "), exit status %d\n", exit_status); + result = -exit_status; + goto cleanup; + } + + cleanup: + close(fd[READ_END]); + return result; + } +} -- cgit v1.2.3