-
Notifications
You must be signed in to change notification settings - Fork 2
/
find_path.c
83 lines (74 loc) · 1.44 KB
/
find_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
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
#include "shell.h"
/**
* *find_path - Find the PATH
* @environ: Environ variable.
*
* Return: string with path or NULL in failure.
*/
char *find_path(char **environ)
{
char **env_ptr;
char *aux;
char *path;
char *token;
if (environ == NULL)
return (NULL);
for (env_ptr = environ; env_ptr != 0; env_ptr++)
{
aux = _strstr(*env_ptr, "PATH");
if (aux != NULL)
{
path = _strdup(aux);
token = strtok(path, "=");
while (token != NULL)
{
token = strtok(NULL, "=");
return (token);
}
}
}
return (NULL);
}
/**
* print_env - Print environment variables.
*
* @environ: Environ variable.
*/
void print_env(char **environ)
{
char **env_ptr;
unsigned int size;
env_ptr = environ;
while (*env_ptr)
{
size = _strlen(*env_ptr);
write(STDOUT_FILENO, *env_ptr, size);
write(STDOUT_FILENO, "\n", 1);
env_ptr++;
}
}
/**
* _which - Find the directory of the command.
* @p_rec: Path received
* @first_arg: Command
*
* Return: string with find path or NULL in failure.
*/
char *_which(char *p_rec, char *first_arg)
{
char *path, *arg, *path_tok, *command;
unsigned int size;
size = _strlen(first_arg);
path = _strdup(p_rec);
arg = _strdup(string_nconcat("/", first_arg, size));
size = _strlen(arg);
path_tok = strtok(path, ":");
while (path_tok != NULL)
{
command = string_nconcat(path_tok, arg, size);
if (access(command, F_OK) == 0)
return (command);
path_tok = strtok(NULL, ":");
}
return (NULL);
}