-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmyshell.c
More file actions
163 lines (146 loc) · 3.06 KB
/
myshell.c
File metadata and controls
163 lines (146 loc) · 3.06 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
/*
* myshell.c
*
* Created on: Apr 17, 2009
* Author: Allyson Schrader
*/
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <errno.h>
#include <string.h>
#include <fcntl.h>
#include <stdlib.h>
extern char **getline();
void execute_cmd(char** arglist, int pipe_in[2] , int background , int pipe_in_flag);
int check_background(char** arglist);
int main() {
char **args;
while(1){
printf("$ ");
args = getline();
if(strcmp(args[0] , "exit") == 0){
exit(0);
}
execute_cmd(args , NULL , check_background(args) , 0);
}
}
int check_background(char** arglist){
int i;
for(i = 0 ; arglist[i] != NULL ; i++){
if(strcmp(arglist[i] , "&") == 0){
return 1;
}
}
return 0;
}
void execute_cmd(char** arglist, int pipe_in[2] , int background , int pipe_in_flag){
int i=0;
int r_fd, w_fd;
pid_t pid;
int status;
char* read_file;
char* write_file;
int write_flag =0, read_flag = 0;
int arg_placeholder;
int pipe_out_flag =0;
int pipe_out[2];
char** pipe_args;
/*
* Stage One: parse through the args, if < or > is found
* set file names and flags, if | is found, set flags, copy argslist
* and end parse
*/
for(i=0; arglist[i] != NULL;i++){
if(strcmp(arglist[i] ,"<") == 0 ){
arglist[i]=NULL;
read_flag = 1;
read_file = arglist[i+1];
}else if(strcmp(arglist[i] ,">") == 0){
arglist[i]=NULL;
write_flag = 1;
write_file = arglist[i+1];
}else if(strcmp(arglist[i] , "|") == 0){
arglist[i] = NULL;
pipe_out_flag = 1;
arg_placeholder = i+1;
if(pipe(pipe_out) < 0){
perror("failedo to open pipe");
exit(errno);
}
break;
}
}
/*
* Stage Two: fork, check for flags, dup files/pipes
* where necessary, execute
*/
pid = fork();
if (pid < 0) {
fprintf(stderr, "Fork failed!\n");
exit(0);
}
else if (pid==0) {
if(read_flag){
r_fd = open(read_file, O_RDONLY);
if (r_fd < 0) {
printf("could not open file for read %s\n" , arglist[i]);
exit(1);
}
if (dup2(r_fd, 0) != 0) {
perror("could not dup2 for read\n");
exit(1);
}
close(r_fd);
}
if(write_flag){
w_fd = open(write_file, O_WRONLY | O_TRUNC | O_CREAT, 0644);
if (w_fd < 0) {
perror("could not open file for write\n");
exit(2);
}
if (dup2(w_fd, 1) != 1) {
perror("could not dup2 for write\n");
exit(1);
}
close(w_fd);
}
if(pipe_out_flag || pipe_in_flag){
if(pipe_in_flag){
if (dup2(pipe_in[0], 0) != 0) {
perror("could not dup2 for read\n");
exit(1);
}
}
if(pipe_out_flag){
if (dup2(pipe_out[1], 1) != 1) {
perror("could not dup2 for write\n");
exit(1);
}
}
close(pipe_out[0]);
close(pipe_in[1]);
}
execvp(arglist[0] , arglist);
}
/*
* Stage Three: check pipe flags, then recursively call
* execute_cmd on the array following the | OR wait for
* process to finish.
*/
else {
if(pipe_out_flag){
close(pipe_out[1]);
if(!background){
waitpid(pid, &status, 0);
}
execute_cmd(&arglist[arg_placeholder] , pipe_out , background , pipe_out_flag);
}
else{
if(!background){
waitpid(pid, &status, 0);
}
}
}
close(pid);
}