-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexecute.c
More file actions
137 lines (126 loc) · 3.99 KB
/
execute.c
File metadata and controls
137 lines (126 loc) · 3.99 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
#include "header.h"
//#include "builtin.c"
//#include "syscomm.c"
//extern int (**builtin_func)(char **);
/*extern int my_cd(char **);
extern int my_pwd(char **);
extern int my_echo(char **);
*/
int execute(char **args)
{
int done=0;
//empty command
if(args[0]==NULL)
return 1;
//check input rdirect, output redirct
int i;
for ( i=0; args[i]!=NULL; i++)
{
//output redirct
if(strcmp(args[i],">")==0)
{
done=1;
int fd;
// open the file to replace stdout
fd = open(args[i+1], O_WRONLY | O_CREAT | O_TRUNC, 0644);
if(fd == -1){
perror("Failed to open file");
exit(1);
}
close(1); // not required, but a good practice
// use dup2() to duplicate the fd
if(dup2(fd, 1) != 1){ // 1 refers to stdout
perror("dup2 fail");
exit(1);
}
args[i]=NULL;
commandRun(args);
// close the original fd
close(fd);
freopen("/dev/tty","w",stdout);
}
//output append redirct
else if(strcmp(args[i],">>")==0)
{
done=1;
int fd;
// open the file to replace stdout
fd = open(args[i+1], O_WRONLY | O_APPEND | O_CREAT , 0644);
if(fd == -1){
perror("Failed to open file");
}
close(1); // not required, but a good practice
// use dup2() to duplicate the fd
if(dup2(fd, 1) != 1) // 1 refers to stdout
perror("dup2 fail");
args[i]=NULL;
commandRun(args);
// close the original fd
close(fd);
freopen("/dev/tty","w",stdout);
}
//output redirct
else if(strcmp(args[i],"<")==0)
{
done=1;
int fd;
// open the file to replace stdout
fd = open(args[i+1], O_RDONLY, 0644);
if(fd == -1){
perror("Failed to open file");
exit(1);
}
close(0); // not required, but a good practice
// use dup2() to duplicate the fd
if(dup2(fd, 0) == -1){ // 1 refers to stdout
perror("dup2 fail");
exit(1);
}
args[i]=NULL;
commandRun(args);
// close the original fd
close(fd);
freopen("/dev/tty","r",stdin);
}
//iterate in list of buitin commands to match the input command
//int n=my_num_builtins();
/* for(i=0;i<n;i++)
{
if(strcmp(args[0], builtin_str[i])==0)
{
//call the corresponding builin command function
(*builtin_func[i])(args);
return 1;
}
}*/
}
if (done!=1) {
commandRun(args);
}
}
int commandRun(char **args)
{
//check which buitin function this command is
if(strcmp(args[0],"pwd")==0)
return pwd(args);
else if(strcmp(args[0],"echo")==0)
return echo(args);
else if(strcmp(args[0],"cd")==0)
return cd(args);
else if(strcmp(args[0],"pinfo")==0)
return pinfo(args);
else if (strcmp(args[0],"jobs")==0)
return jobs();
else if (strcmp(args[0],"fg")==0)
return fg(args);
else if (strcmp(args[0],"killallbg")==0)
return killallbg();
else if(strcmp(args[0],"quit")==0)
return quit();
else if(strcmp(args[0],"kjob")==0)
return kjob(args);
//not a buitin command
//run as a system command
launch(args);
return 1;
}