Repository navigation
Expand file tree
/
Copy pathenv.c
More file actions
60 lines (53 loc) · 1.54 KB
/
Copy pathenv.c
File metadata and controls
60 lines (53 loc) · 1.54 KB
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* fs.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mmomeni <mmomeni@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/05/13 15:21:57 by mmomeni #+# #+# */
/* Updated: 2023/05/13 15:22:15 by mmomeni ### ########.fr */
/* */
/* ************************************************************************** */
#include "pipex.h"
char *get_env(char *key)
{
extern char **environ;
int i;
i = 0;
while (environ[i])
{
if (!ft_strncmp(environ[i], key, ft_strlen(key)))
return (environ[i] + ft_strlen(key) + 1);
i++;
}
return (NULL);
}
char *join_path(char *s1, char *s2)
{
char *parent;
char *path;
parent = ft_strjoin(s1, "/");
path = ft_strjoin(parent, s2);
free(parent);
return (path);
}
char *get_path(char *program)
{
char **v;
char *path;
int i;
i = 0;
v = ft_split(get_env("PATH"), ':');
while (v[i])
{
path = join_path(v[i], program);
if (!access(path, X_OK))
return (path);
free(path);
path = NULL;
i++;
}
ft_free_split(v);
return (NULL);
}