-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcommand_executor.c
More file actions
50 lines (46 loc) · 809 Bytes
/
command_executor.c
File metadata and controls
50 lines (46 loc) · 809 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.h"
/**
* execute_command - executes the command given
* @pathname: the absolute path to the binary file to execute
* @msh: the shell's context
*
* Return: 0 on success, -1 on failure
*/
int execute_command(const char *pathname, shell_t *msh)
{
int status;
pid_t pid;
pid = fork();
if (pid == -1)
{
perror("fork");
return (-1);
}
if (pid == 0)
{
if (execve(pathname, msh->sub_command, environ) == -1)
{
if (errno == EACCES)
{
fprintf(stderr, "%s: %lu: %s\n", msh->prog_name,
++msh->cmd_count, strerror(errno));
return (126);
}
perror("execve");
return (-1);
}
}
else
{
if (waitpid(pid, &status, 0) == -1)
{
perror("wait");
return (-1);
}
if (WIFEXITED(status))
{
return (WEXITSTATUS(status));
}
}
return (0);
}