-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
67 lines (51 loc) · 1.05 KB
/
main.c
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
#include <stdlib.h>
#include <assert.h>
#include <unistd.h>
#include <signal.h>
#include "common.h"
static pid_t current_pid = -1;
static int wait_proc = 1;
static void signal_handler(int signo)
{
if (current_pid != -1) {
assert(kill(current_pid, SIGINT) != -1);
//printf("pid %d killed\n", current_pid);
current_pid = -1;
}
}
int main(int argc, char *argv[])
{
signal(SIGINT, &signal_handler);
while (1) {
char *buf = NULL;
printf(">>> ");
ssize_t len = safe_getline(&buf);
if (len < 1) {
safe_free(buf);
continue;
}
if (strcmp(buf, "exit") == 0)
_exit(EXIT_SUCCESS);
if (buf[len - 1] == '&') {
buf[len - 1] = '\0';
wait_proc = 0;
} else {
wait_proc = 1;
}
char **user_argv = split_string(buf);
current_pid = safe_fork();
if (current_pid == 0) {
if (execvp(user_argv[0], user_argv) == -1)
puts("Error with execute this arguments");
_exit(EXIT_SUCCESS);
} else {
if (wait_proc) {
safe_wait();
current_pid = -1;
}
}
safe_free(user_argv);
safe_free(buf);
}
return EXIT_SUCCESS;
}