-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommands.c
299 lines (286 loc) · 7.3 KB
/
commands.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
// commands.c
//********************************************
#include "commands.h"
#include "job.h"
//********************************************
// function name: ExeCmd
// Description: interperts and executes built-in commands
// Parameters: pointer to jobs, command string
// Returns: 0 - success,1 - failure
//**************************************************************************************
int ExeCmd(pJob* jobs, char* lineSize, char* cmdString)
{
//printf ("start execmd\n");
char* cmd;
char* args[MAX_ARG];
char pwd[MAX_LINE_SIZE];
char* delimiters = " \t\n";
int i = 0, num_arg = 0;
bool illegal_cmd = FALSE; // illegal command
cmd = strtok(lineSize, delimiters);
if (cmd == NULL)
return 0;
args[0] = cmd;
for (i=1; i<MAX_ARG; i++)
{
args[i] = strtok(NULL, delimiters);
if (args[i] != NULL)
num_arg++;
}
if (history_idx < MAX_COMMANDS){
strcpy(history[history_idx], cmdString);
history_idx++;
}
else{
for (int i =0 ; i < MAX_COMMANDS-1 ; i++){
strcpy(history[i], history[i+1]);
}
strcpy(history[history_idx], cmdString);
}
/*************************************************/
// Built in Commands PLEASE NOTE NOT ALL REQUIRED
// ARE IN THIS CHAIN OF IF COMMANDS. PLEASE ADD
// MORE IF STATEMENTS AS REQUIRED
/*************************************************/
if (!strcmp(cmd, "cd") )
{
int result = 0;
char temp1[MAX_LINE_SIZE] = "\0";
char* temp2 = NULL;
if (num_arg != 1){
illegal_cmd = TRUE;
}
//num of args is valid
else{
temp2 = getcwd(temp1, MAX_LINE_SIZE);
if (temp2 == NULL){
perror("");
}
//getcwd succeeded
else{
if (!strcmp(args[1], "-")){
if (strcmp(prev_command, "\0") != 0){
result = chdir(prev_command);
}
}
else{
result = chdir(args[1]);
}
//chdir failed
if (result == -1){
printf("smash error: > \"%s\" - path not found\n", args[1]);
}
else{
strcpy(prev_command, temp2);
}
}
}
}
/*************************************************/
else if (!strcmp(cmd, "pwd"))
{
if (num_arg != 0){
illegal_cmd = TRUE;
}
else{
char temp1[MAX_LINE_SIZE] = "\0";
char* temp2 = NULL;
temp2 = getcwd(temp1, MAX_LINE_SIZE);
if (temp2 == NULL){
perror("");
}
else{
printf("%s\n", temp2);
}
}
}
/*************************************************/
else if (!strcmp(cmd, "history"))
{
if (num_arg != 0){
illegal_cmd = TRUE;
}
else{
if (history_idx<MAX_COMMANDS){
for (int j = 0; j < history_idx - 1; j++){
printf("%s\n", history[j]);
}
}
else{
for (int j = 0; j < MAX_COMMANDS - 2; j++){
printf("%s\n", history[j]);
}
}
}
}
/*************************************************/
else if (!strcmp(cmd, "jobs"))
{
if (num_arg != 0){
illegal_cmd = TRUE;
}
else{
for (int t = 0; t < job_index; t++){
printJob(jobs[t]);
}
}
}
/*************************************************/
else if (!strcmp(cmd, "showpid"))
{
if (num_arg != 0){
illegal_cmd = TRUE;
}
else{
printf("smash pid is %d\n", getpid());
}
}
/*************************************************/
else if (!strcmp(cmd, "kill"))
{
}
/*************************************************/
else if (!strcmp(cmd, "mv"))
{
if (num_arg != 2){
illegal_cmd = TRUE;
}
else{
int result = rename(args[1], args[2]);
if (result == 0){
printf("%s has been renamed to %s\n", args[1], args[2]);
}
if (result == -1){
perror("");
}
}
}
/*************************************************/
else if (!strcmp(cmd, "fg"))
{
}
/*************************************************/
else if (!strcmp(cmd, "bg"))
{
}
/*************************************************/
else if (!strcmp(cmd, "quit"))
{
}
/*************************************************/
else // external command
{
//printf("try to run external command\n");
ExeExternal(args, cmdString);
return 0;
}
if (illegal_cmd == TRUE)
{
printf("smash error: > \"%s\"\n", cmdString);
return 1;
}
return 0;
}
//**************************************************************************************
// function name: ExeExternal
// Description: executes external command
// Parameters: external command arguments, external command string
// Returns: void
//**************************************************************************************
void ExeExternal(char *args[MAX_ARG], char* cmdString)
{
int pid;
pid = fork();
//printf("new pid: %d\n", pid);
//son has created, now we will execute the command in the background
if (pid == 0) {
setpgrp(); //change the group-ID
//printf("args[0]: %s\n", args[0]);
execv(args[0], args);
perror("execv error: ");
exit(-1);
}
//parent- the shell. will wait until the program is executed
if (pid > 0) {
wait(NULL);
perror("wait: ");
return;
}
//fork failed
else {
perror("");
exit(-1);
}
}
//**************************************************************************************
// function name: ExeComp
// Description: executes complicated command
// Parameters: command string
// Returns: 0- if complicated -1- if not
//**************************************************************************************
int ExeComp(char* lineSize)
{
char ExtCmd[MAX_LINE_SIZE+2];
char *args[MAX_ARG];
if ((strstr(lineSize, "|")) || (strstr(lineSize, "<")) || (strstr(lineSize, ">")) || (strstr(lineSize, "*"))
|| (strstr(lineSize, "?")) || (strstr(lineSize, ">>")) || (strstr(lineSize, "|&")))
{
return 0;
}
return -1;
}
//**************************************************************************************
// function name: BgCmd
// Description: if command is in background, insert the command to jobs
// Parameters: command string, pointer to jobs
// Returns: 0- BG command -1- if not
//**************************************************************************************
int BgCmd(char* lineSize, pJob* jobs) {
//printf("start bgCmd\n");
int pid;
char *Command;
char *delimiters = " \t\n";
char *args[MAX_ARG];
if (lineSize[strlen(lineSize) - 2] == '&') {
lineSize[strlen(lineSize) - 2] = '\0';
int i = 0, num_arg = 0;
bool illegal_cmd = FALSE; // illegal command
Command = strtok(lineSize, delimiters);
if (Command == NULL)
return 0;
args[0] = Command;
for (i = 1; i < MAX_ARG; i++) {
args[i] = strtok(NULL, delimiters);
if (args[i] != NULL)
num_arg++;
}
pid = fork();
//son has created, now we will execute the command in the background
if (pid == 0) {
printf("start bg son");
setpgrp(); //change the group-ID
execv(args[0], args);
perror("");
exit(-1);
}
//parent- the shell. we will insert the son to the jobs array
if (pid > 0) {
time_t cur_time = time(NULL);
//printf("job_index: %d\n", job_index);
//printf("pid = %d\n", pid);
//printf("curr time: %d\n", (double)cur_time);
//printf("command: %s\n", Command);
pJob newJob = NewJob(job_index + 1, pid, cur_time, Command);
//printJob(newJob);
jobs[job_index] = newJob;
job_index++;
return 0;
}
//fork failed
else {
perror("");
exit(-1);
}
}
return -1;
}