-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpathfinder.c
More file actions
52 lines (48 loc) · 1.13 KB
/
pathfinder.c
File metadata and controls
52 lines (48 loc) · 1.13 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
52
#include <stdio.h>
#include "shell.h"
/**
* pathfinder - A function that finds absolute path of a command in PAHT
*
* @filename: The command name to find in the PATH.
* Return: The absolute address of the command or NULL.
*/
char *pathfinder(char *filename)
{
char *path_copy, *path_env;
char *token, *delim = ":";
char *absolute_path;
size_t tok_len, file_len;
/*If the filename is accessible, use it as is*/
if (access(filename, F_OK) == 0)
{
return (filename);
}
else
{
path_env = getenv("PATH");
path_copy = strdup(path_env); /*FREE ME !!!!*/
token = strtok(path_copy, delim);
while (token)
{
/*write the full path inside a string */
/*we can use it like a buffer too and use write*/
tok_len = strlen(token);
file_len = strlen(filename);
absolute_path = (char *)malloc(tok_len + file_len + 2);
if (absolute_path == NULL)
{
perror("Error");
free(path_copy);
return ("");
}
sprintf(absolute_path, "%s/%s", token, filename);
if (access(absolute_path, F_OK) == 0)
{
free(path_copy);
return (absolute_path);
}
token = strtok(NULL, delim);
}
}
return ("");
}