-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshell_core.c
88 lines (81 loc) · 1.68 KB
/
shell_core.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
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include "shell.h"
/**
* execute_shell - Execute Shell
* @line: Allocated String from stdin
* @num_shell_calls: Number of Shell calls
* @file_name: File name
* @ctrl_loop: Pointer to a variable that controls a while loop
* @env: Environment
*
* Return: execution_status
*/
int execute_shell(
char *line, int *num_shell_calls,
char *file_name, int *ctrl_loop, char **env
)
{
int cmd_count = 0, exec_status = EXIT_SUCCESS;
char **cmds_args, *file_path;
*num_shell_calls += 1;
cmds_args = hsh_parse_str(line, &cmd_count, " \n\t\r");
if (cmd_count == 0)
{
free_array(cmds_args);
return (EXIT_SUCCESS);
}
if (strcmp(cmds_args[0], "exit") == 0)
{
exec_status = shell_exit(
file_name, cmds_args, cmd_count,
*num_shell_calls, &ctrl_loop);
}
else if (strcmp(cmds_args[0], "env") == 0)
print_env(env);
else
{
file_path = _getpath(cmds_args[0], env);
if (strcmp(file_path, "") != 0)
{
exec_command(file_path, cmds_args, env);
free(file_path);
}
else
{
command_error(file_name, *num_shell_calls, cmds_args[0]);
exec_status = 127;
}
}
free_array(cmds_args);
return (exec_status);
}
/**
* exec_command - Execute commands
* @file_path: Path were executable is found
* @cmds: Array of string arguments
* @env: Environment
*
* Return: command status
*/
int exec_command(char *file_path, char **cmds, char **env)
{
pid_t pid = fork();
int command_status = EXIT_SUCCESS;
if (pid == 0)
{
if (execve(file_path, cmds, env) == -1)
{
command_status = 2;
perror("Error");
}
}
else if (pid < 0)
{
perror("Fork failed");
}
else
{
int status;
waitpid(pid, &status, 0); /* wait for the child process to end*/
}
return (command_status);
}