-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_execve.c
64 lines (61 loc) · 1.25 KB
/
_execve.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include "shell.h"
/**
* c_exit - frees user's typed command and linked list before exiting
* @str: user's typed command
* @env: input the linked list of envirnment
*/
void c_exit(char **str, list_t *env)
{
free_double_ptr(str);
free_linked_list(env);
_exit(0);
}
/**
* _execve - execute command user typed into shell
* @s: command user typed
* @env: environmental variable
* @num: nth user command; to be used in error message
* Return: 0 on success
*/
int _execve(char **s, list_t *env, int num)
{
char *holder;
int status = 0, t = 0;
pid_t pid;
/* check if command is absolute path */
if (access(s[0], F_OK) == 0)
{
holder = s[0];
t = 1;
}
/* else flesh out full path */
else
holder = _which(s[0], env);
/* if not an executable, free */
if (access(holder, X_OK) != 0)
{
not_found(s[0], num, env);
free_double_ptr(s);
return (127);
}
else /* else fork and execute executable command */
{
pid = fork();
if (pid == 0) /* if child process, execute */
{
if (execve(holder, s, NULL) == -1)
{
not_found(s[0], num, env); /* special err msg */
c_exit(s, env);
}
}
else /* if parent, wait for child then free all */
{
wait(&status);
free_double_ptr(s);
if (t == 0)
free(holder);
}
}
return (0);
}