-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathexternal.c
107 lines (92 loc) · 3.22 KB
/
external.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include "shell.h"
#include "utils.h"
#include "external.h"
#include "prompt.h"
#include "process.h"
#include "signals.h"
void handle_external(char* command, int argc, char** argv) {
// make a new argv array with the command as the first string
// and a null pointer at the end
char** new_argv = (char**)malloc(sizeof(char*) * (argc + 2));
new_argv[0] = command; // Not copying strings, just the pointers
bool bg = false;
if (argc > 0 && strcmp(argv[argc - 1], "&") == 0) {
bg = true;
argc--;
}
int new_argc = 1;
for (int i = 0; i < argc; i++) {
new_argv[new_argc] = argv[i];
new_argc++;
}
new_argv[new_argc] = NULL;
not_kishmish(new_argc, new_argv, bg);
free(new_argv); // no need to free individual strings.
}
bool not_kishmish(int argc, char** argv, bool bg) {
if (!bg) {
// FOREGROUND: PARENT WAITS
pid_t pid = fork();
if (pid < 0) {
// IN PARENT, CHILD DIDN'T GET CREATED
perror("Could not execute command");
return false;
} else if (pid > 0) {
// IN PARENT, WAIT FOR CHILD TO COME BACK
FG_CHILD_PID = pid;
char* temp = get_full_command("", argc, argv);
strcpy(FG_CHILD_PNAME, temp);
free(temp);
signal(SIGTTIN, SIG_IGN);
signal(SIGTTOU, SIG_IGN);
// TODO: Error handling here
setpgid(pid, 0);
tcsetpgrp(STDIN_FILENO, pid);
int w_st;
waitpid(pid, &w_st, WUNTRACED);
tcsetpgrp(STDIN_FILENO, getpgrp());
signal(SIGTTIN, SIG_DFL);
signal(SIGTTOU, SIG_DFL);
if (WIFSTOPPED(w_st)) {
// IT WAS STOPPED, NOT TERMINATED
int child_id = store_process(FG_CHILD_PID, FG_CHILD_PNAME);
printf("[%d] %s %d suspended\n", child_id, FG_CHILD_PNAME, FG_CHILD_PID);
}
FG_CHILD_PID = -1;
FG_CHILD_PNAME[0] = '\0';
return (WIFEXITED(w_st) && WEXITSTATUS(w_st) == EXIT_SUCCESS);
} else {
// IN CHILD
setpgid(0, 0);
if (execvp(argv[0], argv) < 0) {
perror("Could not execute command");
exit(EXIT_FAILURE); // kill child if can't execute command
}
}
} else {
// BACKGROUND: PARENT KEEPS GOING
pid_t pid = fork();
if (pid < 0) {
// IN PARENT, CHILD DIDN'T GET CREATED
perror("Could not execute command");
return false;
} else if (pid > 0) {
// IN PARENT, DON'T WAIT
char* temp = get_full_command("", argc, argv);
int id = store_process(pid, temp);
free(temp);
printf("[%d] %d\n", id, pid);
setpgid(pid, 0);
tcsetpgrp(STDIN_FILENO, getpgrp());
return true;
} else {
// IN CHILD
setpgid(0, 0);
// signal(SIGTTIN, child_stop);
if (execvp(argv[0], argv) < 0) {
perror("Could not execute command");
exit(EXIT_FAILURE); // kill child if can't execute command
}
}
}
}