-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils_0.c
155 lines (144 loc) · 2.83 KB
/
utils_0.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
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#include "main.h"
/**
* _getenv - searches the environment list to find the environment variable key
* @key: the environment variable
*
* Return: a pointer to the corresponding value string,
* or NULL if there is no match
*/
char *_getenv(const char *key)
{
char *value = NULL;
int i = 0, j, k, equal, size;
while (__environ[i])
{
equal = 1;
for (j = 0; __environ[i][j] != '=' && key[j]; j++)
{
if (key[j] != __environ[i][j])
equal = 0;
}
if (equal)
{
k = 0;
for (size = j; __environ[i][size]; size++)
;
size -= j;
j++;
value = (char *)malloc(sizeof(char) * (size + 1));
if (value == NULL)
exit(EXIT_FAILURE);
for ( ; __environ[i][j]; j++)
value[k] = __environ[i][j], k++;
value[k] = '\0';
break;
}
i++;
}
return (value);
}
/**
* get_args - construct an argument array similiar to argv
* {"program_name", "arg1", "arg2", ..., NULL}
* @buffer: user input
*
* Return: a null-terminated array of strings on success,
* NULL on failure
*/
char **get_args(char *buffer)
{
char **args = NULL, *token, *tmp, *tmp_token;
int size = 0, i;
tmp = _strdup(buffer);
if (tmp == NULL)
return (NULL);
tmp_token = strtok(tmp, " \t\n");
while (tmp_token != NULL)
{
size++;
tmp_token = strtok(NULL, " \t\n");
}
free(tmp), size++;
args = (char **)malloc(sizeof(char *) * size);
if (args == NULL)
return (NULL);
for (i = 0; i < size - 1; i++)
{
token = strtok((i ? NULL : buffer), " \t\n");
args[i] = _strdup(token);
if (args[i] == NULL)
{
for (i--; i >= 0; i--)
free(args[i]);
free(args);
return (NULL);
}
}
args[size - 1] = NULL;
return (args);
}
/**
* get_dirs - construct an array of PATH directories
* @path: the path environ value
*
* Return: a null-terminated array of strings,
* NULL on failure
*/
char **get_dirs(char *path)
{
char **dirs = NULL, *token, *tmp, *tmp_token;
int size = 0, i;
tmp = _strdup(path);
if (tmp == NULL)
exit(EXIT_FAILURE);
tmp_token = strtok(tmp, ":");
while (tmp_token != NULL)
{
tmp_token = strtok(NULL, ":");
size++;
}
free(tmp), size++;
dirs = (char **)malloc(sizeof(char *) * size);
if (dirs == NULL)
exit(EXIT_FAILURE);
for (i = 0; i < size - 1; i++)
{
token = strtok((i ? NULL : path), ":");
dirs[i] = _strdup(token);
if (dirs[i] == NULL)
{
for (i--; i >= 0; i--)
free(dirs[i]);
free(dirs);
exit(EXIT_FAILURE);
}
}
dirs[size - 1] = NULL;
return (dirs);
}
/**
* free_2d - frees an array of strings form memory
* @array: the array
*/
void free_2d(char **array)
{
int i = 0;
while (array[i])
free(array[i]), i++;
free(array);
}
/**
* print_env - prints the environment
* @env: a null terminated array of strings,
* with the following format var=value
*/
void print_env(char **env)
{
unsigned int i;
i = 0;
while (env[i] != NULL)
{
printf("%s\n", env[i]);
i++;
}
}