-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshell.c
More file actions
245 lines (208 loc) · 6.41 KB
/
Copy pathshell.c
File metadata and controls
245 lines (208 loc) · 6.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
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
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
/* Jordan Quan, 260565221, Sept 26, 2014 */
/* Comp 310, Assignment 1 */
#define MAX_LINE 80 // chars per line, per command, should be enough
char hist[10][MAX_LINE];
int histCount = 0;
pid_t children[50];
int childCount = 0;
// setup() reads in the next command line, separating it into distinct tokens
// using whitespace as delimiters. setup() sets the args parameter as a null-terminated string.
void setup(char inputBuffer[], char *args[], int *background){
int length, // # of chararacters in the command line
i, // loop index for accessing inputBuffer array
start, // index where beginning of next command parameter is
ct; // index of where to place the next parameter into args[]
ct = 0;
// read what the user enters on the command line
length = read(STDIN_FILENO, inputBuffer, MAX_LINE);
//add input to history
strcpy(hist[histCount%10],inputBuffer);
histCount++;
inputBuffer[length]='\0';
start = -1;
if (length == 0) { // ctrl-d was entered, quit the shell normally
printf("\n");
exit(0);
}
if (length < 0){ //something wrong, terminate with error code -1
perror("error reading the command");
exit(-1);
}
// examine every character in the inputBuffer
for (i=0; i<length; i++){
switch (inputBuffer[i]){
case ' ':
case '\t': // arguement separators
if (start != -1){
args[ct]= &inputBuffer[start]; // set up pointer
ct++;
}
inputBuffer[i]='\0'; // add a null char; make a C string
start =-1;
break;
case '\n': // should be the final char examined
if (start != -1){
args[ct] = &inputBuffer[start];
ct++;
}
inputBuffer[i] = '\0';
args[ct] = NULL; // no more arguments to this command
break;
default: // some other character
if (inputBuffer[i] == '&'){
*background = 1;
inputBuffer[i] = '\0';
start = -1;
} else if (start == -1) start = i;
}
}
args[ct]=NULL; // just in case the input line was > 80
}
/**************************************************/
void addChild(pid_t pid){
int i = 0;
while(children[i]!=-1){
i++;
}
children[i]=pid;
}
void checkJobs (){
int i=0;
for (i=0;i<50;i++){
if (kill(children[i],0)){
children[i]=-1;
}
}
}
/**************************************************/
/**************************************************/
int main (void)
{
char inputBuffer[MAX_LINE]; // buffer to hold the command entered
int background; // equals 1 if a command is followed by '&'
char *args[MAX_LINE/2 + 1]; // command line (of 80) has max of 40 arguments
pid_t pid; // pid and childpid
int i, j, k; // loop iterators
int skip; // used to track a shell command run by parent and skip child
char* cwd; // will hold pwd
char buff[80]; // buffer for pwd
int execStatus; // check if child execvp worked
i=j=k=execStatus=skip=0; // initialize some vars
for (i=0; i<50; i++){
children[i]=-1;
}
while (1){
background = 0;
skip = 0;
printf("COMMAND->\n");
setup(inputBuffer, args, &background); // get next command
// built in commands
/**************************************************/
//built in redo command
//NOT WORKING
/*
if (strcmp(inputBuffer,"r")==0){
histCount=histCount-2;
j=0;
printf("%d: ",histCount);
while( hist[histCount%10][j] != '\n' && hist[histCount%10][j] != '\0'){
printf("%c",hist[histCount%10][j]);
j++;
}
printf("\n");
strcpy(inputBuffer,hist[histCount%10]); // set last command as inputBuffer
//setup(inputBuffer, args, &background); // redo setup using last command
}*/
// built in change directory
if (strcmp(inputBuffer,"cd")==0){
skip=1;
printf ("changing directory to %s\n", args[1]);
if (chdir (args[1]) == -1) {
printf("chdir failed!\n");
} else {
printf ("chdir successful\n");
}
}
//built in print working directory
if (strcmp(inputBuffer,"pwd")==0){
skip=1;
cwd = getcwd(buff,80);
if (cwd!=NULL){
printf ("current working directory is %s\n",cwd);
}
}
//built in exit command
if (strcmp(inputBuffer,"exit")==0){
printf("exiting shell\n");
exit(0);
}
//built in history command
if (strcmp(inputBuffer,"history")==0){
skip=1;
j=k=0;
for (i=1;i<10;i++){
j=histCount-i; // j counts 10 down from histCount
if(j>=0){
printf("%d: ",j);
while( hist[j%10][k] != '\n' && hist[j%10][k] != '\0'){
printf("%c",hist[j%10][k]);
k++;
}
printf("\n");
k=0;
}
}
}
/*
//built in jobs command
if (strcmp(inputBuffer,"jobs")==0){
char str[50]="ps -o pid --ppid ";
char ppid[7];
sprintf(ppid,"%d",getpid());
strcat(str,ppid);
system(str);
}
*/
// alt built in jobs command
if (strcmp(inputBuffer,"jobs")==0){
for (i=0;i<50;i++){
if (children[i]!=-1){
printf("Child # %d\n",children[i]);
}
}
}
// forking child process
/**************************************************/
if (skip==0){
pid=fork(); //forks a child process
if (pid<0) { //check forking
printf("forking error\n");
exit(1);
}
else if (pid == 0) { //start of child process
//printf("child pid = %d\n", getpid());
//printf("parent pid = %d\n", getppid());
if (execvp(*args, args)<0){ //check execution
printf("execution error\n");
exit(1);
}
}
else { //start of parent process
if (background == 0){
printf("waiting for child process # %d\n", pid);
addChild(pid);
waitpid(pid);
//printf("finished waiting for child process # %d\n", pid);
} else {
printf("parent NOT waiting\n");
}
printf("resuming parent process\n");
}
}
/**************************************************/
} //close while and end
}