-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.c
More file actions
91 lines (79 loc) · 1.75 KB
/
parser.c
File metadata and controls
91 lines (79 loc) · 1.75 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
#include "shell.h"
/**
* is_cmd - Checks if a given file is executable.
* @info: A pointer to the info_t struct.
* @path: The path to check.
*
* Return: 1 if it is true, 0 otherwise.
*/
int is_cmd(info_t *info, char *path)
{
struct stat str;
(void)info;
if (!path || stat(path, &str))
return (0);
if (str.st_mode & S_IFREG)
{
return (1);
}
return (0);
}
/**
* dup_chars - the duplicate characters.
* @pathstr: the path of the string.
* @start: the starting index of the substring
* @stop: the stopping index of the substring.
*
* Return: the pointer to the duplicated substring.
*/
char *dup_chars(char *pathstr, int start, int stop)
{
static char buf[1024];
int n = 0, m = 0;
for (m = 0, n = start; n < stop; n++)
if (pathstr[n] != ':')
buf[m++] = pathstr[n];
buf[m] = 0;
return (buf);
}
/**
* find_path - Finds cmd in the PATH.
* @info: it is the info struct.
* @pathstr: it is the PATH String.
* @cmd: it is the cmd to find
*
* Return: if full path of cmd found, or NULL
*/
char *find_path(info_t *info, char *pathstr, char *cmd)
{
int m = 0, currpos = 0;
char *path;
if (!pathstr)
return (NULL);
if ((_strlen(cmd) > 2) && starts_with(cmd, "./"))
{
if (is_cmd(info, cmd))
return (cmd);
}
while (1)
{
if (!pathstr[m] || pathstr[m] == ':')
{
path = dup_chars(pathstr, currpos, m);
if (!*path)
_strcat(path, cmd);
else
{
_strcat(path, "/");
_strcat(path, cmd);
}
if (is_cmd(info, path))
return (path);
if (!pathstr[m])
break;
currpos = m;
}
m++;
}
return (NULL);
}