aboutsummaryrefslogtreecommitdiff
path: root/get_next_line.c
diff options
context:
space:
mode:
authorCharles <sircharlesaze@gmail.com>2019-08-25 20:53:54 +0200
committerCharles <sircharlesaze@gmail.com>2019-08-25 20:53:54 +0200
commitbbf855f5a027f8b92eeebe0e3f7a579a7f420771 (patch)
tree3f2806892e58b2e3349b3308808856aede53be34 /get_next_line.c
parentb237f736dde79929f9ba06629151d12902d3e41d (diff)
downloadget_next_line-bbf855f5a027f8b92eeebe0e3f7a579a7f420771.tar.gz
get_next_line-bbf855f5a027f8b92eeebe0e3f7a579a7f420771.tar.bz2
get_next_line-bbf855f5a027f8b92eeebe0e3f7a579a7f420771.zip
add ft_memcpy
Diffstat (limited to 'get_next_line.c')
-rw-r--r--get_next_line.c41
1 files changed, 37 insertions, 4 deletions
diff --git a/get_next_line.c b/get_next_line.c
index 0beef4d..a5c2288 100644
--- a/get_next_line.c
+++ b/get_next_line.c
@@ -1,14 +1,47 @@
#include <unistd.h>
#include <stdlib.h>
+#include "libft.h"
#include "get_next_line.h"
+static unsigned int cursor;
+
+static void *ft_memcat(void *ptr, void *tail,
+ unsigned int size, unsigned int tail_size);
+
int get_next_line(const int fd, char **line)
{
- char buf[BUFF_SIZE];
+ unsigned int newline_i;
+ char buf[BUFF_SIZE];
- while ((read(fd, buf, BUFF_SIZE)) > 0)
+ if (read(fd, buf, BUFF_SIZE) < 0)
+ return (-1);
+ if ((*line = ft_strnew(0)) == NULL)
+ return (-1);
+ newline_i = 0;
+ while (buf[newline_i])
{
-
+ while (buf[newline_i] != '\n')
+ newline_i++;
+ ft_memcat(*line, buf, 0, newline_i);
+ if (read(fd, buf, BUFF_SIZE) < 0)
+ return (-1);
}
- return 0;
+ return (0);
+}
+
+static void *ft_memcat(void *ptr, void *tail,
+ unsigned int size, unsigned int tail_size)
+{
+ void *copy;
+
+ if ((copy = malloc(size)) == NULL)
+ return (NULL);
+ ft_memcpy(copy, ptr, size);
+ free(ptr);
+ if ((ptr = malloc(size + tail_size)) == NULL)
+ return (NULL);
+ ft_memcpy(ptr, copy, size);
+ free(copy);
+ ft_memcpy(ptr + size, tail, tail_size);
+ return (ptr);
}