aboutsummaryrefslogtreecommitdiff
path: root/main.c
diff options
context:
space:
mode:
authorSanttu Lakkala <santtu.lakkala@unikie.com>2025-02-01 14:31:18 +0200
committerSanttu Lakkala <santtu.lakkala@unikie.com>2025-02-05 11:02:10 +0200
commita356ca255acf2119f10acbe577c3f8008ad8e160 (patch)
tree5210a15ab75e6905b0ecc345f3c2c3c1e4cc0d14 /main.c
parent8f76c4d8f6ac9d4fa81ab9fd13d8f5e8e65ea8b8 (diff)
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 (limited to 'main.c')
-rw-r--r--main.c31
1 files changed, 21 insertions, 10 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;
+ int fd = mkstemp(cmd + strlen(EDITOR));
- unlink("/tmp/snac-edit.txt");
- system("$EDITOR /tmp/snac-edit.txt");
+ if (fd >= 0) {
+ int status = system(cmd);
- 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");
+ 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;