-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhandle_env.c
More file actions
97 lines (81 loc) · 1.35 KB
/
handle_env.c
File metadata and controls
97 lines (81 loc) · 1.35 KB
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
#include "shell.h"
/**
* _getenv - This function is used to get the part of
* an environ
* @name: env to search
* Return: a pointer to environ
*/
char *_getenv(const char *name)
{
int comp;
char **envir;
char *value;
size_t len;
if (name == NULL)
return (NULL);
len = _count(name);
for (envir = environ; *envir != NULL; ++envir)
{
comp = _strncmp(*envir, name, len);
if (comp == 0 && (*envir)[len] == '=')
{
value = malloc(_strlen(*envir) - len);
if (value == NULL)
return (NULL);
_strcpy(value, *envir + len + 1);
return (value);
}
}
return (NULL);
}
/**
* printenv - This function prints env variables
*/
void printenv(void)
{
int i = 0, len;
while (environ[i] != NULL)
{
len = _strlen(environ[i]);
write(STDOUT_FILENO, environ[i], len);
_putchar('\n');
++i;
}
}
/**
* *_strcpy - The function copies an input from a string
* to another
* @dest: destination of copied str
* @src: source to copy
* Return: Str (success)
*/
char *_strcpy(char *dest, char *src)
{
int i = 0, j;
while (src[i] != '\0')
{
i++;
}
j = 0;
while (j < i)
{
dest[j] = src[j];
j++;
}
dest[j] = '\0';
return (dest);
}
/**
* _count - function to count a str
* @s: represent str to count
* Return: len of str (success)
*/
size_t _count(const char *s)
{
size_t len = 0;
while (s[len] != '\0')
{
len++;
}
return (len);
}