From 27a2ef5af56e3ee813aa6813f13cb70ad2aa6a3b Mon Sep 17 00:00:00 2001 From: Tristan Riehs Date: Thu, 25 Dec 2025 19:37:27 +0100 Subject: Improve error messages --- src/main.c | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) (limited to 'src') diff --git a/src/main.c b/src/main.c index 8d2e38f..7fab923 100644 --- a/src/main.c +++ b/src/main.c @@ -39,7 +39,8 @@ static void __sqlite3_check(int rc, sqlite3 *db, const char *file, int line, con exit(EXIT_FAILURE); } -#define sqlite3_check(RC, DB) __sqlite3_check(RC, DB, __FILE__, __LINE__, __func__) +#define sqlite3_check(RC, DB) \ + __sqlite3_check(RC, DB, __FILE__, __LINE__, __func__) static int file_exists(const char *path) { @@ -128,7 +129,8 @@ static uint32_t sum(const char *file) size_t buf_len; uint32_t sum = 5381; if (!file) { - perror("fopen"); + fprintf(stderr, "fopen: \"%s\": ", file); + perror(""); exit(EXIT_FAILURE); } while ((buf_len = fread(&buf, 1, sizeof(buf), stream)) != 0) { @@ -138,7 +140,8 @@ static uint32_t sum(const char *file) } } if (ferror(stream)) { - perror("fread"); + fprintf(stderr, "fread: \"%s\": ", file); + perror(""); exit(EXIT_FAILURE); } fclose(stream); @@ -152,7 +155,7 @@ static time_t time_from_str(const char *str) memset(&tm, 0, sizeof(tm)); rc = sscanf(str, "%d-%d-%d", &tm.tm_year, &tm.tm_mon, &tm.tm_mday); if (rc <= 0) { - perror("sccanf"); + perror("sscanf"); exit(EXIT_FAILURE); } printf("%s: debug: year is %d\n", __func__, tm.tm_year); @@ -194,7 +197,8 @@ static void ftag_init(int, char **) { int rc = mkdir(FTAG_ROOT "/files", 0755); if (rc == -1) { - perror(FTAG_ROOT "/files"); + fprintf(stderr, "mkdir: " FTAG_ROOT "/files: "); + perror(""); exit(EXIT_FAILURE); } char cmd[1024]; @@ -202,7 +206,8 @@ static void ftag_init(int, char **) snprintf(cmd, sizeof(cmd)-1, "sqlite3 %s < %s", DATABASE_PATH, FTAG_ROOT "/sql/init.sql"); execl("/usr/bin/sh", "/usr/bin/sh", "-c", cmd, NULL); - perror("exec"); + fprintf(stderr, "exec: /usr/bin/sh -c \"%s\": ", cmd); + perror(""); exit(EXIT_FAILURE); } @@ -256,11 +261,6 @@ static void ftag_add_one_file(sqlite3 *db, int *next_id, const char *file, uint3 size_t line_len = 0; ssize_t read_len; - if (!file_exists(file)) { - perror(file); - exit(EXIT_FAILURE); - } - printf("ftag file add: adding file \"%s\" to database\n", file); printf("Enter the full name, a version of the name that\n" @@ -339,14 +339,16 @@ static void ftag_add_one_file(sqlite3 *db, int *next_id, const char *file, uint3 int out_fd = open(new_path, O_WRONLY | O_CREAT, 0644); if (out_fd == -1) { - perror("open"); + fprintf(stderr, "open: \"%s\": ", new_path); + perror(""); exit(EXIT_FAILURE); } int in_fd = open(file, O_RDONLY); if (in_fd == -1) { - perror("open"); + fprintf(stderr, "open: \"%s\": ", file); + perror(""); exit(EXIT_FAILURE); } -- cgit v1.2.3