-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path6_shell.c
More file actions
41 lines (41 loc) · 791 Bytes
/
Copy path6_shell.c
File metadata and controls
41 lines (41 loc) · 791 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 "celem.h"
/**
* search_path - show file source
* @elem: element
* Return: cmd source
*/
char *search_path(char *elem)
{
char *source = _getenv("PATH"), *source_dup;
char **source_div;
char *source_add = NULL;
int d = 0, source_size = 0;
struct stat info;
if (stat(elem, &info) == 0)
{
return (elem);
}
source_dup = malloc(_strlen(source) + 1);
source_dup = _strcpy(source_dup, source);
source_div = _split(source_dup, ":");
while (source_div[d])
{
source_size = _strlen(source_div[d]);
if (source_div[d][source_size - 1] != '/')
{
source_add = _strcat(source_div[d], "/");
}
source_add = _strcat(source_div[d], elem);
if (stat(source_add, &info) == 0)
break;
d++;
}
free(source_dup);
if (!source_div[d])
{
free(source_div);
return (NULL);
}
free(source_div);
return (source_add);
}