-
Notifications
You must be signed in to change notification settings - Fork 0
/
HW1_0316083.cpp
156 lines (133 loc) · 3.55 KB
/
HW1_0316083.cpp
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
#include <stdio.h>
#include <stdlib.h> //exit()
#include <string.h>
#include <unistd.h> //exec family
#include <sys/types.h> //wait
#include <sys/wait.h> //wait
#define MaxNUM 1000
#include <errno.h>
#include <signal.h>
#include <iostream>
using namespace std;
void signal_handler(){
while(waitpid(-1,NULL,WNOHANG)>0);
/* send fg job/ related process group signal
if (sig != 0) {
kill(-sig, SIGINT);
/*if (sig < 0){
printf("Job [%d] (%d) terminated by signal %d\n", jid, pid, (-sig));
deletejob(jobs, pid);
}*/
}
int main(int argc, const char* argv[]){
signal(SIGCHLD, SIG_IGN); //for linux
char *str_t;
str_t = (char *)malloc(MaxNUM*sizeof(char));
char **arg_t;
char c; //getchar
int i; //index of str_t
char *p; //for strtok
int arg_i; //index of arg_t
int count;
int err;
int nowait; //for &
printf("\x1B[32mWelcome to mysh by 0316083!\n");
while(1){
/* prepare */
i=0;
nowait=0;
strcpy(str_t,"");
printf("\x1B[33myicchen in %s\n", getcwd(NULL, NULL));
printf("\x1B[35mmysh>");
/* getline */
c=getchar();
count=1;
while(c!='\n'){
str_t[i]=c;
if(str_t[i]==' ') count++;
//if(str_t[i]=='&') count--;
i++;
c=getchar();
}
if(i==0) continue;
str_t[i]='\0';
//printf("%s\n",str_t);
/* Counting Arguement & AllocMem Start */
count++;
arg_t = (char**)malloc(count*sizeof(char*));
int k=0;
for(k=0;k<count;k++){
//printf("Size %d: %lu\n",k,sizeof(arg_t[k]));
arg_t[k] = (char*)malloc(MaxNUM*sizeof(char));
}
/* Counting Arguement & AllocMem Finish */
/* Passing Arguement Start */
arg_i=0;
p=strtok(str_t," ");
strcat(p,"\0");
arg_t[arg_i++]=p;
p=strtok(NULL," ");
while(p!=NULL){
if(strcmp(p,"&")!=0){
//printf("Old Arguement %d: %s\n",arg_i,arg_t[arg_i]);
strcat(p,"\0");
arg_t[arg_i]=p;
//printf("Arguement %d: %s\n",arg_i,arg_t[arg_i]);
arg_i++;
p=strtok(NULL," ");
}
else{
nowait=1;
p=strtok(NULL," ");
}
}
arg_t[arg_i]=NULL; //the last one of the argv in execvp must be NULL
/* Passing Arguement Finish */
if(strcmp(arg_t[0],"exit")==0 && arg_t[1]==NULL){ //Build-in Exit
free(str_t);
return 0;
}
if(strcmp(arg_t[0],"cd")==0 && arg_t[2]==NULL){
chdir(arg_t[1]);
}
//else if(arg_t[0],"cd")==0 && arg_t[1]==NULL)
if(strcmp(arg_t[0],"fg")==0 && arg_t[2]==NULL){
}
if(strcmp(arg_t[0],"bg")==0 && arg_t[2]==NULL){
}
if(strcmp(arg_t[0],"kill")==0 && arg_t[2]==NULL){
kill(323,SIGCONT);
}
if(arg_t[0]==NULL) continue;
/* Fork */
pid_t pid;
pid=fork();
if(pid<0){ //error occurred
perror("fork"); //print error in standard lib
//fprintf(stderr,"Fork failed!\n");
exit(-1);
}
else if(pid==0){ //child process
err=execvp(arg_t[0],arg_t);
if(err < 0) {
//11 ///perror(arg_t[0]); //print error in standard lib
//fprintf(stderr,"ERROR : (%s) %s\n",arg_t[0],strerror(errno));
exit(-1);
}
}
else{ //parent
if(nowait==0){ //wait(NULL);
//printf("Waiting for %d!\n",pid);
waitpid(pid,NULL,0);
}
else{
//printf("No wait!\n");
}
//printf("Child Complete!\n");
}
free(arg_t);
arg_t=NULL;
}
free(str_t);
return 0;
}