-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcom_builtins1.c
More file actions
113 lines (104 loc) · 2.17 KB
/
Copy pathcom_builtins1.c
File metadata and controls
113 lines (104 loc) · 2.17 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
#include "main.h"
/**
* isbuiltin - checks if a cmd is a builtin
* @cmd: command to check
* Return: 1 if a builtin, 0 otherwise
*/
int isbuiltin(char *cmd)
{
int i = 0;
char *builtins[] = {"exit", "env", "cd", "setenv", "unsetenv", NULL};
while (builtins[i] != NULL)
{
if (_strcmp(builtins[i], cmd) == 0)
return (1);
i++;
}
return (0);
}
/**
* builtins - handling built commands
* @tokens: array of tokens
* @n_tok: number of tokens
* @buffer: the buffer
* @iter: current iteration
* Return: nothing
*/
void builtins(char **tokens, int n_tok, char *buffer, UINT iter)
{
char *cmd = tokens[0];
if (_strcmp(cmd, "exit") == 0)
builtin_exit(tokens, n_tok, buffer, iter);
else if (_strcmp(cmd, "env") == 0)
builtin_env();
else if (_strcmp(cmd, "setenv") == 0)
{
if (n_tok < 3 || n_tok > 3)
{
_puts2("setenv: Usage: setenv VARIABLE VALUE\n", STDERR_FILENO);
return;
}
builtin_setenv(tokens[1], tokens[2]);
}
else if (_strcmp(cmd, "unsetenv") == 0)
{
if (n_tok < 2 || n_tok > 2)
{
_puts2("unsetenv: Usage: unsetenv VARIABLE\n", STDERR_FILENO);
return;
}
builtin_unsetenv(tokens[1]);
}
else if (_strcmp(cmd, "cd") == 0)
builtin_cd(tokens, iter);
else
not_found(tokens, n_tok, iter);
}
/**
* builtin_exit - handles builtin command exit
* @tokens: array of tokens
* @n_tok: number of tokens
* @buffer: the buffer
* @iter: current iteration
* Return: nothing
*/
void builtin_exit(char **tokens, int n_tok, char *buffer, UINT iter)
{
int status, isvalid_status = 1;
if (tokens[1] == NULL)
status = errno;
else if (_isnumber(tokens[1]) && _atoi(tokens[1]) >= 0)
{
if (_atoi(tokens[1]) <= 255)
status = _atoi(tokens[1]);
else
status = _atoi(tokens[1]) % 256;
}
else
isvalid_status = 0;
if (isvalid_status == 1)
{
free(buffer);
free_grid(tokens, n_tok);
exit(status);
}
print_err(iter, tokens[0], "Illegal number: ");
_puts2(tokens[1], STDERR_FILENO);
_putchar2('\n', STDERR_FILENO);
errno = 2;
free_grid(tokens, n_tok);
}
/**
* builtin_env - prints the environment of the system
* Return: nothing
*/
void builtin_env(void)
{
int i = 0;
while (environ[i])
{
_puts(environ[i]);
_putchar('\n');
i++;
}
}