-
Notifications
You must be signed in to change notification settings - Fork 0
/
reader.c
executable file
·94 lines (85 loc) · 2.07 KB
/
reader.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* reader.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: staeter <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/08/25 15:42:12 by staeter #+# #+# */
/* Updated: 2018/08/25 15:42:17 by staeter ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
/*
** free *s1 allocate a memory containing the cast
** (NB: tion == two in one new)
*/
static int strcat_tion(char **s1, int s1_len, char **s2, int s2_len)
{
char *dest;
int i;
int j;
dest = malloc(sizeof(char) * (s1_len + s2_len + 1));
if (dest != NULL)
{
i = 0;
while (i < s1_len)
{
dest[i] = (*s1)[i];
i++;
}
j = 0;
while (j < s2_len)
{
dest[i + j] = (*s2)[j];
j++;
}
dest[i + j] = 0;
free(*s1);
*s1 = dest;
return (0);
}
return (1);
}
char *reader(int filedesc)
{
int a;
int b;
char *acc;
char *buf;
a = 0;
acc = malloc(sizeof(char));
if (acc == NULL)
return (NULL);
buf = malloc(sizeof(char) * 5001);
if (buf == NULL)
return (NULL);
acc[0] = '\0';
buf[5000] = '\0';
while ((b = read(filedesc, buf, 5000)))
{
if (strcat_tion(&acc, a, &buf, b) || b == -1)
return (NULL);
a += b;
}
free(buf);
acc[a] = 0;
return (acc);
}
char *standi_reader(void)
{
return (reader(0));
}
char *file_reader(const char *filename)
{
int filedesc;
char *r;
filedesc = open(filename, O_RDONLY);
if (filedesc == -1)
return (NULL);
r = reader(filedesc);
close(filedesc);
return (r);
}