-
Notifications
You must be signed in to change notification settings - Fork 0
/
myshell.c
336 lines (291 loc) · 11.9 KB
/
myshell.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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <ctype.h>
#include <sys/wait.h>
#include <sys/queue.h>
#include <sys/types.h>
// MAX SIZE Definitions
#define COMMAND_MAX_SIZE 100
#define HISTORY_MAX_SIZE 10
// Flags
static char isPipeUsed = 0;
static char background = 0;
// Directory
static char directory[COMMAND_MAX_SIZE];
// Delimeters
static char spaceDelimeter[2] = " ";
static const char pipeDelimeter[2] = "|";
static const char ampersandDelimeter[2] = "&";
// Command Array
static char command[COMMAND_MAX_SIZE];
static unsigned historyCount = 0;
// Array Pointers
static char *historyArr[HISTORY_MAX_SIZE];
static const char *builtinArr[4] = {"cd", "dir", "history", "bye"};
// We use '\0' here to achieve null terminated elements.
// NULL is used for to point pointers to nothing
// 0 is used for integers
// Source: https://stackoverflow.com/questions/35331819/c-when-should-char-be-null-terminated
static char *argv[100] = { "\0" };
static char *argv2[100] = { "\0" };
// For the history we will use last in first printed system.
// Function prints the elements in the last to first order.
void printHistoryArrayInReverseOrder(char** histArr, unsigned historyCount){
// Assign i to historyCount and print commands in the newest to oldest order!
printf("Command History\n");
for(int i=historyCount-1;i>=0;i--){
printf("[%d] %s\n", i+1, histArr[i]);
}
}
// For the history we will use first in first out system.
// Functions takes a command and an array
// If array does not exceed max size then adds the command to the proper place
// Else it frees the first argument slither other arguments one index below and adds given command to the last.
void addCommandToHistory(const char *command, char** historyArr)
{
if (historyCount < HISTORY_MAX_SIZE) {
historyArr[historyCount++] = strdup( command );
} else {
free( historyArr[0] );
for (unsigned index = 1; index < HISTORY_MAX_SIZE; index++) {
historyArr[index - 1] = historyArr[index];
}
historyArr[HISTORY_MAX_SIZE - 1] = strdup( command );
}
}
// Function takes a char pointer after removing whitespaces from it
// Returns a substring of the str.
// Source: https://stackoverflow.com/questions/122616/how-do-i-trim-leading-trailing-whitespace-in-a-standard-way
char *trimwhitespace(char *str)
{
char *end;
// Trim leading space
while(isspace((unsigned char)*str)) str++;
if(*str == 0) // Empty string
return str;
// Trim trailing space
end = str + strlen(str) - 1;
while(end > str && isspace((unsigned char)*end)) end--;
// Write new null terminator character
end[1] = '\0';
return str;
}
char** sliceFunction(char * command, char* delimeter, char** argvArr){
static char *tempToken;
// Clean up argument array in every segmentation
memset(argvArr,'\0',100);
tempToken = strtok(command, delimeter);
int tokenCounter = 0;
while( tempToken != NULL ) {
// Trim the whitespace from token to make valid comparison
trimwhitespace(tempToken);
// Record argument to the argv array.
argvArr[tokenCounter] = strdup(tempToken);
// Continue to split command
tempToken = strtok(NULL, spaceDelimeter);
tokenCounter++;
}
return argvArr;
}
int main()
{
while (1) {
/// Printing shell name.
printf("myshell> ");
/// Read line
fgets(command, 101, stdin);
// Trim the whitespace from command
trimwhitespace(command);
// Save command to the historyArr before parsing.
addCommandToHistory(command,historyArr);
/// Parse the command
// Check if the pipe is used or not
char *secondCmdPtr = strchr(command,*pipeDelimeter);
// Check if the background process is used or not.
char *ampersandPtr = strchr(command,*ampersandDelimeter);
if(secondCmdPtr == NULL && ampersandPtr == NULL){ // There is not any pipe or ampersand
isPipeUsed = 0;
background = 0;
// Split the command
sliceFunction(command,spaceDelimeter,argv);
}
else if(secondCmdPtr != NULL && ampersandPtr == NULL){ // Only pipe used
isPipeUsed = 1;
background = 0;
*secondCmdPtr = '\0';
char * secondCommand = secondCmdPtr + 1;
// Split first part of the command
sliceFunction(command,spaceDelimeter,argv);
// Split second part of the command
sliceFunction(secondCommand,spaceDelimeter,argv2);
}
else if(secondCmdPtr == NULL && ampersandPtr != NULL){// Only ampersand used
isPipeUsed = 0;
background = 1;
*ampersandPtr = '\0';
// Split the command
sliceFunction(command,spaceDelimeter,argv);
}
else{ // If pipe and ampersand both used.
isPipeUsed = 1;
background = 1;
*secondCmdPtr = '\0';
*ampersandPtr = '\0';
char * secondCommand = secondCmdPtr + 1;
// Split first part of the command
sliceFunction(command,spaceDelimeter,argv);
// Split second part of the command
sliceFunction(secondCommand,spaceDelimeter,argv2);
}
/// Parse is finished
/// If the command is built in
// If the command is cd
if(strcmp(argv[0],builtinArr[0]) == 0){
// Printing initial working directory
printf("Initial Directory: %s\n", getcwd(directory, 100));
if(!argv[1]){ // Argument is not present.
// Print initial pwd value
printf("%s | Initial Pwd Value\n", getenv("PWD"));
// Change directory to $HOME Path
chdir(getenv("HOME"));
// Printing current working directory
printf("Current Directory: %s\n", getcwd(directory, 100));
// Update pwd
setenv("PWD", getenv("HOME"),1);
// Print updated pwd value
printf("%s | updated Pwd Value\n",getenv("PWD"));
}
else{ // Argument is present.
// We will check the first argument and change the directory accordingly.
if(chdir(argv[1]) == -1){ // If argument is not valid
printf("Error: There is not any directory with given name!\n");
}
else{ // Argument is valid
// Printing current working directory
printf("Current Directory: %s\n", getcwd(directory, 100));
}
}
}
// If the command is dir
else if(strcmp(argv[0],builtinArr[1]) == 0){
// Printing initial working directory.
printf("Current Directory: %s\n", getcwd(directory,100));
}
// If the command is history
else if(strcmp(argv[0],builtinArr[2]) == 0){
// Prints the history array in reverse order.
printHistoryArrayInReverseOrder(historyArr,historyCount);
}
// If the command is bye
else if(strcmp(argv[0],builtinArr[3]) == 0){
// Terminates the program
exit(0);
}
/// Not built in part (else) is started.
else{
if(!isPipeUsed && !background){ // Pipe and ampersand are not used
pid_t pid;
pid = fork();
// Pid could not created error occured. Exit with -1
if (pid < 0)
{
perror("A fork error has occurred.\n");
exit(-1);
}
/* We are in child process */
if (pid == 0){
execvp(argv[0], argv);
}
/* We are in parent process */
// We call wait because we will compile it in foreground.
wait(0);
}
else if(!isPipeUsed && background){ // Only ampersand is used!
pid_t pid;
pid = fork();
// Pid could not created error occured. Exit with -1
if (pid < 0)
{
perror("A fork error has occurred.\n");
exit(-1);
}
/* We are in the child. */
if (pid == 0) {
// The first argument is the file you wish to execute,
// and the second argument is an array of null-terminated strings that represent the appropriate arguments to the file
execvp(argv[0],argv); /* execute the command */
}
/* We are in the parent. */
}
else if(isPipeUsed && !background){ // Only pipe is used!
pid_t pid;
int fileDescripters[2];
if(pipe(fileDescripters) == -1) {
perror("Pipe failed");
exit(1);
}
if(fork() == 0) // First fork
{
close(STDOUT_FILENO); // Closing stdout
dup(fileDescripters[1]); // Replacing stdout with pipe write
close(fileDescripters[0]); // Closing pipe read
close(fileDescripters[1]); // Closing pipe write
execvp(argv[0], argv);
perror("Error: Execvp is failed!");
exit(1);
}
if(fork() == 0) // Creating 2nd child
{
close(STDIN_FILENO); // Closing stdin
dup(fileDescripters[0]); // Replacing stdin with pipe read
close(fileDescripters[1]); // Closing pipe write
close(fileDescripters[0]); // Closing pipe read
execvp(argv2[0], argv2);
perror("Error: Execvp2 is failed");
exit(1);
}
close(fileDescripters[0]);
close(fileDescripters[1]);
// We call wait because we will compile it in foreground. The number of wait is equal to child number.
wait(0);
wait(0);
}
else{ // Pipe and Ampersand are used!
pid_t pid;
int fileDescripters[2];
if(pipe(fileDescripters) == -1) {
perror("Pipe failed");
exit(1);
}
if(fork() == 0) // First fork
{
close(STDOUT_FILENO); // Closing stdout
dup(fileDescripters[1]); // Replacing stdout with pipe write
close(fileDescripters[0]); // Closing pipe read
close(fileDescripters[1]); // Closing pipe write
execvp(argv[0], argv);
perror("Error: Execvp is failed!");
exit(1);
}
if(fork() == 0) // Creating 2nd child
{
close(STDIN_FILENO); // Closing stdin
dup(fileDescripters[0]); // Replacing stdin with pipe read
close(fileDescripters[1]); // Closing pipe write
close(fileDescripters[0]); // Closing pipe read
execvp(argv2[0], argv2);
perror("Error: Execvp2 is failed");
exit(1);
}
close(fileDescripters[0]); // Close reader
close(fileDescripters[1]); // Close writer
// One child is terminated it is background process so we will not wait for second child.
wait(0);
}
}
/// Not built-in part (else) is finished
}
return 0;
}