my code keeps on giving me segfauls when I try to break it up into three smaller functions : I've tried passing the values by reference. the program reads a single line every time the function get_next_line is called and returns 1 if a line is found, 0 if a line is not found and -1 for other errors such as failed memory allocation. the code leaks but for now my main problem is breaking up the get_next_line function.
========================================================================

 

       
        #include "get_next_line.h"
    
    char		*ft_strnew(size_t size)
    {
        char *str;
    
        str = (char *)malloc(size + 1);
        if (!str)
            return (NULL);
        str[strlen(str) + 1] = '\0';
        return (str);
    }
    
    void			*ft_memset(void *start, int init, size_t size)
    {
        unsigned char *ptr;
    
        ptr = (unsigned char*)start;
        while (size > 0)
        {
            *ptr++ = (unsigned char)init;
            size--;
        }
        return (start);
    }
    
    void		ft_bzero(void *s, size_t size)
    {
        ft_memset(s, '\0', size);
    }
    
    char			*ft_strjoin(char const *s1, char const *s2)
    {
        char	*new;
        size_t	len;
    
        if (!s1 || !s2)
            return (NULL);
        len = strlen((char *)s1) + strlen((char *)s2);
        new = (char *)malloc(len + 1);
        if (!new)
            return (NULL);
        while (*s1 != '\0')
            *new++ = *s1++;
        while (*s2 != '\0')
            *new++ = *s2++;
        *new = '\0';
        return (new - len);
    }
    
    char			*ft_strdup(const char *str)
    {
        char	*strdup;
        int		i;
    
        strdup = (char *)malloc(strlen(str) + 1);
        if (strdup == NULL)
            return (NULL);
        i = 0;
        while (str[i] != '\0')
        {
            strdup[i] = str[i];
            i++;
        }
        strdup[i] = '\0';
        return (strdup);
    }
    
    static char      *read_line(int f, int fd, char *buff, char *temp)
    {
        int i;
    
        i = 0;
        while (f > 0)
        {
            if (buff[i] == '\n'){
                break;
            }
            else if (i == BUFF_SIZE)
            {
                f = read(fd, buff, BUFF_SIZE);
                if (f == 0)
                {
                    if (BUFF_SIZE == 1)
                        return (temp);		
                    return ((char *)LINE_NOT_FOUND);
                }
                temp = ft_strjoin(temp, buff);
                i = -1;
            }
            i++;
        }
        return (temp);
    }
    
    int			get_next_line(const int fd, char **line)
    {
        static t_var var;
        char buff[BUFF_SIZE + 1];
        char *new;
        int i;
        int f = 0;
        int s = 0;
        int w = 0;
        i = 0;
    
        if (fd <  0 || line == NULL)
            return (INVALID);
        ft_bzero(buff, BUFF_SIZE + 1);
        if (var.j > 0)
        {
            new = (char *)malloc(sizeof(char) * (var.j + 1));
            if (!new)
                return (INVALID);
            s = strlen(var.temp) - var.j;
            f = s;
            w = var.j;
            while (var.temp[s] != '\0')
            {
                new[i] = var.temp[s];
                if (var.temp[s] == '\n')
                {
                    if(!(*line = (char *)malloc(sizeof(char) * (i + 1))))
                        return (INVALID);
                    *line = strncpy(*line, new, i);
                    var.j--;
                    return (LINE_FOUND);
                }
                s++;
                var.j--;
                i++;
            }
            s = f;
            var.temp = ft_strdup(new);
        }//first read after this if statement
        if (w == 0)
            var.temp = ft_strnew(BUFF_SIZE);
        f = read(fd, buff, BUFF_SIZE);
        if (f == 0)
            return (LINE_NOT_FOUND);
        var.temp = ft_strjoin(var.temp, buff);
        var.temp = read_line(f, fd, buff, var.temp);//function call 
        if (var.temp == (char *)LINE_NOT_FOUND)
            return (0);
        s = 0;
        if (var.temp[s])
        {
            while (var.temp[s] != '\n')
                s++;
        }
        s++;
        if(var.j == 0)
            *line = (char *)malloc(sizeof(char) * (s + 1));
        *line = strncat(*line, var.temp, s - 1);
        var.j = strlen(var.temp) - s;
        return (LINE_FOUND);
    }
    
    #include <fcntl.h>
    int     main(int argc, char **argv)
    {
        int     fd;
        char    *line;
        int     x = 1;
    
        if (argc > 1)
        {
            fd = open(argv[1], O_RDONLY);
            while (x == 1)
            {
                x = get_next_line(fd, &line);
                if (x > 0)
                    printf("%s\n", line);
            }
            close(fd);
        }
        return (0);