diff options
author | Florian Obser <florian@cvs.openbsd.org> | 2019-05-15 13:42:41 +0000 |
---|---|---|
committer | Florian Obser <florian@cvs.openbsd.org> | 2019-05-15 13:42:41 +0000 |
commit | 45537f6b94632b2bb381a5b090f5665f7fa0b410 (patch) | |
tree | a956b117b7cc2d58ad0e6e03c2e48df0219f8d63 | |
parent | a41ab0b66347737359edebf92b3405b27a1f4bdb (diff) |
Display "bytes received" like the csrg ftp used to when the progressbar
is disabled.
Difference pointed out by deraadt
OK kurtm, sunil
-rw-r--r-- | usr.bin/ftp/cmd.c | 10 | ||||
-rw-r--r-- | usr.bin/ftp/ftp.h | 6 | ||||
-rw-r--r-- | usr.bin/ftp/main.c | 6 | ||||
-rw-r--r-- | usr.bin/ftp/progressmeter.c | 43 |
4 files changed, 42 insertions, 23 deletions
diff --git a/usr.bin/ftp/cmd.c b/usr.bin/ftp/cmd.c index 48b3c2ab9e8..544643b9ca5 100644 --- a/usr.bin/ftp/cmd.c +++ b/usr.bin/ftp/cmd.c @@ -1,4 +1,4 @@ -/* $OpenBSD: cmd.c,v 1.2 2019/05/12 20:58:19 jasper Exp $ */ +/* $OpenBSD: cmd.c,v 1.3 2019/05/15 13:42:40 florian Exp $ */ /* * Copyright (c) 2018 Sunil Nimmagadda <sunil@openbsd.org> @@ -422,14 +422,16 @@ do_get(int argc, char **argv) return; } + init_stats(file_sz, &offset); if (progressmeter) { p = basename(remote_fname); - start_progress_meter(p, NULL, file_sz, &offset); + start_progress_meter(p, NULL); } copy_file(dst_fp, data_fp, &offset); if (progressmeter) stop_progress_meter(); + finish_stats(); if (interrupted) ftp_abort(); @@ -590,14 +592,16 @@ do_put(int argc, char **argv) return; } + init_stats(file_sz, &offset); if (progressmeter) { p = basename(remote_fname); - start_progress_meter(p, NULL, file_sz, &offset); + start_progress_meter(p, NULL); } copy_file(data_fp, src_fp, &offset); if (progressmeter) stop_progress_meter(); + finish_stats(); if (interrupted) ftp_abort(); diff --git a/usr.bin/ftp/ftp.h b/usr.bin/ftp/ftp.h index adc2f2b643c..011db6b7ed3 100644 --- a/usr.bin/ftp/ftp.h +++ b/usr.bin/ftp/ftp.h @@ -1,4 +1,4 @@ -/* $OpenBSD: ftp.h,v 1.2 2019/05/12 20:58:19 jasper Exp $ */ +/* $OpenBSD: ftp.h,v 1.3 2019/05/15 13:42:40 florian Exp $ */ /* * Copyright (c) 2015 Sunil Nimmagadda <sunil@openbsd.org> @@ -91,8 +91,10 @@ void http_save(struct url *, FILE *, off_t *); void https_init(char *); /* progressmeter.c */ -void start_progress_meter(const char *, const char *, off_t, off_t *); +void init_stats(off_t, off_t *); +void start_progress_meter(const char *, const char *); void stop_progress_meter(void); +void finish_stats(void); /* url.c */ int scheme_lookup(const char *); diff --git a/usr.bin/ftp/main.c b/usr.bin/ftp/main.c index bae68f47a53..a528266f8e0 100644 --- a/usr.bin/ftp/main.c +++ b/usr.bin/ftp/main.c @@ -1,4 +1,4 @@ -/* $OpenBSD: main.c,v 1.127 2019/05/15 11:53:22 kmos Exp $ */ +/* $OpenBSD: main.c,v 1.128 2019/05/15 13:42:40 florian Exp $ */ /* * Copyright (c) 2015 Sunil Nimmagadda <sunil@openbsd.org> @@ -336,14 +336,16 @@ child(int sock, int argc, char **argv) } else if ((dst_fp = fdopen(fd, "w")) == NULL) err(1, "%s: fdopen", __func__); + init_stats(sz, &offset); if (progressmeter) { p = basename(url->path); - start_progress_meter(p, title, sz, &offset); + start_progress_meter(p, title); } url_save(url, dst_fp, &offset); if (progressmeter) stop_progress_meter(); + finish_stats(); if (!tostdout) fclose(dst_fp); diff --git a/usr.bin/ftp/progressmeter.c b/usr.bin/ftp/progressmeter.c index 1d0d0cff89c..15bbef752c3 100644 --- a/usr.bin/ftp/progressmeter.c +++ b/usr.bin/ftp/progressmeter.c @@ -1,4 +1,4 @@ -/* $OpenBSD: progressmeter.c,v 1.4 2019/05/14 18:51:07 deraadt Exp $ */ +/* $OpenBSD: progressmeter.c,v 1.5 2019/05/15 13:42:40 florian Exp $ */ /* * Copyright (c) 2015 Sunil Nimmagadda <sunil@openbsd.org> @@ -98,7 +98,7 @@ format_rate(char *buf, int size, off_t bytes) i++; bytes = (bytes + 512) / 1024; } - snprintf(buf, size, "%3lld.%1lld%c%s", + snprintf(buf, size, "%lld.%02lld %c%s", (long long) (bytes + 5) / 100, (long long) (bytes + 5) / 10 % 10, unit[i], @@ -272,7 +272,7 @@ update_progress_meter(int ignore) } void -start_progress_meter(const char *fn, const char *t, off_t filesize, off_t *ctr) +init_stats(off_t filesize, off_t *ctr) { start = last_update = monotime(); start_pos = *ctr; @@ -282,6 +282,14 @@ start_progress_meter(const char *fn, const char *t, off_t filesize, off_t *ctr) counter = ctr; stalled = 0; bytes_per_second = 0; + + if (filesize > 0) + end_pos = filesize; +} + +void +start_progress_meter(const char *fn, const char *t) +{ filename = fn; title = t; @@ -289,10 +297,10 @@ start_progress_meter(const char *fn, const char *t, off_t filesize, off_t *ctr) * Suppress progressmeter if filesize isn't known when * Content-Length header has bogus values. */ - if (filesize <= 0) + + if (end_pos == 0) return; - end_pos = filesize; setscreensize(); refresh_progress_meter(); @@ -304,9 +312,6 @@ start_progress_meter(const char *fn, const char *t, off_t filesize, off_t *ctr) void stop_progress_meter(void) { - char rate_str[32]; - double elapsed; - alarm(0); /* Ensure we complete the progress */ @@ -315,21 +320,27 @@ stop_progress_meter(void) if (end_pos) fprintf(stderr, "\n"); +} + +void +finish_stats(void) +{ + char rate_str[32]; + double elapsed; if (!verbose) return; elapsed = monotime() - start; - if (end_pos == 0) { - if (elapsed != 0) - bytes_per_second = *counter / elapsed; - else - bytes_per_second = *counter; - } + + if (elapsed != 0) + bytes_per_second = *counter / elapsed; + else + bytes_per_second = *counter; format_rate(rate_str, sizeof rate_str, bytes_per_second); - log_info("%lld bytes received in %.2f seconds (%s/s)\n", - (end_pos) ? cur_pos - offset : *counter, elapsed, rate_str); + log_info("%lld byte%s received in %.2f seconds (%s/s)\n", + *counter, *counter != 1 ? "s" : "", elapsed, rate_str); } static void |