tGet rid of mkstemp and use full file name in cache - sacc - sacc (saccomys): simple gopher client.
Log
Files
Refs
LICENSE
---
commit e84a502bc400f7050148d37fca81acd7227b6827
parent e9b9894e29151ef7084e13729b629adb9651487c
Author: Quentin Rameau 
Date:   Fri,  1 Sep 2017 15:12:54 +0200

Get rid of mkstemp and use full file name in cache

Diffstat:
  config.mk                           |       2 +-
  sacc.c                              |      36 +++++++++++++++----------------

2 files changed, 18 insertions(+), 20 deletions(-)
---
diff --git a/config.mk b/config.mk
t@@ -9,7 +9,7 @@ UI=txt
 #UIFLAGS=-lcurses
 
 # Stock FLAGS
-SACCCFLAGS = -D_POSIX_C_SOURCE=200809L -D_DEFAULT_SOURCE $(CFLAGS)
+SACCCFLAGS = -D_DEFAULT_SOURCE $(CFLAGS)
 SACCLDFLAGS = $(UIFLAGS) $(LDFLAGS)
 
 .c.o:
diff --git a/sacc.c b/sacc.c
t@@ -493,7 +493,7 @@ plumbitem(Item *item)
 {
         char *file, *path, *tag;
         mode_t mode = S_IRUSR|S_IWUSR|S_IRGRP;
-        int dest;
+        int n, dest, plumbitem;
 
         if (file = strrchr(item->selector, '/'))
                 ++file;
t@@ -510,36 +510,34 @@ plumbitem(Item *item)
                 tag = NULL;
         }
 
-        if (path[0]) {
-                if (tag && !strcmp(tag, path))
-                        goto cleanup;
+        plumbitem = path[0] ? 0 : 1;
 
+        if (!path[0]) {
+                clear(&path);
+                if (!tag) {
+                        n = snprintf(NULL, 0, "%s/%s", "/tmp/sacc", file);
+                        path = xmalloc(++n);
+                        snprintf(path, n, "%s/%s", "/tmp/sacc", file);
+                }
+        }
+
+        if (path && (!tag || strcmp(tag, path))) {
                 if ((dest = open(path, O_WRONLY|O_CREAT|O_EXCL, mode)) < 0) {
                         uistatus("Can't open destination file %s: %s",
                                  path, strerror(errno));
                         errno = 0;
                         goto cleanup;
                 }
-        } else {
-                clear(&path);
-
-                if (!tag) {
-                        path = xstrdup("/tmp/sacc/img-XXXXXX");
-
-                        if ((dest = mkstemp(path)) < 0) {
-                                uistatus("mkstemp: %s: %s", path, strerror(errno));
-                                goto cleanup;
-                        }
-                }
+                if (!download(item, dest) || tag)
+                        goto cleanup;
         }
 
-        if (path && (!download(item, dest) || tag))
-                goto cleanup;
-
         if (!tag)
                 item->tag = path;
 
-        plumb(item->tag);
+        if (plumbitem)
+                plumb(item->tag);
+
         return;
 cleanup:
         free(path);