-
Notifications
You must be signed in to change notification settings - Fork 0
/
nautilus.c
264 lines (197 loc) · 5.28 KB
/
nautilus.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
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
/*
Author: Adam Valiant
Filename: nautilus.c
Description: a basic command-line interface shell for linux implemented in C
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <signal.h>
#include <readline/readline.h>
#include <readline/history.h>
#include <errno.h>
void main(void)
{
// definitions and declarations
char * input;
char * shell_prompt = "nautilus>";
char * token;
char * safety;
char *args[41]; /* command line arguments w/ command at arg[0] */
char *args2[41];
char *args3[41];
pid_t pid;
int bg_pids[50];
int status;
int i;
int j;
int k;
int l; //zombie index
int m;
int amp_position;
int bg_flag = 0;
int redirectionflag = 0;
static int id_index;
FILE *fp;
// readline autocomplete feature
rl_bind_key('\t', rl_complete);
id_index = 0;
// infinite loop for prompt display
while(1) {
// look for zombies and reap them
if(id_index!=0){
for(l=0; l<id_index; l++)
{
if(waitpid(bg_pids[l],&status,WNOHANG) == bg_pids[l])
printf("\njob %d exited with status 0\n\n", bg_pids[l]);
}
}
// display prompt and read input (note: input must be freed after use since readline uses malloc)
input = readline(shell_prompt);
if (!input || input == NULL || strcmp(input, "")==0)
continue;
// add input to readline history.
add_history(input);
// parse out arguments from input and place into args array
char whitechars[] = " \n\t\r";
token = strtok_r(input, whitechars, &safety);
args[0] = token;
i=1;
while (token != NULL) {
token = strtok_r(NULL, whitechars, &safety);
args[i] = token;
i++;
}
i=1; //reset index
// if the exit command is given, proceed to exit
if(strcmp(args[0], "exit")==0)
exit(0);
else if(strcmp(args[0], "jobs")==0)
{
printf("\nLIVE PROCESSES: \n\n");
if(id_index!=0){
for(l=0; l<id_index; l++)
{
if(kill(bg_pids[l], 0)==0)
printf("%d\n", bg_pids[l]);
}
printf("\n");
}
continue;
}
else if(strcmp(args[0], "cd")==0)
{
chdir(args[1]);
continue;
}
else
{
// check for & or >
j=0;
k=0;
while (args[j] != NULL)
{
// look for ampersand, save index, and set flag
if(strcmp(args[j], "&")==0){
amp_position=j; // save position of & so that it can be excluded later
bg_flag = 1; // background process execution flag
}
else if( (strcmp(args[j], ">")==0) )
//these will be implemented later
//if( (strcmp(args[0], ">")==0)||(strcmp(args[0], "<")==0)||(strcmp(args[0], ">>")==0)||(strcmp(args[0], "<<")==0))
{
redirectionflag = 1;
k = j+1; // position of file argument
m = j-1; // position of command and argument whose output is being redirected
}
j++;
}
j=0;
}
// fork from parent
pid = fork();
if (pid < 0) {
printf("FORK FAILURE!\n");
exit(1);
}
else if (pid == 0) // if child is forked, execute
{
//child process
if(bg_flag == 1){ //child background execution
// extract necessary arguments and exclude &
j=0;
while (j<amp_position)
{
args2[j] = args[j];
j++;
}
args2[amp_position] = NULL;
j=0;
k=0;
fclose(stdin); // close child's stdin
fopen("/dev/null", "r"); // open a new stdin that is always empty
execvp(args2[0], args2);
// if this line is reached, execvp has failed
printf("\nEXECVP ERROR! (bg execution failure)\n\n");
printf("errno = %d.\n", errno);
exit(1);
}
else if(redirectionflag == 1){ //child redirection execution
// I/O redirection procedures
/* File descriptors:
0 = stdin
1 = stdout
*/
//open file
fp = fopen(args[k], "w+");
//duplicate file descriptors
dup2( fileno(fp), 1);
// extract necessary input
j=0;
while (j<=m )
{
args3[j] = args[j];
j++;
}
args3[m+1] = NULL;
j=0;
k=0;
//close((fileno(fp)));
execvp(args3[0], args3);
// if this line is reached, execvp has failed
printf("\nEXECVP ERROR! (primary execution failure)\n\n");
printf("errno = %d.\n", errno);
exit(1);
}
else{ //child foreground execution
execvp(args[0], args);
// if this line is reached, execvp has failed
printf("\nEXECVP ERROR! (primary execution failure)\n\n");
printf("errno = %d.\n", errno);
exit(1);
}
}
else if (pid >0){ // if parent ....
//parent process
// if child executed as a foreground process suspend execution of
// the current process (parent) until child has exited...
// else if the child was executed as a background process, print appropriate message
// and add the child's pid to the list of background processes
if((bg_flag == 0)) //foreground execution
pid = wait(&status);
else if (bg_flag==1){ //background execution
printf("started job %d\n\n", (int)pid);
bg_pids[id_index] = pid;
id_index++;
bg_flag = 0; //reset flag
}
if(redirectionflag==1)
redirectionflag = 0; //reset flag
}
// we don't need the input anymore so we free it since
// readline used malloc
free(input);
} //infinite while loop for prompt
return;
}