forked from erikaosgue/simple_shell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwhich.c
More file actions
41 lines (38 loc) · 875 Bytes
/
which.c
File metadata and controls
41 lines (38 loc) · 875 Bytes
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 "shell.h"
/**
* _which - function that finds a file in the variable PATH
* @filename: it's the file or path to find
* Return: A pointer of the path or if it doesn't find a file NULL
*/
char *_which(char *filename)
{
char *newpoin, *fullpath = NULL, *ptrPATH;
int index;
ptrPATH = "PATH";
fullpath = _getenv(ptrPATH);
if (fullpath == NULL)
{
newpoin = PATH_empty(filename);
if (newpoin != NULL)
return (newpoin);
return (NULL);
}
if (filename[0] == '/')
{
newpoin = find_slash(filename);
if (newpoin != NULL)
return (newpoin);
return (NULL);
}
else if (fullpath[0] == ':' || filename[0] == '.')
{
newpoin = _find_exe_cwd(filename);
if (newpoin != NULL)
return (newpoin);
}
index = cwd_inside_PATH(fullpath);
newpoin = find_exe_Path(filename, index, fullpath);
if (newpoin != NULL)
return (newpoin);
return (NULL);
}