-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpath_handler.c
More file actions
43 lines (39 loc) · 759 Bytes
/
path_handler.c
File metadata and controls
43 lines (39 loc) · 759 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
#include "main.h"
/**
* *path_search - makes a complete path from path.
* @input_str: command to make complete path from.
* Return: complete path.
*/
char *path_search(char *input_str)
{
char *path = NULL, *token = NULL, chs = '/';
char *dirs[1024];
int i = 0;
char *cwd = getcwd(NULL, 0);
struct stat cota;
path = getenv("PATH");
token = strtok(path, ":");
if (dirs[0] == NULL)
return NULL;
while (token != NULL)
{
dirs[i] = token;
token = strtok(NULL, ":");
i++;
}
dirs[i] = NULL;
i = 0;
while (dirs[i] != NULL)
{
chdir(dirs[i]);
if (stat(input_str, &cota) == 0)
{
dirs[i] = string_combine(dirs[i], &chs, 1);
input_str = strjoin(dirs[i], input_str);
break;
}
i++;
}
chdir(cwd);
return (input_str);
}