-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuiltInCommands.c
180 lines (159 loc) · 3.76 KB
/
builtInCommands.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <sys/wait.h>
#include <stdbool.h>
#include <limits.h>
#include <errno.h>
#include "builtIn.h"
void change_dir(char *path);
void kill_job(job *head, char *cmd);
void kill_all_jobs(job *head);
bool parseLong(const char *str, long *val);
bool is_running(job *foo);
const char *builtInArray[LEN] = {
"exit",
"cd",
"history",
"jobs",
"kill"
};
int isBuiltInCommand(char *command) {
for (int i = EXIT; i < LEN; i++) {
if (strncmp(command, builtInArray[i], strlen(builtInArray[i])) == 0) {
return i;
}
}
return NO_SUCH_BUILTIN;
}
void executeBuiltInCommand(commandType *command, int type, HISTORY_STATE *history_state, job *head) {
switch (type) {
case CD:
change_dir(command->VarList[1]);
break;
case HISTORY:
// Source: https://stackoverflow.com/questions/38792542/readline-h-history-usage-in-c
for (int i = 0; i < history_state->length; i++) {
printf("%d %8s %s", i + 1, history_state->entries[i]->line, history_state->entries[i]->timestamp);
}
putchar('\n');
break;
case JOBS:
print_running_jobs(head);
break;
case KILL:
if (strcmp(command->VarList[1], "all") == 0)
kill_all_jobs(head);
else
kill_job(head, command->VarList[1]);
break;
default:
printf("\nError! No such builtin command!\n");
}
free(history_state);
}
void change_dir(char *path) {
int status = chdir(path);
if (status) {
printf("\nError! No such path exists!\n");
}
}
void print_running_jobs(job *head) {
job *current = head;
int check = false;
while (current != NULL) {
if (is_running(current)) {
if (!check) {
printf("PID\t\tCommand\t\t\tStatus\n");
check = true;
}
printf("%d\t\t%s\t\tRunning", current->pid, current->command);
current = current->next;
printf("\n");
} else {
current = current->next;
}
}
if (!check) {
printf("jobs: There are no jobs running!\n");
}
}
// add job to the beginning of the linked list of jobs
void add_job(job **head, pid_t pid, char cmd[]) {
job *new_job = NULL;
new_job = malloc(sizeof(job));
new_job->pid = pid;
new_job->next = *head;
strcpy(new_job->command, cmd);
*head = new_job;
}
void copy_running_jobs(job **head) {
job *new = NULL;
job *current = *head;
while (current != NULL) {
if (is_running(current)) {
add_job(&new, current->pid, current->command);
}
current = current->next;
}
job *temp = *head;
*head = new;
free_jobs(temp);
}
void free_jobs(job *head) {
job *tmp;
while (head != NULL) {
tmp = head;
head = head->next;
free(tmp);
}
}
void kill_all_jobs(job *head) {
job *tmp = head;
while (tmp != NULL) {
kill(tmp->pid, SIGKILL);
tmp = tmp->next;
}
}
void kill_job(job *head, char *cmd) {
job *current = head;
long int pid = 0;
if ((cmd != NULL) && (!parseLong(cmd, &pid))) {
printf("Error: Invalid command!\n");
return;
}
while (current != NULL) {
if (current->pid == pid && is_running(current)) {
kill(current->pid, SIGKILL);
return;
} else if (current->pid == pid) {
printf("Error: No such job exists!\n");
return;
}
current = current->next;
}
printf("Error: No such job exists!\n");
}
// Source: https://stackoverflow.com/questions/14176123/correct-usage-of-strtol
bool parseLong(const char *str, long *val) {
char *temp;
bool rc = true;
errno = 0;
*val = strtol(str, &temp, 0);
if (temp == str || *temp != '\0' ||
((*val == LONG_MIN || *val == LONG_MAX) && errno == ERANGE))
rc = false;
return rc;
}
bool is_running(job *foo) {
// check if process exist in the job list, 0 = true
int kill_signal = kill(foo->pid, 0);
// check if process is running, 0 = true
int wait_signal = waitpid(foo->pid, 0, WNOHANG);
if (kill_signal == 0 && wait_signal == 0) {
return true;
}
return false;
}