-
Notifications
You must be signed in to change notification settings - Fork 0
/
read.c
93 lines (84 loc) · 2.02 KB
/
read.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* read.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: nnaumenk <nnaumenk@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/10/06 20:47:55 by nnaumenk #+# #+# */
/* Updated: 2018/11/15 19:09:39 by nnaumenk ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_ssl.h"
#define READ_BUF 3000000
void ft_close_fd(int fd)
{
if (fd == 0 || fd == 1 || fd == 2)
return ;
close(fd);
}
char *ft_read_from(int fd, size_t *len)
{
char *str;
char buf[READ_BUF];
size_t buf_len;
str = ft_strnew(0);
*len = 0;
while (1)
{
buf_len = read(fd, buf, READ_BUF);
str = ft_memjoin_del(str, buf, *len, buf_len);
*len += buf_len;
if (buf_len == 0)
break ;
}
return (str);
}
int ft_make_input_fd(char *file)
{
int fd;
if (file)
{
fd = open(file, O_RDONLY);
if (fd == -1)
{
ft_print_fd(2, "ft_ssl: '%s' %s\n", file, "is not a file");
return (-1);
}
}
else
{
file = "STDIN";
fd = 0;
}
if (read(fd, 0, 0) == -1)
{
ft_print_fd(2, "ft_ssl: error in reading '%s'\n", file);
return (-1);
}
return (fd);
}
int ft_make_output_fd(char *file)
{
int fd;
if (file)
{
fd = open(file, O_WRONLY | O_CREAT | O_TRUNC, 0664);
if (fd == -1)
{
ft_print_fd(2, "ft_ssl: '%s' %s\n", file, "is not a file");
return (-1);
}
}
else
{
file = "STDOUT";
fd = 1;
}
if (write(fd, 0, 0) == -1)
{
ft_print_fd(2, "ft_ssl: error in reading '%s'\n", file);
return (-1);
}
return (fd);
}