diff options
| -rw-r--r-- | CMakeLists.txt | 8 | ||||
| -rw-r--r-- | src/linebuffer.c | 19 | ||||
| -rw-r--r-- | src/main.c | 24 | ||||
| -rw-r--r-- | src/strlcat.c | 31 | ||||
| -rwxr-xr-x | test.sh | 7 | ||||
| -rw-r--r-- | tests/test_linebuffer.c | 2 |
6 files changed, 30 insertions, 61 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index 35aba96..6bec2cc 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -17,14 +17,6 @@ set(SRC_FILES src/main.c ) -include(CheckFunctionExists) -check_function_exists(strlcat HAVE_STRLCAT) -if(HAVE_STRLCAT) - add_definitions(-DHAVE_STRLCAT=1) -else() - list(APPEND SRC_FILES src/strlcat.c) -endif() - include_directories(src) add_executable(justify ${SRC_FILES}) diff --git a/src/linebuffer.c b/src/linebuffer.c index 772ba77..6a7a30b 100644 --- a/src/linebuffer.c +++ b/src/linebuffer.c @@ -18,7 +18,6 @@ LineBuffer *linebuffer_create(size_t buffer_capacity) { free(lb); return NULL; } - memset(lb->_buffer, 0, buffer_capacity + NULL_TERMINATOR_LEN); lb->_num_words = 0; lb->_buffer_length = 0; lb->_buffer_capacity = buffer_capacity; @@ -31,7 +30,6 @@ void linebuffer_destroy(LineBuffer *lb) { } void linebuffer_clear(LineBuffer *lb) { - memset(lb->_buffer, 0, lb->_buffer_capacity + NULL_TERMINATOR_LEN); lb->_num_words = 0; lb->_buffer_length = 0; } @@ -44,8 +42,9 @@ bool linebuffer_append_word(LineBuffer *lb, const char *word) { lb->_buffer[lb->_buffer_length] = ' '; lb->_buffer_length++; } - strlcat(lb->_buffer, word, lb->_buffer_capacity + NULL_TERMINATOR_LEN); - lb->_buffer_length += strlen(word); + size_t word_length = strlen(word); + memcpy(lb->_buffer + lb->_buffer_length, word, word_length); + lb->_buffer_length += word_length; lb->_num_words++; return true; } @@ -65,16 +64,16 @@ size_t linebuffer_space_remaining(LineBuffer *lb) { int linebuffer_write(LineBuffer *lb, FILE *fp) { int return_code = 0; - if (lb->_buffer_length > 0) { - return_code = fputs(lb->_buffer, fp); - if (return_code == EOF) { - return EOF; - } - return_code = fputc('\n', fp); + for (size_t i = 0; i < lb->_buffer_length; i++) { + return_code = fputc(lb->_buffer[i], fp); if (return_code == EOF) { return EOF; } } + return_code = fputc('\n', fp); + if (return_code == EOF) { + return EOF; + } return return_code; } @@ -7,9 +7,9 @@ #include "linebuffer.h" #include "word.h" -int justify(size_t linebuffer_capacity, FILE *in, FILE *out); +int justify(size_t line_width, FILE *in, FILE *out); -#define DEFAULT_LINEBUFFER_CAPACITY 72 +#define DEFAULT_LINE_WIDTH 72 void print_usage(const char *prog) { fprintf(stderr, "Usage: %s [-w WIDTH]\n", prog); @@ -17,8 +17,7 @@ void print_usage(const char *prog) { int main(int argc, char *argv[]) { int opt; - int width; - size_t linebuffer_capacity = DEFAULT_LINEBUFFER_CAPACITY; + int width = DEFAULT_LINE_WIDTH; while ((opt = getopt(argc, argv, "w:h")) != -1) { switch (opt) { @@ -28,7 +27,6 @@ int main(int argc, char *argv[]) { print_usage(argv[0]); return EXIT_FAILURE; } - linebuffer_capacity = (size_t) width; break; case 'h': print_usage(argv[0]); @@ -38,15 +36,19 @@ int main(int argc, char *argv[]) { return EXIT_FAILURE; } } - return justify(linebuffer_capacity, stdin, stdout); + return justify((size_t) width, stdin, stdout); } -int justify(size_t linebuffer_capacity, FILE *in, FILE *out) { - const size_t MAX_WORD_LENGTH = linebuffer_capacity; +int justify(size_t line_width, FILE *in, FILE *out) { + const size_t MAX_WORD_LENGTH = line_width; char word[MAX_WORD_LENGTH + 1]; size_t word_length; int read_word_result; - LineBuffer *lb = linebuffer_create(linebuffer_capacity); + LineBuffer *lb = linebuffer_create(line_width); + if (lb == NULL) { + fprintf(stderr, "An error occurred while allocating memory for a line buffer\n"); + return EXIT_FAILURE; + } read_word_result = read_word(in, word, MAX_WORD_LENGTH + 1); while (read_word_result == READ_WORD_SUCCESS) { @@ -58,11 +60,11 @@ int justify(size_t linebuffer_capacity, FILE *in, FILE *out) { read_word_result = read_word(in, word, MAX_WORD_LENGTH + 1); } if (read_word_result == READ_WORD_ERR_READ) { - fprintf(stderr, "Error reading word\n"); + fprintf(stderr, "An error occurred while attempting to read in a word\n"); linebuffer_destroy(lb); return EXIT_FAILURE; } - if (linebuffer_space_remaining(lb) < linebuffer_capacity) { + if (linebuffer_space_remaining(lb) < line_width) { linebuffer_write(lb, out); } linebuffer_destroy(lb); diff --git a/src/strlcat.c b/src/strlcat.c deleted file mode 100644 index ccd3720..0000000 --- a/src/strlcat.c +++ /dev/null @@ -1,31 +0,0 @@ -// NOTE: `strlcat` is a safer version of `strcat` that ensures the destination -// buffer is not overflowed. However, it's not available in all standard -// libraries. Therefore, we conditionally include it here depending on if the -// system has it available by default. -#include <stddef.h> -#include <string.h> - -size_t strlcat(char *dst, const char *src, size_t dstsize) { - char *d = dst; - const char *s = src; - size_t n = dstsize; - size_t dlen; - - /* Find the end of dst and adjust bytes left but don't go past end */ - while (n-- != 0 && *d != '\0') - d++; - dlen = d - dst; - n = dstsize - dlen; - - if (n == 0) return (dlen + strlen(s)); - while (*s != '\0') { - if (n != 1) { - *d++ = *s; - n--; - } - s++; - } - *d = '\0'; - - return (dlen + (s - src)); -} @@ -0,0 +1,7 @@ +#!/usr/bin/env bash +set -euo pipefail + +source ./build.sh + +(cd build && ctest --output-on-failure) +./tests/vendor/bats-core/bin/bats ./tests/justify.bats diff --git a/tests/test_linebuffer.c b/tests/test_linebuffer.c index 2c097f6..ae233cc 100644 --- a/tests/test_linebuffer.c +++ b/tests/test_linebuffer.c @@ -64,7 +64,7 @@ void test_linebuffer_clear(void **state) { assert_int_equal(lb->_num_words, 1); linebuffer_clear(lb); - assert_string_equal(lb->_buffer, ""); + assert_string_equal(lb->_buffer, "hello"); assert_int_equal(lb->_buffer_length, 0); assert_int_equal(lb->_num_words, 0); |