-
Notifications
You must be signed in to change notification settings - Fork 0
/
_fork.c
49 lines (40 loc) · 843 Bytes
/
_fork.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
#include "main.h"
/**
* _fork - function to fork a process
* @array_Of_Words: the array of argv
* @argv: the array of arguments
* @index: line number.
* Return: 0 on success
*/
int _fork(char **array_Of_Words, char **argv, int index)
{
char *full_command;
pid_t child_pid;
int status;
full_command = _get_path(array_Of_Words[0]);
if (!full_command)
{
/*_error();*/
_error_print(argv[0], array_Of_Words[0], index);
_freeArrOfWords(array_Of_Words);
return (127);
}
child_pid = fork();
if (child_pid == -1)
{
perror("error");
return (2);
}
if (child_pid == 0)
{
/*printf("FORK: this is child process");*/
_exec(full_command, array_Of_Words);
}
else
{
waitpid(child_pid, &status, 0);
_freeArrOfWords(array_Of_Words);
free(full_command), full_command = NULL;
}
return (WEXITSTATUS(status));
}