blob: 67664eb63aa220d353c3cce015ecc38eda86a57c (
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
|
#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;
}
|