Works for BUFFER_SIZE=42

This commit is contained in:
Khaïs COLIN 2024-10-31 13:43:56 +01:00
parent fd2c903d48
commit 1f79bbbb12

View file

@ -129,12 +129,10 @@ char *get_next_line(int fd)
static char *buffer = NULL; static char *buffer = NULL;
char *read_buffer; char *read_buffer;
int num_bytes_read; int num_bytes_read;
size_t length;
char *outbuf; char *outbuf;
read_buffer = malloc(BUFFER_SIZE * sizeof(char)); read_buffer = malloc(BUFFER_SIZE * sizeof(char));
num_bytes_read = 1; num_bytes_read = 1;
length = 0;
if (buffer == NULL) if (buffer == NULL)
{ {
buffer = malloc(sizeof(char)); buffer = malloc(sizeof(char));
@ -162,7 +160,7 @@ char *get_next_line(int fd)
free(read_buffer); free(read_buffer);
return (outbuf); return (outbuf);
} }
num_bytes_read = read(fd, read_buffer + length, BUFFER_SIZE - length - 1); num_bytes_read = read(fd, read_buffer, BUFFER_SIZE - 1);
if (num_bytes_read < 0) if (num_bytes_read < 0)
{ {
free(read_buffer); free(read_buffer);
@ -171,7 +169,6 @@ char *get_next_line(int fd)
return (NULL); return (NULL);
} }
read_buffer[num_bytes_read] = '\0'; read_buffer[num_bytes_read] = '\0';
length += num_bytes_read;
// DONE: join with big buffer // DONE: join with big buffer
// FIXME: correctly free buffer // FIXME: correctly free buffer
buffer = ft_strjoin_free_s1(buffer, read_buffer); buffer = ft_strjoin_free_s1(buffer, read_buffer);