-
Notifications
You must be signed in to change notification settings - Fork 0
/
path.c
47 lines (41 loc) · 1.04 KB
/
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
/*
** EPITECH PROJECT, 2019
** PSU_minishell1_2018
** File description:
** something small
*/
#include "shell.h"
static bool is_path(char const *p)
{
for (; p && *p; p++)
if (*p == '/')
return true;
return false;
}
static char *fpath(char const *p, dict_t *env)
{
char **path = str_to_tab(dict_get(env, "PATH"), ":");
char *tmppath = 0;
for (size_t i = 0; path && path[i]; i++) {
tmppath = my_strcat(my_strcat(my_strcat(gib(sizeof(*tmppath) * \
(my_strlen(path[i]) + my_strlen(p) + 2)), path[i]), "/"), p);
if (!access(tmppath, X_OK))
break;
free(tmppath);
}
free_array(path);
return tmppath;
}
char *get_path(char *p, dict_t *env)
{
char *base;
if (is_path(p)) {
if (*p != '/') {
base = dict_get(env, "PWD");
return my_strcat(my_strcat(my_strcat(gib(sizeof(char) * \
(my_strlen(base) + my_strlen(p) + 2)), base), "/"), p);
} else
return p;
} else
return fpath(p, env);
}