aboutsummaryrefslogtreecommitdiff
path: root/get_next_line.c
diff options
context:
space:
mode:
authorCharles <sircharlesaze@gmail.com>2019-10-27 21:53:47 +0100
committerCharles <sircharlesaze@gmail.com>2019-10-27 21:53:47 +0100
commitc3ba96321654d148f338c90443c5dd2d299f6b4c (patch)
tree60fb22db0d073bd70a445545a257894974a967ce /get_next_line.c
parent44dd5832ad0b7af9186bd5369884d258378a5781 (diff)
downloadget_next_line-c3ba96321654d148f338c90443c5dd2d299f6b4c.tar.gz
get_next_line-c3ba96321654d148f338c90443c5dd2d299f6b4c.tar.bz2
get_next_line-c3ba96321654d148f338c90443c5dd2d299f6b4c.zip
previous fix broke some tests, fixed the fix
Diffstat (limited to 'get_next_line.c')
-rw-r--r--get_next_line.c26
1 files changed, 17 insertions, 9 deletions
diff --git a/get_next_line.c b/get_next_line.c
index d09ab9e..b689c8d 100644
--- a/get_next_line.c
+++ b/get_next_line.c
@@ -6,7 +6,7 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/10/19 09:08:36 by cacharle #+# #+# */
-/* Updated: 2019/10/27 19:06:04 by cacharle ### ########.fr */
+/* Updated: 2019/10/27 21:50:24 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -30,10 +30,10 @@
** return LINE_READ
** push buf in line
**
-** return END_OF_FILE
+** return GNL_EOF
*/
-int real_get_next_line(int fd, char **line, int ret)
+int real_get_next_line(int fd, char **line, int ret, int counter)
{
int split_at;
t_bool had_rest;
@@ -44,6 +44,7 @@ int real_get_next_line(int fd, char **line, int ret)
return (LINE_READ);
while (rest[fd][0] == '\0' && (ret = read(fd, buf, BUFFER_SIZE)) > 0)
{
+ counter++;
buf[ret] = '\0';
if ((split_at = find_newline(buf)) != -1)
{
@@ -55,18 +56,18 @@ int real_get_next_line(int fd, char **line, int ret)
if ((*line = ft_strappend(*line, buf)) == NULL)
return (ERROR);
}
- if (had_rest)
- return (LINE_READ);
- free(*line);
- *line = NULL;
- return (ret == -1 ? ERROR : END_OF_FILE);
+ if (ret == -1)
+ return (clean_line(line, ERROR));
+ return (had_rest || counter != 0 ? LINE_READ : clean_line(line, GNL_EOF));
}
int get_next_line(int fd, char **line)
{
int ret;
+ int counter;
ret = 0;
+ counter = 0;
if (fd < 0 || fd > OPEN_MAX || line == NULL || BUFFER_SIZE < 0)
return (ERROR);
if (BUFFER_SIZE == 0)
@@ -74,7 +75,7 @@ int get_next_line(int fd, char **line)
*line = NULL;
return (LINE_READ);
}
- return (real_get_next_line(fd, line, ret));
+ return (real_get_next_line(fd, line, ret, counter));
}
int put_rest(char **line, char *rest)
@@ -109,3 +110,10 @@ int find_newline(char *str)
return (i);
return (-1);
}
+
+int clean_line(char **line, int ret)
+{
+ free(*line);
+ *line = NULL;
+ return (ret);
+}