-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpath.c
More file actions
121 lines (107 loc) · 1.9 KB
/
Copy pathpath.c
File metadata and controls
121 lines (107 loc) · 1.9 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include "shell.h"
/**
* build_path - Adds each path directory to a NULL-terminated array
*
* @path: colon separated string of all paths
* Return: NULL-terminated array of dir strings
*/
char **build_path(char *path)
{
char **pathlist;
int i = 0;
if (!path)
return (NULL);
pathlist = strtow(path, ':');
if (pathlist)
return (pathlist);
pathlist = malloc(sizeof(char **));
if (!pathlist)
return (NULL);
pathlist[i] = malloc(sizeof(char) * 2);
if (!pathlist[i])
{
free(pathlist);
return (NULL);
}
pathlist[i][i] = '.';
pathlist[i][1] = '\0';
return (pathlist);
}
/**
* print_dir - prints the directory of the path found, one per line
*
* @path: colon separated string of all paths
* Return: void
*/
void print_dir(char *path)
{
if (!path)
return;
while (*path)
{
if (*path != ':')
write(STDOUT_FILENO, path, 1);
else
write(STDOUT_FILENO, "\n", 1);
path++;
}
}
/**
* print_env - prints the environment variables
*
* @env: pointer to array of environment variables
* Return: void
*/
void print_env(char **env)
{
int i = 0, k = 0;
if (!env)
return;
while (env[i])
{
for (k = 0; env[i][k];)
k++;
write(STDOUT_FILENO, env[i], k);
print_str("\n");
i++;
}
}
/**
* _getenv - gets a matching env variable or returns NULL
*
* @name: name of env variable we want
* Return: the contents of a matching variable or NULL
*/
char *_getenv(char *name)
{
int i = 0, k = 0;
if (!environ || !name)
return (NULL);
while (environ[k])
{
if (*(name + i) != environ[k][i])
{
if (!(*(name + i)) && environ[k][i] == '=')
return (cut_env(environ[k]));
i = 0;
k++;
}
else
{
i++;
}
}
return (NULL);
}
/**
* cut_env - cuts the environment variable to be only after the =
*
* @env: string of environment variable
* Return: the cut string
*/
char *cut_env(char *env)
{
while (*env != '=')
env++;
return (++env);
}