-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_process.c
50 lines (49 loc) · 1.31 KB
/
create_process.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
#include "main.h"
/**
* create_process - This function create a child process and executes
* @av: Pointer to an array of strings with all arguments of input buffer
* @count_exe: Executions counter in each postiion.
* @env: Enviroment variable.
* Return: void, if executed properly
**/
int create_process(char *av[], int count_exe, char **env)
{
char *path = NULL, *full_file = NULL, error_msg[100];
int statu = 0, found = 0, alloc = 0, exit_status = 0;
pid_t child_pid;
struct stat st;
if (stat(av[1], &st) == 0)
found = 1, full_file = av[1];
else
{
path = _getenv("PATH", env), full_file = validate_file(path, av[1]);
if (full_file != NULL)
found = 1, alloc = 1;
free(path);
}
if (found)
{
child_pid = fork();
if (child_pid == 0)
{
if ((execve(full_file, (av + 1), env)) == -1)
{
sprintf(error_msg, "%s: %d: %s: not found\n", av[0], count_exe, av[1]);
write(2, error_msg, _strlen(error_msg));
(alloc == 1) ? free(full_file) : (void) alloc;
}
}
waitpid(child_pid, &statu, 0), (alloc == 1) ? free(full_file) : (void) alloc;
if (WIFEXITED(statu))
{
exit_status = WEXITSTATUS(statu);
return (exit_status);
}
} else
{
sprintf(error_msg, "%s: %d: %s: not found\n", av[0], count_exe, av[1]);
write(2, error_msg, _strlen(error_msg));
return (127);
}
return (0);
}