-
Notifications
You must be signed in to change notification settings - Fork 0
/
builtins.c
147 lines (139 loc) · 2.95 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
#include "shell.h"
/**
* shellexit - exits the shell
*
* @self: the shell's state
* @arguments: command line arguments passed to the shell
*
* Return: the exit value
*/
exit_status shellexit(state *self, char **arguments)
{
exit_status status = 0;
char *arg = arguments[0];
if (arg == NULL)
{
status = self->_errno;
deinit(self);
exit(status);
}
if (checkatoi(arg) == false)
{
fprinterr(format(
"%s: %d: exit: Illegal number: %s\n",
self->prog, self->lineno, arg
));
return (2);
}
else
status = atoi(arg);
deinit(self);
exit(status);
}
/**
* shellenv - prints out all shell environment variables
*
* @self: the shell's state
* @arguments: command line arguments passed to the shell
*
* Return: an integer representing the command's exit status
*/
exit_status shellenv(state *self, char **arguments)
{
(void)arguments;
print_list(self->env, false);
return (0);
}
/**
* shellsetenv - create/modify a shell enviroment variable
*
* @self: the shell's state
* @arguments: command line arguments passed to the shell
*
* Return: an integer representing the command's exit status
*/
exit_status shellsetenv(state *self, char **arguments)
{
if (!arguments[0])
{
printerr("Usage: setenv VARIABLE VALUE\n");
return (1);
}
else if (!arguments[1])
{
printerr("setenv: VALUE not supplied\n");
return (2);
}
if (!set_node(&(self->env), arguments[0], arguments[1]))
{
perror("setenv");
return (3);
}
return (0);
}
/**
* shellunenv - delete a shell environment variable
*
* @self: the shell's state
* @arguments: command line arguments passed to the shell
*
* Return: an integer representing the command's exit status
*/
exit_status shellunsetenv(state *self, char **arguments)
{
if (!arguments[0])
{
printerr("unsetenv: VARIABLE missing\n");
return (1);
}
if (!get_node(self->env, arguments[0]))
{
fprinterr(format("unsetenv: %s does not exist\n", arguments[0]));
return (2);
}
if (!delete_node(&(self->env), arguments[0]))
{
perror("unsetenv");
return (3);
}
return (0);
}
/**
* shellenv - change the shell's working directory
*
* @self: the shell's state
* @arguments: command line arguments passed to the shell
*
* Return: an integer representing the command's exit status
*/
exit_status shellcd(state *self, char **arguments)
{
char *path = arguments[0];
char *cwd;
node *pwd;
cwd = getcwd(NULL, 0);
pwd = set_default(&(self->env), "PWD", cwd);
set_default(&(self->env), "OLDPWD", cwd);
free(cwd);
cwd = NULL;
if (!path || !_strcmp(path, "~"))
path = get_node(self->env, "HOME")->val;
else if (!_strcmp(path, "-"))
path = get_node(self->env, "OLDPWD")->val;
do {
if (access(path, X_OK) == -1)
break;
if (chdir(path) == -1)
break;
cwd = getcwd(NULL, 0);
set_node(&(self->env), "OLDPWD", pwd->val);
set_node(&(self->env), "PWD", cwd);
free(cwd);
return (0);
} while (false);
fprinterr(format(
"%s: %d: cd: can't cd to %s\n",
self->prog, self->lineno, path
));
return (2);
}