-
Notifications
You must be signed in to change notification settings - Fork 0
/
_searchpath.c
37 lines (35 loc) · 707 Bytes
/
_searchpath.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
#include "minishell.h"
/**
* _searchpath - searches for a given command in the path
*
* @com: command to search in path
*
* Return: pointer to the string that represents the complete path of com.
* NULL if not found in any dir of PATH.
*/
char *_searchpath(char *com)
{
char *path = _getenv("PATH");
char *token = NULL, *dir = NULL;
struct stat st;
if (path == NULL)
{
return (NULL);
}
token = strtok(path, ":");
while (token != NULL)
{
dir = _concat(3, token, "/", com);
if (stat(dir, &st) == 0)
{
free(path);
return (dir);
}
free(dir);
token = strtok(NULL, ":");
}
/*At this points means it was not found in PATH*/
free(path);
dir = NULL;
return (dir);
}