-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandle_builtin.c
68 lines (61 loc) · 1.61 KB
/
handle_builtin.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
#include "shell.h"
int is_builtin(char *command)
{
char *builtins[] = {
"exit", "env", "setenv", "cd", NULL
};
int i = 0;
for (i = 0; builtins[i]; i++)
{
if(_compare(command, builtins[i]) == 0)
return (1);
}
return (0);
}
void handle_builtin(char **command, char**argv, int *status, int idx)
{
if(_compare(command[0], "exit") == 0)
exit_shell(command, argv, status, idx);
else if(_compare(command[0], "env") == 0)
print_env(command, status);
}
void exit_shell(char **command, char **argv, int *status, int idx)
{
int exit_status = (*status);
char *index, message[] = ": exit: Illegal number: ";
if(command[1])
{
if(is_positive(command[1]))
{
exit_status = _atoi(command[1]);
}
else
{
index = _itoa(idx);
write(STDERR_FILENO, argv[0], _length(argv[0]));
write(STDERR_FILENO, ": ", 2);
write(STDERR_FILENO, index, _length(index));
write(STDERR_FILENO, message, _length(message));
write(STDERR_FILENO, command[1], _length(command[1]));
write(STDERR_FILENO, "\n", 1);
free(index);
freearray2D(command);
(*status) = 2;
return;
}
}
freearray2D(command);
exit(exit_status);
}
void print_env(char **command, int *status)
{
int i = 0;
(void) status;
for(i = 0; environ[i]; i++)
{
write(STDOUT_FILENO, environ[i], _length(environ[i]));
write(STDOUT_FILENO, "\n", 1);
}
freearray2D(command);
(*status) = 0;
}