diff --git a/check_failure.sh b/check_failure.sh index f7e0411..3249449 100755 --- a/check_failure.sh +++ b/check_failure.sh @@ -1,9 +1,12 @@ #!/usr/bin/env bash -set -xuo pipefail +set -euo pipefail for FAIL_AFTER in `seq 1 1000` do - cc -ggdb -Wall -Wextra -Werror -D FAIL_AFTER=$FAIL_AFTER *.c - valgrind -q --leak-check=full ./a.out get_next_line.c + cc -ggdb -Wall -Wextra -Werror -D FAIL_AFTER=$FAIL_AFTER -D BUFFER_SIZE=$2 *.c + echo + echo "num failed malloc: $FAIL_AFTER" + echo + valgrind -q --leak-check=full --error-exitcode=1 ./a.out $1 done diff --git a/get_next_line.c b/get_next_line.c index 282814e..e904ad1 100644 --- a/get_next_line.c +++ b/get_next_line.c @@ -6,7 +6,7 @@ /* By: kcolin +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/23 20:32:46 by kcolin #+# #+# */ -/* Updated: 2024/11/01 13:26:41 by kcolin ### ########.fr */ +/* Updated: 2024/11/01 17:02:27 by kcolin ### ########.fr */ /* */ /* ************************************************************************** */ @@ -61,8 +61,10 @@ char *shorten_buffer(char *buffer) /* ** returns 1 on error, 0 on success */ -int setup_buffers(char **buffer, char **read_buffer) +int setup(char **buffer, char **read_buffer, int fd) { + if (fd < 0) + return (1); if (*buffer == NULL) { *buffer = malloc(1 * sizeof(char)); @@ -116,7 +118,7 @@ char *get_next_line(int fd) char *read_buffer; int num_bytes_read; - if (setup_buffers(&buffer, &read_buffer) != 0) + if (setup(&buffer, &read_buffer, fd) != 0) return (NULL); num_bytes_read = 1; while (num_bytes_read != 0) diff --git a/test.c b/test.c index 99bbfbe..4061f8b 100644 --- a/test.c +++ b/test.c @@ -6,7 +6,7 @@ /* By: kcolin +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/24 11:51:57 by kcolin #+# #+# */ -/* Updated: 2024/10/24 11:59:09 by kcolin ### ########.fr */ +/* Updated: 2024/11/01 16:48:40 by kcolin ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,23 +17,30 @@ int main(int argc, char **argv) { int fd; - int i; + int line_idx; + int file_idx; char *line; - if (argc == 2) + if (argc > 1) { - fd = open(argv[1], O_RDONLY); - line = ""; - i = 0; - while (line != NULL) + file_idx = 1; + while (file_idx < argc) { - line = get_next_line(fd); - if (line != NULL) + fd = open(argv[file_idx], O_RDONLY); + line = ""; + line_idx = 0; + while (line != NULL) { - i++; - printf("%d\t%s", i, line); - free(line); + line = get_next_line(fd); + if (line != NULL) + { + line_idx++; + printf("%d\t%s", line_idx, line); + free(line); + } } + file_idx++; + close(fd); } return (0); }