forked from erikaosgue/simple_shell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfind_exe_Path.c
More file actions
51 lines (50 loc) · 1.21 KB
/
find_exe_Path.c
File metadata and controls
51 lines (50 loc) · 1.21 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
#include "shell.h"
/**
* find_exe_Path - Checks if the file exist in the PATH variable
* @filename: name of the executable file
* @index: for checking the current directory at that index compare with token
* @fullpath: its the path value of the env variable usually PATH
* Return: A pointer with the pathname NULL otherwise
*/
char *find_exe_Path(char *filename, int index, char *fullpath)
{
struct stat st;
char *token, *newpoin, *newpath;
int size = 0, Dir = 0;
newpath = _strdup(fullpath);
token = _strtok(newpath, ":");
while (token)
{
Dir += 1;
if (Dir == index)
{
newpoin = _find_exe_cwd(filename);
if (newpoin != NULL)
return (newpoin);
}
size = (_strlen(token) + _strlen(filename) + 2);
newpoin = malloc(sizeof(char) * size);
if (newpoin == NULL)
{ free(newpath);
return (NULL);
}
newpoin = _strcpy1(newpoin, token, 1);
newpoin = _strcat(newpoin, filename);
if (stat(newpoin, &st) == 0)
{ free(newpath);
return (newpoin);
}
free(newpoin);
token = _strtok(NULL, ":");
if (token == NULL)
break;
}
free(newpath);
if (fullpath[_strlen(fullpath) - 1] == ':')
{
newpoin = _find_exe_cwd(filename);
if (newpoin != NULL)
return (newpoin);
}
return (NULL);
}