aboutsummaryrefslogtreecommitdiff
path: root/get_next_line.c
diff options
context:
space:
mode:
authorCharles <sircharlesaze@gmail.com>2019-11-03 23:38:32 +0100
committerCharles <sircharlesaze@gmail.com>2019-11-05 06:14:28 +0100
commit47ca76eb98d578cc78a6315a5f98504a8241cfec (patch)
treee0ad2d9bc522b9a59adfdbc36167276e04cac90b /get_next_line.c
parent3e54b8257ed66c8f10fa904655f554990931ab88 (diff)
downloadget_next_line-47ca76eb98d578cc78a6315a5f98504a8241cfec.tar.gz
get_next_line-47ca76eb98d578cc78a6315a5f98504a8241cfec.tar.bz2
get_next_line-47ca76eb98d578cc78a6315a5f98504a8241cfec.zip
changed rest bufs back to not malloc'd, empty line on EOFHEADrawmaster
Diffstat (limited to 'get_next_line.c')
-rw-r--r--get_next_line.c33
1 files changed, 18 insertions, 15 deletions
diff --git a/get_next_line.c b/get_next_line.c
index 62878b8..602bbb7 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/11/03 00:33:16 by cacharle ### ########.fr */
+/* Updated: 2019/11/04 00:00:16 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -39,15 +39,17 @@
int get_next_line(int fd, char **line)
{
int split_at;
- static char *rest[OPEN_MAX] = {NULL};
+ static char rest[OPEN_MAX][BUFFER_SIZE + 1] = {{0}};
if (fd < 0 || fd > OPEN_MAX || line == NULL || BUFFER_SIZE <= 0)
return (STATUS_ERROR);
- *line = NULL;
- if (rest[fd] == NULL || rest[fd][0] == 0)
- return (read_line(fd, line, &rest[fd]));
+ if ((*line = ft_strdup("")) == NULL)
+ return (STATUS_ERROR);
+ if (rest[fd][0] == '\0')
+ return (read_line(fd, line, rest[fd]));
if (HAS_NEWLINE(rest[fd], split_at))
{
+ free(*line);
if ((*line = (char*)malloc(sizeof(char) * (split_at + 1))) == NULL)
return (STATUS_ERROR);
ft_strncpy(*line, rest[fd], split_at);
@@ -55,38 +57,39 @@ int get_next_line(int fd, char **line)
ft_strcpy(rest[fd], rest[fd] + split_at + 1);
return (STATUS_LINE);
}
+ free(*line);
if (!(*line = (char*)malloc(sizeof(char) * (ft_strlen(rest[fd]) + 1))))
return (STATUS_ERROR);
ft_strcpy(*line, rest[fd]);
- free(rest[fd]);
- rest[fd] = NULL;
- return (read_line(fd, line, &rest[fd]));
+ rest[fd][0] = '\0';
+ return (read_line(fd, line, rest[fd]));
}
-int read_line(int fd, char **line, char **rest)
+int read_line(int fd, char **line, char *rest)
{
int ret;
int split_at;
char *buf;
if ((buf = malloc(sizeof(char) * (BUFFER_SIZE + 1))) == NULL)
- return (STATUS_ERROR);
+ return (free_return(line, NULL, STATUS_ERROR));
while ((ret = read(fd, buf, BUFFER_SIZE)) > 0)
{
buf[ret] = '\0';
if (HAS_NEWLINE(buf, split_at))
{
- free(*rest);
- *rest = ft_strdup(buf + split_at + 1);
+ ft_strcpy(rest, buf + split_at + 1);
buf[split_at] = '\0';
if ((*line = ft_strappend(*line, buf)) == NULL)
- return (free_return(&buf, rest, STATUS_ERROR));
+ return (free_return(&buf, NULL, STATUS_ERROR));
return (free_return(&buf, NULL, STATUS_LINE));
}
if ((*line = ft_strappend(*line, buf)) == NULL)
- return (free_return(&buf, rest, STATUS_ERROR));
+ return (free_return(&buf, NULL, STATUS_ERROR));
}
- return (free_return(&buf, rest, ret));
+ if (ret == -1)
+ return (free_return(&buf, line, STATUS_ERROR));
+ return (free_return(&buf, NULL, ret));
}
int find_newline(char *str)