1
0
This commit is contained in:
Adam Joly
2023-12-06 16:48:37 +01:00
parent e2c6462e90
commit aa18417fe2
5 changed files with 13 additions and 68 deletions

View File

@ -1,42 +0,0 @@
# **************************************************************************** #
# #
# ::: :::::::: #
# Makefile :+: :+: :+: #
# +:+ +:+ +:+ #
# By: adjoly <adjoly@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2023/12/01 17:10:39 by adjoly #+# #+# #
# Updated: 2023/12/04 17:04:21 by adjoly ### ########.fr #
# #
# **************************************************************************** #
NAME = get_next_line.a
CC = clang
SRCS = get_next_line.c \
get_next_line_utils.c \
OBJS = $(SRCS:.c=.o)
FLAGS = -Werror -Wall -Wextra
HEADER = get_next_line.h
$(NAME): $(OBJS)
$(CC) -o $@ $(SRCS) -g
%.o: %.c
$(CC) $(FLAGS) -I $(HEADER) $< -c -o $@ -g
all: $(NAME)
clean:
rm -f $(OBJS)
fclean: clean
rm -f $(NAME)
re: fclean all
.PHONY: clean all re fclean

BIN
a.out

Binary file not shown.

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/01 17:11:59 by adjoly #+# #+# */ /* Created: 2023/12/01 17:11:59 by adjoly #+# #+# */
/* Updated: 2023/12/06 11:18:18 by adjoly ### ########.fr */ /* Updated: 2023/12/06 12:34:24 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -28,7 +28,6 @@ char *get_next_line(int fd)
{ {
static char *buf; static char *buf;
char *res; char *res;
char *tmp;
int size; int size;
int bytes_read; int bytes_read;
@ -37,37 +36,24 @@ char *get_next_line(int fd)
res = ft_calloc(1, 1); res = ft_calloc(1, 1);
if (!buf) if (!buf)
buf = ft_calloc(sizeof(char), BUFFER_SIZE + 1); buf = ft_calloc(sizeof(char), BUFFER_SIZE + 1);
while (1) while (buf && res)
{ {
tmp = ft_strjoin(res, buf); // size = check_line(res);
if (!tmp)
return (NULL);
free(res);
res = tmp;
size = check_line(res);
bytes_read = read(fd, buf, BUFFER_SIZE); bytes_read = read(fd, buf, BUFFER_SIZE);
buf[bytes_read] = 0; res = ft_strjoin(res, buf);
if (bytes_read == 0 && ft_strlen(res) != 0) buf[bytes_read] = '\0';
if (bytes_read < 1)
{ {
if (ft_strlen(res) > 0)
return (res);
free(buf); free(buf);
buf = NULL; buf = NULL;
return (NULL);
}
else if (bytes_read >= 1)
return (res); return (res);
} }
else if (bytes_read == 0)
return (NULL); return (NULL);
if (size != 0)
{
tmp = ft_strjoin(res, buf);
if (!tmp)
return (NULL);
free(res);
res = tmp;
ft_strlcpy(buf, &res[size], ft_strlen(&res[size]));
buf[ft_strlen(&res[size])] = 0;
res[size] = 0;
return (res);
}
}
} }
#include <unistd.h> #include <unistd.h>

Binary file not shown.

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/01 17:12:02 by adjoly #+# #+# */ /* Created: 2023/12/01 17:12:02 by adjoly #+# #+# */
/* Updated: 2023/12/06 11:21:57 by adjoly ### ########.fr */ /* Updated: 2023/12/06 12:05:08 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -58,6 +58,7 @@ char *ft_strjoin(char *s1, char *s2)
i++; i++;
j++; j++;
} }
free(s1);
result[i] = '\0'; result[i] = '\0';
return (result); return (result);
} }