-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnyush.c
311 lines (253 loc) · 6.33 KB
/
nyush.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
// nyush.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <libgen.h>
#include <stdarg.h>
#include <sys/wait.h>
#include <signal.h>
#include <fcntl.h>
#include <stdbool.h>
#include "nyush.h"
#include "builtInCommands.h"
int main()
{
// ignore signals
signal(SIGINT, SIG_IGN);
signal(SIGQUIT, SIG_IGN);
signal(SIGTSTP, SIG_IGN);
while (1)
{
size_t inputSize = 100;
char *input = (char *)malloc(inputSize * sizeof(char));
int inputArraySize = 1; // We dynamically increase array size as we add elements
char **inputArray = malloc(inputArraySize * sizeof(char *));
printPrompt();
if (getUserInput(input, inputSize) != EXIT_SUCCESS)
{
freeArgumentsAndExit(2, input, inputArray);
}
splitStringBySpace(input, &inputArray, &inputArraySize);
char *builtInCommand = getBuiltInCommand(inputArray[0]);
if (strcmp(inputArray[0],"|") == 0)
{
fprintf(stderr, "Error: invalid command\n");
continue;
}
if (builtInCommand)
{
if (executeBuiltInCommand(inputArray, inputArraySize) == EXIT_FAILURE)
{
freeArgumentsAndExit(2, input, inputArray);
}
continue;
}
// command is not a built-in command
char *executablePath = getExecutablePath(inputArray[0]);
// create args
char **args = malloc((inputArraySize + 1) * sizeof(char *));
args[0] = inputArray[0];
for (int i = 1; i < inputArraySize; i++)
{
args[i] = inputArray[i];
}
args[inputArraySize] = NULL;
pid_t pid = fork();
if (pid < 0) // fork failed
{
fprintf(stderr, "Fork failed");
}
else if (pid == 0) // child process
{
char *file;
int operatorCount;
for (int i = 0; i < inputArraySize; i++)
{
// check for ">" or ">>" or "<"
bool create = strcmp(inputArray[i], ">") == 0 ? true : false;
bool append = strcmp(inputArray[i], ">>") == 0 ? true : false;
bool inputRedirect = strcmp(inputArray[i], "<") == 0 ? true : false;
if (create || append || inputRedirect) //handle i/o redirection
{
operatorCount++;
file = inputArray[i + 1];
if (file == NULL)
{
fprintf(stderr, "Error: invalid command\n");
exit(-1);
}
// modify args to only include part before ">" ">>" or "<"
args[i] = NULL;
int fd;
if (inputRedirect)
{
int fd = open(file, O_RDONLY);
if (fd == -1)
{
fprintf(stderr, "Error: invalid file\n");
exit(-1);
}
dup2(fd, 0); //duplicate the file descriptor to standard input
close(fd);
continue;
}
// output redirect
if (create)
{
fd = open(file, O_CREAT | O_WRONLY | O_TRUNC, S_IRUSR | S_IWUSR);
}
else //append
{
fd = open(file, O_CREAT | O_WRONLY | O_APPEND, S_IRUSR | S_IWUSR);
if (fd == -1)
{
fprintf(stderr, "Error: invalid file\n");
exit(-1);
}
}
dup2(fd, 1); // duplicate the file descriptor to standard output
close(fd);
// After this point any calls to stdout will write to "file";
continue;
}
}
execv(executablePath, args);
// TO DO: check if we have access to file with access()
// execv error
fprintf(stderr, "Error: invalid program\n");
exit(-1);
}
else // parent process
{
int status;
waitpid(-1, &status, 0);
}
free(args);
free(executablePath);
}
}
void printPrompt()
{
fflush(stdout);
char cwd[256];
if (getcwd(cwd, sizeof(cwd)) == NULL)
{
perror("getcwd() error");
}
printf("[nyush %s]$ ", basename(cwd));
fflush(stdout);
}
// https://c-for-dummies.com/blog/?p=1112
int getUserInput(char *buffer, size_t bufsize)
{
if (getline(&buffer, &bufsize, stdin) == -1)
{
if (!feof(stdin))
{
perror("Error reading input with getLine()");
}
// the user has pressed Ctrl+D
return EXIT_FAILURE;
}
// change \n at the end of the buffer to \0 so it's a null terminated string
buffer[strcspn(buffer, "\n")] = '\0';
return EXIT_SUCCESS;
}
void splitStringBySpace(char *inputString, char ***outputArrayPtr, int *arraySize)
{
char **outputArray = *outputArrayPtr;
int tokenCount = 0;
char *token = strtok(inputString, " ");
while (token != NULL)
{
if (tokenCount == *arraySize)
{
(*arraySize)++;
outputArray = realloc(outputArray, sizeof(char *) * *arraySize);
*outputArrayPtr = outputArray;
}
outputArray[tokenCount] = token;
token = strtok(NULL, " ");
tokenCount++;
}
}
void freeArgumentsAndExit(int argumentCount, ...)
{
va_list args;
va_start(args, argumentCount);
for (int i = 0; i < argumentCount; i++)
{
void *ptr = va_arg(args, void *);
free(ptr);
}
va_end(args);
exit(0);
}
char *getExecutablePath(char *command)
{
if (strchr(command, '/') == 0) // Just the base name
{
char *path = "/bin";
char *result = (char *)malloc(strlen(path) + strlen(command) + 2);
sprintf(result, "%s/%s", path, command);
return result;
}
if (command[0] == '/') // abosolute path
{
char *result = (char *)malloc(strlen(command) + 1); // return a new string to avoid double free() errors
strcpy(result, command);
return result;
}
// relative path
char cwd[256];
if (getcwd(cwd, sizeof(cwd)) == NULL)
{
perror("getcwd() error");
}
char *result = (char *)malloc(strlen(cwd) + strlen(command) + 2);
sprintf(result, "%s/%s", cwd, command);
return result;
}
char *getProgramName(char *command)
{
char *delimiter = strrchr(command, '/');
if (delimiter == NULL) // only base name
{
char *result = (char *)malloc(strlen(command) + 1); // return a new string to avoid double free() errors
strcpy(result, command);
return result;
}
int length = delimiter - command;
char *result = malloc(length + 1);
strncpy(result, command, length);
result[length] = '\0';
return result;
}
int outputRedirection(char ***inputArray, int size, char ***args)
{
char *file;
for (int i = 0; i < size; i++)
{
if (strcmp(*inputArray[i], ">") == 0)
{
file = *inputArray[i + 1];
if (file == NULL)
{
fprintf(stderr, "Error: invalid command");
}
// modify args to only include part before >
*args[i] = NULL;
break;
}
if (i == size - 1) // there is no ">"
{
return EXIT_FAILURE;
}
}
int fd = open(file, O_CREAT | O_WRONLY | O_TRUNC, S_IRUSR | S_IWUSR);
dup2(fd, 1); // duplicate the file descriptor
close(fd); // close the unused file descriptor
// After this point any calls to stdout will write to "file"
return EXIT_SUCCESS;
}