blob: d1fe8bc25c3d36bd301fd50103bfda8f1b051814 (
plain)
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
|
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* helper.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/07/19 15:19:53 by cacharle #+# #+# */
/* Updated: 2019/07/19 15:19:56 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
#include "include.h"
int read_file(int fildes, char **file)
{
char buf[BUF_SIZE];
int file_size;
int read_size;
file_size = 0;
while ((read_size = read(fildes, buf, BUF_SIZE)))
{
if (read_size < 0)
return (-1);
*file = ft_memcat(*file, buf, file_size, read_size);
file_size += read_size;
}
return (file_size);
}
char *ft_memcat(char *file, char buf[BUF_SIZE], int file_size,
int read_size)
{
int i;
char *file_clone;
if ((file_clone = malloc(sizeof(char) * (file_size + 1))) == NULL)
return (NULL);
i = -1;
while (++i < file_size)
file_clone[i] = file[i];
free(file);
if ((file = malloc(sizeof(char) * (file_size + read_size + 1))) == NULL)
return (NULL);
i = 0;
while (i < file_size)
{
file[i] = file_clone[i];
i++;
}
free(file_clone);
while (i < file_size + read_size)
{
file[i] = buf[i - file_size];
i++;
}
return (file);
}
|