blob: b9d0f7e4eb9f57c8e97c8d3321328f23202bad7f (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
|
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>
#include <sys/wait.h> // for wait
#include <errno.h>
#include "macros.h"
#include "conf.h"
#include "color.h"
#include "utils/string.h"
#include "tui.h"
#include "sc.h"
#include "main.h" // exit_app
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
int argc = 1;
char * p;
for (p = line; *p; p++)
argc += (*p == ' ');
char ** argv = calloc(argc+1, sizeof(char*));
int i;
for (i = 0; i < argc; i++)
argv[i] = strsep(&line, " ");
execvp(argv[0], argv);
free(argv);
sc_error("Error executing command. ");
exit_app(-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) {
if (!write(1, reading_buf, 1)) {
sc_error("Failed to read command output: %s", strerror(errno));
exit_app(-1);
}
}
close(my_pipe[0]);
wait(&waitres);
if (system("echo -n 'Press ENTER to return.'") == -1) {
sc_error("Failed to show ENTER prompt: %s", strerror(errno));
exit_app(-1);
}
getchar();
reset_prog_mode();
refresh();
update(TRUE);
}
return 0;
}
|