-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuiltin_commands.c
53 lines (47 loc) · 1000 Bytes
/
builtin_commands.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
#include "main.h"
/**
* builtin_exit - Exit the shell
* @argv_exec: Null-terminated array of arguments
*
* Return: Does not return because the function exits the shell
*/
int builtin_exit(char **argv_exec)
{
(void)argv_exec;
exit(0);
}
/**
* builtin_env - Print the environment
* @argv_exec: Null-terminated array of arguments
*
* Return: 0 on success
*/
int builtin_env(char **argv_exec)
{
(void)argv_exec;
print_env();
return (0);
}
/**
* execute_builtin_command - Execute a builtin command
* @argv_exec: Null-terminated array of arguments
*
* Return: 0 on success, or -1 if the command is not a builtin command
*/
int execute_builtin_command(char **argv_exec)
{
builtin_command_t builtin_commands[] = {
{"exit", builtin_exit},
{"env", builtin_env},
{NULL, NULL}
};
int i;
for (i = 0; builtin_commands[i].name != NULL; i++)
{
if (strcmp(argv_exec[0], builtin_commands[i].name) == 0)
{
return (builtin_commands[i].func(argv_exec));
}
}
return (-1);
}