-
Notifications
You must be signed in to change notification settings - Fork 0
/
command-0x02.c
156 lines (136 loc) · 2.7 KB
/
command-0x02.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
#include <libgen.h>
#include <limits.h>
#include <sys/wait.h>
#include "alias.h"
#include "command.h"
#include "shutils.h"
#include "simple_shell.h"
/**
* populate_commands - Populates the command list
* with the default commands
* @list: Command list
*/
void populate_commands(cmd_list_t **list)
{
add_command(*list, "env", printenv);
add_command(*list, "exit", terminate);
add_command(*list, "setenv", _setenv);
add_command(*list, "unsetenv", _unsetenv);
add_command(*list, "cd", change_directory);
}
/**
* run_command - A function that executes a command with given arguments.
* @shell: Shell structure
* @args: An array of strings representing the command and its arguments.
* Return: int
*/
int run_command(shell_t *shell, char **args)
{
int status;
status = handle_builtin(shell, args);
if (status != SS_OK)
{
status = handle_external_command(args);
}
return (status);
}
/**
* handle_builtin - Handles builtin commands
* @shell: Global shell object
* @args: Arguments
* Return: int
*/
int handle_builtin(shell_t *shell, char **args)
{
icmd_mapping *cmd = find_command(shell->internal_cmd_list, args[0]);
if (cmd != NULL)
{
return (cmd->function(args));
}
return (SS_CMD_ERR);
}
/**
* handle_builtin_alias - Handle alias command
* @shell: Global shell object
* @args: Arguments
* Return: int
*/
int handle_builtin_alias(shell_t *shell, char *args)
{
size_t i;
char **tokens;
alias_ct *aliases = &(shell->alias);
size_t num_tokens = 0;
if (args == NULL)
{
print_aliases(aliases);
fflush(stdin);
return (SS_CLOSE);
}
tokens = tokenize_alias_arg(args, &num_tokens);
for (i = 0; i < num_tokens; ++i)
{
if (is_pair(tokens[i]) == 0)
{
alias_t alias = create_alias(tokens[i]);
if (find_alias(&(shell->alias), alias.name) != NULL)
{
set_alias(&(shell->alias), alias);
}
else
{
add_alias(&(shell->alias), alias);
}
}
else
{
print_alias(&(shell->alias), tokens[i]);
}
}
free(tokens);
return (SS_CLOSE);
}
/**
* handle_external_command - Handles external commands
* @args: Arguments
* Return: int
*/
int handle_external_command(char **args)
{
int status;
char *path;
if (is_absolute(args[0]) == 0)
path = _strdup(args[0]);
else
path = check_path(args[0]);
if (path == NULL)
{
_puts(args[0]);
_puts(" : not found\n");
return (SS_CLOSE);
}
else
{
pid_t pid = fork();
if (pid == 0)
{
char *envp[] = {NULL};
if (execve(path, args, envp) == -1)
{
_puts("./shell: No such file or directory\n");
return (SS_CLOSE);
}
}
else if (pid < 0)
{
_puts("./shell: No such file or directory\n");
return (SS_CLOSE);
}
else
{
waitpid(pid, &status, 0);
free(path);
}
return (SS_CLOSE);
}
}