-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2-exec_cmd.c
69 lines (58 loc) · 1.1 KB
/
2-exec_cmd.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
#include "main.h"
void exec_cmd(char **args, char **env);
/**
* exec_cmd - This function will execute the command
* @args: The command and its arguments
* @env: The environment variables
* Return: Nothing
*/
void exec_cmd(char **args, char **env)
{
char *full_path = getenv("PATH"), *path, *token;
char our_path[1024];
if (args[0] == NULL)
{
perror("Command not provided");
exit(EXIT_FAILURE);
}
if (args[0][0] == '/')
{
if (access(args[0], X_OK) == 0)
{
execve(args[0], args, env);
exit(EXIT_FAILURE);
}
else
{
perror("execve");
exit(EXIT_FAILURE);
}
}
if (full_path == NULL)
{
perror("Unable to locate PATH environment variable");
exit(EXIT_FAILURE);
}
path = gb_strdup(full_path);
if (path == NULL)
{
perror("gb_strdup");
exit(2);
}
token = gb_strtok(path, ":");
while (token != NULL)
{
gb_strcpy(our_path, token);
gb_strcat(our_path, "/");
gb_strcat(our_path, args[0]);
if (access(our_path, X_OK) == 0)
{
execve(our_path, args, env);
exit(EXIT_FAILURE);
}
token = gb_strtok(NULL, ":");
}
perror("./hsh");
free(path);
exit(2);
}