-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexecute.c
55 lines (46 loc) · 1.07 KB
/
execute.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
#include "shell.h"
/**
* execute_command - Executes a command entered by the user.
* @input: The command to be executed.
*/
void execute_command(char *input)
{
char *args[4];
char *env[1];
args[0] = NULL;
args[1] = NULL;
args[2] = NULL;
args[3] = NULL;
env[0] = NULL;
if (input == NULL)
return;
if (str_cmp(input, "exit") == 0)
{
free(input);
exit(EXIT_SUCCESS);
}
else if (str_cmp(input, "env") == 0)
{
display_environment();
}
else
{
if (access(input, F_OK | X_OK) == 0)
{
args[0] = input;
exec_cmd(input, args, env);
}
else
{
char *path = getenv("PATH");
char *token = str_tok(path, ":");
while (token != NULL)
{
char full_path[MAX_INPUT_SIZE];
str_format(full_path, MAX_INPUT_SIZE, "%s/%s", token, input);
token = str_tok(NULL, ":");
}
printf("%s: command not found\n", input);
}
}
}