generated from cs-muic/icsh-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathicsh.c
173 lines (160 loc) · 4.61 KB
/
icsh.c
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
/* ICCS227: Project 1: icsh
* Name: Yossatorn Phithakjiraroj
* StudentID: 6280942
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <unistd.h>
#include <sys/wait.h>
#include <signal.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/types.h>
#define MAX_CMD_BUFFER 255
int i, status, pid;
char *res, last_command[MAX_CMD_BUFFER], *argv[3];
void my_history(char *), my_read(char *);
int command(char *);
struct test
{
pid_t pid;
int stage; // 0-> Non, 1=running, 2=stop, 3=done
char str[100];
};
struct test tli[100]; // -> tli [1] [2] ....
// -> tli [test1] ....
int addStruct(struct test *tli, pid_t pid, char *str, int stage);
void handler(int signum) {
if (signum == SIGINT && pid == 0) {
kill(pid, SIGINT);
} else if (signum == SIGTSTP && pid == 0) {
signal(SIGTSTP, SIG_DFL);
}
}
int main(int argc, char * argv []) {
char buffer[MAX_CMD_BUFFER];
struct sigaction new_action;
sigemptyset(&new_action.sa_mask);
new_action.sa_handler = handler;
new_action.sa_flags = 0;
sigaction(SIGINT, &new_action, NULL);
sigaction(SIGTSTP, &new_action, NULL);
if (argc > 1) {
my_read(argv[1]);
}
while(1) {
printf("icsh $ ");
fgets(buffer, 255, stdin);
command(buffer);
}
}
int command(char *buffer) {
if (strstr(buffer, "echo")){
my_history(buffer);
for (res = buffer ; *res && *res != ' ' ; res++)
;
if (*res) res++;
printf("%s", res);
}
else if (strstr(buffer, "!!")){
strcpy(buffer, last_command);
// printf("%s", buffer);
command(buffer);
}
else if (strstr(buffer, "exit")) {
for (res = buffer ; *res && *res != ' ' ; res++)
;
if (*res) res++;
if (atoi(res) && atoi(res) >= 1) {
// printf("bye\n");
exit(atoi(res));
}
// else { printf("bad command\n"); }
} else if (strstr(buffer, ">")) {
char * token = strtok(buffer, ">");
char *p = strrchr(buffer, ' ');
int file_desc = open(p+2, O_TRUNC | O_CREAT | O_WRONLY, 0666);
if ((pid=fork())==0){
dup2(file_desc, 1);
system(token);
close(file_desc);
}
close(file_desc);
} else if (strstr(buffer, "help")) {
printf("This shell's built-in function contains\n");
printf("--> echo <text> -- the echo command prints a given text (until EOL) back to the console.\n");
printf("--> echo $? -- this prints out the exit status code of the previous command. You may assume that all build-in commands exits with exit code 0\n");
printf("--> In the case of >, the output of the program will be redirected to the specified file name.\n");
printf("--> jobs - list current jobs \n");
printf("--> fg %c<job_id>: Brings the job identified by <job_id> into the foreground.\n", '%');
printf("--> etc.\n")
} else if (strstr(buffer, "jobs")) {
for (int i = 1; i < 100; i++) {
if (tli[i].pid != 0) {
if (tli[i].stage == 1) {
char Stage[] = "Running";
printf("[%d] %s %s", i, Stage, tli[i].str);
} else if (tli[i].stage == 2) {
char Stage[] = "Stopped";
printf("[%d] %s %s", i, Stage, tli[i].str);
}
}
}
} else if (strstr(buffer, "fg")){
for (res = buffer ; *res && *res != '%' ; res++)
;
char *b = res + 1;
int i = atoi(b);
kill(-(tli[i].pid), SIGCONT);
waitpid(tli[i].pid, NULL, WUNTRACED);
tli[i].stage = 3;
return 1;
} else if (strstr(buffer, "&")) {
pid = fork();
if (pid == 0) { // <-- Child
char * token = strtok(buffer, "&");
system(token);
printf("\n Done %s", token);
exit(0);
} else {
int job_id = addStruct(tli, pid, buffer, 1);
printf("[%d] %d \n", job_id, pid);
return 1;
}
} else {
pid = fork();
if (pid < 0) {
perror("Fork Error");
} else if (pid == 0) {
system(buffer);
exit(0);
}
waitpid(pid, NULL, 0);
}
return 1;
}
void my_history(char *buffer){
strcpy(last_command, buffer);
}
void my_read(char *filename) {
FILE* ptr;
ptr = fopen(filename, "r");
char line[500];
while (fgets(line, sizeof(line), ptr)) {
command(line);
}
fclose(ptr);
}
int addStruct(struct test *tli, pid_t pid, char *str, int stage){
for (int i = 1; i < 100; i++){
if (tli[i].pid == 0) {
tli[i].pid = pid;
tli[i].stage = stage;
strcpy(tli[i].str, str);
return i;
}
}
return -1;
}