summaryrefslogtreecommitdiffstats
path: root/02_exercise/shell.c
diff options
context:
space:
mode:
Diffstat (limited to '02_exercise/shell.c')
-rw-r--r--02_exercise/shell.c56
1 files changed, 2 insertions, 54 deletions
diff --git a/02_exercise/shell.c b/02_exercise/shell.c
index e21957d..f4b6738 100644
--- a/02_exercise/shell.c
+++ b/02_exercise/shell.c
@@ -9,19 +9,13 @@
#include "array.h"
#include "process.h"
#include "prompt_utils.h"
+#include "builtins.h"
-void handle(int sig) {
- /* Do nothing. */
-}
int main(void) {
setvbuf(stderr, NULL, _IONBF, 0);
setvbuf(stdout, NULL, _IONBF, 0);
- struct sigaction handler;
- handler.sa_handler = handle;
- sigemptyset(&handler.sa_mask);
- handler.sa_flags = 0;
printf("Welcome! Available built-ins are:\n"
"cd: `cd <path>` - if no path is given, return to the current dir\n"
@@ -85,53 +79,7 @@ int main(void) {
} else if (strcmp(processes[0].argv[0], "exit") == 0) {
done = true;
} else if (strcmp(processes[0].argv[0], "wait") == 0) {
- bool error = false;
- pid_t *pids;
- arrayInit(pids);
- size_t i = 1;
- while (!error && processes[0].argv[i] != NULL) {
- char *end;
- pid_t tmp = strtol(processes[0].argv[i], &end, 10);
- if (*end != '\0') {
- fprintf(stderr, "Not a valid pid: %s", processes[0].argv[i]);
- error = true;
- break;
- }
- arrayPush(pids) = tmp;
- ++i;
- }
-
- if (!error) {
- while (!arrayIsEmpty(pids)) {
- int status;
- pid_t current_pid = arrayPop(pids);
- printf("Waiting for %ld...\n", (long) current_pid);
- // install signal handler without SA_RESTART, so waitpid gets interrupted
- sigaction(SIGINT, &handler, NULL);
- if (waitpid(current_pid, &status, WUNTRACED) < 0) {
- if (EINTR == errno) {
- // cancelled by ctrl-c
- } else {
- perror("Could not wait on process");
- }
- } else {
- printf("[%i] TERMINATED\n", current_pid);
- if (WIFEXITED(status)) {
- printf("[%i] exited normally with status: %i\n", current_pid, WEXITSTATUS(status));
- } else if (WIFSTOPPED(status)) {
- printf("[%i] was stopped by signal: %s\n", current_pid, strsignal(WSTOPSIG(status)));
- } else if (WIFSIGNALED(status)) {
- printf("[%i] was terminated by signal: %s\n", current_pid, strsignal(WTERMSIG(status)));
- } else {
- printf("[%i] exited (not sure why), exit status: %i\n", current_pid, WEXITSTATUS(status));
- }
- }
- signal(SIGINT, SIG_DFL);
- }
- }
-
- arrayRelease(pids);
-
+ builtin_wait(processes[0]);
} else {
for (size_t i = 0; i < arrayLen(processes); ++i) {
int ret = exec_command(processes[i]);