-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuiltin.c
More file actions
197 lines (183 loc) · 4.34 KB
/
builtin.c
File metadata and controls
197 lines (183 loc) · 4.34 KB
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*
* File: builtin.c
* Auth: Paschal Ugwu
*/
#include "shell.h"
int (*get_builtin(char *command))(char **args, char **front);
int alxshell_exit(char **args, char **front);
int alxshell_cd(char **args, char __attribute__((__unused__)) **front);
int alxshell_help(char **args, char __attribute__((__unused__)) **front);
/**
* get_builtin - Matches a command with a corresponding
* alxshell builtin function.
* @command: The command to match.
*
* Return: A function pointer to the corresponding builtin.
*/
int (*get_builtin(char *command))(char **args, char **front)
{
builtin_t funcs[] = {
{ "exit", alxshell_exit },
{ "env", alxshell_env },
{ "setenv", alxshell_setenv },
{ "unsetenv", alxshell_unsetenv },
{ "cd", alxshell_cd },
{ "alias", alxshell_alias },
{ "help", alxshell_help },
{ NULL, NULL }
};
int i;
for (i = 0; funcs[i].name; i++)
{
if (_strcmp(funcs[i].name, command) == 0)
break;
}
return (funcs[i].f);
}
/**
* alxshell_exit - Causes normal process termination
* for the alxshell shell.
* @args: An array of arguments containing the exit value.
* @front: A double pointer to the beginning of args.
*
* Return: If there are no arguments - -3.
* If the given exit value is invalid - 2.
* O/w - exits with the given status value.
*
* Description: Upon returning -3, the program exits back in the main function.
*/
int alxshell_exit(char **args, char **front)
{
int i, len_of_int = 10;
unsigned int num = 0, max = 1 << (sizeof(int) * 8 - 1);
if (args[0])
{
if (args[0][0] == '+')
{
i = 1;
len_of_int++;
}
for (; args[0][i]; i++)
{
if (i <= len_of_int && args[0][i] >= '0' && args[0][i] <= '9')
num = (num * 10) + (args[0][i] - '0');
else
return (create_error(--args, 2));
}
}
else
{
return (-3);
}
if (num > max - 1)
return (create_error(--args, 2));
args -= 1;
free_args(args, front);
free_env();
free_alias_list(aliases);
exit(num);
}
/**
* alxshell_cd - Changes the current directory of the alxshell process.
* @args: An array of arguments.
* @front: A double pointer to the beginning of args.
*
* Return: If the given string is not a directory - 2.
* If an error occurs - -1.
* Otherwise - 0.
*/
int alxshell_cd(char **args, char __attribute__((__unused__)) **front)
{
char **dir_info, *new_line = "\n";
char *oldpwd = NULL, *pwd = NULL;
struct stat dir;
oldpwd = getcwd(oldpwd, 0);
if (!oldpwd)
return (-1);
if (args[0])
{
if (*(args[0]) == '-' || _strcmp(args[0], "--") == 0)
{
if ((args[0][1] == '-' && args[0][2] == '\0') ||
args[0][1] == '\0')
{
if (_getenv("OLDPWD") != NULL)
(chdir(*_getenv("OLDPWD") + 7));
}
else
{
free(oldpwd);
return (create_error(args, 2));
}
}
else
{
if (stat(args[0], &dir) == 0 && S_ISDIR(dir.st_mode)
&& ((dir.st_mode & S_IXUSR) != 0))
chdir(args[0]);
else
{
free(oldpwd);
return (create_error(args, 2));
}
}
}
else
{
if (_getenv("HOME") != NULL)
chdir(*(_getenv("HOME")) + 5);
}
pwd = getcwd(pwd, 0);
if (!pwd)
return (-1);
dir_info = malloc(sizeof(char *) * 2);
if (!dir_info)
return (-1);
dir_info[0] = "OLDPWD";
dir_info[1] = oldpwd;
if (alxshell_setenv(dir_info, dir_info) == -1)
return (-1);
dir_info[0] = "PWD";
dir_info[1] = pwd;
if (alxshell_setenv(dir_info, dir_info) == -1)
return (-1);
if (args[0] && args[0][0] == '-' && args[0][1] != '-')
{
write(STDOUT_FILENO, pwd, _strlen(pwd));
write(STDOUT_FILENO, new_line, 1);
}
free(oldpwd);
free(pwd);
free(dir_info);
return (0);
}
/**
* alxshell_help - Displays information about alxshell builtin commands.
* @args: An array of arguments.
* @front: A pointer to the beginning of args.
*
* Return: If an error occurs - -1.
* Otherwise - 0.
*/
int alxshell_help(char **args, char __attribute__((__unused__)) **front)
{
if (!args[0])
help_all();
else if (_strcmp(args[0], "alias") == 0)
help_alias();
else if (_strcmp(args[0], "cd") == 0)
help_cd();
else if (_strcmp(args[0], "exit") == 0)
help_exit();
else if (_strcmp(args[0], "env") == 0)
help_env();
else if (_strcmp(args[0], "setenv") == 0)
help_setenv();
else if (_strcmp(args[0], "unsetenv") == 0)
help_unsetenv();
else if (_strcmp(args[0], "help") == 0)
help_help();
else
write(STDERR_FILENO, name, _strlen(name));
return (0);
}