-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathosshell.c
More file actions
157 lines (127 loc) · 2.41 KB
/
osshell.c
File metadata and controls
157 lines (127 loc) · 2.41 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
#include <stdio.h>
#include <stdlib.h> // Needed by malloc()
#include <limits.h> // Needed by PATH_MAX
#include <unistd.h> // Needed by getcwd(),execvp
#include <string.h> // Needed by strtok()
#include <errno.h>
#include <sys/wait.h> // Needed by waitpid()
#include <sys/types.h> // Needed by waitpid()
char cwd[PATH_MAX+1];
int free_mem(char** argList)
{
int i = 0;
while(argList[i])
free(argList[i++]);
free(argList);
return 0;
}
int print_header()
{
if(getcwd(cwd,PATH_MAX+1) == NULL)
return -1;
printf("[3150 shell:%s]$ ",cwd);
return 0;
}
int check_builtin(char** argList)
{
if(strcmp(argList[0],"exit") == 0) // == 0 means two strings equal
{
if(argList[1] != NULL)
{
printf("exit: wrong number of arguments\n");
free_mem(argList);
return 1;
}
free_mem(argList);
exit(0);
}
if(strcmp(argList[0],"cd") == 0)
{
if(argList[2] != NULL || argList[1] == NULL)
{
//only 2 args (<2 or >2)
printf("cd: wrong number of arguments\n");
}
else
{
if(chdir(argList[1]) != -1)
{
getcwd(cwd,PATH_MAX+1);
}
else
{
printf("%s: Cannot Change Directory\n", argList[1]);
}
}
free_mem(argList);
return 1;
}
return 0; //not a builtin function
}
char** read_input()
{
//read the whole command
char cmd[256];
if(!fgets(cmd, 256, stdin))
{
//EOF is read
printf("\n");
exit(0);
}
if(strlen(cmd) == 1 || cmd[0] == ' ')
{
//empty string OR a space is read
return NULL;
}
cmd[strlen(cmd)-1] = '\0';
//tokenize
char **argList = (char**) malloc(sizeof(char*) * 129); //255/2
char *token = strtok(cmd," ");
int i = 0;
while(token != NULL)
{
argList[i] = (char*)malloc(sizeof(char) * (strlen(token) + 1));
strcpy(argList[i++],token);
token = strtok(NULL," ");
}
argList[i] = NULL;
return argList;
}
int execution(char** argList)
{
pid_t child_pid;
if((child_pid = fork()))
{
//parent
waitpid(child_pid,NULL,WUNTRACED);
free_mem(argList);
}
else
{
//child
setenv("PATH","/bin:/usr/bin:.",1);
execvp(*argList,argList);
if(errno == ENOENT){
printf("%s: command not found\n", argList[0]);
} else {
printf("%s: unknown error\n", argList[0]);
}
free_mem(argList);
exit(0);
}
return 0;
}
int main(int argc, char *argv[])
{
while(1)
{
print_header();
char** argList = read_input();
if(argList) //if argList not empty
{
if(check_builtin(argList) == 0)
execution(argList);
}
}
return 0;
}