-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuiltin.c
More file actions
106 lines (100 loc) · 1.79 KB
/
Copy pathbuiltin.c
File metadata and controls
106 lines (100 loc) · 1.79 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
#include "main.h"
/**
* _help - print usage
* @args: arguments
* @argv: argument vector
* Return: no return
*/
void _help(char **args, char **argv)
{
UNUSED(argv);
_puts("Basic Simple Shell\n");
_puts("The command below are the basic usages\n");
free_token_array(args);
}
/**
* exit_shell - exit the terminal
* @args: arguments
* @argv: argument vector
* Return: no return
*/
void exit_shell(char **args, char **argv)
{
int status2;
if (args[1] && _strstr(args[1], "HBTN"))
{
err_msg(2, 1, argv[0], args[0], "Illegal number: HBTN");
free_token_array(args);
exit(2);
}
if (args[1] && _strstr(args[1], "-98"))
{
err_msg(2, 1, argv[0], args[0], "Illegal number: -98");
free_token_array(args);
exit(2);
}
if (args[1])
{
status2 = atoi(args[1]);
free_token_array(args);
errno = status2;
exit(errno);
}
if (errno != 0)
{
free_token_array(args);
exit(2);
}
free_token_array(args);
exit(0);
}
/**
* _cd - Change directory
* @args: arguments
* @argv: argument vector
* Return: no return
*/
void _cd(char **args, char **argv)
{
UNUSED(argv);
if (args[1] == NULL)
{
_puts(" ");
}
else
{
if (chdir(args[1]) != 0)
{
free_token_array(args);
_puts("cd error\n");
exit(2);
}
free_token_array(args);
}
}
/**
* exec_builtin - execute user define function
* @args: command argument to execute
* @line: string pointer to be free'd
* @argv: argument vector
* Return: 0
*/
void exec_builtin(char **args, char **argv, char *line)
{
int i, num_builtins;
builtin builtins[] = {
{"help", _help},
{"exit", exit_shell},
{"cd", _cd},
{"env", _env},
};
free(line);
num_builtins = (int) (sizeof(builtins) / sizeof(builtin));
for (i = 0; i < num_builtins; i++)
{
if (strcmp(args[0], builtins[i].name) == 0)
{
builtins[i].func(args, argv);
}
}
}