Untitled

mail@pastecode.io avatar
unknown
diff
2 years ago
2.3 kB
2
Indexable
Never
index 78eb6b5..ffacf03 100644
--- a/foo.txt
+++ b/foo.txt
@@ -1,2 +1,2 @@
-abcdefg
-0123456
+a
+0
diff --git a/get_next_line.c b/get_next_line.c
index 7b90587..b9fe8c5 100644
--- a/get_next_line.c
+++ b/get_next_line.c
@@ -3,14 +3,13 @@
 char *get_next_line(int fd)
     {

- static char *reminder = "" ;
+ static char *reminder = NULL;
       char buf[BUFFER_SIZE + 1];
       int nbytes;
      char *ptr = malloc( BUFFER_SIZE  + 1 );
-      if (!ptr || reminder == NULL)
-    {
+     char *temp;
+      if (!ptr)
             return NULL;
-    }

      if (fd < 0 ||fd > 999 )
       {
@@ -21,17 +20,19 @@ char *get_next_line(int fd)
       while ( (nbytes = read(fd, buf, BUFFER_SIZE)) > 0)
       {
         buf[nbytes] = '\0';
-         reminder  =ft_strjoin(reminder, buf);
+         reminder  = ft_strjoin(reminder, buf);

        if (ft_strchr(reminder, '\n'))
         {
           // ft_strcpy(reminder,ft_strchr(temp, '\n'));
-               ptr = ft_substr(reminder,0,'\n');
-            reminder = ft_strchr(reminder, '\n');
+               temp = ptr;
+               ptr = ft_substr(reminder, 0 ,'\n');
+               free(temp);
+               reminder = ft_strchr(reminder, '\n');
             return ptr;
         }
-      }
-      if (ft_strcmp(reminder, "") == 0 || nbytes < 0 || reminder == NULL)
+      }
+      if (!reminder || !ft_strlen(reminder) || nbytes < 0)
     {
        free(ptr);
         return NULL;
diff --git a/get_next_line_utils.c b/get_next_line_utils.c
index 45bd2d0..4cc6650 100644
--- a/get_next_line_utils.c
+++ b/get_next_line_utils.c
@@ -24,7 +24,7 @@ char *ft_strcpy(char *dest, char *src)
        int i;

        i = 0;
-       while (src[i] != '\0')
+       while (src && src[i] != '\0')
        {
                dest[i] = src[i];
                i++;
@@ -37,6 +37,8 @@ int ft_strlen(char *str)
 {
        int i;

+       if (!str)
+               return (0);
        i = 0;
        while (str[i] != '\0')
        {
@@ -99,8 +101,8 @@ char    *ft_strjoin(char  *s1, char  *s2)
         return (NULL);
     ft_strcpy(strjoin, s1);
     ft_strcat(strjoin, s2);
-    if(!(ft_strcmp(s1, "") == 0))
-        free(s1);
+    if (s1 && !(ft_strcmp(s1, "") == 0))
+       free(s1);
     return (strjoin);
 }