URI: 
       tUse a default filename for downloaditem. - 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 bdb2f6a27c4d8e74a49c7ca2764207a1f63b3646
   DIR parent ae8b3fb2dda1bdc6750a38595c4c77ace2907c6c
  HTML Author: Quentin Rameau <quinq@fifth.space>
       Date:   Mon, 17 Jul 2017 16:56:00 +0200
       
       Use a default filename for downloaditem.
       
       Also fix a potention use-after-free there.
       
       Diffstat:
         M common.h                            |       2 +-
         M sacc.c                              |      19 ++++++++++++-------
         M ui_ti.c                             |      10 ++++++++--
         M ui_txt.c                            |       9 +++++++--
       
       4 files changed, 28 insertions(+), 12 deletions(-)
       ---
   DIR diff --git a/common.h b/common.h
       t@@ -26,5 +26,5 @@ void display(Item *item);
        Item *selectitem(Item *entry);
        const char *typedisplay(char t);
        void uicleanup(void);
       -char *uiprompt(char *s);
       +char *uiprompt(char *fmt, ...);
        void uisetup(void);
   DIR diff --git a/sacc.c b/sacc.c
       t@@ -357,24 +357,29 @@ connectto(const char *host, const char *port)
        static int
        downloaditem(Item *item)
        {
       -        char buf[BUFSIZ], *path;
       +        char buf[BUFSIZ], *path, *file;
                ssize_t r, w;
                mode_t mode = S_IRUSR|S_IWUSR|S_IRGRP;
                int sock, dest;
        
       -        if (!(path = uiprompt("Download file to: ")))
       -                return 0;
       +        if (file = strrchr(item->selector, '/'))
       +                ++file;
       +        else
       +                file = item->selector;
        
       -        path[strlen(path)-1] = '\0';
       +        if (path = uiprompt("Download file to [%s]: ", file))
       +                path[strlen(path)-1] = '\0';
       +        else
       +                path = xstrdup(file);
        
       -        dest = open(path, O_WRONLY|O_CREAT|O_EXCL, mode);
       -        free(path);
       -        if (dest < 0) {
       +        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;
                }
       +        free(path);
        
                sock = connectto(item->host, item->port);
                sendselector(sock, item->selector);
   DIR diff --git a/ui_ti.c b/ui_ti.c
       t@@ -1,3 +1,4 @@
       +#include <stdarg.h>
        #include <stdio.h>
        #include <stdlib.h>
        #include <term.h>
       t@@ -50,8 +51,9 @@ uicleanup(void)
        }
        
        char *
       -uiprompt(char *s)
       +uiprompt(char *fmt, ...)
        {
       +        va_list ap;
                char *input = NULL;
                size_t n = 0;
                ssize_t r;
       t@@ -61,7 +63,11 @@ uiprompt(char *s)
                putp(tparm(cursor_address, lines-1, 0));
                putp(tparm(clr_eol));
                putp(tparm(enter_standout_mode));
       -        fputs(s, stdout);
       +
       +        va_start(ap, fmt);
       +        vprintf(fmt, ap);
       +        va_end(ap);
       +
                putp(tparm(exit_standout_mode));
        
                tsacc.c_lflag |= (ECHO|ICANON);
   DIR diff --git a/ui_txt.c b/ui_txt.c
       t@@ -1,5 +1,6 @@
        #include <ctype.h>
        #include <errno.h>
       +#include <stdarg.h>
        #include <stdio.h>
        #include <stdlib.h>
        #include <string.h>
       t@@ -69,12 +70,16 @@ printstatus(Item *item, char c)
        }
        
        char *
       -uiprompt(char *s)
       +uiprompt(char *fmt, ...)
        {
       +        va_list ap;
                char *input = NULL;
                size_t n = 0;
        
       -        fputs(s, stdout);
       +        va_start(ap, fmt);
       +        vprintf(fmt, ap);
       +        va_end(ap);
       +
                fflush(stdout);
        
                if (getline(&input, &n, stdin) > 1)