tUse a default filename for downloaditem. - sacc - sacc (saccomys): simple gopher client.
Log
Files
Refs
LICENSE
---
commit bdb2f6a27c4d8e74a49c7ca2764207a1f63b3646
parent ae8b3fb2dda1bdc6750a38595c4c77ace2907c6c
Author: Quentin Rameau 
Date:   Mon, 17 Jul 2017 16:56:00 +0200

Use a default filename for downloaditem.

Also fix a potention use-after-free there.

Diffstat:
  common.h                            |       2 +-
  sacc.c                              |      19 ++++++++++++-------
  ui_ti.c                             |      10 ++++++++--
  ui_txt.c                            |       9 +++++++--

4 files changed, 28 insertions(+), 12 deletions(-)
---
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);
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);
diff --git a/ui_ti.c b/ui_ti.c
t@@ -1,3 +1,4 @@
+#include 
 #include 
 #include 
 #include 
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);
diff --git a/ui_txt.c b/ui_txt.c
t@@ -1,5 +1,6 @@
 #include 
 #include 
+#include 
 #include 
 #include 
 #include 
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)