-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbuilt_in_functions.c
More file actions
182 lines (157 loc) · 3.8 KB
/
Copy pathbuilt_in_functions.c
File metadata and controls
182 lines (157 loc) · 3.8 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
#include "main.h"
/**
* match_built_in - Find if the built in to use.
*
* @r: Pointer to the structure (tokens, holder).
*
* @err: Pointer to the counter of errors.
*
* @av: Name of the executable.
*
* @exit_status: Pointer to the exit status of the prev command.
*
* Return: Return 1 if use a function, 0 otherwise.
*/
int match_built_in(response *r, int *err, char *av, int *exit_status)
{
int i = 0;
built options[] = {
{"env", built_env},
{"exit", built_exit},
{"unsetenv", built_unsetenv},
{"setenv", built_setenv},
{"cd", built_cd},
/* {"help", built_help}, */
/* {"alias", built_alias} */
{NULL, NULL}
};
while (options[i].key != NULL)
{
if (strcmp(r->toks[0], options[i].key) == 0)
{
options[i].func(r, err, av, exit_status);
return (1);
}
i++;
}
return (0);
}
/**
* built_env - Command of the simple shell to show the environment.
*
* @r: Pointer to the structure (tokens, holder).
*
* @err: Pointer to the counter of errors.
*
* @av: Name of the executable.
*
* @exit_status: Pointer to the exit status of the prev command.
*
* Return: Return 1 if use a function, 0 otherwise.
*/
int built_env(response *r, int *err, char *av, int *exit_status)
{
int i = 0, number_tokens = 0;
UNUSED(err), UNUSED(av), UNUSED(exit_status);
number_tokens = number_of_tokens(r->toks);
if (number_tokens >= 2)
{
fprintf(stderr, "env: '%s': No such file or directory\n", r->toks[1]);
return (1);
}
while (environ[i])
printf("%s\n", environ[i]), i++;
return (1);
}
/**
* built_exit - Command of the simple shell to exit with status.
*
* @r: Pointer to the structure (tokens, holder).
*
* @err: Pointer to the counter of errors.
*
* @av: Name of the executable.
*
* @exit_status: Pointer to the exit status of the prev command.
*
* Return: Return 1 if use a function, 0 otherwise.
*/
int built_exit(response *r, int *err, char *av, int *exit_status)
{
int number_tokens = 0, new_number = 0;
number_tokens = number_of_tokens(r->toks);
if (number_tokens == 1)
{
free_tokens(r->toks);
free(r->hold);
free(r);
exit(*exit_status);
}
if (is_number(r->toks[1]) && atoi(r->toks[1]) >= 0)
{
new_number = atoi(r->toks[1]);
}
else
{
fprintf(stderr, "%s: %d: exit: Illegal number: %s\n", av, *err, r->toks[1]);
*err += 1;
*exit_status = 2;
free_tokens(r->toks);
free(r->hold);
free(r);
exit(2);
return (1);
}
free_tokens(r->toks);
free(r->hold);
free(r);
exit(new_number);
}
/**
* built_setenv - Command to set or overwrite a env variable.
*
* @r: Pointer to the structure (tokens, holder).
*
* @err: Pointer to the counter of errors.
*
* @av: Name of the executable.
*
* @exit_status: Pointer to the exit status of the prev command.
*
* Return: Return 1 if use a function, 0 otherwise.
*/
int built_setenv(response *r, int *err, char *av, int *exit_status)
{
int num_tokens = number_of_tokens(r->toks);
UNUSED(err), UNUSED(av), UNUSED(exit_status);
if (num_tokens == 3)
setenv(r->toks[1], r->toks[2], 1);
else if (num_tokens < 3)
fprintf(stderr, "To few arguments: Try use \"setenv [KEY] [VALUE]\"\n");
else
fprintf(stderr, "To many arguments: Try use \"setenv [KEY] [VALUE]\"\n");
return (1);
}
/**
* built_unsetenv - Command of the simple shell eliminate a env var.
*
* @r: Pointer to the structure (tokens, holder).
*
* @err: Pointer to the counter of errors.
*
* @av: Name of the executable.
*
* @exit_status: Pointer to the exit status of the prev command.
*
* Return: Return 1 if use a function, 0 otherwise.
*/
int built_unsetenv(response *r, int *err, char *av, int *exit_status)
{
int num_tokens = number_of_tokens(r->toks);
UNUSED(err), UNUSED(av), UNUSED(exit_status);
if (num_tokens == 2)
unsetenv(r->toks[1]);
else
fprintf(stderr, "Wrong number of arguments: Try use \"unsetenv [KEY]\"\n");
return (1);
}