-
Notifications
You must be signed in to change notification settings - Fork 1
/
built_ins.c
80 lines (72 loc) · 1.03 KB
/
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
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
#include "holberton.h"
/**
* _strlen - get string length.
* @s: string.
*
* Return: length.
*/
int _strlen(char *s)
{
int num = 0;
while (*(s + num) != '\0')
num++;
return (num);
}
/**
* to_exit - exit the program.
* @input: pointer to exit.
*
* Return: always.
*/
int to_exit(char *input)
{
free(input);
exit(1);
return (0);
}
/**
* printenv - print environment.
* @input: pointer to env.
*
* Return: environment.
*/
int printenv(char *input)
{
int i = 0, size = 0;
while (environ[i] != '\0')
{
size = _strlen(environ[i]);
write(1, environ[i], size);
write(1, "\n", 1);
i++;
}
free(input);
return (0);
}
/**
* built_ins - get builtins.
* @input: pointer to input.
*
* Return: builtin.
*/
int built_ins(char *input)
{
int i = 0;
builtins built_ins[] = {
{"exit", to_exit},
{"env", printenv},
{NULL, NULL}
};
if (input == NULL)
return (0);
while (built_ins[i].name != NULL)
{
if (_strcmp(input, built_ins[i].name) == 0)
{
built_ins[i].f(input);
return (0);
}
i++;
}
return (1);
}