fix a few small things

This commit is contained in:
Khaïs COLIN 2024-10-31 14:22:52 +01:00
parent ff928f6366
commit 9339be5e45
3 changed files with 11 additions and 12 deletions

View file

@ -6,7 +6,7 @@
/* By: kcolin <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/23 20:32:46 by kcolin #+# #+# */
/* Updated: 2024/10/31 12:45:27 by kcolin ### ########.fr */
/* Updated: 2024/10/31 14:11:11 by kcolin ### ########.fr */
/* */
/* ************************************************************************** */
@ -84,7 +84,6 @@ size_t ft_strlcat(char *dst, const char *src, size_t size)
return (dst_len + src_len);
}
char *ft_strjoin_free_s1(char const *s1, char const *s2)
{
char *out;
@ -131,20 +130,20 @@ char *get_next_line(int fd)
int num_bytes_read;
char *outbuf;
read_buffer = malloc(BUFFER_SIZE * sizeof(char));
read_buffer = malloc((BUFFER_SIZE + 1) * sizeof(char));
num_bytes_read = 1;
if (buffer == NULL)
{
buffer = malloc(sizeof(char));
buffer[0] = '\0';
}
if (buffer == NULL)
return (NULL);
buffer[0] = '\0';
while (num_bytes_read != 0)
{
// DONE: if newline found,
if (ft_strchr(buffer, '\n'))
{
// DONE: copy string until newline to new buffer,
outbuf = malloc((ft_strchr(buffer, '\n') - buffer + 2) * sizeof(char));
outbuf = malloc((ft_strchr(buffer, '\n') - buffer + 2));
if (outbuf == NULL)
{
free(read_buffer);
@ -160,7 +159,7 @@ char *get_next_line(int fd)
free(read_buffer);
return (outbuf);
}
num_bytes_read = read(fd, read_buffer, BUFFER_SIZE - 1);
num_bytes_read = read(fd, read_buffer, BUFFER_SIZE);
if (num_bytes_read < 0)
{
free(read_buffer);
@ -176,8 +175,8 @@ char *get_next_line(int fd)
free(read_buffer);
if (ft_strlen(buffer) == 0)
{
free(buffer);
buffer = NULL;
free(buffer);
buffer = NULL;
}
return (buffer);
}

View file

@ -19,8 +19,8 @@
# ifndef BUFFER_SIZE
# define BUFFER_SIZE 1024
# endif
# if BUFFER_SIZE <= 1
# error BUFFER_SIZE must be at least 2
# if BUFFER_SIZE <= 0
# error BUFFER_SIZE must be at least 1
# endif
char *get_next_line(int fd);

0
get_next_line_utils.c Normal file
View file