-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexec_prgrms.c
More file actions
50 lines (47 loc) · 908 Bytes
/
Copy pathexec_prgrms.c
File metadata and controls
50 lines (47 loc) · 908 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
#include "shell_hdr.h"
/**
* exec_prgrms - Executes shell programs with fork.
* @tokens: Commands received from stdin.
* @line: Commands received from stdin.
*
* Return: status (int).
*/
int exec_prgrms(char **tokens, char *line)
{
char *env_var;
char *env_var_val;
char *full_comd_path;
pid_t pid;
int status;
env_var = "PATH";
env_var_val = get_env_var_val(env_var);
full_comd_path = get_full_comd_path(tokens, env_var_val);
if (full_comd_path != NULL)
{
pid = fork();
if (pid == -1)
{
perror("fork");
free(full_comd_path);
return (1);
}
if (pid == 0)
{
if (execve(full_comd_path, tokens, environ) == -1)
{
perror(*tokens);
free(tokens);
free(env_var_val);
free(full_comd_path);
exit(EXIT_FAILURE);
}
return (EXIT_SUCCESS);
}
wait(&status);
}
free(line);
free(tokens);
free(env_var_val);
free(full_comd_path);
return (0);
}