-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_path.c
37 lines (36 loc) · 751 Bytes
/
get_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
#include "shell.h"
/**
* get_path - search for the full path of a command
* @cmd: the command
* Return: full directory path or NULL
*/
/* BY CHARIFA MASBAHI & NORA JEOUT*/
char *get_path(const char *cmd)
{
char *cp_path, *dir_path, *token_path;
cp_path = get_p_cp();
if (!cp_path)
return (NULL);
dir_path = NULL;
token_path = strtok(cp_path, ":");
while (token_path != NULL)
{
dir_path = get_dir_p(token_path, cmd);
if (!dir_path)
break;
if (check_file(dir_path) == 1)
{
free(cp_path);
return (dir_path);
} else if (check_file(dir_path) != 1)
{
free(dir_path);
dir_path = NULL;
token_path = strtok(NULL, ":");
}
}
free(cp_path);
if (check_file(cmd) == 1)
return ((char *) cmd);
return (dir_path);
}