-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpathhandling.c
More file actions
50 lines (47 loc) · 967 Bytes
/
pathhandling.c
File metadata and controls
50 lines (47 loc) · 967 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
42
43
44
45
46
47
48
49
50
#include "simple_shell.h"
/**
* pathhand - loops through path var and concats cmd
* @s: pointer to path var
* @env: double pointer to env var
* Return: pointer to concat s
*/
char *pathhand(char *s, char **env)
{
char *cant, *route;
path_t *lists, *temp;
struct stat mk;
if (s == NULL || env == NULL || *env == NULL)
return (NULL);
route = get_env("PATH", env);
if (route == NULL)
{
write(STDERR_FILENO, "No path available", 17);
_exit(0);
}
lists = develop_likedlist(route);
if (lists == NULL)
{
write(STDERR_FILENO, "faulty path", 11);
_exit(0);
}
lists = appendnode(route, lists);
temp = lists;
while (temp != NULL)
{
if (route[0] == ':')
cant = str_concat("./", s);
else
cant = str_concat(temp->dir, s);
if (cant == NULL)
return (NULL);
if (stat(cant, &mk) == 0 && access(cant, X_OK) == 0)
{
list_free(lists);
return (cant);
}
temp = temp->next;
free(cant);
}
list_free(lists);
return (NULL);
}