From a356ca255acf2119f10acbe577c3f8008ad8e160 Mon Sep 17 00:00:00 2001 From: Santtu Lakkala Date: Sat, 1 Feb 2025 14:31:18 +0200 Subject: Allow multiple editors at the same time Instead of a hard-coded message file, use mkstemp() to have an unique temp file for each invocation. --- main.c | 35 +++++++++++++++++++++++------------ 1 file changed, 23 insertions(+), 12 deletions(-) diff --git a/main.c b/main.c index 347c495..58a46de 100644 --- a/main.c +++ b/main.c @@ -676,19 +676,25 @@ int main(int argc, char *argv[]) if (strcmp(url, "-e") == 0) { /* get the content from an editor */ +#define EDITOR "$EDITOR " + char cmd[] = EDITOR "/tmp/snac-XXXXXX"; FILE *f; - - unlink("/tmp/snac-edit.txt"); - system("$EDITOR /tmp/snac-edit.txt"); - - if ((f = fopen("/tmp/snac-edit.txt", "r")) != NULL) { - content = xs_readall(f); - fclose(f); - - unlink("/tmp/snac-edit.txt"); - } - else { - printf("Nothing to send\n"); + int fd = mkstemp(cmd + strlen(EDITOR)); + + if (fd >= 0) { + int status = system(cmd); + + if (WIFEXITED(status) && WEXITSTATUS(status) == 0 && (f = fdopen(fd, "r")) != NULL) { + content = xs_readall(f); + fclose(f); + unlink(cmd + strlen(EDITOR)); + } else { + printf("Nothing to send\n"); + close(fd); + return 1; + } + } else { + fprintf(stderr, "Temp file creation failed\n"); return 1; } } @@ -700,6 +706,11 @@ int main(int argc, char *argv[]) else content = xs_dup(url); + if (!content || !*content) { + printf("Nothing to send\n"); + return 1; + } + int scope = 0; if (strcmp(cmd, "note_mention") == 0) scope = 1; -- cgit v1.2.3