From 1b56994db1de69be83ef252e8dcd0edc9e2932f7 Mon Sep 17 00:00:00 2001 From: choc Date: Tue, 13 Aug 2024 16:27:53 +0800 Subject: [PATCH] handle EDITOR properly --- ssm.c | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/ssm.c b/ssm.c index e9eba99..353b855 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\n"); + fprintf(stderr, "$HOME not defined\n"); return NULL; } db_path = malloc((strlen(db) + strlen(home)) * sizeof(char)); @@ -257,21 +257,19 @@ main(int argc, char **argv) /* todo: accept time */ add_event(time(NULL) + 60, name, description); } else if (strcmp(argv[1], "edit") == 0) { - char *editor = getenv("EDITOR"); - if (editor == NULL) { - fprintf(stderr, "EDITOR not defined\n"); - return EXIT_FAILURE; + const char *e = getenv("EDITOR"); + if (e == NULL) { + e = editor; + fprintf(stderr, "$EDITOR not defined, falling back to %s\n", e); } char *db_path = get_database_path(); - char *cmd = malloc((strlen(editor) + strlen(db_path) + 1) * sizeof(char)); - if (cmd == NULL) { - perror("malloc"); + if (!db_path) { + fprintf(stderr, "Cannot find database path\n"); + return EXIT_FAILURE; } - sprintf(cmd, "%s %s", editor, db_path); - /* spawn $EDITOR */ - system(cmd); - free(cmd); - free(db_path); + execlp(e, e, db_path, NULL); + perror("Failed to spawn editor"); + return EXIT_FAILURE; } else if (strcmp(argv[1], "list") == 0) { /* accept argv[2] as timerange */ list_events(events, &num_events);