Author: scottl
Date: Wed Jun 20 04:11:34 2012
New Revision: 237285
URL: http://svn.freebsd.org/changeset/base/237285

Log:
  Add progress.c and progress.h, missed in the previous commit to camcontrol.
  
  Submitted by:   Garrett Cooper
  Obtained from:  Netflix, Inc.

Added:
  head/sbin/camcontrol/progress.c   (contents, props changed)
  head/sbin/camcontrol/progress.h   (contents, props changed)

Added: head/sbin/camcontrol/progress.c
==============================================================================
--- /dev/null   00:00:00 1970   (empty, because file is newly added)
+++ head/sbin/camcontrol/progress.c     Wed Jun 20 04:11:34 2012        
(r237285)
@@ -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);
+}

Added: head/sbin/camcontrol/progress.h
==============================================================================
--- /dev/null   00:00:00 1970   (empty, because file is newly added)
+++ head/sbin/camcontrol/progress.h     Wed Jun 20 04:11:34 2012        
(r237285)
@@ -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
_______________________________________________
svn-src-head@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/svn-src-head
To unsubscribe, send any mail to "svn-src-head-unsubscr...@freebsd.org"

Reply via email to