URI: 
       tDownload images to temporary file and plumb them - sacc - sacc(omys), simple console gopher client (mirror)
  HTML git clone https://git.parazyd.org/sacc
   DIR Log
   DIR Files
   DIR Refs
   DIR LICENSE
       ---
   DIR commit d2146543349ef8bde18732075dff69de5fd346a3
   DIR parent 92b222eb21149fa7fcd4044f53b2139d10527a1a
  HTML Author: Quentin Rameau <quinq@fifth.space>
       Date:   Tue, 18 Jul 2017 14:29:19 +0200
       
       Download images to temporary file and plumb them
       
       Fix downloaditem() return value
       
       Diffstat:
         M common.h                            |       1 +
         M sacc.c                              |      65 +++++++++++++++++++++----------
       
       2 files changed, 46 insertions(+), 20 deletions(-)
       ---
   DIR diff --git a/common.h b/common.h
       t@@ -10,6 +10,7 @@ struct item {
                char *host;
                char *port;
                char *raw;
       +        char *tag;
                void *dat;
                Item *entry;
        };
   DIR diff --git a/sacc.c b/sacc.c
       t@@ -97,6 +97,7 @@ clearitem(Item *item)
        {
                Dir *dir;
                Item **items;
       +        char *tag;
                size_t i;
        
                if (!item)
       t@@ -112,6 +113,11 @@ clearitem(Item *item)
                        clear(&item->dat);
                }
        
       +        if ((tag = item->tag) &&
       +            !strncmp(tag, "/tmp/sacc/img-", 14) && strlen(tag) == 20)
       +                unlink(tag);
       +
       +        clear(&item->tag);
                clear(&item->raw);
        }
        
       t@@ -355,31 +361,33 @@ connectto(const char *host, const char *port)
        }
        
        static int
       -downloaditem(Item *item)
       +downloaditem(Item *item, int dest)
        {
                char buf[BUFSIZ], *path, *file;
                ssize_t r, w;
                mode_t mode = S_IRUSR|S_IWUSR|S_IRGRP;
       -        int sock, dest;
       +        int sock;
        
       -        if (file = strrchr(item->selector, '/'))
       -                ++file;
       -        else
       -                file = item->selector;
       +        if (dest < 0) {
       +                if (file = strrchr(item->selector, '/'))
       +                        ++file;
       +                else
       +                        file = item->selector;
        
       -        if (path = uiprompt("Download file to [%s]: ", file))
       -                path[strlen(path)-1] = '\0';
       -        else
       -                path = xstrdup(file);
       +                if (path = uiprompt("Download file to [%s]: ", file))
       +                        path[strlen(path)-1] = '\0';
       +                else
       +                        path = xstrdup(file);
        
       -        if ((dest = open(path, O_WRONLY|O_CREAT|O_EXCL, mode)) < 0) {
       -                printf("Can't open destination file %s: %s\n",
       -                       path, strerror(errno));
       -                errno = 0;
       -                free(path);
       -                return 0;
       +                item->tag = path;
       +
       +                if ((dest = open(path, O_WRONLY|O_CREAT|O_EXCL, mode)) < 0) {
       +                        printf("Can't open destination file %s: %s\n",
       +                               path, strerror(errno));
       +                        errno = 0;
       +                        return 0;
       +                }
                }
       -        free(path);
        
                sock = connectto(item->host, item->port);
                sendselector(sock, item->selector);
       t@@ -398,7 +406,7 @@ downloaditem(Item *item)
                close(dest);
                close(sock);
        
       -        return (r == 0 && w > 0);
       +        return (r == 0 && w == 0);
        }
        
        static int
       t@@ -452,6 +460,20 @@ plumb(char *url)
        }
        
        static int
       +displayimg(Item *item)
       +{
       +        int tmpfd;
       +
       +        item->tag = xstrdup("/tmp/sacc/img-XXXXXX");
       +
       +        if ((tmpfd = mkstemp(item->tag)) < 0)
       +                die("mkstemp: %s: %s", item->tag, strerror(errno));
       +
       +        if (downloaditem(item, tmpfd))
       +                plumb(item->tag);
       +}
       +
       +static int
        dig(Item *entry, Item *item)
        {
                if (item->raw) /* already in cache */
       t@@ -484,7 +506,7 @@ dig(Item *entry, Item *item)
                case '9':
                case 'g':
                case 'I':
       -                if (!downloaditem(item))
       +                if (!displayimg(item))
                                return 0;
                        break;
                default:
       t@@ -597,7 +619,7 @@ moldentry(char *url)
                entry->host = host;
                entry->port = port;
                entry->entry = entry;
       -        entry->raw = entry->dat = NULL;
       +        entry->raw = entry->tag = entry->dat = NULL;
        
                return entry;
        }
       t@@ -606,6 +628,7 @@ static void
        cleanup(void)
        {
                clearitem(mainentry);
       +        rmdir("/tmp/sacc");
                free(mainentry);
                free(mainurl);
                uicleanup();
       t@@ -616,6 +639,8 @@ setup(void)
        {
                setenv("PAGER", "more", 0);
                atexit(cleanup);
       +        if (mkdir("/tmp/sacc", S_IRWXU) < 0 && errno != EEXIST)
       +                die("mkdir: %s: %s", "/tmp/sacc", errno);
                uisetup();
        }