From d22307899fdcc0d8796fbc098252316cb4a2f30a Mon Sep 17 00:00:00 2001 From: Tristan Riehs Date: Sat, 27 Dec 2025 15:55:05 +0100 Subject: Create routine for removing ending newlines This also fixes a bug where user input containing single quotes would be truncated. --- src/main.c | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/main.c b/src/main.c index 4ca6cfe..3ec14cb 100644 --- a/src/main.c +++ b/src/main.c @@ -92,6 +92,13 @@ int prompt_yes_no(void) return 0; } +static void remove_ending_newline(char *str) +{ + int idx = strlen(str) - 1; + assert(str[idx] == '\n'); + str[idx] = '\0'; +} + /* Convert heap-allocated *STR from C string to SQL string, essentially by * adding single quotes to escape single quotes. */ static void sanitize_sql_str(char **str) @@ -271,9 +278,9 @@ static void ftag_add_one_file(sqlite3 *db, int *next_id, const char *file, uint3 exit(EXIT_FAILURE); } sanitize_sql_str(&full_name); - full_name[read_len-1] = '\0'; + remove_ending_newline(full_name); - line_len = read_len+1; + line_len = strlen(full_name) + 1; canonical_name = malloc(line_len); canonicalize(canonical_name, full_name); @@ -289,7 +296,7 @@ static void ftag_add_one_file(sqlite3 *db, int *next_id, const char *file, uint3 if (canonical_name[0] == '\n') canonicalize(canonical_name, full_name); else - canonical_name[read_len-1] = '\0'; + remove_ending_newline(canonical_name); sanitize_sql_str(&canonical_name); printf("Enter the description.\n"); @@ -304,7 +311,7 @@ static void ftag_add_one_file(sqlite3 *db, int *next_id, const char *file, uint3 description = strdup(""); } else { - description[read_len-1] = '\0'; + remove_ending_newline(description); } sanitize_sql_str(&description); -- cgit v1.2.3