-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenviro.c
94 lines (88 loc) · 1.84 KB
/
enviro.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
#include "shell.h"
/**
* envFunc - prints the enviroment
* @build: input build
* Return: Always 1
*/
int envFunc(vars_t *build)
{
printList(build->enviroment);
return (1);
}
/**
* setenvFunc - adds env variable if it does not exist;
* modify env variable if it does
* @build: input build
* Return: Always 1
*/
int setenvFunc(vars_t *build)
{
register int index, len;
static char buffer[BUFSIZE];
if (countArgs(build->args) != 3)
{
errno = EWSIZE;
errorHandler(build);
return (1);
}
len = _strlen(build->args[1]) + _strlen(build->args[2]) + 2;
_strcat(buffer, build->args[1]);
_strcat(buffer, "=");
_strcat(buffer, build->args[2]);
insertNullByte(buffer, len - 1);
index = searchNode(build->enviroment, build->args[1]);
if (index == -1)
{
addNodeEnd(&build->enviroment, buffer);
insertNullByte(buffer, 0);
return (1);
}
deleteNodeAtIndex(&build->enviroment, index);
addNodeAtIndex(&build->enviroment, index, buffer);
insertNullByte(buffer, 0);
return (1);
}
/**
* unsetenvFunc - deletes env variable if exists;
* will only accept valid variables names
* @build: input build
* Return: Always 1
*/
int unsetenvFunc(vars_t *build)
{
register int foundVar, i = 1;
bool foundMatch = false;
while (build->args[i])
{
if (_isalpha(build->args[i][0]) || build->args[i][0] == '_')
{
foundVar = searchNode(build->enviroment, build->args[i]);
if (foundVar > -1)
{
deleteNodeAtIndex(&build->enviroment, foundVar);
foundMatch = true;
}
}
i++;
}
if (foundMatch == false)
{
errno = ENOSTRING;
errorHandler(build);
}
return (1);
}
/**
* _isalpha - checks if c is an alphabetic character
* @c: potential alphabetical value
* Return: if c is a letter, returns 1. Otherwise, return 0.
*/
int _isalpha(int c)
{
if (c > 64 && c < 91)
return (1);
else if (c > 96 && c < 123)
return (1);
else
return (0);
}