-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuilt_ins.c
172 lines (157 loc) · 3.45 KB
/
built_ins.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
#include "shell.h"
/**
* builtin - checks if command is builtin
* @cmd_list: List of commands
* @alias_list: List of aliases
* @free_env_list: List of envs
* @tokes: command string
* @argv: argv 0
* @env: environment
* @ret: signal value for parser2 function
* Return: 0 if not builtin
*/
char *builtin(char **cmd_list, ll *alias_list,
char *free_env_list, char **tokes, char *argv, char **env, int *ret)
{
int x = 0;
b_in built_in[] = {
{"exit", my_exit}, {"setenv", _setenv}, {"env", b_env},
{"unsetenv", _unsetenv}, {"cd", _cd}, {NULL, NULL}
};
*ret = 0;
for (x = 0; built_in[x].var; x++)
{
if ((_strcmp(tokes[0], built_in[x].var)) == 0)
{
free_env_list = built_in[x].f(cmd_list, alias_list,
free_env_list, tokes, argv, env);
(*ret)++;
}
}
return (free_env_list);
}
/**
* _setenv - sets environment
* @tokes: tokens
* @argv: argv[0]
* @env: environment
* @cmd_list: list of commands
* @alias_list: list of aliases
* @free_env_list: list of envs
* Return: int
*/
char *_setenv(char **cmd_list, ll *alias_list,
char *free_env_list, char **tokes, char *argv, char **env)
{
int x = 0;
char *tmp;
(void)cmd_list;
(void)alias_list;
(void)argv;
while (env[x])
{
if (_strncmp(env[x], tokes[1], _strlen(tokes[1])) == 0)
break;
x++;
}
if (env[x] != NULL)
{
free_env_list = add_to_free_env(free_env_list, tokes[1]);
tmp = str_mul_cat(tokes[1], tokes[2], "=");
env[x] = _strdup(tmp);
}
else
{
free_env_list = add_to_free_env(free_env_list, tokes[1]);
tmp = str_mul_cat(tokes[1], tokes[2], "=");
env[x] = _strdup(tmp);
env[x + 1] = NULL;
}
return (free_env_list);
}
/**
* add_to_free_env - adds to free env list
* @free_env_list: where we are adding
* @token: what we are adding
* Return: env list
*/
char *add_to_free_env(char *free_env_list, char *token)
{
int newsize = 0, len = 0, x = 0;
if (!free_env_list)
{
free_env_list = malloc(_strlen(token) + 1);
for (x = 0; token[x]; x++)
free_env_list[x] = token[x];
free_env_list[x] = '\0';
}
else
{
len = _strlen(free_env_list);
newsize = len + _strlen(token);
free_env_list = _realloc(free_env_list, len, newsize + 2);
free_env_list = str_mul_cat(free_env_list, token, " ");
}
return (free_env_list);
}
/**
* _unsetenv - unsets env var
* @tokes: tokens
* @argv: argv[0]
* @env: environment
* @cmd_list: list of commadns
* @alias_list: list of aliases
* @free_env_list: list of envs
* Return: int
*/
char *_unsetenv(char **cmd_list, ll *alias_list,
char *free_env_list, char **tokes, char *argv, char **env)
{
int x = 0;
(void)cmd_list;
(void)alias_list;
(void)argv;
while (env[x])
{
if (_strncmp(env[x], tokes[1], _strlen(tokes[1])) == 0)
break;
x++;
}
if (env[x] != NULL)
{
free(env[x]);
while (env[x + 1] != NULL)
{
env[x] = env[x + 1];
x++;
}
env[x] = env[x + 1];
free_env_list = remove_free_list_node(free_env_list, tokes[1]);
}
return (free_env_list);
}
/**
* b_env - env builtin cmd
* @tokes: tokens
* @argv: argv[0]
* @env: environment
* @cmd_list: list of commands
* @alias_list: list of aliasaes
* @free_env_list: list of envs
* Return: int
*/
char *b_env(char **cmd_list, ll *alias_list,
char *free_env_list, char **tokes, char *argv, char **env)
{
int x = 0;
(void)cmd_list;
(void)alias_list;
(void)argv;
(void)tokes;
for (x = 0; env[x]; x++)
{
write(STDOUT_FILENO, env[x], _strlen(env[x]));
write(STDOUT_FILENO, "\n", 1);
}
return (free_env_list);
}