-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbuiltin.c
64 lines (62 loc) · 1.7 KB
/
builtin.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
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <signal.h>
#include "common.h"
#include "extern.h"
/* Checks job->arg_stringss for builtin functions and executes them. */
int check_builtin() {
if (strcmp(cur_job->arg_strings[0], "cd") == 0 && cur_job->num_args >= 2) {
chdir(cur_job->arg_strings[1]);
return SUCCESS;
}
if (strcmp(cur_job->arg_strings[0], "exit") == 0) {
exit(EXIT_SUCCESS);
}
if (strcmp(cur_job->arg_strings[0], "help") == 0) {
help();
return SUCCESS;
}
if (strcmp(cur_job->arg_strings[0], "jobs") == 0) {
print_jobs(jobs_list);
return SUCCESS;
}
if (strcmp(cur_job->arg_strings[0], "bg") == 0) {
if (jobs_list != NULL) {
delete_jobs(&cur_job);
cur_job = wake_job(&jobs_list, FALSE);
if (cur_job != NULL) {
kill(cur_job->pid, SIGCONT);
}
}
return SUCCESS;
}
if (strcmp(cur_job->arg_strings[0], "fg") == 0) {
if (jobs_list != NULL) {
int status;
delete_jobs(&cur_job);
cur_job = wake_job(&jobs_list, TRUE);
if (cur_job != NULL) {
kill(cur_job->pid, SIGCONT);
wait(&status);
}
}
return SUCCESS;
}
return FAILURE;
}
/* Opens the README file and prints it on the screen.*/
void help(void) {
FILE *readme = fopen("README", "r");
char c;
if (readme != NULL) {
while ((c = getc(readme)) != EOF)
putchar(c);
fclose(readme);
} else
printf("Couldn't find README.\n");
return;
}