-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild_path.c
More file actions
42 lines (38 loc) · 887 Bytes
/
build_path.c
File metadata and controls
42 lines (38 loc) · 887 Bytes
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
// HEADER
#include "minishell.h"
/*
** Itère sur tout les répertoires spécifié dans la variable d'environment PATH
**
** Paramètres : "paths" pour PATH, bname pour binary name
**
** Construit un chemin absolu tel que : dirs[x] + bname
** Vérifie que le chemin existe et mène à un executable
** Le renvoie si c'est le cas
**
** Retourne NULL dans les cas suivants :
**
** PATH est vide (en réalité, l'argument paths)
** Aucun chemin construit n'existait
*/
char *build_path(char *paths, char *bname)
{
char **dirs;
int i;
if (!paths)
return (NULL);
dirs = ft_split(paths, ':');
i = -1;
while (dirs[++i])
{
dirs[i] = ft_strjoinplus(ft_strjoinplus(dirs[i], "/", 1), bname, 1);
if (try_path(dirs[i]))
{
bname = ft_strdup(dirs[i]);
ft_tabfree((void**)dirs);
return (bname);
}
}
ft_tabfree((void**)dirs);
errno = COMMAND_NOT_FOUND;
return (NULL);
}