-
Notifications
You must be signed in to change notification settings - Fork 0
/
execution.c
47 lines (46 loc) · 1.03 KB
/
execution.c
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
#include "simple_shell.h"
/**
* execution - function that compares our string and execute the command.
*
* @tokens: Our array of tokens
* @env: Our enviorment variables
*
* Return: Always return one
*
* Description: First our function will compare our token position 0
* with the built-in (exit & env). If its not a built in he will check
* for the command and print out the result of the execution.
*/
int execution(char **tokens, char **env)
{
int status;
pid_t child_pid;
char *path_tok, **cmd;
struct stat buffer;
if (stat(tokens[0], &buffer) != 0)
{
path_tok = path(env);
cmd = tokenization(path_tok, ":");
free(path_tok);
tokens[0] = add_path(tokens, cmd);
if (stat(tokens[0], &buffer) != 0)
{
free_array(cmd);
free_array(tokens);
perror("cmd not found");
return (0);
}
}
child_pid = fork();
if (child_pid == -1)
perror("Child process failed");
else if (child_pid == 0)
{
if (execve(tokens[0], tokens, env) == -1)
perror("./hsh");
}
else
wait(&status);
free_array(tokens);
return (1);
}