blob: 62df8df2b38242d2811c5903b0ada01a4a92e29c (
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
|
#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 "color.h"
#include "utils/string.h"
#include "screen.h"
int exec_cmd (char * line) {
int waitres;
def_prog_mode();
endwin();
int my_pipe[2];
if (pipe(my_pipe) == -1) {
error("Error creating pipe");
getchar();
reset_prog_mode();
refresh();
update();
return -1;
}
pid_t child_id = fork();
if (child_id == -1) {
error("Fork error");
getchar();
reset_prog_mode();
refresh();
update();
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;
l = rtrim(ltrim(line, ' '), ' ');
char ** param = split(l, ' ', 1);
execvp(param[0], param);
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();
}
return 0;
}
|