-
Notifications
You must be signed in to change notification settings - Fork 0
/
env_builtins.c
139 lines (119 loc) · 3.15 KB
/
env_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
/*
* File: env_builtins.c
* Auth: Wandile Dlamini
* Lwazi
*/
#include "shell.h"
int shellby_env(char **args, char __attribute__((__unused__)) **front);
int shellby_setenv(char **args, char __attribute__((__unused__)) **front);
int shellby_unsetenv(char **args, char __attribute__((__unused__)) **front);
/**
* shellby_env - Prints the current environment.
* @args: An array of arguments passed to the shell.
* @front: A double pointer to the beginning of args.
*
* Return: If an error occurs - -1.
* Otherwise - 0.
*
* Description: Prints one variable per line in the
* format 'variable'='value'.
*/
int shellby_env(char **args, char __attribute__((__unused__)) **front)
{
int index;
char nc = '\n';
if (!environ)
return (-1);
for (index = 0; environ[index]; index++)
{
write(STDOUT_FILENO, environ[index], _strlen(environ[index]));
write(STDOUT_FILENO, &nc, 1);
}
(void)args;
return (0);
}
/**
* shellby_setenv - Changes or adds an environmental variable to the PATH.
* @args: An array of arguments passed to the shell.
* @front: A double pointer to the beginning of args.
* Description: args[1] is the name of the new or existing PATH variable.
* args[2] is the value to set the new or changed variable to.
*
* Return: If an error occurs - -1.
* Otherwise - 0.
*/
int shellby_setenv(char **args, char __attribute__((__unused__)) **front)
{
char **env_var = NULL, **new_environ, *new_value;
size_t size;
int index;
if (!args[0] || !args[1])
return (create_error(args, -1));
new_value = malloc(_strlen(args[0]) + 1 + _strlen(args[1]) + 1);
if (!new_value)
return (create_error(args, -1));
_strcpy(new_value, args[0]);
_strcat(new_value, "=");
_strcat(new_value, args[1]);
env_var = _getenv(args[0]);
if (env_var)
{
free(*env_var);
*env_var = new_value;
return (0);
}
for (size = 0; environ[size]; size++)
;
new_environ = malloc(sizeof(char *) * (size + 2));
if (!new_environ)
{
free(new_value);
return (create_error(args, -1));
}
for (index = 0; environ[index]; index++)
new_environ[index] = environ[index];
free(environ);
environ = new_environ;
environ[index] = new_value;
environ[index + 1] = NULL;
return (0);
}
/**
* shellby_unsetenv - Deletes an environmental variable from the PATH.
* @args: An array of arguments passed to the shell.
* @front: A double pointer to the beginning of args.
* Description: args[1] is the PATH variable to remove.
*
* Return: If an error occurs - -1.
* Otherwise - 0.
*/
int shellby_unsetenv(char **args, char __attribute__((__unused__)) **front)
{
char **env_var, **new_environ;
size_t size;
int index, index2;
if (!args[0])
return (create_error(args, -1));
env_var = _getenv(args[0]);
if (!env_var)
return (0);
for (size = 0; environ[size]; size++)
;
new_environ = malloc(sizeof(char *) * size);
if (!new_environ)
return (create_error(args, -1));
for (index = 0, index2 = 0; environ[index]; index++)
{
if (*env_var == environ[index])
{
free(*env_var);
continue;
}
new_environ[index2] = environ[index];
index2++;
}
free(environ);
environ = new_environ;
environ[size - 1] = NULL;
return (0);
}