-
Notifications
You must be signed in to change notification settings - Fork 0
/
pipex_utils.c
65 lines (58 loc) · 1.63 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* pipex_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: asoler <asoler@student.42sp.org.br> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/27 02:21:29 by asoler #+# #+# */
/* Updated: 2022/08/05 19:37:30 by asoler ### ########.fr */
/* */
/* ************************************************************************** */
#include "pipex.h"
char *find_path(char **envp)
{
size_t len;
char *path;
char *result;
int i;
i = 0;
len = ft_strlen(envp[i]);
path = ft_strnstr(envp[i], "PATH", len);
while (!path)
{
i++;
len = ft_strlen(envp[i]);
path = ft_strnstr(envp[i], "PATH", len);
}
result = ft_strdup(&path[5]);
return (result);
}
void free_array(char **array)
{
int i;
i = 0;
while (array[i])
{
free(array[i]);
i++;
}
free(array);
}
int wait_and_free(pid_t pid, char **cmd, int *status, int *ret)
{
if (waitpid(pid, status, 0) < 0)
{
ft_printf("Wait fail %s\n", strerror(errno));
return (0);
}
free_array(cmd);
if (WIFEXITED(*status))
*ret = WEXITSTATUS(*status);
return (1);
}
void free_crowd(char **vector, char *s)
{
free_array(vector);
free(s);
}