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
105
106
|
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* helper.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cacharle <cacharle@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/07/20 07:25:21 by cacharle #+# #+# */
/* Updated: 2019/07/21 08:23:41 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
#include "include.h"
/*
** Read an already openned file in `file`
** return the file_size (-1 if a error occurs)
*/
int read_file(int fildes, char **file_ptr)
{
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_ptr = ft_memcat(*file_ptr, buf, file_size, read_size);
file_size += read_size;
}
return (file_size);
}
/*
** Concatenate buf[buf_size] into file[file_size]
** Create a copy of the file, free file,
** reallocate it with sufficient memory for the concatenation
** copy each character of file_copy then buf into file
** free file_copy
*/
char *ft_memcat(char *file, char buf[BUF_SIZE], int file_size,
int buf_size)
{
int i;
char *file_copy;
if ((file_copy = malloc(sizeof(char) * (file_size + 1))) == NULL)
return (NULL);
i = -1;
while (++i < file_size)
file_copy[i] = file[i];
free(file);
if ((file = malloc(sizeof(char) * (file_size + buf_size + 1))) == NULL)
return (NULL);
i = 0;
while (i < file_size)
{
file[i] = file_copy[i];
i++;
}
free(file_copy);
while (i < file_size + buf_size)
{
file[i] = buf[i - file_size];
i++;
}
return (file);
}
char *ft_strndup(char *src, int n)
{
int i;
char *dup_ptr;
dup_ptr = (char*)malloc(sizeof(char) * (n + 1));
if (dup_ptr == NULL)
return (NULL);
i = 0;
while (src[i] && i < n)
{
dup_ptr[i] = src[i];
i++;
}
dup_ptr[i] = '\0';
return (dup_ptr);
}
/*
** Print a string char by char on the standard output
*/
void ft_putstr(char *str)
{
while (*str)
{
write(1, str, 1);
str++;
}
}
|