-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.c
130 lines (120 loc) · 2.69 KB
/
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ldoppler <ldoppler@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/16 17:50:23 by ldoppler #+# #+# */
/* Updated: 2024/01/29 09:45:09 by ldoppler ### ########.fr */
/* */
/* ************************************************************************** */
#include "pipex.h"
void free_char_array(char **array)
{
int i;
if (array == NULL)
{
return ;
}
i = 0;
while (array[i] != NULL)
{
free(array[i]);
i++;
}
free(array);
}
char *search_path(char **envp)
{
int i;
i = 0;
while (envp[i])
{
if (ft_strncmp(envp[i], "PATH=", 5) == 0)
{
return (envp[i] + 5);
}
i++;
}
return (NULL);
}
void start_fork_pipe(int *pipefd, t_execve *info_execve, char **envp)
{
if (pipe(pipefd) == -1)
{
perror("pipe");
close(info_execve->fd);
free_char_array(info_execve->args_bis);
free_char_array(info_execve->args);
free_info_execve(info_execve);
exit(EXIT_FAILURE);
}
info_execve->id = fork();
if (info_execve->id == -1)
{
perror("fork");
close(info_execve->fd);
free_char_array(info_execve->args_bis);
free_char_array(info_execve->args);
free_info_execve(info_execve);
exit(EXIT_FAILURE);
}
if (info_execve->id == 0)
child_process(info_execve->fd, info_execve->args, info_execve, pipefd);
else
parent_process(pipefd, info_execve, envp);
}
char *ft_strchr_reverse(char *string, int c)
{
int i;
int size;
char *ret;
i = -1;
size = 0;
if (!string)
return (NULL);
while (string[++i])
{
if (string[i] == c)
{
size = i + 1;
ret = ft_calloc(sizeof(char), size);
if (!ret)
return (NULL);
break ;
}
}
if (size == 0)
return (NULL);
i = -1;
while (++i < size - 1)
ret[i] = string[i];
return (ret);
}
char *ft_strjoin2(char *s1, char *s2)
{
char *ret;
size_t string_size;
int i;
int j;
j = 0;
i = 0;
if (!s2 || !s1)
return (NULL);
string_size = ft_strlen(s1) + ft_strlen(s2);
ret = malloc(sizeof(char) * (string_size + 1));
if (ret == NULL)
return (NULL);
while (s1[j] != '\0')
{
ret[i++] = s1[j++];
}
j = 0;
while (s2[j] != '\0')
{
ret[i++] = s2[j++];
}
ret[i] = '\0';
return (free(s1), ret);
}