-
Notifications
You must be signed in to change notification settings - Fork 0
/
veryFinal.c
83 lines (76 loc) · 1.51 KB
/
veryFinal.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/wait.h>
int main( int argc, char *argv[], char *env[] )
{
pid_t child_pid;
int status;
char command[1001];
char temp[1001];
char history[1001][1001];
char *token;
char *args[10];
int i = 0;
int commandCount = 0;
while (1) {
command[0] = '\0';
printf("[CustomShell] ");
fgets(command, 1000, stdin);
command[strlen(command) - 1] = '\0';
strcpy(temp, command);
token = strtok(command, " ");
int j = 0;
while( token != NULL )
{
args[j] = token;
j++;
token = strtok(NULL, " ");
}
if (strcmp(args[0], "history") == 0) {
int j = 0, k=0;
for (j = 0; j < commandCount; j++)
{
while(i<strlen(history[j])) {
if (history[j][i]==' ') {
for (k=i; k<strlen(history[j]); k++)
history[j][k]=history[j][k+1];
} else break;
}
printf("%d. %s\n", j+1, history[j]);
}
} else if (strcmp(args[0], "exit") == 0) {
return 0;
} else {
}
if (commandCount>=100) {
for(i=0; i<commandCount-1; i++) {
strcpy(history[i], history[i+1]);
}
strcpy(history[i], temp);
} else {
strcpy(history[commandCount], temp);
commandCount++;
}
args[j] = NULL;
child_pid = fork();
if(child_pid < 0)
{
perror("Fork Failed: ");
exit(1);
}
else if(child_pid == 0)
{
execvp(args[0], args);
if(strcmp(args[0], "history") != 0) perror("Execution Failed: ");
exit(0);
}
else
{
wait(NULL);
}
}
return 0;
}