diff options
| -rw-r--r-- | src/main.c | 33 |
1 files changed, 19 insertions, 14 deletions
@@ -168,7 +168,7 @@ static void ftag_add_one_file(sqlite3 *db, char *full_name = NULL; char *canonical_name = NULL; char *description = NULL; - char *date = NULL; + time_t date = time(NULL); size_t line_len = 0; ssize_t read_len; @@ -221,27 +221,32 @@ static void ftag_add_one_file(sqlite3 *db, description[read_len-1] = '\0'; assert_no_single_quote(description); - line_len = 32; - date = malloc(line_len); - time_t now = time(NULL); - struct tm *now_tm = localtime(&now); - strftime(date, line_len-1, "%Y-%m-%d", now_tm); + line_len = 64; + char *date_str = malloc(line_len); + struct tm *now_tm = localtime(&date); + strftime(date_str, line_len-1, "%Y-%m-%d", now_tm); printf("Enter the date in the format YYYY-MM-DD. If no input is\n" - "given, date will be \"%s\".\n", date); - read_len = getline(&date, &line_len, stdin); + "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 (date[0] == '\n') - strftime(date, line_len-1, "%Y-%m-%d", now_tm); - else - date[read_len-1] = '\0'; + if (read_len > 1) { + struct tm tm; + memset(&tm, 0, sizeof(tm)); + rc = sscanf(date_str, "%d-%d-%d\n", &tm.tm_year, &tm.tm_mon, &tm.tm_mday); + date = mktime(&tm); + if (rc != 3) { + perror("sccanf"); + exit(EXIT_FAILURE); + } + } memset(sql, 0, sizeof(sql)); snprintf(sql, sizeof(sql)-1, - "INSERT INTO files VALUES(%d, '%s', '%s', '%s', '%s')", - *next_id, canonical_name, full_name, description, date); + "INSERT INTO files VALUES(%d, '%s', '%s', '%s', %ld)", + next_id, canonical_name, full_name, description, date); rc = sqlite3_exec(db, sql, NULL, NULL, NULL); sqlite3_check(rc, db); |
