-
Notifications
You must be signed in to change notification settings - Fork 0
/
handle_path.c
40 lines (37 loc) · 1.03 KB
/
handle_path.c
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
#include "main.h"
/**
* _which - fleshes out command by appending it to a matching PATH directory
* @str: first command user typed into shell (e.g. "ls" if user typed "ls -l")
* @env: environmental variable
* Return: a copy of fleshed out command (e.g. "/bin/ls" if originally "ls")
*/
char *_which(char *str, list_t *env)
{
char *pth, *cat = NULL, **toks;
int i = 0;
/* get and tokenize PATH directories, then free original string */
pth = get_env("PATH", env);
toks = c_str_tok(pth, ":");
free(pth);
/* append "/cmd" to each token to see it's legit */
i = 0;
while (toks[i] != NULL)
{
if (toks[i][0] == '\0')
cat = getcwd(cat, 0);
else
cat = _strdup(toks[i]);
cat = _strcat(cat, "/");
cat = _strcat(cat, str);
if (access(cat, F_OK) == 0)
{
/* free tokens before returning legit fleshed path */
free_double_ptr(toks);
return (cat);
}
free(cat); /* free concatenated string if cmd is never found */
i++;
}
free_double_ptr(toks);
return (str); /* return string if not found; won't pass execve */
}