aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCharles <sircharlesaze@gmail.com>2019-10-20 07:34:25 +0200
committerCharles <sircharlesaze@gmail.com>2019-10-20 07:39:47 +0200
commit891dce09989e8ede692d4a6c23e7a208b09ca4d6 (patch)
treecae404760be9b47f2f63c2a9e02427a37a093843
parenta407fe611354d71330fc352fbc2a9fe268b4104f (diff)
downloadget_next_line-891dce09989e8ede692d4a6c23e7a208b09ca4d6.tar.gz
get_next_line-891dce09989e8ede692d4a6c23e7a208b09ca4d6.tar.bz2
get_next_line-891dce09989e8ede692d4a6c23e7a208b09ca4d6.zip
WIP: Normed, fixed double free in ft_strappend
-rw-r--r--get_next_line.c67
-rw-r--r--get_next_line.h6
-rw-r--r--get_next_line_utils.c14
-rw-r--r--main.c2
4 files changed, 48 insertions, 41 deletions
diff --git a/get_next_line.c b/get_next_line.c
index 36edac1..0a5b001 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/19 16:19:25 by cacharle ### ########.fr */
+/* Updated: 2019/10/20 07:39:13 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -14,8 +14,27 @@
#include <stdlib.h>
#include "get_next_line.h"
+/*
+** if has rest:
+** if rest has newline:
+** push rest until newline in line, shift rest
+** return LINE_READ
+** else:
+** push rest in line
+**
+** while can read fd in buf
+** if buf has newline:
+** push buf until newline in line
+** push buf after newline in rest
+** return LINE_READ
+** push buf in line
+**
+** return END_OF_FILE
+*/
+
int get_next_line(int fd, char **line)
{
+ t_bool had_rest;
int ret;
int split_at;
char buf[BUFFER_SIZE + 1];
@@ -23,47 +42,43 @@ int get_next_line(int fd, char **line)
if (fd < 0 || line == NULL)
return (ERROR);
- int has_stuff = rest[0] != 0;
- *line = put_rest(*line, rest);
- if (rest[0] != '\0')
- return (LINE_READ);
- while ((ret = read(fd, buf, BUFFER_SIZE)) > 0)// || rest[0] != '\0')
+ had_rest = put_rest(line, rest);
+ while (rest[0] == '\0' && (ret = read(fd, buf, BUFFER_SIZE)) > 0)
{
buf[ret] = '\0';
- split_at = find_newline(buf);
- if (split_at == -1)
+ if ((split_at = find_newline(buf)) != -1)
{
+ ft_strncpy(rest, buf + split_at + 1, BUFFER_SIZE);
+ buf[split_at] = '\0';
*line = ft_strappend(*line, buf);
- continue ;
+ return (*line == NULL ? ERROR : LINE_READ);
}
- buf[split_at] = '\0';
- ft_strncpy(rest, buf + split_at + 1, BUFFER_SIZE + 1);
- *line = ft_strappend(*line, buf);
- return LINE_READ;
+ if ((*line = ft_strappend(*line, buf)) == NULL)
+ return (ERROR);
}
- if (has_stuff)
+ if (had_rest)
return (LINE_READ);
return (ret == -1 ? ERROR : END_OF_FILE);
}
-char *put_rest(char *line, char *rest)
+int put_rest(char **line, char *rest)
{
- int split_at;
+ int split_at;
+ t_bool had_rest;
- split_at = find_newline(rest);
- if (split_at == -1)
+ had_rest = rest[0] != '\0';
+ if ((split_at = find_newline(rest)) == -1)
{
- line = malloc(sizeof(char) * (ft_strlen(rest) + 1));
- ft_strcpy(line, rest);
- /* line[ft_strlen(rest)] = 0; */
+ *line = malloc(sizeof(char) * (ft_strlen(rest) + 1));
+ ft_strcpy(*line, rest);
rest[0] = '\0';
- return (line);
+ return (had_rest);
}
- line = malloc(sizeof(char) * (split_at + 1));
- ft_strncpy(line, rest, split_at + 1);
- line[split_at] = '\0';
+ *line = malloc(sizeof(char) * (split_at + 1));
+ ft_strncpy(*line, rest, split_at);
+ (*line)[split_at] = '\0';
ft_strncpy(rest, rest + split_at + 1, BUFFER_SIZE);
- return (line);
+ return (had_rest);
}
int find_newline(char *str)
diff --git a/get_next_line.h b/get_next_line.h
index 619cb9b..15f9475 100644
--- a/get_next_line.h
+++ b/get_next_line.h
@@ -6,7 +6,7 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/10/09 13:52:59 by cacharle #+# #+# */
-/* Updated: 2019/10/19 16:24:03 by cacharle ### ########.fr */
+/* Updated: 2019/10/20 07:39:00 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -14,7 +14,7 @@
# define GET_NEXT_LINE_H
# ifndef BUFFER_SIZE
-# define BUFFER_SIZE 100000
+# define BUFFER_SIZE 100
# endif
# define LINE_READ 1
@@ -32,7 +32,7 @@ typedef int t_bool;
*/
int get_next_line(int fd, char **line);
-char *put_rest(char *line, char *rest);
+int put_rest(char **line, char *rest);
int find_newline(char *str);
/*
diff --git a/get_next_line_utils.c b/get_next_line_utils.c
index 929e37b..9bc96d9 100644
--- a/get_next_line_utils.c
+++ b/get_next_line_utils.c
@@ -6,7 +6,7 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/10/08 08:52:59 by cacharle #+# #+# */
-/* Updated: 2019/10/19 14:09:48 by cacharle ### ########.fr */
+/* Updated: 2019/10/20 07:39:00 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -42,19 +42,11 @@ char *ft_strappend(char *dest, char *src)
{
void *copy;
- if (dest == NULL)
- {
- if ((dest = (char*)malloc(sizeof(char) * (ft_strlen(src) + 1))) == NULL)
- return (NULL);
- ft_strcpy(dest, src);
- /* printf("%s - %s\n", dest, src); */
- return (dest);
- }
if ((copy = (char*)malloc(sizeof(char) * (ft_strlen(dest) + 1))) == NULL)
return (NULL);
ft_strcpy(copy, dest);
- /* free(dest); */
- dest = (char*)malloc(sizeof(char) * (ft_strlen(dest) + ft_strlen(src) + 1));
+ free(dest);
+ dest = (char*)malloc(sizeof(char) * (ft_strlen(copy) + ft_strlen(src) + 1));
if (dest == NULL)
return (NULL);
ft_strcpy(dest, copy);
diff --git a/main.c b/main.c
index da6662f..7ab241a 100644
--- a/main.c
+++ b/main.c
@@ -6,7 +6,7 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/10/10 09:27:41 by cacharle #+# #+# */
-/* Updated: 2019/10/19 09:01:18 by cacharle ### ########.fr */
+/* Updated: 2019/10/20 07:13:16 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */