-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpath.c
More file actions
86 lines (74 loc) · 1.51 KB
/
Copy pathpath.c
File metadata and controls
86 lines (74 loc) · 1.51 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
#include "shell.h"
/**
* get_path - Get the cmd in the source path string
* @sh_ctx: the sh_ctx struct
* @src: source to the PATH string
* @cmd: the cmd to search
* Return: the path of the cmd or NULL
*/
char *get_path(sh_context *sh_ctx, char *src, char *cmd)
{
int i = 0, idx = 0;
char *path;
if (!src)
return (NULL);
if ((_strlen(cmd) > 2) && _strstr(cmd, "./"))
{
if (is_execmd(sh_ctx, cmd))
return (cmd);
}
while (1)
{
if (!src[i] || src[i] == ':')
{
path = _substr(src, idx, i);
if (!*path)
_strcat(path, cmd);
else
{
_strcat(path, "/");
_strcat(path, cmd);
}
if (is_execmd(sh_ctx, path))
return (path);
if (!src[i])
break;
idx = i;
}
i++;
}
return (NULL);
}
/**
* is_execmd - Check if a file is an executable command
* @sh_ctx: pointer to the sh_ctx struct
* @path: pointer to the file path
* Return: 1 if true, 0 otherwise
*/
int is_execmd(sh_context *sh_ctx, char *path)
{
struct stat file_st;
UNUSED(sh_ctx);
if (!path || stat(path, &file_st))
return (0);
if (file_st.st_mode & __S_IFREG)
return (1);
return (0);
}
/**
* _substr - Extracts a substring from a given string, without colons
* @src: source to the path string
* @start: starting index
* @end: ending index
* Return: pointer to a new buffer
*/
char *_substr(char *src, int start, int end)
{
int i = 0, j = 0;
static char buffer[BUFFER_SIZE];
for (i = start, j = 0; i < end; i++)
if (src[i] != ':')
buffer[j++] = src[i];
buffer[j] = '\0';
return (buffer);
}