forked from mlgomez0/simple_shell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exec_handler.c
41 lines (38 loc) · 976 Bytes
/
exec_handler.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
#include"holberton.h"
/**
*exec_handler - creates a child process and schedule execution
*@_args: arguments to use in the execution of the program
*@av: parameter to indicate the name of the shell
*@env:environ variable
*@cicles: number of times parameters are passed
*Return: Integer 1 or 0
*/
int exec_handler(int *cicles, char **_args, char **av, char **env)
{
int status, childPID, _flag = 0, no_path;
char *_str_ev = NULL;
no_path = stat_fun(&_str_ev, &_flag, _args[0], *cicles, av[0], env);
if (_str_ev != NULL)
{
_flag++;
childPID = fork();
if (childPID == 0)
execve(_str_ev, _args, NULL);
else if (childPID < 0)
perror("command not ok");
else if (childPID > 0)
{
do {
waitpid(childPID, &status, WUNTRACED);
} while (!WIFEXITED(status) && !WIFSIGNALED(status));
}
}
if (_flag == 0)
{
if (no_path != 1)
_printf("%s: %d: %s: not found\n", av[0], *cicles, _args[0]);
}
if (_flag == 2)
free(_str_ev);
return (1);
}