-
Notifications
You must be signed in to change notification settings - Fork 0
/
pipex_utils_bonus.c
104 lines (93 loc) · 2.42 KB
/
pipex_utils_bonus.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* pipex_utils_bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: melkholy <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/08 20:54:52 by melkholy #+# #+# */
/* Updated: 2022/09/12 21:51:32 by melkholy ### ########.fr */
/* */
/* ************************************************************************** */
#include "pipex_bonus.h"
int ft_infile_fd(t_pipe *buff)
{
int infile;
int status;
infile = 0;
status = 0;
if (access(buff->argv[1], F_OK | R_OK))
{
if (access(buff->argv[1], F_OK))
status = 1;
ft_printf("%s: %s: %s\n", buff->sh, strerror(errno), buff->argv[1]);
}
else
{
infile = open(buff->argv[1], O_RDONLY);
dup2(infile, buff->pipefd[0][0]);
close(infile);
}
return (status);
}
void ft_outfile_fd(t_pipe *buff)
{
int outfile;
int argc;
outfile = 0;
argc = buff->argc;
if (!access(buff->argv[argc - 1], F_OK | W_OK))
outfile = open(buff->argv[argc - 1], O_WRONLY | O_TRUNC);
else if (!access(buff->argv[argc - 1], F_OK))
{
ft_printf("%s: %s: %s\n", \
buff->sh, strerror(errno), buff->argv[argc - 1]);
ft_free_pipes(buff->pipefd, buff->procs);
free(buff);
exit(1);
}
else
outfile = open(buff->argv[argc - 1], O_RDWR | O_CREAT | O_TRUNC, 0666);
dup2(outfile, buff->pipefd[0][1]);
close(outfile);
}
int **ft_create_pipes(int proc)
{
int **pipes;
int *pip;
int count;
pipes = (int **)ft_calloc(proc, sizeof(int *));
if (!pipes)
return (NULL);
count = 0;
while (count < proc)
{
pip = (int *)ft_calloc(2, sizeof(int));
if (!pip)
return (NULL);
if (pipe(pip) < 0)
perror("Pipe creation failed");
pipes[count] = pip;
count ++;
}
return (pipes);
}
int *ft_create_pid(int proc)
{
int *pid;
pid = (int *)ft_calloc(proc, sizeof(int));
if (!pid)
return (NULL);
return (pid);
}
void ft_free_pth(char **cmd)
{
int count;
count = 0;
while (cmd[count])
{
free(cmd[count]);
count ++;
}
free(cmd);
}