tKeep downloaded images and keep them at fork exit - sacc - sacc (saccomys): simple gopher client.
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) LICENSE
       ---
 (DIR) commit 4f271e2e9864f65f96a356eb607963cc1326bf95
 (DIR) parent 15dc331af5d4edf8a73aba2badb74e636fed71e2
 (HTM) Author: Quentin Rameau <quinq@fifth.space>
       Date:   Tue, 18 Jul 2017 15:34:42 +0200
       
       Keep downloaded images and keep them at fork exit
       
       Diffstat:
         sacc.c                              |      24 +++++++++++++++++-------
       
       1 file changed, 17 insertions(+), 7 deletions(-)
       ---
 (DIR) diff --git a/sacc.c b/sacc.c
       t@@ -22,6 +22,7 @@
        
        static char *mainurl;
        static Item *mainentry;
       +static int parent = 1;
        
        void
        die(const char *fmt, ...)
       t@@ -113,7 +114,7 @@ clearitem(Item *item)
                        clear(&item->dat);
                }
        
       -        if ((tag = item->tag) &&
       +        if (parent && (tag = item->tag) &&
                    !strncmp(tag, "/tmp/sacc/img-", 14) && strlen(tag) == 20)
                        unlink(tag);
        
       t@@ -176,6 +177,7 @@ displaytextitem(Item *item)
                        fprintf(stderr, "Couldn't fork.\n");
                        return;
                case 0:
       +                parent = 0;
                        pagerin = popen("$PAGER", "we");
                        fputs(item->raw, pagerin);
                        status = pclose(pagerin);
       t@@ -454,6 +456,7 @@ plumb(char *url)
                        fprintf(stderr, "Couldn't fork.\n");
                        return;
                case 0:
       +                parent = 0;
                        if (execlp("xdg-open", "xdg-open", url, NULL) < 0)
                                die("execlp: %s", strerror(errno));
                }
       t@@ -464,13 +467,19 @@ displayimg(Item *item)
        {
                int tmpfd;
        
       -        item->tag = xstrdup("/tmp/sacc/img-XXXXXX");
       +        if (!item->tag) {
       +                item->tag = xstrdup("/tmp/sacc/img-XXXXXX");
        
       -        if ((tmpfd = mkstemp(item->tag)) < 0)
       -                die("mkstemp: %s: %s", item->tag, strerror(errno));
       +                if ((tmpfd = mkstemp(item->tag)) < 0)
       +                        die("mkstemp: %s: %s", item->tag, strerror(errno));
        
       -        if (downloaditem(item, tmpfd))
       -                plumb(item->tag);
       +                if (!downloaditem(item, tmpfd)) {
       +                        clear(&item->tag);
       +                        return 0;
       +                }
       +        }
       +
       +        plumb(item->tag);
        }
        
        static int
       t@@ -627,7 +636,8 @@ static void
        cleanup(void)
        {
                clearitem(mainentry);
       -        rmdir("/tmp/sacc");
       +        if (parent)
       +                rmdir("/tmp/sacc");
                free(mainentry);
                free(mainurl);
                uicleanup();