-
Notifications
You must be signed in to change notification settings - Fork 1
/
getAll.c
201 lines (173 loc) · 3.45 KB
/
getAll.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#include "shell.h"
/**
* get_argv - a function to get the command we typed using strtok.
* @strRead: the command we typed.
* Return: array of chars.
*/
char **get_argv(char *strRead)
{
char **argv = NULL, *token, *tmp, *tmp_token;
int size = 0, i;
tmp = _strdup(strRead);
if (tmp == NULL)
return (NULL);
tmp_token = strtokMod(tmp, " \t\n");
while (tmp_token != NULL)
{
size++;
tmp_token = strtokMod(NULL, " \t\n");
}
free(tmp), size++;
argv = (char **)malloc(sizeof(char *) * size);
if (argv == NULL)
return (NULL);
for (i = 0; i < size - 1; i++)
{
token = strtokMod((i ? NULL : strRead), " \t\n");
argv[i] = _strdup(token);
if (argv[i] == NULL)
{
for (i--; i >= 0; i--)
free(argv[i]);
free(argv);
return (NULL);
}
}
argv[size - 1] = NULL;
return (argv);
}
/**
* get_dirs - a function to get the directory we want.
* @path: path of the file.
* Return: the path we want.
*/
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 = strtokMod(tmp, ":");
while (tmp_token != NULL)
{
tmp_token = strtokMod(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 = strtokMod((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);
}
/**
* string_concat - a function to copy string to another.
* @s1: first string .
* @s2: second string.
* Return: the finals string (s).
*/
char *string_concat(char *s1, char *s2)
{
int i, j, k;
char *s;
for (i = 0; s1[i]; i++)
;
for (j = 0; s2[j]; j++)
;
s = (char *)malloc(sizeof(char) * (i + j + 1));
if (s == NULL)
return (NULL);
for (k = 0; k < i; k++)
s[k] = s1[k];
for (k = 0; k < j; k++)
s[k + i] = s2[k];
s[i + j] = '\0';
return (s);
}
/**
*check_path - a function to check the path if correct or not.
*@file: path of the file.
*@dirs: list of directories.
*Return: the path we want.
*/
char *check_path(char *file, char **dirs)
{
struct stat sb;
int i = 0;
char *concat, *concat_path;
concat = string_concat("/", file);
if (concat == NULL)
{
exit(EXIT_FAILURE);
}
for (i = 0; dirs[i]; i++)
{
concat_path = string_concat(dirs[i], concat);
if (concat_path == NULL)
{
free(concat);
exit(EXIT_FAILURE);
}
if (stat(concat_path, &sb) == 0 && sb.st_mode & S_IXUSR)
{
free(concat);
return (concat_path);
}
free(concat_path);
}
free(concat);
return (NULL); /*no path was found */
}
/**
* _getenv - a function to get the enivroment and split it.
* @key: path of the file.
* Return: the value.
*/
char *_getenv(const char *key)
{
char *value = NULL;
int i = 0, j, k, equal, size;
while (__environ[i])
{
equal = 1;
/*Split the current environment variable into name and value*/
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)
{
free(value);
exit(EXIT_FAILURE);
}
for (; __environ[i][j]; j++)
value[k] = __environ[i][j], k++;
value[k] = '\0';
break;
}
i++;
}
return (value);
}