-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuiltins.c
212 lines (192 loc) · 4.94 KB
/
builtins.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#include "shell.h"
/**
* _setenv - set a custom environment variable
* @name: the name of the variable
* @value: the value to set
* @overwrite: if true, overwrite the variable if it exists
*
* Description: This function sets a custom environment variable with the given
* name and value. If the variable already exists and overwrite is true, it
* will be replaced.
*
* Return: 0 on success, 1 on error
*/
int _setenv(const char *name, const char *value, int overwrite)
{
size_t len, i;
char *env_var = NULL;
if (name == NULL || name[0] == '\0' || _strchr(name, '=') != NULL)
{
fprintf(stderr, "Invalid variable name: %s\n", name);
return (1);
}
/* check if the variable already exists */
if (_getenv(name) != NULL)
{
if (overwrite)
{
if (_unsetenv(name) != 0)
return (1);
}
else
return (0); /* variable exists, and overwrite is false */
}
len = _strlen(name) + _strlen(value) + 2;
env_var = malloc(len);
if (env_var == NULL)
return (1);
sprintf(env_var, "%s=%s", name, value);
for (i = 0; environ[i] != NULL; i++)
{
if (_strncmp(environ[i], name, len) == 0 && environ[i][len] == '=')
{
environ[i] = env_var;
safe_free(env_var);
return (0);
}
}
/* the variable doesn't exist, create it */
environ[i++] = env_var;
environ[i] = NULL;
safe_free(env_var);
return (0);
}
/**
* _unsetenv - unset a custom environment variable
* @name: The name of the variable to unset
*
* Description: This function unsets a custom environment variable with the
* given name.
*
* Return: 0 on success, 1 on error
*/
int _unsetenv(const char *name)
{
size_t i, j, len;
/* check for invalid variable names */
if (name == NULL || name[0] == '\0' || _strchr(name, '=') != NULL)
{
fprintf(stderr, "Invalid variable name: %s\n", name);
return (1);
}
len = _strlen(name);
/* iterate through the environment variables and find the one to remove */
for (i = 0; environ[i] != NULL; i++)
{
if (_strncmp(environ[i], name, len) == 0 && environ[i][len] == '=')
{
/* shift all environment variables after the one to be removed */
for (j = i; environ[j] != NULL; j++)
{
environ[j] = environ[j + 1];
}
return (0);
}
}
dprintf(STDERR_FILENO, "Variable not found: %s\n", name);
return (1);
}
/**
* handle_exit - handles the built-in `exit` command for the shell
* @msh: contains all the data relevant to the shell's operation
* @cleanup: a cleanup function
*
* Return: 2 on error, else exits with the last the provided exit code
*/
int handle_exit(shell_t *msh, void (*cleanup)(const char *format, ...))
{
const char *status_code = (msh->sub_command) ? msh->sub_command[1] : NULL;
int exit_code = msh->exit_code;
if (status_code == NULL)
{
cleanup("spattt", msh->line, &msh->path_list, &msh->aliases,
&msh->commands, &msh->sub_command, &msh->tokens);
safe_free(msh);
exit(exit_code);
}
if (isalpha(*status_code) || _atoi(status_code) < 0 || *status_code == '-')
{
dprintf(STDERR_FILENO, "%s: %lu: exit: Illegal number: %s\n",
msh->prog_name, msh->cmd_count, status_code);
return (CMD_ERR);
}
exit_code = _atoi(status_code);
cleanup("spattt", msh->line, &msh->path_list, &msh->aliases,
&msh->commands, &msh->sub_command, &msh->tokens);
safe_free(msh);
exit(exit_code);
}
/**
* handle_cd - handles the builtin `cd` command
* @msh: contains all the data relevant to the shell's operation
*
* Return: 0 on success, else 2 on error
*/
int handle_cd(shell_t *msh)
{
char path[PATH_SIZE], pwd[BUFF_SIZE];
const char *pathname = msh->sub_command[1];
char *home = _getenv("HOME"), *oldpath = _getenv("OLDPWD");
getcwd(pwd, BUFF_SIZE);
oldpath = (oldpath) ? oldpath : pwd;
if (pathname != NULL && *pathname != '~')
{
int dash = !_strcmp(pathname, "-") || !_strcmp(pathname, "--");
if (!_strchr(pathname, '/') && !dash)
sprintf(path, "%s/%s", pwd, ((dash) ? oldpath : pathname));
else
sprintf(path, "%s", ((dash) ? oldpath : pathname));
if (chdir(path) == -1)
{
if (_strspn(pathname, "-") > 2)
fprintf(stderr, "%s: %lu: cd: Illegal option: --\n", msh->prog_name,
msh->cmd_count);
else
fprintf(stderr, "%s: %lu: cd: can't cd to %s\n", msh->prog_name,
msh->cmd_count, pathname);
return (CMD_ERR);
}
if (dash)
printf("%s\n", oldpath);
setenv("OLDPWD", pwd, 1);
getcwd(path, PATH_SIZE);
setenv("PWD", path, 1);
}
else
{
if (home == NULL)
return (0); /* HOME is not set */
if (chdir(home) == -1)
return (CMD_ERR);
setenv("OLDPWD", pwd, 1);
setenv("PWD", home, 1);
}
return (0);
}
/**
* _printenv - prints all environment variables
*/
void _printenv(void)
{
int fd, status;
char *filename = "/tmp/env";
pid_t child;
fd = open(filename, O_RDWR | O_CREAT | O_TRUNC, 0644);
if (fd == -1)
return;
if (write(fd, "env", 3) != -1)
{
char *argv[] = {"/bin/sh", "/tmp/env", NULL};
child = fork();
if (child == 0)
execve(argv[0], argv, environ);
else
waitpid(child, &status, 0);
close(fd);
}
else
{
close(fd);
return;
}
}