-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_next_line.c
66 lines (58 loc) · 1.03 KB
/
get_next_line.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
/*
** get_next_line.c for dfs in /home/enzo/rendu/prog/sudoki-bi
**
** Made by Enzo
** Login <[email protected]>
**
** Started on Sat Feb 27 11:39:42 2016 Enzo
** Last update Sun Feb 28 21:21:58 2016 Enzo
*/
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
void my_strcpy(char *str1, char *str2)
{
int i;
i = 0;
if (str1 == NULL || str2 == NULL)
return;
while (str2[i])
{
str1[i] = str2[i];
i++;
}
str1[i] = '\0';
}
char *my_realloc(char *str)
{
char *ret;
if ((ret = malloc(sizeof(char) * (strlen(str) + 1))) == NULL)
return (NULL);
my_strcpy(ret, str);
free(str);
return (ret);
}
char *get_next_line(const int fd)
{
char buffer;
char *res;
int i;
buffer = 0;
i = 0;
if ((res = malloc(sizeof(char))) == NULL)
return (NULL);
while (buffer != '\n')
{
read(fd, &buffer, 1);
if (buffer != '\n')
{
res[i] = buffer;
if ((res = my_realloc(res)) == NULL)
return (NULL);
i++;
}
else
res[i] = 0;
}
return (res);
}