tidy up
This commit is contained in:
parent
25cb232842
commit
44ade79c6b
1 changed files with 62 additions and 65 deletions
61
ssm.c
61
ssm.c
|
@ -31,7 +31,7 @@ get_database_path()
|
||||||
if (db[0] == '~') {
|
if (db[0] == '~') {
|
||||||
char *home = getenv("HOME");
|
char *home = getenv("HOME");
|
||||||
if (home == NULL) {
|
if (home == NULL) {
|
||||||
fprintf(stderr, "HOME not defined");
|
fprintf(stderr, "HOME not defined\n");
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
db_path = malloc((strlen(db) + strlen(home)) * sizeof(char));
|
db_path = malloc((strlen(db) + strlen(home)) * sizeof(char));
|
||||||
|
@ -41,27 +41,28 @@ get_database_path()
|
||||||
} else {
|
} else {
|
||||||
db_path = strdup(db);
|
db_path = strdup(db);
|
||||||
}
|
}
|
||||||
if (db_path == NULL) {
|
|
||||||
return NULL;
|
|
||||||
} else {
|
|
||||||
return db_path;
|
return db_path;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
typedef enum {
|
||||||
* Convert timestmap to string
|
FULL, // YYYY-MM-DD HH:MM:SS
|
||||||
* format: 0 - YYYY-MM-DD HH:MM:SS
|
HHMM, // HH:MM
|
||||||
* format: 1 - HH:MM
|
} datefmt;
|
||||||
*/
|
|
||||||
char *
|
char *
|
||||||
convert_timestamp(time_t timestamp, int format)
|
convert_timestamp(time_t timestamp, datefmt format)
|
||||||
{
|
{
|
||||||
struct tm *time_info = localtime(×tamp);
|
struct tm *time_info = localtime(×tamp);
|
||||||
char *time_buf = malloc(20 * sizeof(char));
|
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);
|
strftime(time_buf, 20, "%Y-%m-%d %H:%M:%S", time_info);
|
||||||
} else {
|
break;
|
||||||
|
case HHMM:
|
||||||
strftime(time_buf, 6, "%H:%M", time_info);
|
strftime(time_buf, 6, "%H:%M", time_info);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
fprintf(stderr, "Invalid datefmt\n");
|
||||||
}
|
}
|
||||||
return time_buf;
|
return time_buf;
|
||||||
}
|
}
|
||||||
|
@ -70,10 +71,11 @@ void
|
||||||
load_events(event **events, int *num_events)
|
load_events(event **events, int *num_events)
|
||||||
{
|
{
|
||||||
char *db_path = get_database_path();
|
char *db_path = get_database_path();
|
||||||
if (db_path != NULL) {
|
if (db_path == NULL)
|
||||||
|
return;
|
||||||
FILE *file = fopen(db_path, "r");
|
FILE *file = fopen(db_path, "r");
|
||||||
if (!file) {
|
if (!file) {
|
||||||
fprintf(stderr, "Cannot open database file\n");
|
fprintf(stderr, "Cannot open database file: %s\n", db_path);
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -90,15 +92,12 @@ load_events(event **events, int *num_events)
|
||||||
}
|
}
|
||||||
|
|
||||||
fseek(file, 0, SEEK_SET);
|
fseek(file, 0, SEEK_SET);
|
||||||
int i = 0;
|
for (int i = 0; fgets(line, sizeof(line), file); i++) {
|
||||||
while (fgets(line, sizeof(line), file)) {
|
|
||||||
sscanf(line, "%ld\t%[^\t]\t%[^\n]", &(*events)[i].timestamp, (*events)[i].name, (*events)[i].description);
|
sscanf(line, "%ld\t%[^\t]\t%[^\n]", &(*events)[i].timestamp, (*events)[i].name, (*events)[i].description);
|
||||||
i++;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fclose(file);
|
fclose(file);
|
||||||
free(db_path);
|
free(db_path);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
|
@ -106,11 +105,7 @@ add_event(time_t timestamp, char *name, char *description)
|
||||||
{
|
{
|
||||||
char *db_path = get_database_path();
|
char *db_path = get_database_path();
|
||||||
FILE *file;
|
FILE *file;
|
||||||
if (access(db_path, F_OK) != 0) {
|
file = fopen(db_path, access(db_path, F_OK) ? "w" : "a");
|
||||||
file = fopen(db_path, "w");
|
|
||||||
} else {
|
|
||||||
file = fopen(db_path, "a");
|
|
||||||
}
|
|
||||||
if (!file) {
|
if (!file) {
|
||||||
perror("fopen");
|
perror("fopen");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
|
@ -165,7 +160,7 @@ list_events(event *events, int *num_events)
|
||||||
{
|
{
|
||||||
load_events(&events, num_events);
|
load_events(&events, num_events);
|
||||||
for (int i = 0; i < *num_events; i++) {
|
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);
|
free(events);
|
||||||
}
|
}
|
||||||
|
@ -225,11 +220,11 @@ watch_file(event *events, int *num_events)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static _Noreturn void
|
||||||
usage(int code)
|
usage(int code)
|
||||||
{
|
{
|
||||||
fprintf(code ? stderr : stdout,
|
fprintf(code ? stderr : stdout,
|
||||||
"Simple Scheduler Manager %s\n\n"
|
"Simple Scheduler Manager " VERSION "\n\n"
|
||||||
"Usage: ssm <command>\n\n"
|
"Usage: ssm <command>\n\n"
|
||||||
" help Show this help message\n"
|
" help Show this help message\n"
|
||||||
" sched <time> <title> [description] Schedule an event\n"
|
" sched <time> <title> [description] Schedule an event\n"
|
||||||
|
@ -237,7 +232,7 @@ usage(int code)
|
||||||
" list <timerange> List all upcoming events\n"
|
" list <timerange> List all upcoming events\n"
|
||||||
" search Search for events\n"
|
" search Search for events\n"
|
||||||
" run Spawn notifier daemon\n"
|
" run Spawn notifier daemon\n"
|
||||||
, VERSION);
|
);
|
||||||
exit(code);
|
exit(code);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -264,7 +259,8 @@ main(int argc, char **argv)
|
||||||
} else if (strcmp(argv[1], "edit") == 0) {
|
} else if (strcmp(argv[1], "edit") == 0) {
|
||||||
char *editor = getenv("EDITOR");
|
char *editor = getenv("EDITOR");
|
||||||
if (editor == NULL) {
|
if (editor == NULL) {
|
||||||
fprintf(stderr, "EDITOR not defined");
|
fprintf(stderr, "EDITOR not defined\n");
|
||||||
|
return EXIT_FAILURE;
|
||||||
}
|
}
|
||||||
char *db_path = get_database_path();
|
char *db_path = get_database_path();
|
||||||
char *cmd = malloc((strlen(editor) + strlen(db_path) + 1) * sizeof(char));
|
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) {
|
} else if (strcmp(argv[1], "run") == 0) {
|
||||||
watch_file(events, &num_events);
|
watch_file(events, &num_events);
|
||||||
} else if (strcmp(argv[1], "help") == 0) {
|
} else if (strcmp(argv[1], "help") == 0) {
|
||||||
usage(1);
|
usage(0);
|
||||||
}else {
|
} else {
|
||||||
|
fprintf(stderr, "Unknown command: %s\n", argv[1]);
|
||||||
usage(1);
|
usage(1);
|
||||||
}
|
}
|
||||||
return EXIT_FAILURE;
|
return EXIT_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue