-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathenv-get-set.c
152 lines (139 loc) · 2.73 KB
/
env-get-set.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
#include "shell.h"
/* for accesing environmental variables */
extern char **environ;
/**
* checkEnv - checks if environmenta variable name is null or contains '='
* @name: pointer to variable name
* Return: if name is valid returns its length, else 0
**/
int checkEnv(char *name)
{
int i;
if (name == NULL || *name == '\0')
return (0);
for (i = 0; name[i] != '\0'; i++)
{
if (name[i] == '=')
return (0);
}
return (i);
}
/**
* _getenv - finds an environmental variable
* @name: pointer to variable name
* Return: pointer to found value or null
**/
char *_getenv(char *name)
{
char **dupeenv, **token;
dupeenv = deepDupe(environ);
if (dupeenv == NULL)
{
_puts("Memory Allocation Error\n");
return (NULL);
}
while (*dupeenv)
{
token = tokenize(*dupeenv, '=');
if (_strcmp(token[0], name) == 0)
return (token[1]);
dupeenv++;
}
_puts("No such enviornent variable\n");
return (NULL);
}
/**
* _setenv - sets an environment variable based on user input
* @tokens: pointer to user input
* Return: nothing
**/
void _setenv(char **tokens)
{
char *new, **tok, **dupeenv;
int i;
if (tokens[1] == NULL || tokens[2] == NULL)
{
_puts("Usage: setenv name value\n");
return;
}
new = _strcat(tokens[1], tokens[2], '=');
if (new == NULL)
{
_puts("Memory Allocation Error\n");
return;
}
i = 0;
dupeenv = deepDupe(environ);
if (dupeenv == NULL)
{
_puts("Memory Allocation Error\n");
return;
}
for (i = 0; dupeenv[i]; i++)
{
tok = tokenize(dupeenv[i], '=');
if (*tok)
{
if (_strcmp(tok[0], tokens[1]) == 0)
{
environ[i] = new;
/*change value to given value and return 0 */
return;
}
}
}
environ[i++] = new;
environ[i] = NULL;
}
/**
* _unsetenv - unsets an envriornment variable
* @tokens: pointer to user input
* Return: nothing
**/
void _unsetenv(char **tokens)
{
char **dupeenv;
int i;
char **token;
i = checkEnv(tokens[1]);
if (i == 0)
{
_puts("Usage: unsetenv name\n");
return;
}
dupeenv = deepDupe(environ);
if (dupeenv == NULL)
{
_puts("Memory Allocation Error\n");
return;
}
for (i = 0; dupeenv[i]; i++)
{
token = tokenize(dupeenv[i], '=');
/* delete variable name from environment */
if (_strcmp(token[0], tokens[1]) == 0)
{
for (; environ[i] != NULL; i++)
environ[i] = environ [i + 1];
environ[i] = NULL;
return;
}
}
/* name does not exist in the environment, function succeeds */
_puts("No such environment variable exists\n");
}
/**
* printEnv - prints list of all environment variables
* @tokens: pointer to user input as part of builtin prototype
* Return: nothing
**/
void printEnv(char **tokens)
{
int i;
UNUSED(tokens);
for (i = 0; environ[i]; i++)
{
_puts(environ[i]);
_puts("\n");
}
}