commit a356ca2

Santtu Lakkala  ·  2025-02-01 12:31:18 +0000 UTC
parent 8f76c4d
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.
1 files changed,  +23, -12
M main.c
M main.c
+23, -12
 1@@ -676,19 +676,25 @@ int main(int argc, char *argv[])
 2 
 3         if (strcmp(url, "-e") == 0) {
 4             /* get the content from an editor */
 5+#define EDITOR "$EDITOR "
 6+            char cmd[] = EDITOR "/tmp/snac-XXXXXX";
 7             FILE *f;
 8-
 9-            unlink("/tmp/snac-edit.txt");
10-            system("$EDITOR /tmp/snac-edit.txt");
11-
12-            if ((f = fopen("/tmp/snac-edit.txt", "r")) != NULL) {
13-                content = xs_readall(f);
14-                fclose(f);
15-
16-                unlink("/tmp/snac-edit.txt");
17-            }
18-            else {
19-                printf("Nothing to send\n");
20+            int fd = mkstemp(cmd + strlen(EDITOR));
21+
22+            if (fd >= 0) {
23+                int status = system(cmd);
24+
25+                if (WIFEXITED(status) && WEXITSTATUS(status) == 0 && (f = fdopen(fd, "r")) != NULL) {
26+                    content = xs_readall(f);
27+                    fclose(f);
28+                    unlink(cmd + strlen(EDITOR));
29+                } else {
30+                    printf("Nothing to send\n");
31+                    close(fd);
32+                    return 1;
33+                }
34+            } else {
35+                fprintf(stderr, "Temp file creation failed\n");
36                 return 1;
37             }
38         }
39@@ -700,6 +706,11 @@ int main(int argc, char *argv[])
40         else
41             content = xs_dup(url);
42 
43+        if (!content || !*content) {
44+            printf("Nothing to send\n");
45+            return 1;
46+        }
47+
48         int scope = 0;
49         if (strcmp(cmd, "note_mention") == 0)
50             scope = 1;