-
Notifications
You must be signed in to change notification settings - Fork 0
/
_unsetenv.c
85 lines (82 loc) · 1.75 KB
/
_unsetenv.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
#include "sshell.h"
/**
* _isunsetenv - finds if line input is unsetenv
* @p: input of user, array of pointers
* @myenv: copy of environmental variables
* @loop: loops counter
* @v: arguments in input
* @e: number of elements in myenv
* Return: -1 if fails or 0 if success
*/
int _isunsetenv(char **p, char **myenv, int *e, int loop, char *v[])
{
char str[] = "unsetenv";
int i = 0, cont = 0, salida = -1;
i = 0;
while (p[0][i] != '\0')
{
if (i < 8)
{
if (p[0][i] == str[i])
cont++;
}
i++;
}
if (i == 8)
cont++;
if (cont == 9)
{
_unsetenv(p, myenv, e, loop, v);
salida = 0;
}
else if (cont == 8)
{
salida = 0;
_put_err(p, loop, 3, v);
}
return (salida);
}
/**
* _unsetenv - function to remove an environment variable
* environ points to an array of pointers to strings called the "environment"
* @p: input of user, array of pointers
* @myenv: icopy of environmental
* @loop: loops counter
* @v: arguments in input
* @e: number of elements in myenv
*/
void _unsetenv(char **p, char **myenv, int *e, int loop, char *v[])
{
int i, lg, j, k = 0, k2 = 0, k3 = 0, cont = 0;
lg = _strlen(p[1]);
for (i = 0; myenv[i] != NULL; i++, cont = 0)
{
for (j = 0; p[1][j] != '\0' && j < lg; j++)
{
if (p[1][j] == myenv[i][j])
cont++;
}
if (cont == lg)
break;
}
if (cont == lg)
{
for (k = i; myenv[k] != NULL && myenv[k + 1] != NULL; k++)
{
for (k2 = 0; myenv[k][k2] != '\0'; k2++)
myenv[k][k2] = 0;
for (k3 = 0; myenv[k + 1][k3] != '\0'; k3++)
;
if (k2 < k3)
myenv[k] = _realloc(myenv[k], k2, k3);
for (k2 = 0; myenv[k + 1][k2] != '\0'; k2++)
myenv[k][k2] = myenv[k + 1][k2];
}
free(myenv[k]);
myenv[k] = NULL;
*e = *e - 1;
free(myenv[k + 1]);
}
else
_put_err(p, loop, 1, v);
}