summaryrefslogtreecommitdiffstats
path: root/sbin/camcontrol
diff options
context:
space:
mode:
authorscottl <scottl@FreeBSD.org>2012-06-20 04:11:34 +0000
committerscottl <scottl@FreeBSD.org>2012-06-20 04:11:34 +0000
commit8cc89048d427827afa803b515443badf6afd5387 (patch)
tree0b3a8dbc09fe387e91e3333703a5385e85adc9f3 /sbin/camcontrol
parent592e847dfeee3afbd0174cd2a749345b1d68a951 (diff)
downloadFreeBSD-src-8cc89048d427827afa803b515443badf6afd5387.zip
FreeBSD-src-8cc89048d427827afa803b515443badf6afd5387.tar.gz
Add progress.c and progress.h, missed in the previous commit to camcontrol.
Submitted by: Garrett Cooper Obtained from: Netflix, Inc.
Diffstat (limited to 'sbin/camcontrol')
-rw-r--r--sbin/camcontrol/progress.c186
-rw-r--r--sbin/camcontrol/progress.h60
2 files changed, 246 insertions, 0 deletions
diff --git a/sbin/camcontrol/progress.c b/sbin/camcontrol/progress.c
new file mode 100644
index 0000000..dc6109f
--- /dev/null
+++ b/sbin/camcontrol/progress.c
@@ -0,0 +1,186 @@
+/* $NetBSD: progressbar.c,v 1.21 2009/04/12 10:18:52 lukem Exp $ */
+
+/*-
+ * Copyright (c) 1997-2009 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Luke Mewburn.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <sys/types.h>
+#include <sys/param.h>
+#include <sys/ioctl.h>
+
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <termios.h>
+#include <time.h>
+#include <unistd.h>
+
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+#include "progress.h"
+
+static const char * const suffixes[] = {
+ "", /* 2^0 (byte) */
+ "KiB", /* 2^10 Kibibyte */
+ "MiB", /* 2^20 Mebibyte */
+ "GiB", /* 2^30 Gibibyte */
+ "TiB", /* 2^40 Tebibyte */
+ "PiB", /* 2^50 Pebibyte */
+ "EiB", /* 2^60 Exbibyte */
+};
+
+#define NSUFFIXES (sizeof(suffixes) / sizeof(suffixes[0]))
+#define SECSPERHOUR (60 * 60)
+#define DEFAULT_TTYWIDTH 80
+
+/* initialise progress meter structure */
+int
+progress_init(progress_t *prog, const char *prefix, uint64_t total)
+{
+ struct winsize winsize;
+ int oerrno = errno;
+
+ (void) memset(prog, 0x0, sizeof(*prog));
+ prog->size = total;
+ prog->prefix = strdup(prefix);
+ prog->start = time(NULL);
+ if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &winsize) != -1 &&
+ winsize.ws_col != 0) {
+ prog->ttywidth = winsize.ws_col;
+ } else {
+ prog->ttywidth = DEFAULT_TTYWIDTH;
+ }
+ errno = oerrno;
+ return 1;
+}
+
+/* update the values in the progress meter */
+int
+progress_update(progress_t *prog, uint64_t done)
+{
+ prog->done = done;
+ prog->percent = (prog->done * 100) / prog->size;
+ prog->now = time(NULL);
+ prog->elapsed = prog->now - prog->start;
+ if (done == 0 || prog->elapsed == 0 || prog->done / prog->elapsed == 0) {
+ prog->eta = 0;
+ } else {
+ prog->eta = prog->size / (prog->done / prog->elapsed) - prog->elapsed;
+ }
+ return 1;
+}
+
+/* update the values in the progress meter */
+int
+progress_reset_size(progress_t *prog, uint64_t size)
+{
+ prog->size = size;
+ return 1;
+}
+
+/* make it look pretty at the end - display done bytes (usually total) */
+int
+progress_complete(progress_t *prog, uint64_t done)
+{
+ progress_update(prog, done);
+ progress_draw(prog);
+ printf("\n");
+ return 1;
+}
+
+/* draw the progress meter */
+int
+progress_draw(progress_t *prog)
+{
+#define BAROVERHEAD 45 /* non `*' portion of progress bar */
+ /*
+ * stars should contain at least
+ * sizeof(buf) - BAROVERHEAD entries
+ */
+ static const char stars[] =
+"*****************************************************************************"
+"*****************************************************************************"
+"*****************************************************************************";
+ unsigned bytesabbrev;
+ unsigned bpsabbrev;
+ int64_t secs;
+ uint64_t bytespersec;
+ uint64_t abbrevsize;
+ int64_t secsleft;
+ size_t barlength;
+ size_t starc;
+ char hours[12];
+ char buf[256];
+ int len;
+
+ barlength = MIN(sizeof(buf) - 1, (unsigned)prog->ttywidth) - BAROVERHEAD - strlen(prog->prefix);
+ starc = (barlength * prog->percent) / 100;
+ abbrevsize = prog->done;
+ for (bytesabbrev = 0; abbrevsize >= 100000 && bytesabbrev < NSUFFIXES; bytesabbrev++) {
+ abbrevsize >>= 10;
+ }
+ if (bytesabbrev == NSUFFIXES) {
+ bytesabbrev--;
+ }
+ bytespersec = 0;
+ if (prog->done > 0) {
+ bytespersec = prog->done;
+ if (prog->elapsed > 0) {
+ bytespersec /= prog->elapsed;
+ }
+ }
+ for (bpsabbrev = 1; bytespersec >= 1024000 && bpsabbrev < NSUFFIXES; bpsabbrev++) {
+ bytespersec >>= 10;
+ }
+ if (prog->done == 0 || prog->elapsed <= 0 || prog->done > prog->size) {
+ secsleft = 0;
+ } else {
+ secsleft = prog->eta;
+ }
+ if ((secs = secsleft / SECSPERHOUR) > 0) {
+ (void) snprintf(hours, sizeof(hours), "%2lld:", (long long)secs);
+ } else {
+ (void) snprintf(hours, sizeof(hours), " ");
+ }
+ secs = secsleft % SECSPERHOUR;
+ len = snprintf(buf, sizeof(buf),
+ "\r%s %3lld%% |%.*s%*s| %5lld %-3s %3lld.%02d %.2sB/s %s%02d:%02d ETA",
+ (prog->prefix) ? prog->prefix : "",
+ (long long)prog->percent,
+ (int)starc, stars, (int)(barlength - starc), "",
+ (long long)abbrevsize,
+ suffixes[bytesabbrev],
+ (long long)(bytespersec / 1024),
+ (int)((bytespersec % 1024) * 100 / 1024),
+ suffixes[bpsabbrev],
+ hours,
+ (int)secs / 60, (int)secs % 60);
+ return (int)write(STDOUT_FILENO, buf, len);
+}
diff --git a/sbin/camcontrol/progress.h b/sbin/camcontrol/progress.h
new file mode 100644
index 0000000..a457c32
--- /dev/null
+++ b/sbin/camcontrol/progress.h
@@ -0,0 +1,60 @@
+/* $NetBSD: progressbar.c,v 1.21 2009/04/12 10:18:52 lukem Exp $ */
+
+/*-
+ * Copyright (c) 1997-2012 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Luke Mewburn.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ * $FreeBSD$
+ */
+
+#ifndef PROGRESS_H_
+#define PROGRESS_H_ 20100228
+
+#include <sys/types.h>
+
+#include <inttypes.h>
+
+/* structure used to display a progress meter */
+typedef struct progress_t {
+ char *prefix; /* any prefix explanation */
+ uint64_t size; /* total of bytes/units to be counted */
+ uint64_t done; /* number of units counted to date */
+ uint64_t percent; /* cache the percentage complete */
+ time_t start; /* time we started this */
+ time_t now; /* time now */
+ time_t eta; /* estimated # of secs until completion */
+ int64_t elapsed; /* cached # of elapsed seconds */
+ int32_t ttywidth; /* width of tty in columns */
+} progress_t;
+
+int progress_init(progress_t */*meter*/, const char */*prefix*/, uint64_t /*size*/);
+int progress_update(progress_t */*meter*/, uint64_t /*done*/);
+int progress_draw(progress_t */*meter*/);
+int progress_reset_size(progress_t */*meter*/, uint64_t /*size*/);
+int progress_complete(progress_t */*meter*/, uint64_t /*done*/);
+
+#endif
OpenPOWER on IntegriCloud