-
Notifications
You must be signed in to change notification settings - Fork 2
/
_findpath.c
41 lines (38 loc) · 952 Bytes
/
_findpath.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
38
39
40
41
#include "headers.h"
/**
* findpath - find the directory of a command
*@command: string with the command
*@retVal: return value of exit
* Return: the directory of the command
*/
char *findpath(char *command, int *retVal)
{
char *path, *commandtoprint;
struct stat stats;
char *current_source;
char *tok;
if (stat(command, &stats) == 0)
return (command);
path = _getenv("PATH");
tok = strtok(path, ":");
commandtoprint = command;
command = str_concat("/", command);
/*stat() returns 0 on successful operation,*/
/* otherwise returns -1 if unable to get file properties.*/
while (tok != NULL)
{
current_source = str_concat(tok, command);
if (stat(current_source, &stats) == 0)
{
free(command);
return (current_source);
}
free(current_source);
tok = strtok(NULL, ":");
}
error_printing(path, find_length(command), commandtoprint);
print_string(": not found", 0);
free(command);
*retVal = 127;
return (NULL);
}