-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.c
251 lines (212 loc) · 6.24 KB
/
test.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/wait.h>
void tst_error()
{
char error_message[30] = "An error has occurred\n";
write(STDERR_FILENO, error_message, strlen(error_message));
exit(1);
}
int changeDir(char** comand)
{
char* cdCommand = "cd";
if(strcmp(comand[0], cdCommand) == 0)
{
if(comand[1] != NULL && comand[2] == NULL && access(comand[1], R_OK)==0)
{
chdir(comand[1]);
return 1;
}
if(comand[2] != NULL || comand[1] == NULL)
{
printf("Only accept one argument.\n");
}
else if(access(comand[1], R_OK)!=0)
{
printf("Directory not found : %s\n", comand[1]);
}
}
return 0;
}
//Function to print current directory
void printPrompt()
{
char dir[1024];
getcwd(dir, sizeof(dir));
printf("\033[01;33m"); //change color
printf("%s", dir); //print current dir path
printf("\033[0m"); //reset color
printf(" >> ");
}
char ** tst_path(char *a[])
{
int size = 1;
int bcount = 0;
char **b = malloc(sizeof(char*));
while(a[size] != NULL)
{
b[bcount] = malloc(strlen(a[size])+1);
strcpy(b[bcount], a[size]);
bcount++;
size++;
}
b[bcount] = NULL;
return b; //should return char** which is array of strings
}
char** split(char* orig, char* delim, int* counter){
char **arr = malloc(sizeof(char*));
char *tok = strtok(orig, delim); //parse arguments
int currindex = 0;
//traverse token
while(tok != NULL){
arr[currindex] = tok;
// printf("%d - arg %d: %s\n", getpid(), currindex, argTok);
currindex++;
arr = realloc(arr, (currindex+1) * sizeof(char*));
tok = strtok(NULL, delim);
}
arr[currindex] = NULL;
if (counter != NULL)
*counter = currindex;
return arr;
}
char* removeSpaces(char* s)
{
char* cpy = s; // an alias to iterate through s without moving s
char* temp = s;
while (*cpy)
{
if (*cpy != ' ' && *cpy != '\t')
*temp++ = *cpy;
cpy++;
}
*temp = 0;
//printf("wo white space :%s\n", s); // This prints out the desired result: abbcccd
return s;
}
/*
free each element in array that is dynamically allocated
*/
void freeArr(char** s){
for(int i = 0; s[i] != NULL; i++){
free(s[i]);
}
free(s);
}
int
main(int argc, char *argv[])
{
char *line = NULL;
size_t len = 0;
ssize_t nread;
FILE* input; //input source
char **paths = malloc(2 * sizeof(char*));
//default paths
paths[0] = strdup("/bin");
paths[1] = NULL;
//check if batch file exists
if(argv[1] != NULL){
if(access(argv[1], R_OK) == 0){
input = fopen(argv[1], "r");
} else {
tst_error(); //error if path invalid
free(paths);
exit(0);
}
} else{
input = stdin;
printPrompt();
}
while ((nread = getline(&line, &len, input)) != -1) {
//exit
if(strcmp(line, "exit\n") == 0){
exit(EXIT_SUCCESS);
}
int commandCount; //number of commands
char **commandArr = split(line, "&\n", &commandCount); //create array of commands
int procressCount = 0; //number of processes called
//traverse array of commands
for(int i = 0; i < commandCount; i++){
int redirCount = 0;
//parse redirection
char **redir = split(commandArr[procressCount], ">", &redirCount);
if(redirCount > 2){
tst_error();
free(redir);
continue;
}
int argCount = 0; // arg count in command
char **arg = split(redir[0], " :\t", &argCount); //create array of arg
int child = -1;
//printf("%s", arg[0]);
if (arg[0] != NULL){
if(strcmp(arg[0], "cd") == 0){
changeDir(arg); //change directory
}
else if(strcmp(arg[0], "path") == 0){
freeArr(paths);
paths = tst_path(arg); // change path
printf("Paths changed");
}
else{
child = fork();
}
}
//child procress
if(child == 0){
int ofileCount = 0;
if(redirCount == 2){
char ** output = split(redir[1], " \t", &ofileCount);
close(STDOUT_FILENO);
if(ofileCount == 1)
open(output[0], O_CREAT|O_WRONLY|O_TRUNC, S_IRWXU);
}
if(strcmp(arg[0], "exit") == 0 || ofileCount > 1){
tst_error();
}
//exec
else{
char* wholename = NULL;
int i;
//traverse array of paths
for(i = 0; paths[i] != NULL; i++){
wholename = malloc(sizeof(paths[i]) + sizeof(arg[0]) + 2);
strcpy(wholename, paths[i]);
strcat(wholename,"/");
strcat(wholename, arg[0]);
if (access(wholename, X_OK) == 0){ //check if execeutable
execv(wholename,arg); //run
free(wholename);
break;
}
}
free(wholename);
if(paths[i] == NULL){
printf("command not found\n");
}
}
}
else{
procressCount++; //increment no. of process called
}
free(arg);
free(redir);
if(child == 0)
exit(0);
}
for(int i=0; i < commandCount; i++){
wait(NULL);
}
free(commandArr);
if(input == stdin){
printPrompt();
}
}
freeArr(paths);
free(line);
exit(EXIT_SUCCESS);
}