handle EDITOR properly

This commit is contained in:
choc 2024-08-13 16:27:53 +08:00
parent 44ade79c6b
commit 1b56994db1
No known key found for this signature in database

24
ssm.c
View file

@ -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\n"); 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));
@ -257,21 +257,19 @@ main(int argc, char **argv)
/* todo: accept time */ /* todo: accept time */
add_event(time(NULL) + 60, name, description); add_event(time(NULL) + 60, name, description);
} else if (strcmp(argv[1], "edit") == 0) { } else if (strcmp(argv[1], "edit") == 0) {
char *editor = getenv("EDITOR"); const char *e = getenv("EDITOR");
if (editor == NULL) { if (e == NULL) {
fprintf(stderr, "EDITOR not defined\n"); e = editor;
return EXIT_FAILURE; fprintf(stderr, "$EDITOR not defined, falling back to %s\n", e);
} }
char *db_path = get_database_path(); char *db_path = get_database_path();
char *cmd = malloc((strlen(editor) + strlen(db_path) + 1) * sizeof(char)); if (!db_path) {
if (cmd == NULL) { fprintf(stderr, "Cannot find database path\n");
perror("malloc"); return EXIT_FAILURE;
} }
sprintf(cmd, "%s %s", editor, db_path); execlp(e, e, db_path, NULL);
/* spawn $EDITOR */ perror("Failed to spawn editor");
system(cmd); return EXIT_FAILURE;
free(cmd);
free(db_path);
} else if (strcmp(argv[1], "list") == 0) { } else if (strcmp(argv[1], "list") == 0) {
/* accept argv[2] as timerange */ /* accept argv[2] as timerange */
list_events(events, &num_events); list_events(events, &num_events);