-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgnsh.c
More file actions
223 lines (211 loc) · 5.6 KB
/
Copy pathgnsh.c
File metadata and controls
223 lines (211 loc) · 5.6 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
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include "gnsh.h"
PathList *path_list_head;
char **
createArgs(char *line, int *args_num)
{
char *temp_string = NULL;
char *separated_string = strdup(line);
char **command_args = (char **)malloc(BUFF * sizeof(char *));
int i;
for (i = 0; (temp_string = strsep(&separated_string, " \t")) != NULL; i++)
command_args[i] = temp_string;
*args_num = i;
free(temp_string);
free(separated_string);
return command_args;
}
void execCommand(char *path, char **command_args, int args_num)
{
pid_t pid = fork();
if (pid < 0)
{
fprintf(stderr, "error occured\n");
exit(1);
}
else if (pid == 0)
{
FILE *out = stdout;
if (args_num > 2 && strcmp(command_args[args_num - 2], ">") == 0)
{
if ((out = fopen(command_args[args_num - 1], "w")) == NULL)
out = stdout;
command_args[args_num - 1] = NULL;
command_args[args_num - 2] = NULL;
}
dup2(fileno(out), STDOUT_FILENO);
dup2(fileno(out), STDERR_FILENO);
if (execv(path, command_args) == -1)
{
fprintf(stderr, "error occured\n");
exit(1);
}
fclose(out);
}
else
{
waitpid(pid, NULL, 0);
}
}
void warnUnknownCommand(char *command)
{
fprintf(stderr, "gnsh: Unknown command: %s\n", command);
}
/*
return value == 0: succeeded
return value == -1: failed
*/
int handleDefaultCommand(char *command, char **command_args, PathList *path_list_head, int args_num)
{
PathList *path_list = path_list_head;
while (path_list != NULL)
{
char *bin_path = (char *)malloc(strlen(command) + strlen(path_list->path) + 1);
sprintf(bin_path, "%s/%s", path_list->path, command);
if (access(bin_path, X_OK) == 0)
{
execCommand(bin_path, command_args, args_num);
free(bin_path);
return 0;
}
free(bin_path);
path_list = path_list->next;
}
return -1;
}
/*
return value == 0: built-in command exists
return value == -1: built-in command does not exist
*/
int handleBuiltIn(char *command, char **command_args, PathList **path_list_head)
{
if (strcmp(command, "exit") == 0)
exit(EXIT_SUCCESS);
else if (strcmp(command, "cd") == 0)
{
if (command_args[2] != NULL)
fprintf(stderr, "Too many args for cd command\n");
else if (command_args[1] == NULL)
fprintf(stderr, "Specify a path to the directory\n");
else if (chdir(command_args[1]) == -1)
fprintf(stderr, "cd: The directory '%s' does not exist\n", command_args[1]);
return 0;
}
else if (strcmp(command, "path") == 0)
{
if (command_args[1] == NULL)
{
// reset paths
PathList *temp_path_list = malloc(sizeof(PathList));
while (*path_list_head != NULL)
{
temp_path_list = *path_list_head;
*path_list_head = temp_path_list->next;
}
free(temp_path_list);
}
else
{
// add paths
for (int i = 1; command_args[i] != NULL; i++)
{
PathList *next_path_list = malloc(sizeof(PathList));
if (next_path_list == NULL)
{
fprintf(stderr, "malloc failed\n");
exit(EXIT_FAILURE);
}
next_path_list->path = strdup(command_args[i]);
next_path_list->next = *path_list_head;
*path_list_head = next_path_list;
}
}
return 0;
}
return -1;
}
PathList *initializePathList(void)
{
PathList *path_list = malloc(sizeof(PathList));
path_list->path = BIN_PATH;
path_list->next = NULL;
return path_list;
}
void *handleCommand(void *arg)
{
Command *command_struct = (Command *)arg;
int *args_num = (int *)malloc(sizeof(int *));
char **command_args = createArgs(command_struct->command, args_num);
char *command = strdup(command_args[0]);
if ((handleBuiltIn(command, command_args, &path_list_head)) == 0)
return NULL;
if ((handleDefaultCommand(command, command_args, path_list_head, *args_num)) == 0)
return NULL;
warnUnknownCommand(command);
return NULL;
}
char *trimCommand(char *command)
{
char *trimmed_command = strdup(command);
// remove spaces after a command
for (int i = strlen(trimmed_command) - 1; trimmed_command[i] == ' ' || trimmed_command[i] == '\n'; i--)
trimmed_command[i] = 0;
// remove spaces before a command
while (*trimmed_command == ' ')
trimmed_command++;
return trimmed_command;
}
int main(int argc, char *argv[])
{
FILE *in = NULL;
char *line;
size_t linecap = 0;
ssize_t input_char_num;
path_list_head = initializePathList();
if (argc > 2)
{
fprintf(stderr, "Only one input file is supported");
exit(1);
}
// interactive mode
if (argc == 1)
in = stdin;
// batch mode
else if (argc == 2)
if ((in = fopen(argv[1], "r")) == NULL)
{
fprintf(stderr, "There is no such file");
exit(EXIT_FAILURE);
}
while (1)
{
if (argc == 1)
printf("gnsh> ");
input_char_num = getline(&line, &linecap, in);
if (input_char_num == -1)
{
fclose(in);
exit(EXIT_SUCCESS);
}
if (strcmp(line, "\0") == 0)
continue;
char *temp_line = strdup(line);
Command *commands = (Command *)malloc(BUFF * sizeof(Command *));
char *temp_command;
int command_num;
for (command_num = 0; (temp_command = strsep(&temp_line, "&")) != NULL; command_num++)
{
char *trimmed_command = trimCommand(temp_command);
commands[command_num].command = strdup(trimmed_command);
}
for (int i = 0; i < command_num; i++)
pthread_create(&commands[i].thread, NULL, &handleCommand, &commands[i]);
for (int i = 0; i < command_num; i++)
pthread_join(commands[i].thread, NULL);
free(commands);
}
return 0;
}