-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbuiltins.c
157 lines (146 loc) · 2.8 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
#include "shell.h"
/**
* check_for_builtins - checks if the command is a builtin
* @vars: variables
* Return: pointer to the function or NULL
*/
void (*check_for_builtins(vars_t *vars))(vars_t *vars)
{
unsigned int i;
builtins_t check[] = {
{"exit", new_exit},
{"env", _env},
{"setenv", new_setenv},
{"unsetenv", new_unsetenv},
{NULL, NULL}
};
for (i = 0; check[i].f != NULL; i++)
{
if (_strcmpr(vars->av[0], check[i].name) == 0)
break;
}
if (check[i].f != NULL)
check[i].f(vars);
return (check[i].f);
}
/**
* new_exit - exit program
* @vars: variables
* Return: void
*/
void new_exit(vars_t *vars)
{
int status;
if (_strcmpr(vars->av[0], "exit") == 0 && vars->av[1] != NULL)
{
status = _atoi(vars->av[1]);
if (status == -1)
{
vars->status = 2;
print_error(vars, ": Illegal number: ");
_puts2(vars->av[1]);
_puts2("\n");
free(vars->commands);
vars->commands = NULL;
return;
}
vars->status = status;
}
free(vars->buffer);
free(vars->av);
free(vars->commands);
free_env(vars->env);
exit(vars->status);
}
/**
* _env - prints the current environment
* @vars: struct of variables
* Return: void.
*/
void _env(vars_t *vars)
{
unsigned int i;
for (i = 0; vars->env[i]; i++)
{
_puts(vars->env[i]);
_puts("\n");
}
vars->status = 0;
}
/**
* new_setenv - create a new environment variable, or edit an existing variable
* @vars: pointer to struct of variables
*
* Return: void
*/
void new_setenv(vars_t *vars)
{
char **key;
char *var;
if (vars->av[1] == NULL || vars->av[2] == NULL)
{
print_error(vars, ": Incorrect number of arguments\n");
vars->status = 2;
return;
}
key = find_key(vars->env, vars->av[1]);
if (key == NULL)
add_key(vars);
else
{
var = add_value(vars->av[1], vars->av[2]);
if (var == NULL)
{
print_error(vars, NULL);
free(vars->buffer);
free(vars->commands);
free(vars->av);
free_env(vars->env);
exit(127);
}
free(*key);
*key = var;
}
vars->status = 0;
}
/**
* new_unsetenv - remove an environment variable
* @vars: pointer to a struct of variables
*
* Return: void
*/
void new_unsetenv(vars_t *vars)
{
char **key, **newenv;
unsigned int i, j;
if (vars->av[1] == NULL)
{
print_error(vars, ": Incorrect number of arguments\n");
vars->status = 2;
return;
}
key = find_key(vars->env, vars->av[1]);
if (key == NULL)
{
print_error(vars, ": No variable to unset");
return;
}
for (i = 0; vars->env[i] != NULL; i++)
;
newenv = malloc(sizeof(char *) * i);
if (newenv == NULL)
{
print_error(vars, NULL);
vars->status = 127;
new_exit(vars);
}
for (i = 0; vars->env[i] != *key; i++)
newenv[i] = vars->env[i];
for (j = i + 1; vars->env[j] != NULL; j++, i++)
newenv[i] = vars->env[j];
newenv[i] = NULL;
free(*key);
free(vars->env);
vars->env = newenv;
vars->status = 0;
}