-
Notifications
You must be signed in to change notification settings - Fork 1
/
run.c
45 lines (40 loc) · 764 Bytes
/
run.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
#include "simpleshell.h"
/**
* run - func create a child proccess with an exec
* @args: arguments
* Return: (1)
*
*/
int run(char **args)
{
pid_t pid;
int status;
char *buffer = _getenv("PATH");
char *path = malloc(sizeof(char) * 1024);
char *cp = malloc(sizeof(char) * 1024);
char *new = malloc(sizeof(char) * 1024);
pid = fork();
if (pid == 0)
{
path = strtok(buffer, ":");
cp = str_concat("/", *args);
while (path)
{
if (_strchr(*args, '/') != NULL)
execve(*args, args, environ);
new = str_concat(path, cp);
if ((access(new, X_OK | R_OK)) == 0)
{
execve(new, args, environ);
break;
}
path = strtok(NULL, ":");
}
free(args);
}
waitpid(pid, &status, 0);
free(path);
free(cp);
free(new);
return (1);
}