-
Notifications
You must be signed in to change notification settings - Fork 1
/
check_built_ins.c
50 lines (45 loc) · 937 Bytes
/
check_built_ins.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
#include "shellby.h"
/**
* check_built_ins - checks if token belongs to any built-in function.
* frees any necessary allocated memory before an exit.
* @buffer: allocated string from which commands are obtaned.
* @token: token to be check as a built-in.
*
* Return: 1 if found a valid built-in. 0 if not found.
*/
int check_built_ins(char *buffer, char *token)
{
if ((_strcmp("exit", token)) == 0)
{
free(buffer);
exit(0);
}
else if ((_strcmp("env", token)) == 0)
{
print_env();
return (1);
}
return (0);
}
/**
* print_env - prints the environment variables.
*
* Return: void
*/
void print_env(void)
{
unsigned int i, length;
if (environ == NULL || *environ[0] == '\0')
return;
/*print env*/
i = 0;
while (environ[i] != NULL)
{
/*env len*/
length = _strlen(environ[i]);
/*print env*/
write(STDOUT_FILENO, environ[i], length);
write(STDOUT_FILENO, "\n", 1);
i++;
}
}