Recently Written · git

clic

MIRROR ONLY Gopher client with pretty colours in Common Lisp

git clone https://github.com/equwal/clic

Log | Files | Refs


commit d07922a2554fc78ba5efe7e9818e4cb7fbfd6938
Solene Rapenne <solene@perso.pw>
2018-01-02 10:22:35 +0100

Fix a crash when downloading binary

 clic.lisp | 24 +++++++++++++++---------
 1 file changed, 15 insertions(+), 9 deletions(-)
diff --git a/clic.lisp b/clic.lisp
index a1e66a0..8b95013 100644
--- a/clic.lisp
+++ b/clic.lisp
@@ -274,15 +274,19 @@
           ;; not terminal = write to stdio
           (if (ttyp)
               ;; save into a file in /tmp
-              (let ((filename (subseq uri (1+ (position #\/ uri :from-end t)))))
-                (with-open-file (output (concatenate 'string "/tmp/" filename)
+              (let* ((filename (subseq uri (1+ (position #\/ uri :from-end t))))
+                     (path (concatenate 'string "/tmp/" filename)))
+                (with-open-file (output path
                                         :element-type '(unsigned-byte 8)
                                         :direction :output :if-exists :supersede)
                   (let ((buf (make-array 4096 :element-type '(unsigned-byte 8))))
                     (loop for pos = (read-sequence buf stream)
                        while (plusp pos)
                        do
-                         (write-sequence buf output :end pos)))))
+                         (format t ".")
+                         (force-output)
+                         (write-sequence buf output :end pos)))
+                  (format t "~%File downloaded into ~a (~a bytes)~%" path (file-length output))))
 
               ;; write to the standard output
               (let ((buf (make-array 4096 :element-type '(unsigned-byte 8))))
@@ -474,9 +478,10 @@
                                :output :interactive))
            ;; display last menu
            (pop *history*)
-           (setf *buffer* (copy-array *previous-buffer*))
-           (setf *links* (make-hash-table))
-           (display-buffer "1")))
+           (when *previous-buffer*
+             (setf *buffer* (copy-array *previous-buffer*))
+             (setf *links* (make-hash-table))
+             (display-buffer "1"))))
 
         ;; image
         ((or
@@ -492,9 +497,10 @@
                                                                (location-uri location)
                                                                :from-end t)))))))
          (pop *history*)
-         (setf *buffer* (copy-array *previous-buffer*))
-         (setf *links* (make-hash-table))
-         (display-buffer "1"))
+         (when *previous-buffer*
+           (setf *buffer* (copy-array *previous-buffer*))
+           (setf *links* (make-hash-table))
+           (display-buffer "1")))
 
 
         ;;;; output is a menu ?