diff options
author | mongo <mongo@iomega> | 2016-04-15 16:20:17 -0300 |
---|---|---|
committer | mongo <mongo@iomega> | 2016-04-15 16:20:17 -0300 |
commit | f686ba184e0af3fd37aa8a743631a7a376f30843 (patch) | |
tree | e9a48dc691511a2961f93163944ba0ca1a84e5b3 /src/exec.c | |
parent | c0a088d7a4bc61e6e69fa5bd8964c39f68507c71 (diff) |
Renamed src.scim2 to src
Diffstat (limited to 'src/exec.c')
-rwxr-xr-x | src/exec.c | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/src/exec.c b/src/exec.c new file mode 100755 index 0000000..67664eb --- /dev/null +++ b/src/exec.c @@ -0,0 +1,77 @@ +#include <stdio.h> +#include <stdlib.h> +#include <sys/types.h> +#include <unistd.h> +#include <string.h> +#include <sys/wait.h> // for wait + +#include "macros.h" +#include "conf.h" +#include "color.h" +#include "utils/string.h" +#include "screen.h" +#include "sc.h" + +int exec_cmd (char * line) { + int waitres; + + def_prog_mode(); + endwin(); + + int my_pipe[2]; + if (pipe(my_pipe) == -1) { + sc_error("Error creating pipe"); + getchar(); + reset_prog_mode(); + refresh(); + update(TRUE); + return -1; + } + + pid_t child_id = fork(); + if (child_id == -1) { + sc_error("Fork error"); + getchar(); + reset_prog_mode(); + refresh(); + update(TRUE); + return -1; + } + + if (child_id == 0) { // we are in the child process + close(my_pipe[0]); // child doesn't read + dup2(my_pipe[1], 1); // redirect stdout + + char * l = line; + int j; + l = rtrim(ltrim(line, ' '), ' '); + char ** param = split(l, ' ', 1); + execvp(param[0], param); + + for (j=0; param[j]; j++) { + free(param[j]); + } + free(param); + param = NULL; + + printf("Error executing command. "); + exit(-1); + + } else { // we are in parent process + close(my_pipe[1]); // parent doesn't write + char reading_buf[2]; + + while (read(my_pipe[0], reading_buf, 1) > 0) + write(1, reading_buf, 1); + + close(my_pipe[0]); + wait(&waitres); + system("echo -n 'Press ENTER to return.'"); + + getchar(); + reset_prog_mode(); + refresh(); + update(TRUE); + } + return 0; +} |