-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexec.c
More file actions
executable file
·32 lines (31 loc) · 875 Bytes
/
exec.c
File metadata and controls
executable file
·32 lines (31 loc) · 875 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
#include "shell.h"
/**
* exec - Creates a new child process,
* executes a command and wait for the child process
* to update the status
* @arguments: Array of inputs by the user
* Return: 0 if success
*/
int exec(char **arguments)
{
pid_t pid = 0;/**Child process id*/
int stat = 0, exe_stat = 0;/**indicates the status of the child process*/
pid = fork();/**Create a child process*/
if (pid == -1)/**Failed to create*/
_printp("failed\n", 7);
else if (pid == 0)/**He is the son...*/
{
exe_stat = execve(arguments[0], arguments, environ);/**Run the command*/
if (exe_stat == -1)
{
exe_stat = 126;
perror("hsh");
exit(exe_stat);
} /**Finish the child process successfully*/
exit(0);
}
else /**He is the father*/
wait(&stat);/**Stops the execution of the parent until the child ends*/
exe_stat = WEXITSTATUS(stat);
return (exe_stat);
}