-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprocess.c
More file actions
56 lines (50 loc) · 871 Bytes
/
process.c
File metadata and controls
56 lines (50 loc) · 871 Bytes
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
#include "main.h"
/**
* process - starts new process
* @args: arguments
* Return: 1
*/
int process(char **args)
{
char **environ = NULL, *pathc, *path, *_dir;
pid_t pid;
int _status;
pid = fork();
if (_strcmp(args[0], "exit") == 0)
{
return (status(args));
}
if (pid == 0)
{
path = getenv("PATH");
_dir = strtok(path, ":");
while (_dir != NULL)
{
pathc = malloc(_strlen(_dir) + _strlen(args[0]) + 2);
sprintf(pathc, "%s/%s", _dir, args[0]);
if (access(pathc, X_OK) == 0)
{
args[0] = pathc;
break;
}
free(pathc);
_dir = strtok(NULL, ":");
}
if (execve(args[0], args, environ) == -1)
{
perror("execve");
}
exit(EXIT_FAILURE);
}
else if (pid < 0)
{
perror("fork");
}
else
{
do {
waitpid(pid, &_status, WUNTRACED);
} while (!WIFEXITED(_status) && !WIFSIGNALED(_status));
}
return (1);
}