-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpath.c
More file actions
104 lines (91 loc) · 2.01 KB
/
path.c
File metadata and controls
104 lines (91 loc) · 2.01 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include "main.h"
/**
* full_path - Get the full path of a command in the environment
* @env: Array of strings containing environment variables
* @cmd: String containing the command to search for in the environment
*
* Return: Pointer to the full path
*/
char *full_path(char *env[], char cmd[])
{
char *path_env = NULL, *path_copy, *token, *result;
char test_path[1024];
int i;
const char *delim = ":";
/* Find PATH environment variable */
for (i = 0; env[i]; i++)
{
if (env[i][0] == 'P' && env[i][1] == 'A' && env[i][2] == 'T' &&
env[i][3] == 'H' && env[i][4] == '=')
{
path_env = env[i] + 5;
break;
}
}
if (!path_env)
return (_strdup(cmd));
path_copy = _strdup(path_env);
if (!path_copy)
return (_strdup(cmd));
token = strtok(path_copy, delim);
while (token)
{
size_t token_len;
token_len = _strlen(token);
_strcpy(test_path, token);
if (token_len > 0 && test_path[token_len - 1] != '/')
{
test_path[token_len] = '/';
test_path[token_len + 1] = '\0';
token_len++;
}
_strcpy(test_path + token_len, cmd);
if (access(test_path, X_OK) == 0)
{
result = _strdup(test_path);
free(path_copy);
return (result);
}
token = strtok(NULL, delim);
}
free(path_copy);
return (_strdup(cmd));
}
/**
* path_check - Check if a file exists in the specified paths
* @path: String containing multiple paths separated by colons
*
* Return: Pointer to the first path
*/
char *path_check(char path[])
{
char *token, *path_ex = NULL, final_path[256];
char *path_copy;
int i = 0;
const char *delim = ":";
path_copy = _strdup(path);
if (!path_copy)
return (NULL);
token = strtok(path_copy, delim);
while (token)
{
path_ex = token;
if (access(path_ex, F_OK) != -1)
{
while (path_ex[i] != '\0')
{
final_path[i] = path_ex[i];
i++;
}
final_path[i] = '\0';
free(path_copy);
_strcpy(path, final_path);
return (path);
}
token = strtok(NULL, delim);
}
free(path_copy);
if (path_ex)
_strcpy(path, path_ex);
return (path);
}