-
Notifications
You must be signed in to change notification settings - Fork 0
/
_built_in.c
214 lines (203 loc) · 4.42 KB
/
_built_in.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
#include "main.h"
/**
* _exit_ - exits the shell terminal.
* @parse: the command to be checked.
*
* Return: void
*/
void _exit_(char **parse)
{
int length = 0, status = 0, i, check = 0;
char **ptr = parse;
while (ptr[length] != NULL)
length++;
if (length == 1)
logical_counter = 0, should_run = 0, exit(status);
else if (length == 2)
{
for (i = 0; ptr[1][i]; i++)
{
if (ptr[1][i] < 48 || ptr[1][i] > 57)
{
if (i > 0 || ptr[1][i] != 45)
{
check = 1;
break;
}
}
}
if (check == 1 || (_strlen(ptr[1]) == 1 && ptr[1][0] == 45))
logical_counter = 1, exit_status = 2,
_exit_error(ptr[1]);
else
{
status = _atoi(ptr[1]);
if (status < 0)
logical_counter = 1, exit_status = 2,
_exit_error(ptr[1]);
else
{
logical_counter = 0, should_run = 0;
exit(status);
}
}
}
else
logical_counter = 1, exit_status = 2, _exit_error(ptr[1]);
}
/**
* _get_function - gets a corresponding function based on the command given.
* @cmd: the command name to be checked.
*
* Return: a pointer to the corresponding function,
* otherwise NULL if it fails.
*/
void (*_get_function(char *cmd))(char **)
{
builtin bif[] = {
{"exit", _exit_},
{"env", _env_},
{"setenv", _setenv_},
{"unsetenv", _unsetenv_},
{"cd", _cd_}
};
int i = 0;
/* Iterate through the array of built-in functions */
while (i < 5)
{
/*
* Check the given command it it matches the name of
* the current built-in function.
*/
if (_strcmp(cmd, bif[i].cmd_name) == 0)
return (bif[i].function);
i++;
}
return (NULL);
}
/**
* _env_ - prints the current environment.
* @parse: the command to be checked, not used here.
*
* Return: void.
*/
void _env_(char **parse)
{
int length = 0, i = 0;
char **ptr = parse, **ptr_env = environ, *msg;
while (ptr[length] != NULL)
length++;
if (length == 1)
{
while (ptr_env[i] != NULL)
{
_print(ptr_env[i]);
_print("\n");
i++;
}
logical_counter = 0; /* Command succeeds */
}
else
{
logical_counter = 1; /* Command fails */
exit_status = 127;
msg = _env_error(ptr[1]);
if (access(msg, F_OK) == -1)
perror(msg);
}
}
/**
* _setenv_ - initialize a new environment variable, or modify an existing one.
* @parse: the command to be checked.
*
* Return void.
*/
void _setenv_(char **parse)
{
int len_parse = 0, i = 0, j = 0, len_new_line, len_name;
char *env_value, **new_env = environ;
while (parse[len_parse] != NULL)
len_parse++;
if (len_parse != 3)
{
logical_counter = 1; /* Command fails */
_print("Command syntax: setenv VARIABLE VALUE\n");
exit_status = 127;
}
else
{
len_name = _strlen(parse[1]);
len_new_line = len_name + _strlen(parse[2]) + 2;
env_value = _getenv(parse[1]);
if (env_value != NULL)
{
while (new_env[j] != NULL && _strncmp(parse[1],
new_env[j], len_name) != 0)
j++;
new_env[j] = _realloc(new_env[j],
len_name + _strlen(env_value) + 2, len_new_line);
if (new_env[j] == NULL)
perror("Error: realloc\n");
_snprint(new_env[j], len_new_line, parse[1], parse[2]);
}
else
{
while (new_env[i] != NULL)
i++;
new_env[i] = malloc(len_new_line);
if (new_env[i] == NULL)
perror("Error: malloc\n");
_snprint(new_env[i], len_new_line, parse[1], parse[2]);
new_env[i + 1] = NULL;
}
logical_counter = 0; /* Command succeeds */
}
}
/**
* _unsetenv_ - removes an environment variable.
* @parse: the command to be checked.
*
* Return: void.
*/
void _unsetenv_(char **parse)
{
char **ptr = parse, **env = environ;
int len_parse, len_var, i, j, count = 0, k, l, m;
len_parse = _ptrlen(ptr);
if (len_parse != 2)
logical_counter = 1,
_print("Command syntax: unsetenv VARIABLE\n"),
exit_status = 127;
else
{
len_var = _strlen(parse[1]);
for (i = 0; env[i] != NULL; i++, count = 0)
{
for (j = 0; parse[1][j] != '\0' && j < len_var; j++)
if (parse[1][j] == env[i][j])
count++;
if (len_var == count)
break;
}
if (len_var == count)
{
for (k = i; env[k] != NULL && env[k + 1] != NULL; k++)
{
for (l = 0; env[k][l] != '\0'; l++)
env[k][l] = 0;
for (m = 0; env[k + 1][m] != '\0'; m++)
;
if (l < m)
env[k] = _realloc(env[k], l, m);
for (l = 0; env[k + 1][l] != '\0'; l++)
env[k][l] = env[k + 1][l];
}
free(env[k]), env[k] = NULL, free(env[k + 1]),
logical_counter = 0; /* Command succeeds */
}
else
logical_counter = 1,
_print("Error: VARIABLE not found\n"),
exit_status = 127;
}
}