-
Notifications
You must be signed in to change notification settings - Fork 0
/
shell_env.c
executable file
·138 lines (118 loc) · 3.29 KB
/
shell_env.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
/*
* File: shell_env.c
* Auth: lUCY OUMA
* PESSY KAPERE
*/
#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_er;
char nc = '\n';
if (!environ)
return (-1);
for (index_er = 0; environ[index_er]; index_er++)
{
write(STDOUT_FILENO, environ[index_er], _strlen(environ[index_er]));
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_er = NULL, **new_environment, *new_val_er;
size_t size;
int index_er;
if (!args[0] || !args[1])
return (create_error(args, -1));
new_val_er = malloc(_strlen(args[0]) + 1 + _strlen(args[1]) + 1);
if (!new_val_er)
return (create_error(args, -1));
_strcpy(new_val_er, args[0]);
_strcat(new_val_er, "=");
_strcat(new_val_er, args[1]);
env_var_er = _getenv(args[0]);
if (env_var_er)
{
free(*env_var_er);
*env_var_er = new_val_er;
return (0);
}
for (size = 0; environ[size]; size++)
;
new_environment = malloc(sizeof(char *) * (size + 2));
if (!new_environment)
{
free(new_val_er);
return (create_error(args, -1));
}
for (index_er = 0; environ[index_er]; index_er++)
new_environment[index_er] = environ[index_er];
free(environ);
environ = new_environment;
environ[index_er] = new_val_er;
environ[index_er + 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_er, **new_environment;
size_t size;
int index_er, index_er2;
if (!args[0])
return (create_error(args, -1));
env_var_er = _getenv(args[0]);
if (!env_var_er)
return (0);
for (size = 0; environ[size]; size++)
;
new_environment = malloc(sizeof(char *) * size);
if (!new_environment)
return (create_error(args, -1));
for (index_er = 0, index_er2 = 0; environ[index_er]; index_er++)
{
if (*env_var_er == environ[index_er])
{
free(*env_var_er);
continue;
}
new_environment[index_er2] = environ[index_er];
index_er2++;
}
free(environ);
environ = new_environment;
environ[size - 1] = NULL;
return (0);
}