-
Notifications
You must be signed in to change notification settings - Fork 0
/
handlepath2.c
69 lines (63 loc) · 1.45 KB
/
handlepath2.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
#include "shell.h"
/**
* total_dir - finds total number of directories in the path
* @path: the path string
*
* Return: number of directories in the path
*/
unsigned int total_dir(char *path)
{
unsigned int dir, i, flag;
i = 0, dir = 0, flag = 0;
while (path[i])
{
if (path[i] != ':')
flag = 1;
if ((flag && path[i + 1] == ':') || (flag && path[i + 1] == '\0'))
{
dir++;
flag = 0;
}
++i;
}
return (dir);
}
/**
* _getenv - gets the environment variable value
* @name: name of the environment vaariable you are looking for
* @environ: the enviroment variables
*
* Return: the value associated with the variable
*/
char *_getenv(const char *name, char **environ)
{
char *env_value;
char *name_copy;
unsigned int i, length;
/* find the length of the argument, then malloc space for it */
length = _conststrlen(name);
name_copy = malloc((sizeof(char) * length) + 1);
if (name_copy == NULL)
return (NULL);
/* copy the contents of the name argument to the new variable, name_copy */
_conststrncpy(name_copy, name, length);
/*
* find the enviroment variable that matches the name_copy variable
* assign the value to the value variable and return the address
*/
i = 0;
env_value = strtok(environ[i], "=");
while (environ[i])
{
if (_strcmp(env_value, name_copy))
{
env_value = strtok(NULL, "\n");
free(name_copy);
return (env_value);
}
++i;
env_value = strtok(environ[i], "=");
}
free(name_copy);
return (NULL);
}