-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
355 lines (299 loc) · 5.85 KB
/
main.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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
#include "header.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#define MAX_INPUT_SIZE 1024
#define MAX_PATH_LENGTH 4096
extern char **environ;
/**
* free_token_array - a
* @tokens: b
*
*/
void free_tokens_array(char **tokens)
{
int i = 0;
while (tokens[i] != NULL)
{
free(tokens[i]);
i++;
}
free(tokens);
}
/**
* construct_path - x
*
* @command: y
*
* Return: Always 0.
*/
int construct_path(char **command)
{
char *path = getenv("PATH");
char full_path[MAX_PATH_LENGTH];
char *token;
char *original_command;
if (path == NULL || path[0] == '\0')
return (127);
original_command = strdup(command[0]);
if (original_command == NULL)
{
perror("Memory allocation error");
exit(EXIT_FAILURE);
}
token = strtok(path, ":");
while (token != NULL)
{
snprintf(full_path, sizeof(full_path), "%s/%s", token, original_command);
if (access(full_path, F_OK | X_OK) == 0)
{
free(original_command);
free(command[0]);
command[0] = strdup(full_path);
if (command[0] == NULL)
{
perror("Memory allocation error");
exit(EXIT_FAILURE);
}
return (EXIT_SUCCESS);
}
token = strtok(NULL, ":");
}
free(original_command);
return (EXIT_FAILURE);
}
/**
* print_environment - Print environment variables in reverse order.
*/
void print_environment(void)
{
char **env = environ;
char **env_array;
int count = 0;
int i;
/* Count the number of environment variables */
while (*env != NULL)
{
count++;
env++;
}
/* Allocate an array to store environment variables */
env_array = malloc((count + 1) * sizeof(char *));
if (env_array == NULL)
{
perror("Memory allocation error");
exit(EXIT_FAILURE);
}
/* Copy environment variables to the array */
env = environ;
for (i = 0; i < count; i++)
{
env_array[i] = *env;
env++;
}
env_array[count] = NULL;
for (i = count - 1; i >= 0; i--)
printf("%s\n", env_array[i]);
free(env_array);
}
/**
* set_environment_variable - Set environment variable
*
* @variable: varaible name
* @value: value to set
*
* Return: Always 0.
*/
void set_environment_variable(char *variable, char *value)
{
if (setenv(variable, value, 1) == -1)
{
perror("setenv");
fprintf(stderr, "Failed to set environment variable %s\n", variable);
}
}
/**
* unset_environment_variable - Set environment variable
*
* @variable: varaible name
*
* Return: Always 0.
*/
void unset_environment_variable(char *variable)
{
if (unsetenv(variable) == -1)
{
perror("unsetenv");
fprintf(stderr, "Failed to unset environment variable %s\n", variable);
}
}
/**
* change_directory - change directory
*
* @directory: directory to change into
*
* Return: Always 0.
*/
void change_directory(char *directory)
{
if (directory == NULL || strcmp(directory, "-") == 0)
{
directory = getenv("HOME");
if (directory == NULL)
return;
}
if (chdir(directory) == -1)
fprintf(stderr, "./hsh: 1: cd: can't cd to %s\n", directory);
}
/**
* main - Simple shell
*
* Return: Always 0.
*/
int main(void)
{
char *command = NULL;
char *comment_start;
int num_read;
size_t n = 0;
char **argv = NULL;
pid_t child_pid;
int status;
int found_path;
int exit_status;
char **env = environ;
while (1)
{
if (isatty(0))
{
write(STDOUT_FILENO, "($) ", 4);
fflush(stdout);
}
num_read = getline(&command, &n, stdin);
if (num_read == -1)
{
if (isatty(0))
write(STDOUT_FILENO, "\n", 1);
free(command);
return (EXIT_SUCCESS);
}
command[strcspn(command, "\n")] = '\0';
comment_start = strchr(command, '#');
if (comment_start != NULL)
*comment_start = '\0';
argv = tokenize(command, " \n\t");
if (argv[0] != NULL)
{
/*built in*/
if (argv[0][0] != '/' && argv[0][0] != '.')
{
if (strcmp(argv[0], "exit") == 0)
{
exit_status = 0;
if (argv[1] != NULL)
exit_status = atoi(argv[1]);
free(command);
free_tokens_array(argv);
return (exit_status);
}
else if (strcmp(argv[0], "cd") == 0)
{
change_directory(argv[1]);
continue;
}
/* Check for the setenv built-in command */
else if (strcmp(argv[0], "setenv") == 0)
{
if (argv[1] != NULL && argv[2] != NULL)
{
set_environment_variable(argv[1], argv[2]);
continue;
}
else
{
char error_message[] = "Usage: setenv VARIABLE VALUE\n";
write(STDERR_FILENO, error_message, sizeof(error_message) - 1);
}
}
/* Check for the unsetenv built-in command */
else if (strcmp(argv[0], "unsetenv") == 0)
{
if (argv[1] != NULL)
{
unset_environment_variable(argv[1]);
continue;
}
else
{
char error_message[] = "Usage: unsetenv VARIABLE\n";
write(STDERR_FILENO, error_message, sizeof(error_message) - 1);
}
}
else if (strcmp(argv[0], "cd") == 0)
{
change_directory(argv[1]);
}
/* Check for the env built-in command */
if (strcmp(argv[0], "env") == 0)
{
while (*env != NULL)
{
printf("%s\n", *env);
env++;
}
if (!isatty(0))
return (0);
}
found_path = construct_path(argv);
if (found_path != 0)
{
fprintf(stderr, "./hsh: 1: %s: not found\n", argv[0]);
if (!isatty(0))
{
free(command);
free_tokens_array(argv);
return (127);
}
}
}
else
{
if (access(argv[0], F_OK | X_OK) == -1)
{
fprintf(stderr, "./hsh: 1: %s: not found\n", argv[0]);
free_tokens_array(argv);
if (!isatty(0))
return (127);
continue;
}
}
child_pid = fork();
if (child_pid == -1)
{
free_tokens_array(argv);
exit(EXIT_FAILURE);
}
if (child_pid == 0)
{
if (execve(argv[0], argv, environ) == -1)
exit(EXIT_FAILURE);
}
else
{
free_tokens_array(argv);
wait(&status);
}
}
else
{
free_tokens_array(argv);
if (!isatty(0))
return (0);
}
}
free(command);
return (0);
}