diff --git a/ssm.c b/ssm.c index 1b3c8c7..e9eba99 100644 --- a/ssm.c +++ b/ssm.c @@ -31,7 +31,7 @@ get_database_path() if (db[0] == '~') { char *home = getenv("HOME"); if (home == NULL) { - fprintf(stderr, "HOME not defined"); + fprintf(stderr, "HOME not defined\n"); return NULL; } db_path = malloc((strlen(db) + strlen(home)) * sizeof(char)); @@ -41,27 +41,28 @@ get_database_path() } else { db_path = strdup(db); } - if (db_path == NULL) { - return NULL; - } else { - return db_path; - } + return db_path; } -/* - * Convert timestmap to string - * format: 0 - YYYY-MM-DD HH:MM:SS - * format: 1 - HH:MM - */ +typedef enum { + FULL, // YYYY-MM-DD HH:MM:SS + HHMM, // HH:MM +} datefmt; + char * -convert_timestamp(time_t timestamp, int format) +convert_timestamp(time_t timestamp, datefmt format) { struct tm *time_info = localtime(×tamp); char *time_buf = malloc(20 * sizeof(char)); - if (format == 0) { + switch(format) { + case FULL: strftime(time_buf, 20, "%Y-%m-%d %H:%M:%S", time_info); - } else { + break; + case HHMM: strftime(time_buf, 6, "%H:%M", time_info); + break; + default: + fprintf(stderr, "Invalid datefmt\n"); } return time_buf; } @@ -70,35 +71,33 @@ void load_events(event **events, int *num_events) { char *db_path = get_database_path(); - if (db_path != NULL) { - FILE *file = fopen(db_path, "r"); - if (!file) { - fprintf(stderr, "Cannot open database file\n"); - exit(EXIT_FAILURE); - } - - char line[MAX_LINE_LEN]; - *num_events = 0; - while (fgets(line, sizeof(line), file)) { - (*num_events)++; - } - - *events = malloc(sizeof(event) * (*num_events)); - if (*events == NULL) { - perror("malloc"); - exit(EXIT_FAILURE); - } - - fseek(file, 0, SEEK_SET); - int i = 0; - while (fgets(line, sizeof(line), file)) { - sscanf(line, "%ld\t%[^\t]\t%[^\n]", &(*events)[i].timestamp, (*events)[i].name, (*events)[i].description); - i++; - } - - fclose(file); - free(db_path); + if (db_path == NULL) + return; + FILE *file = fopen(db_path, "r"); + if (!file) { + fprintf(stderr, "Cannot open database file: %s\n", db_path); + exit(EXIT_FAILURE); } + + char line[MAX_LINE_LEN]; + *num_events = 0; + while (fgets(line, sizeof(line), file)) { + (*num_events)++; + } + + *events = malloc(sizeof(event) * (*num_events)); + if (*events == NULL) { + perror("malloc"); + exit(EXIT_FAILURE); + } + + fseek(file, 0, SEEK_SET); + for (int i = 0; fgets(line, sizeof(line), file); i++) { + sscanf(line, "%ld\t%[^\t]\t%[^\n]", &(*events)[i].timestamp, (*events)[i].name, (*events)[i].description); + } + + fclose(file); + free(db_path); } void @@ -106,18 +105,14 @@ add_event(time_t timestamp, char *name, char *description) { char *db_path = get_database_path(); FILE *file; - if (access(db_path, F_OK) != 0) { - file = fopen(db_path, "w"); - } else { - file = fopen(db_path, "a"); + file = fopen(db_path, access(db_path, F_OK) ? "w" : "a"); + if (!file) { + perror("fopen"); + exit(EXIT_FAILURE); } - if (!file) { - perror("fopen"); - exit(EXIT_FAILURE); - } - fprintf(file, "%ld\t%s\t%s\n", timestamp, name, description); - fclose(file); + fprintf(file, "%ld\t%s\t%s\n", timestamp, name, description); + fclose(file); free(db_path); char *time_buf = convert_timestamp(timestamp, 0); printf("Added \"%s\" with description \"%s\" at \"%s\"\n", name, description, time_buf); @@ -165,7 +160,7 @@ list_events(event *events, int *num_events) { load_events(&events, num_events); for (int i = 0; i < *num_events; i++) { - printf("Timestamp: %d\nName: %s\nDescription: %s\n\n", events[i].timestamp, events[i].name, events[i].description); + printf("Timestamp: %ld\nName: %s\nDescription: %s\n\n", events[i].timestamp, events[i].name, events[i].description); } free(events); } @@ -225,19 +220,19 @@ watch_file(event *events, int *num_events) } -static void +static _Noreturn void usage(int code) { fprintf(code ? stderr : stdout, - "Simple Scheduler Manager %s\n\n" + "Simple Scheduler Manager " VERSION "\n\n" "Usage: ssm \n\n" - " help Show this help message\n" + " help Show this help message\n" " sched