forked from glyif/simple_shell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
executeA.c
143 lines (129 loc) · 2.96 KB
/
executeA.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
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include "header.h"
/**
* exec_builtins - custom function to execute builtin commands
* @arginv: arguments inventory
*
* Return: 1 on success, 0 on failure
*/
int exec_builtins(arg_inventory_t *arginv)
{
int i, retval;
/* old_stdout */
char *str, **commands;
builtins_t builtins_list[] = {
{"arsine", _arsine}, {"env", _env}, {"setenv", _setenv},
{"unsetenv", _unsetenv}, {"history", _history}, {"cd", _cd},
{"alias", _alias}, {"unalias", _unalias}, {"help", the_help},
{"exit", the_exit},
{NULL, NULL}
};
retval = EXT_FAILURE;
commands = (char **)arginv->commands;
/* old_stdout = redirect_output(arginv, 0); */
for (i = 0; ((str = builtins_list[i].command) != NULL); i++)
{
if (_strcmp(str, commands[0]) == 0)
{
retval = builtins_list[i].builtin_func(arginv);
break;
}
}
/*
* if (arginv->io_redir == TOKEN_REWRITE || arginv->io_redir ==
* TOKEN_APPEND || arginv->pipeout)
* {
* safe_dup2(old_stdout, STDOUT_FILENO);
*
* close(old_stdout);
* }
*/
arginv->exit_status = retval;
return (retval);
}
/**
* exec_error_exit - frees all and exits if exec error
* @msg: message to display
* @command: command to free
* @_environ: env double pointer to free
* @arginv: arg inventory to free
*/
void exec_error_exit(char *msg, char *command, char **_environ,
arg_inventory_t *arginv)
{
delete_pipeline(&arginv->pipeline);
delete_parser(&arginv->parser);
delete_tokens(&arginv->tokens);
free(command);
free_paths(_environ);
freeall(arginv);
_perror(msg);
exit(1);
}
/**
* exec_path - custom function to execute from PATH
* @command: command to execute
* @arginv: arg inventory
*
* Return: pid of parent
*/
pid_t exec_path(char *command, arg_inventory_t *arginv)
{
pid_t pid;
char **_environ;
pid = fork();
if (pid < 0)
{
_perror("Critical error: unable to fork()!\n");
exit(1);
}
if (pid == 0)
{
_environ = zelda_to_ganondorf(arginv->envlist);
/*
* redirect_output(arginv, 1);
* if (redirect_input(arginv))
* exec_error_exit("No such file or directory\n", command,
*_environ, arginv);
*/
if (execve(command, (char **)arginv->commands, _environ) < 0)
exec_error_exit("No Command\n", command, _environ, arginv);
}
free(command);
return (pid);
}
/**
* execute - completes execution of input commands
* @arginv: arguments inventory
*
* Return: void
*/
pid_t execute(arg_inventory_t *arginv)
{
env_t *envlist;
char **commands;
char *path, *command;
char **paths;
envlist = arginv->envlist;
commands = (char **)arginv->commands;
command = safe_malloc(sizeof(char) * BUFSIZE);
command = _strcpy(command, *commands);
if (exec_builtins(arginv) == EXT_FAILURE)
{
if (is_path(command))
{
return (exec_path(command, arginv));
}
else
{
path = safe_malloc(sizeof(char) * BUFSIZE);
locate_path(path, envlist);
paths = tokenize_path(path);
cat_path(paths, command);
free_paths(paths);
free(path);
return (exec_path(command, arginv));
}
}
free(command);
return (-1);
}