-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetnextline.c
87 lines (78 loc) · 1.53 KB
/
getnextline.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
/*
** getnextline.c for getnextline in /home/franck_r/rendu/getnextline
**
** Made by Romain Franck
** Login <franck_r@epitech.net>
**
** Started on Sun Mar 9 18:42:52 2014 Romain Franck
** Last update Thu May 1 05:38:33 2014 Galleg_a
*/
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include "lemin.h"
#define BUF_SIZE 4096
char *my_strcpy(char *dest, char *src)
{
int i;
i = 0;
while (src[i] != '\0')
{
dest[i] = src[i];
i++;
}
dest[i] = '\0';
return (dest);
}
char *my_realloc(char *buff)
{
char *buf2;
if (buff == 0)
{
if ((buf2 = malloc(sizeof(char) * (BUF_SIZE + 1))) == NULL)
return (NULL);
}
else
{
if ((buf2 = malloc(sizeof(char) *
(my_strlen_no_troll(buff) + BUF_SIZE + 1))) == 0)
return (NULL);
my_strcpy(buf2, buff);
free(buff);
}
return (buf2);
}
char *norme_get_next(int *i, char *str, int j)
{
(*i)++;
str[j] = 0;
printf("%s\n", str);
return (str);
}
char *getnextline(int fd)
{
static char buff[BUF_SIZE + 1];
static int i;
int j;
char *str;
int len;
if ((j = 0) == 0 && (str = malloc(sizeof(char) * (BUF_SIZE + 1))) == NULL)
return (NULL);
str[0] = '\0';
while (1)
{
if (buff[i] == '\0')
{
str[j] = '\0';
str = my_realloc(str);
if ((len = read(fd, buff, BUF_SIZE)) == 0)
return (str[0] != 0) ? (str) : (0);
if ((i = 0) == 0 && len < 0)
return (0);
buff[len] = 0;
}
if (buff[i] == '\n')
return (norme_get_next(&i, str, j));
str[j++] = buff[i++];
}
}