-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpipex_utils.c
100 lines (90 loc) · 2.08 KB
/
pipex_utils.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* pipex_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ajakob <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/07/25 21:13:15 by ajakob #+# #+# */
/* Updated: 2023/08/01 14:40:24 by ajakob ### ########.fr */
/* */
/* ************************************************************************** */
#include "pipex.h"
void ft_free(char **ptr)
{
int i;
i = 0;
while (ptr[i])
{
free(ptr[i]);
ptr[i] = NULL;
i++;
}
free(ptr);
}
char *pathing(char **path_arr, char *cmd)
{
char *tmp;
char *tmp2;
int i;
i = 0;
if (access(cmd, F_OK) == 0)
return (cmd);
while (path_arr[i])
{
tmp = ft_strjoin(path_arr[i], "/");
tmp2 = ft_strjoin(tmp, cmd);
free(tmp);
if (access(tmp2, F_OK) == 0)
return (tmp2);
else
{
free(tmp2);
tmp2 = NULL;
}
i++;
}
return (NULL);
}
char *find_path(char *cmd, char **env)
{
int i;
char *path;
char **path_arr;
char *tmp;
i = 0;
while (env[i])
{
if (ft_strncmp(env[i], "PATH=", 5) == 0)
{
path = ft_strdup(env[i] + 5);
path_arr = ft_split(path, ':');
free(path);
tmp = pathing(path_arr, cmd);
ft_free(path_arr);
if (tmp)
return (tmp);
else
return (free(tmp), tmp = NULL, NULL);
}
i++;
}
return (ft_error("Env variable PATH= not found"), NULL);
}
char **alloc_arr(char *cmd)
{
char **arr;
arr = ft_split(cmd, ' ');
if (!arr)
return (NULL);
return (arr);
}
int dup2_close(int fd, int std, int pipefd[])
{
if ((dup2(fd, std)) == -1)
ft_error("dup2");
close(fd);
close(pipefd[0]);
close(pipefd[1]);
return (0);
}