-
Notifications
You must be signed in to change notification settings - Fork 0
/
execution_functions.c
126 lines (119 loc) · 2.7 KB
/
execution_functions.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include "shell.h"
/**
* cmd_execute - function that check user commands
* and execute them
*
* @tokens: pointer to pointer of strings
* @lineptr: Raw user input to be freed
* @env: program environment to be used by env built-in
*
* Return: 1 on Success to make infiniti loop continue
*/
int cmd_execute(char **tokens, char *lineptr, char **env)
{
char *path = NULL;
int shell_exit_flag = 1, path_var_access_flag = 0;
int (*check_builtin)(char **, char *, char **);
struct shell_info shell_exit;
/*handle case of spaces*/
if (tokens == NULL || tokens[0] == NULL)
return (1);
/*check if command built-in*/
check_builtin = Isbuiltin(tokens[0]);
if (check_builtin)
{
shell_exit_flag = check_builtin(tokens, lineptr, env);
return (shell_exit_flag);
}
/*check if command sent in full path or check for its path*/
if (access(tokens[0], X_OK) == 0)
{
shell_exit_flag = fullpath_execution(tokens, lineptr, path_var_access_flag);
return (shell_exit_flag);
}
else
{
path = check_cmd_in_PATH(tokens[0]);
if (!path)
{
cmd_error(tokens[0]);
_free(2, tokens, lineptr);
shell_exit.status = 127;
exit(shell_exit.status);
}
else
{
tokens[0] = path;
path_var_access_flag = 1;
shell_exit_flag = fullpath_execution(tokens, lineptr, path_var_access_flag);
return (shell_exit_flag);
}
}
return (1);
}
/**
* fullpath_execution - function that executes
* user command with full path
*
* @tokens: pointer to pointer of strings
* @lineptr: Raw user input to be freed
* @path_var_access_flag: a flag that indicates whether the path
* environment variable accessed or not
*
* Return: 1 on Success
*/
int fullpath_execution(char **tokens, char *lineptr, int path_var_access_flag)
{
pid_t pid;
int child_status;
struct shell_info shell_exit;
pid = fork();
if (pid < 0)
{
perror("fork");
shell_exit.status = EXIT_FAILURE;
exit(shell_exit.status);
}
else if (pid == 0)
{
if (execve(tokens[0], tokens, NULL) == -1)
{
cmd_error(tokens[0]);
_free(2, tokens, lineptr);
shell_exit.status = 127;
exit(shell_exit.status);
}
}
else
{
if (path_var_access_flag)
free(tokens[0]);
do {
waitpid(pid, &child_status, WUNTRACED);
} while (!WIFEXITED(child_status) && !WIFSIGNALED(child_status));
shell_exit.status = WEXITSTATUS(child_status);
}
return (1);
}
/**
* cmd_error - function that handle commands errors
*
* @arg: pointer to user command
*
* Return: no return
*/
void cmd_error(char *arg)
{
char *error = NULL;
error = malloc(_strlen(arg) + 40);
if (!error)
{
perror("malloc");
return;
}
_strcpy(error, "./hsh: 1: ");
_strcat(error, arg);
_strcat(error, ": not found\n");
write(STDERR_FILENO, error, _strlen(error));
free(error);
}