-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfree_open.c
45 lines (40 loc) · 1.35 KB
/
free_open.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
/* ************************************************************************** */
/* */
/* :::::::: */
/* free_open.c :+: :+: */
/* +:+ */
/* By: lbartels <lbartels@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2024/01/24 16:49:14 by lbartels #+# #+# */
/* Updated: 2024/01/24 16:49:43 by lbartels ######## odam.nl */
/* */
/* ************************************************************************** */
#include "pipex.h"
void ft_free_tab(char **tab)
{
int i;
i = 0;
while (tab[i])
{
free(tab[i]);
i++;
}
free(tab);
}
int open_file(char *file, char flag)
{
int fd;
if (flag == 'i')
fd = open(file, O_RDONLY);
if (flag == 'o')
fd = open(file, O_WRONLY | O_CREAT | O_TRUNC, 0777);
if (flag == 'h')
fd = open(file, O_WRONLY | O_CREAT | O_APPEND, 0777);
if (fd == -1)
{
ft_putstr_fd(RED, 2);
perror("Error opening file");
exit(EXIT_FAILURE);
}
return (fd);
}