-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexecution.c
More file actions
49 lines (46 loc) · 1.02 KB
/
execution.c
File metadata and controls
49 lines (46 loc) · 1.02 KB
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
#include "shell.h"
/**
* exec_args - Uses execve and fork to create processes to execute commands
* @tokens_made: double pointer to commands/tokens
* Return: 0 if sucessful or bring out an error message for failure
*/
int exec_args(char **tokens_made)
{
int condition, cmd_count;
char *cmd;
const char *errcmd, *name, *errmsg;
pid_t ourchild;
if (tokens_made[0] == NULL)
return (1);
blt_matcher(tokens_made); /* handle built_ins if presented */
cmd = commandExists(tokens_made[0]);
name = "./hsh", cmd_count = 1, errmsg = "not found";
errcmd = tokens_made[0];
if (cmd == NULL)
{
free(cmd);
status = 127;
print_error(name, cmd_count, errcmd, errmsg);
return (-1);
}
ourchild = fork();
if (ourchild == -1)
{perror("child process failed to be created");
return (1);
}
if (!ourchild)
{
if (execve(cmd, tokens_made, environ) == -1)
{perror(cmd);
exit(1);
}
}
else
{
waitpid(ourchild, &condition, 0);
if (WIFEXITED(condition))
status = WEXITSTATUS(condition);
}
free(cmd);
return (1);
}