-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexecute_command.c
More file actions
74 lines (69 loc) · 1.45 KB
/
Copy pathexecute_command.c
File metadata and controls
74 lines (69 loc) · 1.45 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include "shell.h"
/**
* execute_command - execute command entered by user
* @line: the line as entered
* @enva: pointer to environ array
* @pathl: linked list of path variable
* @histl: linked list for history
* Return: no idea
*/
int execute_command(char *line, char ***enva, node_t **pathl, node_t **histl)
{
int ret_value;
char **args, *function;
char *error_message = ": command not found\n";
args = NULL;
args = strtow(line, " \t\v\r\f");
free(line);
if (args == NULL)
{
return (0);
}
ret_value = bi_function(args, enva, pathl, histl);
if (ret_value != 101)
{
free_strtow(args);
return (ret_value);
}
function = what_path(args[0], *pathl);
if (function == NULL)
{
write(STDERR_FILENO, args[0], _strlen(args[0]));
write(STDERR_FILENO, error_message, _strlen(error_message));
free_strtow(args);
return (0);
}
return (run_non_bi(args, function, *enva));
}
/**
* run_non_bi - run a non built-in command
* @args: arguments for function
* @function: function name
* @enva: pointer to environment array
* Return: int value
*/
int run_non_bi(char **args, char *function, char **enva)
{
pid_t childpid;
int status;
childpid = fork();
if (childpid == 0)
{
set_to_kill();
if (execve(function, (char *const *) args, enva) == -1)
{
perror("simple shell");
free_strtow(args);
free(function);
_exit(1);
return (-1);
}
}
else
{
waitpid(childpid, &status, 0);
free(function);
}
free_strtow(args);
return (0);
}