-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsish.c
More file actions
297 lines (271 loc) · 9.9 KB
/
sish.c
File metadata and controls
297 lines (271 loc) · 9.9 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
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
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <error.h>
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>
int getNoArgs(char *); // this function returns the number of "args" or number commands seperatted by a space
void addToHistory(char *history[], int *historyIndex, char *buffer, int *historyStart); // add's command to the history
void printHistory(char *history[], int historyStart);
void clearHistory(char *history[]);
int main(int argc, char *argv[])
{
char *history[100]; // initializes the history to have a max of 100
for (int i = 0; i < 100; i++)
{
history[i] = NULL; // set's every element to null
}
int historyStart = 0; // the pointer to the start of the history
int historyIdx = 0; // pointer to the next empty slot in the history
int fromHist = 0; // boolean for if this command is called from the history
char *cmdFromHistory; // copy of the command from history
while (1)
{
// prereqs for getline
size_t bufferSize;
char *buffer;
size_t characters;
if (fromHist)
{
// if this command is from history, we don't want to print nor get the line
fromHist = 0;
buffer = cmdFromHistory;
}
else
{
// get the string
printf("sish> ");
characters = getline(&buffer, &bufferSize, stdin);
buffer[characters - 1] = '\0';
addToHistory(history, &historyIdx, buffer, &historyStart);
}
// find the number of pipes. This is important for figuring out the amount of pipes
int noOfPipes = 0;
for (int i = 0; buffer[i] != '\0'; i++)
{
if (buffer[i] == '|')
{
noOfPipes++;
}
}
// initialize a dynamic pipe array
int *pipes = (int *)(malloc(sizeof(int) * noOfPipes * 2));
// create as many pipes we need in the array.
// odd indexes are read file descriptors, even ones are write file descriptors
for (int i = 0; i < noOfPipes; i++)
{
if (pipe(pipes + (2 * i)) == -1)
{
perror("pipe failed");
}
}
// here we tokenize the string by separating each command separated by pipes
char *token = NULL;
char *saveptr = NULL;
char *delim = "|";
// we break the loop if the token equals NULL
for (int i = 0;; buffer = NULL, i++)
{
token = __strtok_r(buffer, delim, &saveptr);
if (token == NULL)
{
break;
}
// get the number of args. This is important if
int noArgs = getNoArgs(token);
if (noArgs > 0)
{
char *argu[noArgs + 1];
char *arguTok = NULL;
char *arguDelim = " ";
char *arguSavePtr = NULL;
// tokenize and put the commands in an array
for (int j = 0;; token = NULL, j++)
{
arguTok = __strtok_r(token, arguDelim, &arguSavePtr);
argu[j] = arguTok;
if (arguTok == NULL)
{
break;
}
}
// if we found an exit command
if (strcmp(argu[0], "exit") == 0)
{
// clean up the pipes
free(pipes);
for (int i = 0; i < 100; i++)
{
if (history[i] != NULL)
{
// since every entry is manually allocated, free everything
free(history[i]);
}
}
exit(0);
}
// if we find a cd command
else if (strcmp(argu[0], "cd") == 0)
{
if (argu[1] == NULL)
{
// incase there is no dir provided
printf("cd usage: cd <directory> \n");
}
else
{
// change dir
chdir(argu[1]);
}
continue;
}
// if we have a history command
else if (strcmp(argu[0], "history") == 0)
{
// if we have more than one arrgument and the second one is '-c'
if (noArgs > 1 && strcmp(argu[1], "-c") == 0)
{
clearHistory(history);
historyIdx = 0;
}
// if we just have more than one arg, it's a number arg
else if (noArgs > 1)
{
int translatedPtr = atoi(argu[1]) + historyStart;
if (translatedPtr > 99 || history[translatedPtr] == NULL)
{
// this is beyond our history array
printf("Invalid history index");
}
// if we do have this command
else if (history[translatedPtr] != NULL)
{
cmdFromHistory = history[translatedPtr]; // get the history command
fromHist = 1; // let sish know this command is from the history
}
}
// only one arg, so it's a lone history command
else
{
printHistory(history, historyStart);
}
continue;
}
int cpid = fork();
if (cpid == -1)
{
perror("Fork failed");
}
else if (cpid == 0) // child process
{
// assign pipes
for (int j = 0; j < (noOfPipes * 2); j++)
{
if (!(j == (i * 2 - 2) || j == (i * 2 + 1)))
{
// i * 2 - 2 is the read pipe, i * 2 + 1 is the write pipe
// close the pipes we don't use
close(pipes[j]);
}
}
// redirect and close read pipe based on the rule
if (i * 2 - 2 >= 0)
{
// read pipe
// this won't run for the first cmd before the very first pipe. why?
// it has an i == 0, thus a read fid at -2, which is < 0
dup2(pipes[i * 2 - 2], STDIN_FILENO);
close(pipes[i * 2 - 2]);
}
// redirect and close write pipes based on the rule
if (i * 2 + 1 < noOfPipes * 2)
{
// write pipe
// say we have two pipes, i at the last pipe == 2.
// thus this won't run for the very last cmd since it'll attempt to write beyond the array
dup2(pipes[i * 2 + 1], STDOUT_FILENO);
close(pipes[i * 2 + 1]);
}
// execvp
if (execvp(argu[0], argu) == -1)
{
perror("execvp failed");
}
}
// parent
// here we want to close the pipes we redirected in child ONLY
// this is so that the parent doesn't interfere with inter process communications
if (i * 2 - 2 >= 0)
{
// read pipe
close(pipes[i * 2 - 2]);
}
if (i * 2 + 1 < noOfPipes * 2)
{
// write pipe
close(pipes[i * 2 + 1]);
}
// wait for child to finish executing
waitpid(cpid, NULL, 0);
}
else
{
// this execs if commands end with an empty pipe
printf("Illegal pipe '|' usage");
}
}
}
}
int getNoArgs(char *cmd)
{
int index = 0;
int noOfArgs = 0;
// if command starts with a letter
if (cmd[index] != ' ')
{
noOfArgs++;
while (cmd[index] != ' ' && cmd[index] != '\0')
{
// find the next space of end of string
index++;
}
}
// while we find a space
while (cmd[index] == ' ')
{
index++;
// if space ends in empty string, exit
if (cmd[index] == '\0')
{
return noOfArgs;
}
else if (cmd[index] != ' ')
{
// if we have potential arguments
noOfArgs++;
while (cmd[index] != ' ' && cmd[index] != '\0')
{
// take it to the next space or newline.
// though if it's a newline we return at the bottom
index++;
}
}
}
return noOfArgs;
}
void addToHistory(char *history[], int *historyIndex, char *str, int *historyStart)
{
// copy string
char *ptr = (char *)malloc(sizeof(char) * strlen(str));
strcpy(ptr, str);
// slap it into our history
history[*historyIndex] = ptr;
// increment or wrap to 0
*historyIndex = (*historyIndex + 1) % 100;
// check to see if we need to increase the history start index
if (*historyIndex == *historyStart)
{
*historyStart = (*historyStart + 1) % 100;
}
}