diff options
| author | Tristan Riehs <tristan.riehs@inria.fr> | 2025-12-27 15:55:05 +0100 |
|---|---|---|
| committer | Tristan Riehs <tristan.riehs@inria.fr> | 2025-12-27 15:55:05 +0100 |
| commit | d22307899fdcc0d8796fbc098252316cb4a2f30a (patch) | |
| tree | 41ab8286abb89c9cef0c55e7fd6dec9ea15b1cf8 /src/main.c | |
| parent | fe97d16fe3f5f434d084080d5e8d71ca43d86b73 (diff) | |
Create routine for removing ending newlines
This also fixes a bug where user input containing single quotes would be
truncated.
Diffstat (limited to 'src/main.c')
| -rw-r--r-- | src/main.c | 15 |
1 files changed, 11 insertions, 4 deletions
@@ -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); |
