-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathshell_utils.c
175 lines (160 loc) · 3.74 KB
/
shell_utils.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#include "shell.h"
/**
* Auth: Emma Udeji
* Pericles Adjovi
*
* Description:
* the extended functions for main.c
*/
/** parse_command - determines the type of the command
* @command: command to be parsed
*
* Return: constant representing the type of the command
* Description -
* EXTERNAL_COMMAND (1) represents commands like /bin/ls
* INTERNAL_COMMAND (2) represents commands like exit, env
* PATH_COMMAND (3) represents commands found in the PATH like ls
* INVALID_COMMAND (-1) represents invalid commands
*/
int parse_command(char *command)
{
int i;
char *internal_command[] = {"env", "exit", NULL};
char *path = NULL;
for (i = 0; command[i] != '\0'; i++)
{
if (command[i] == '/')
return (EXTERNAL_COMMAND);
}
for (i = 0; internal_command[i] != NULL; i++)
{
if (_strcmp(command, internal_command[i]) == 0)
return (INTERNAL_COMMAND);
}
/* @check_path - checks if a command is found in the PATH */
path = check_path(command);
if (path != NULL)
{
free(path);
return (PATH_COMMAND);
}
return (INVALID_COMMAND);
}
/**
* execute_command - executes a command based on it's type
* @tokenized_command: tokenized form of the command (ls -l == {ls, -l, NULL})
* @command_type: type of the command
*
* Return: void
*/
void execute_command(char **tokenized_command, int command_type)
{
void (*func)(char **command);
if (command_type == EXTERNAL_COMMAND)
{
if (execve(tokenized_command[0], tokenized_command, NULL) == -1)
{
perror(_getenv("PWD"));
exit(2);
}
}
if (command_type == PATH_COMMAND)
{
if (execve(check_path(tokenized_command[0]), tokenized_command, NULL) == -1)
{
perror(_getenv("PWD"));
exit(2);
}
}
if (command_type == INTERNAL_COMMAND)
{
func = get_func(tokenized_command[0]);
func(tokenized_command);
}
if (command_type == INVALID_COMMAND)
{
print(shell_name, STDERR_FILENO);
print(": 1: ", STDERR_FILENO);
print(tokenized_command[0], STDERR_FILENO);
print(": not found\n", STDERR_FILENO);
status = 127;
}
}
/**
* check_path - checks if a command is found in the PATH
* @command: command to be used
*
* Return: path where the command is found in, NULL if not found
*/
char *check_path(char *command)
{
char **path_array = NULL;
char *temp, *temp2, *path_cpy;
char *path = _getenv("PATH");
int i;
if (path == NULL || _strlen(path) == 0)
return (NULL);
path_cpy = malloc(sizeof(*path_cpy) * (_strlen(path) + 1));
_strcpy(path, path_cpy);
path_array = tokenizer(path_cpy, ":");
for (i = 0; path_array[i] != NULL; i++)
{
temp2 = _strcat(path_array[i], "/");
temp = _strcat(temp2, command);
if (access(temp, F_OK) == 0)
{
free(temp2);
free(path_array);
free(path_cpy);
return (temp);
}
free(temp);
free(temp2);
}
free(path_cpy);
free(path_array);
return (NULL);
}
/**
* get_func - retrieves a function based on the command given and a mapping
* @command: string to check against the mapping
*
* Return: pointer to the proper function, or null on fail
*/
void (*get_func(char *command))(char **)
{
int i;
function_map mapping[] = {
{"env", env}, {"exit", quit}
};
for (i = 0; i < 2; i++)
{
if (_strcmp(command, mapping[i].command_name) == 0)
return (mapping[i].func);
}
return (NULL);
}
/**
* _getenv - gets the value of an environment variable
* @name: name of the environment variable
*
* Return: the value of the variable as a string
*/
char *_getenv(char *name)
{
char **my_environ;
char *pair_ptr;
char *name_cpy;
for (my_environ = environ; *my_environ != NULL; my_environ++)
{
for (pair_ptr = *my_environ, name_cpy = name;
*pair_ptr == *name_cpy; pair_ptr++, name_cpy++)
{
if (*pair_ptr == '=')
break;
}
if ((*pair_ptr == '=') && (*name_cpy == '\0'))
return (pair_ptr + 1);
}
return (NULL);
}