From a0be689cf7154f8cbe2e09901d8ce7a660ac68cb Mon Sep 17 00:00:00 2001 From: Tristan Riehs Date: Sun, 2 Aug 2026 20:01:41 +0200 Subject: Rewrite "ftag file add" using readline --- src/main.c | 48 +++++++++++------------------------------------- 1 file changed, 11 insertions(+), 37 deletions(-) (limited to 'src') diff --git a/src/main.c b/src/main.c index 5e3c4b5..b07f43d 100644 --- a/src/main.c +++ b/src/main.c @@ -345,8 +345,8 @@ ftag_add_one_file(sqlite3 *db, int *next_id, const char *file, uint32_t file_sum char *canonical_name = NULL; char *description = NULL; time_t date = time(NULL); - size_t line_len = 0; - ssize_t read_len; + char *input = NULL; + int line_len; const char *cache_dir = ftag_config_get(FC_CACHE_DIR); /* TODO: possibly be non-interactive @@ -357,13 +357,8 @@ ftag_add_one_file(sqlite3 *db, int *next_id, const char *file, uint32_t file_sum printf("Enter the full name, a version of the name that\n" "may contain blanks and every type of character.\n"); - read_len = getline(&full_name, &line_len, stdin); - if (read_len == -1) { - perror("getline"); - exit(EXIT_FAILURE); - } + full_name = readline_with_check(NULL); sanitize_sql_str(&full_name); - remove_ending_newline(full_name); line_len = strlen(full_name) + 1; canonical_name = malloc(line_len); @@ -373,31 +368,14 @@ ftag_add_one_file(sqlite3 *db, int *next_id, const char *file, uint32_t file_sum "ideally contains no blank and, if possible, no upper\n" "case letters and no symbols. If no input is given,\n" "canonical name will be \"%s\".\n", canonical_name); - read_len = getline(&canonical_name, &line_len, stdin); - if (read_len == -1) { - perror("getline"); - exit(EXIT_FAILURE); - } - if (canonical_name[0] == '\n') - canonicalize(canonical_name, full_name); - else - remove_ending_newline(canonical_name); + input = readline_with_check(NULL); + if (input[0] != '\0') + canonicalize(canonical_name, input); + free(input); sanitize_sql_str(&canonical_name); printf("Enter the description.\n"); - line_len = 0; - read_len = getline(&description, &line_len, stdin); - if (read_len == -1) { - perror("getline"); - exit(EXIT_FAILURE); - } - if (read_len == 1) { - free(description); - description = strdup(""); - } - else { - remove_ending_newline(description); - } + description = readline_with_check(NULL); sanitize_sql_str(&description); line_len = 64; @@ -407,13 +385,9 @@ ftag_add_one_file(sqlite3 *db, int *next_id, const char *file, uint32_t file_sum printf("Enter the date in the format YYYY-MM-DD, day and month may be " "omitted. If no input is\n" "given, date will be \"%s\".\n", date_str); - read_len = getline(&date_str, &line_len, stdin); - if (read_len == -1) { - perror("getline"); - exit(EXIT_FAILURE); - } - if (read_len > 1) { - date = time_from_str(date_str); + input = readline_with_check(NULL); + if (input[0] != '\0') { + date = time_from_str(input); } char extension[16]; -- cgit v1.2.3