This commit is contained in:
choc 2024-08-13 12:25:26 +08:00
parent 25cb232842
commit 44ade79c6b
No known key found for this signature in database

59
ssm.c
View file

@ -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;
}
}
/*
* 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(&timestamp);
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,10 +71,11 @@ void
load_events(event **events, int *num_events)
{
char *db_path = get_database_path();
if (db_path != NULL) {
if (db_path == NULL)
return;
FILE *file = fopen(db_path, "r");
if (!file) {
fprintf(stderr, "Cannot open database file\n");
fprintf(stderr, "Cannot open database file: %s\n", db_path);
exit(EXIT_FAILURE);
}
@ -90,27 +92,20 @@ load_events(event **events, int *num_events)
}
fseek(file, 0, SEEK_SET);
int i = 0;
while (fgets(line, sizeof(line), file)) {
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);
i++;
}
fclose(file);
free(db_path);
}
}
void
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);
@ -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,11 +220,11 @@ 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 <command>\n\n"
" help Show this help message\n"
" sched <time> <title> [description] Schedule an event\n"
@ -237,7 +232,7 @@ usage(int code)
" list <timerange> List all upcoming events\n"
" search Search for events\n"
" run Spawn notifier daemon\n"
, VERSION);
);
exit(code);
}
@ -264,7 +259,8 @@ main(int argc, char **argv)
} else if (strcmp(argv[1], "edit") == 0) {
char *editor = getenv("EDITOR");
if (editor == NULL) {
fprintf(stderr, "EDITOR not defined");
fprintf(stderr, "EDITOR not defined\n");
return EXIT_FAILURE;
}
char *db_path = get_database_path();
char *cmd = malloc((strlen(editor) + strlen(db_path) + 1) * sizeof(char));
@ -284,9 +280,10 @@ main(int argc, char **argv)
} else if (strcmp(argv[1], "run") == 0) {
watch_file(events, &num_events);
} else if (strcmp(argv[1], "help") == 0) {
usage(1);
usage(0);
} else {
fprintf(stderr, "Unknown command: %s\n", argv[1]);
usage(1);
}
return EXIT_FAILURE;
return EXIT_SUCCESS;
}