-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpath.c
41 lines (37 loc) · 799 Bytes
/
path.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
#include "main.h"
/**
* _which - identifies the path of the command (fpath)
* that is being passed to it
* @fpath: the command that is being passed to it
*
* Return: an array of directories containing the command or NULL on failure
*/
char **_which(char *fpath)
{
int size = 64;
int i = 0;
char *copy_path = NULL;
char *delim = ":=";
char **dirs = _calloc(sizeof(char *), size);
char *token = NULL;
if (fpath == NULL)
{
free(fpath);
return (0);
}
if (dirs == NULL)
{
free(fpath);
perror("Error allocated memory");
return (NULL);
}
copy_path = _strdup(fpath); /* Copy the fpath string */
token = strtok(copy_path, delim); /* Split the string by the delimiter */
while (token != NULL)
{
dirs[i] = token;
i++;
token = strtok(NULL, delim);
}
return (dirs);
}