-
Notifications
You must be signed in to change notification settings - Fork 0
/
error_code.c
69 lines (61 loc) · 1.3 KB
/
error_code.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
#include "shs.h"
/**
* error_code - calls the error according to the builtin, syntax, or permission
* @shdata: data structure that contains arguments
* @eval: error value
* Return: error value
*/
int error_code(shell_data *shdata, int eval)
{
char *error = NULL;
switch (eval)
{
case -1:
error = error_env(shdata);
break;
case 126:
error = error_path_126(shdata);
break;
case 127:
error = cmd_not_found(shdata);
break;
case 2:
if (_strcmp("exit", shdata->args[0]) == 0)
error = msg_exit_shell(shdata);
else if (_strcmp("cd", shdata->args[0]) == 0)
error = error_message(shdata);
break;
}
if (error)
{
write(STDERR_FILENO, error, _strlen(error));
free(error);
}
shdata->status = eval;
return (eval);
}
/**
* get_builtin - returns the function pointer
* to the corresponding built-in command
* @cmd: command string
* Return: function pointer to the built-in command, or NULL if not found
*/
int (*get_builtin(char *cmd))(shell_data *)
{
built_in builtin[] = {
{ "env", _env },
{ "exit", exit_shell },
{ "setenv", _setenv },
{ "unsetenv", _delenv },
{ "cd", cd_dir },
{ "help", get_help },
{ NULL, NULL }
};
int i;
for (i = 0; builtin[i].name; i++)
{
if (_strcmp(builtin[i].name, cmd) == 0)
return (builtin[i].f);
}
return (NULL);
}