commit a356ca255acf2119f10acbe577c3f8008ad8e160
parent 8f76c4d8f6ac9d4fa81ab9fd13d8f5e8e65ea8b8
Author: Santtu Lakkala <santtu.lakkala@unikie.com>
Date: Sat, 1 Feb 2025 14:31:18 +0200
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.
Diffstat:
M | main.c | | | 35 | +++++++++++++++++++++++------------ |
1 file changed, 23 insertions(+), 12 deletions(-)
diff --git 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;