-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpipex_util.c
76 lines (69 loc) · 1.68 KB
/
pipex_util.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* pipex_util.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: yli <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/24 17:56:07 by yli #+# #+# */
/* Updated: 2023/01/24 17:56:09 by yli ### ########.fr */
/* */
/* ************************************************************************** */
#include "pipex.h"
int ft_perror(char *str)
{
perror(str);
exit(1);
}
char **split_path(char **envp)
{
char **path;
int i;
i = 0;
while (envp[i])
{
if (!strncmp(envp[i], "PATH=", 5))
{
path = ft_split(envp[i] + 5, ':');
return (path);
}
i++;
}
return (NULL);
}
char *ft_strjoin_path_cmd(char const *s1, char c, char const *s2)
{
char *str;
size_t i;
size_t j;
if (!s1 || !s2)
return (NULL);
str = (char *)malloc(sizeof(char) * (ft_strlen(s1) + ft_strlen(s2) + 2));
if (!str)
return (NULL);
i = 0;
j = 0;
while (s1[j])
str[i++] = s1[j++];
str[i++] = c;
j = 0;
while (s2[j])
str[i++] = s2[j++];
str[i] = '\0';
return (str);
}
void check_free_all(char **str)
{
int i;
i = 0;
while (str[i])
{
if (*str != NULL)
{
free(str[i]);
i++;
}
*str = NULL;
}
free (str);
}