-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
127 lines (117 loc) · 3.34 KB
/
Copy pathmain.cpp
File metadata and controls
127 lines (117 loc) · 3.34 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
#include "minish.h"
pid_t parent_pid;
void sig_int(int sig){
print_sigint();
if(getpid() != parent_pid){ // child -> running command in minish
exit(0);
}
}
void execute(char** args){
int status;
int pid = fork();
if(pid < 0){
perror("Cannot create child process");
return;
}
if(pid == 0){ //child
char location[500];
strcpy(location, "/bin/");
strcat(location, args[0]);
int i, fd, status1;
char *beforepipe[500], *afterpipe[500];
pid_t pid1, pid2;
for(i=0; args[i] != NULL; i++){
if(!strcmp(args[i], "|")){ //pipe should be go first
if(args[i]){
if(!args[i+1]){ // just execute first
args[i] = NULL;
break;
}
for(int j=0; j<i; j++){
beforepipe[j] = args[j];
}
for(int j=i+1; args[j] != NULL; j++){
afterpipe[j - i - 1] = args[j];
}
pid1 = fork();
if(pid1 < 0){
perror("Fork error");
exit(1);
}
else if(pid1 == 0){ // child
execute(beforepipe);
execute(afterpipe);
}
else{
pid_t waitPid = wait(&status1);
if(waitPid == -1){
perror("Child process aborted");
return;
}
}
}
}
if(!strcmp(args[i], ">")){
if(args[i]){
if(!args[i+1]){
perror("No file to redirect");
exit(1);
}
if((fd = open(args[i+1], O_RDWR|O_CREAT|S_IROTH, 0644)) == -1){
perror("Cannot open redirection file");
exit(1);
}
dup2(fd, STDOUT_FILENO);
close(fd);
args[i] = NULL;
args[i+1] = NULL;
for(; args[i] != NULL; i++){
args[i] = args[i+2];
}
args[i] = NULL;
}
}
}
int result = execvp(location, args);
if(result == -1){
perror("Cannot execute command (execvp error)");
exit(1);
}
if(args[0] == "nano") exit(0); // To exit well after executing nano
}
else{
pid_t waitPid = wait(&status);
if(waitPid == -1){
perror("Child process aborted");
return;
}
}
}
void execute_command(char* cmd){
char **args = NULL;
args = get_cmd(cmd);
if(args == NULL){
return;
}
else if(strcmp(args[0], "quit") == 0){
printf("Bye\n");
exit(0);
}
else{
if(args[0] != NULL){
execute(args);
}
}
free(args);
}
void print_string(char* str){
printf("%s", str);
}
int main(){
parent_pid = getpid();
signal(SIGINT, sig_int);
strcpy(pwd, getcwd(pwd, 500));
strcat(pwd, "$ ");
shell();
return 0;
}