-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoscode.c
More file actions
371 lines (336 loc) · 7.67 KB
/
Copy pathoscode.c
File metadata and controls
371 lines (336 loc) · 7.67 KB
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <signal.h>
#include <fcntl.h>
//#include <sys/types.h>
typedef struct History{
int num;
char args[20][80];
int bg;
int status;
pid_t pid;
struct History * prev;
struct History * next;
} Hist;
int getcmd(char *prompt, char *args[], int *background){
int length, i = 0;
char *token, *loc;
char *line;
size_t linecap = 0;
printf("%s", prompt);
length = getline(&line, &linecap, stdin);
if (length <= 0) {
exit(-1);
}
// Check if background is specified..
if ((loc = index(line, '&')) != NULL) {
*background = 1;
*loc = ' ';
} else
*background = 0;
while ((token = strsep(&line, " \t\n")) != NULL) {
for (int j = 0; j < strlen(token); j++)
if (token[j] <= 32)
token[j] = '\0';
if (strlen(token) > 0)
args[i++] = token;
}
//args[i++] = '\0';
args[i]=NULL;
return i;
}
void freecmd (char *args[]){
for(int i=0; i<20; i++){
if(args[i] != NULL){
args[i] = NULL;
}
}
}
/*void checkup(Hist *node){
//do {
int status;
pid_t pid = node->pid;
pid_t result = waitpid (pid, &status, WNOHANG); //wont actually wait
printf("pid:%d,status:%d\n", pid, result);
if (result == 0){ //still running
continue;
} else if (result == -1) { // error
printf("error checking child status\n");
} else { // child exited
node->status = status;
}
//} while (node->prev != NULL);
}*/
void update(){
printf("received signal\n");
}
void printhistory(Hist *node){
//print out the last 10 cmds
Hist *cur = node->prev;
if (cur == NULL){
printf("no previous command history found");
} else {
int i = 0;
while (cur != NULL && i<10){
printf("%d: ", cur->num);
int j = 0;
while (strcmp(cur->args[j], "")!=0){
printf("%s ", cur->args[j]);
j++;
}
printf("\n");
cur = cur->prev;
i++;
}
}
}
Hist* findhist(Hist *node, int num){
Hist *cur = node->prev;
if (cur == NULL){
printf("no previous command history found\n");
} else if (cur->num < num || cur->num-10 > num){
printf("command not found / out of range\n");
} else {
int i = 0;
while (cur != NULL && i<10){
if (cur->num == num){
return cur;
}
cur = cur->prev;
i++;
}
printf("command not found / out of range\n");
}
return NULL;
}
void pwd(){
int PATH_MAX = 200; // arbitrary char limit
char* cwd;
char buff[PATH_MAX + 1];
cwd = getcwd( buff, PATH_MAX + 1 );
if( cwd != NULL ) {
printf( "Current working directory is %s.\n", cwd );
}
}
void printjobs(Hist *node){
Hist *cur = node->prev;
if (cur == NULL){
printf("no previous jobs found");
} else {
while (cur != NULL){
if (cur->bg==1){
//print job
printf("%d: ", cur->num);
int j = 0;
while (strcmp(cur->args[j], "")!=0){
printf("%s ", cur->args[j]);
j++;
}
// check if it already finished
int status;
pid_t rpid = waitpid(cur->pid, &status, WNOHANG); //wnohang does not actually wait
if (rpid == -1) {
perror("error checking job");
cur->bg = 0;
} else if (rpid == 0) {
printf("still running");
} else if (rpid == cur->pid) {
printf("finished, status: %d", status);
cur->status = status; // update status
cur->bg = 0; // remove from background list
}
printf("\n");
}
cur = cur->prev;
}
printf("Done printing background jobs\n");
}
}
void fg(Hist *node, int num){
Hist *cur = node->prev;
if (cur == NULL){
printf("no previous jobs found");
} else {
while (cur != NULL){
if (cur->num==num){ //found the job
if (cur->bg==0){
printf("This job was already foreground\n");
return;
} else if (cur->status==0){
printf("This job has already finished\n");
cur->bg = 0;
return;
} else {
//print job
printf("Bringing %d: ", cur->num);
int j = 0;
while (strcmp(cur->args[j], "")!=0){
printf("%s ", cur->args[j]);
j++;
}
printf("to foreground...");
pid_t pid = cur->pid;
int status;
waitpid(pid, &status, 0);
cur->status = status;
cur->bg = 0;
printf(" finished with status %d", status);
return;
}
}
cur = cur->prev;
}
printf ("Could not find specified job\n");
return;
}
}
void redirect(char *args[], int cnt){
for (int k=0; k<cnt; k++){
if (strcasecmp(args[k],">")==0){
close(1);
int PATH_MAX = 200;
char* path;
char buff[PATH_MAX + 1];
path = getcwd( buff, PATH_MAX + 1 );
strcat(buff, "/");
strcat(buff, args[k+1]);
if (open(path, O_WRONLY) == -1) {
perror(path);
_exit(1);
}
args[k]='\0';
args[k++]='\0';
}
}
}
int main(){
char *args[20];
int bg;
int num=1;
Hist * node = NULL;
node = malloc(sizeof(Hist));
node->prev = NULL;
//pid_t ppid = getpid();
while(1){
bg = 0;
int cnt = getcmd("\n>> ", args, &bg);
// save cmd to history
node->num = num;
int i=0;
while(args[i]!=NULL){
strcpy(node->args[i], args[i]);
i++;
}
node->bg = bg;
//printf("%d: %s, %d",node->num, node->args[0], node->bg);
// built in commands
int builtin=0; // built in flag, prevents child forking
// print history
if(strcasecmp(args[0],"history")==0){
builtin = 1;
printhistory(node);
freecmd(args);
continue;
}
// run history
int ret;
int runhistflag = 0;
ret = atoi(args[0]); // convert string to integer
if (ret != 0){ // check if integer
Hist * recall = findhist(node, ret); // find history node associated with that number
if (recall!=NULL){
//printf("%d: %s\n", recall->num, recall->args[0]);
if (recall->status!=0){
printf("this was a bad command");
freecmd(args);
continue;
}
int r=0;
while (strcmp(recall->args[r], "")!=0){
args[r] = recall->args[r]; // overwrite current args with old ones
r++;
}
bg = recall->bg;
runhistflag = 1;
} else {
freecmd(args);
continue;
}
}
// change directory
if(strcasecmp(args[0],"cd")==0 && cnt==2){
builtin = 1;
chdir(args[1]);
// already saved the args to the node, need to refresh manually
strcpy(node->args[1], "");
freecmd(args);
continue;
}
// print working directory
if(strcasecmp(args[0],"pwd")==0 && cnt==1){
builtin = 1;
pwd();
freecmd(args);
continue;
}
// exit shell
if(strcasecmp(args[0],"exit")==0 && cnt == 1){
// also kill all children
kill(0, SIGTERM); // kill all P in same P group as sender
exit(0);
}
// print background jobs
if(strcasecmp(args[0],"jobs")==0 && cnt == 1){
builtin = 1;
printjobs(node);
freecmd(args);
continue;
}
// bring to foreground
if(strcasecmp(args[0],"fg")==0 && cnt==2){
builtin = 1;
fg(node, atoi(args[1]));
strcpy(node->args[1], "");
freecmd(args);
continue;
}
// non-built in commands
if (!builtin){
// fork the job
pid_t ppid = getpid();
pid_t pid = fork();
if (pid < 0){
perror("fork failed");
exit(1);
}else if (pid == 0){ // child
redirect(args, cnt); // check for redirects
execvp(args[0],args);
perror("error in execvp");
//kill(ppid, SIGINT);
// do something to update history of bad command
_exit(EXIT_FAILURE);
}else{ // parent
node->pid = pid;
int status;
if(bg == 0){ //not bg
waitpid(pid, &status, 0);
node->status = status;
}
}
}
// catch error signal from child... didnt work as planned
//signal(SIGINT, update);
if (!runhistflag){ // dont save reruns
// create next node and link up
Hist * next = malloc(sizeof(Hist));
next->prev = node;
node->next = next;
node = next;
num++;
}
freecmd(args); // reset commands
}
}