-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpipex_utils.c
61 lines (56 loc) · 1.82 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* pipex_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mskeleto <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/12/07 07:18:55 by mskeleto #+# #+# */
/* Updated: 2021/12/07 07:47:29 by mskeleto ### ########.fr */
/* */
/* ************************************************************************** */
#include "pipex.h"
int ft_error(int flag, char *text, char *t2)
{
if (flag == 1)
write(2, "pipex: bad count arguments\n", 27);
if (flag == 2)
{
write(2, "pipex: ", 7);
write(2, t2, ft_strlen(t2));
write(2, ": ", 2);
write(2, text, ft_strlen(text));
write(2, "\n", 1);
}
if (flag == 3)
write(2, "pipex: unexpected error\n", 24);
if (flag == 4)
{
write(2, "pipex: ", 7);
write(2, text, ft_strlen(text));
write(2, ": command not found\n", 20);
}
return (0);
}
char **split_path(char **envp)
{
int i;
i = 0;
while (envp[i] != NULL)
{
if (ft_strncmp("PATH", envp[i], 4) == 0)
return (ft_split(envp[i] + 5, ':'));
i++;
}
return (NULL);
}
void open_and_pipe(int *files, int *fd, char **argv)
{
pipe(fd);
files[0] = open(argv[1], O_RDONLY);
if (files[0] == -1)
ft_error(2, strerror(errno), argv[1]);
files[1] = open(argv[4], O_RDWR | O_CREAT | O_TRUNC, 0777);
if (files[1] == -1)
ft_error(2, strerror(errno), argv[4]);
}