-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_exec.c
More file actions
36 lines (34 loc) · 778 Bytes
/
_exec.c
File metadata and controls
36 lines (34 loc) · 778 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
#include "shs.h"
/**
* _exec - receives command and arguments for command
* then calls execve with those parameters
*
* @command: Command to execute with execve
* @cmd_arg: Arguments for command to execute
* @exit_c: pointer to string with exit codes
* @cmd_count: keeps track of number of commands executed
*/
void _exec(char *command, char **cmd_arg, char *exit_c, int *cmd_count)
{
int wait_status;
pid_t child_pid;
child_pid = fork();
if (child_pid == -1)
exit(1);
if (child_pid == 0)
{
execve(command, cmd_arg, environ);
ntty_free(cmd_arg, command);
} else
{
wait(&wait_status);
(*cmd_count)++;
if (WIFEXITED(wait_status))
{
wait_status = WEXITSTATUS(wait_status);
_itoa(wait_status, exit_c);
}
}
if (child_pid == 0)
_exit(0);
}