-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetenv.c
70 lines (66 loc) · 1.27 KB
/
getenv.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
#include "header.h"
/**
* _get_env - gets the current env
* @env: the env
* Return: _env_parser function on success, NULL on failure
**/
char **_get_env(char *env)
{
int inner;
int outer;
char *name = NULL;
for (outer = 0; environ[outer] != NULL; outer++)
{
for (inner = 0; environ[outer][inner] != '='; inner++)
{
if (environ[outer][inner] != env[inner])
break;
if (environ[outer][inner] == env[inner])
{
if (env[inner + 1] == '\0' && environ[outer][inner + 1] == '=')
{
name = _strdup(&(environ[outer][inner + 2]));
return (_env_parser(name));
}
}
}
}
return (NULL);
}
/**
* _env_parser- tokenizes the PATH
* @name: the full PATH seperated by :'s
* Return: an array of strings
**/
char **_env_parser(char *name)
{
int token_inc;
int tokencount;
char *tokenize = NULL;
int i;
char **p = NULL;
char *namestore = name;
tokencount = 0;
for (i = 0; name[i] != '\0'; i++)
{
if (name[i] == ':')
{
tokencount++;
}
}
p = malloc(sizeof(char *) * (tokencount + 2));
if (p != NULL)
{
token_inc = 0;
tokenize = strtok(name, ":");
while (token_inc < (tokencount + 1))
{
p[token_inc] = _strdup(tokenize);
tokenize = strtok(NULL, ":");
token_inc++;
}
p[token_inc] = NULL;
}
free(namestore);
return (p);
}