1237285Sscottl/* $NetBSD: progressbar.c,v 1.21 2009/04/12 10:18:52 lukem Exp $ */ 2237285Sscottl 3237285Sscottl/*- 4330449Seadler * SPDX-License-Identifier: BSD-2-Clause-NetBSD 5330449Seadler * 6237285Sscottl * Copyright (c) 1997-2009 The NetBSD Foundation, Inc. 7237285Sscottl * All rights reserved. 8237285Sscottl * 9237285Sscottl * This code is derived from software contributed to The NetBSD Foundation 10237285Sscottl * by Luke Mewburn. 11237285Sscottl * 12237285Sscottl * Redistribution and use in source and binary forms, with or without 13237285Sscottl * modification, are permitted provided that the following conditions 14237285Sscottl * are met: 15237285Sscottl * 1. Redistributions of source code must retain the above copyright 16237285Sscottl * notice, this list of conditions and the following disclaimer. 17237285Sscottl * 2. Redistributions in binary form must reproduce the above copyright 18237285Sscottl * notice, this list of conditions and the following disclaimer in the 19237285Sscottl * documentation and/or other materials provided with the distribution. 20237285Sscottl * 21237285Sscottl * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 22237285Sscottl * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 23237285Sscottl * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 24237285Sscottl * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 25237285Sscottl * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 26237285Sscottl * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 27237285Sscottl * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 28237285Sscottl * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 29237285Sscottl * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 30237285Sscottl * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 31237285Sscottl * POSSIBILITY OF SUCH DAMAGE. 32237285Sscottl */ 33237285Sscottl 34237285Sscottl#include <sys/types.h> 35237285Sscottl#include <sys/param.h> 36237285Sscottl#include <sys/ioctl.h> 37237285Sscottl 38237285Sscottl#include <errno.h> 39237285Sscottl#include <stdio.h> 40237285Sscottl#include <stdlib.h> 41237285Sscottl#include <string.h> 42237285Sscottl#include <termios.h> 43237285Sscottl#include <time.h> 44237285Sscottl#include <unistd.h> 45237285Sscottl 46237285Sscottl#include <sys/cdefs.h> 47237285Sscottl__FBSDID("$FreeBSD: stable/11/sbin/camcontrol/progress.c 330449 2018-03-05 07:26:05Z eadler $"); 48237285Sscottl 49237285Sscottl#include "progress.h" 50237285Sscottl 51237285Sscottlstatic const char * const suffixes[] = { 52237285Sscottl "", /* 2^0 (byte) */ 53237285Sscottl "KiB", /* 2^10 Kibibyte */ 54237285Sscottl "MiB", /* 2^20 Mebibyte */ 55237285Sscottl "GiB", /* 2^30 Gibibyte */ 56237285Sscottl "TiB", /* 2^40 Tebibyte */ 57237285Sscottl "PiB", /* 2^50 Pebibyte */ 58237285Sscottl "EiB", /* 2^60 Exbibyte */ 59237285Sscottl}; 60237285Sscottl 61298262Saraujo#define NSUFFIXES nitems(suffixes) 62237285Sscottl#define SECSPERHOUR (60 * 60) 63237285Sscottl#define DEFAULT_TTYWIDTH 80 64237285Sscottl 65237285Sscottl/* initialise progress meter structure */ 66237285Sscottlint 67237285Sscottlprogress_init(progress_t *prog, const char *prefix, uint64_t total) 68237285Sscottl{ 69237285Sscottl struct winsize winsize; 70237285Sscottl int oerrno = errno; 71237285Sscottl 72237285Sscottl (void) memset(prog, 0x0, sizeof(*prog)); 73237285Sscottl prog->size = total; 74237285Sscottl prog->prefix = strdup(prefix); 75237285Sscottl prog->start = time(NULL); 76237285Sscottl if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &winsize) != -1 && 77237285Sscottl winsize.ws_col != 0) { 78237285Sscottl prog->ttywidth = winsize.ws_col; 79237285Sscottl } else { 80237285Sscottl prog->ttywidth = DEFAULT_TTYWIDTH; 81237285Sscottl } 82237285Sscottl errno = oerrno; 83237285Sscottl return 1; 84237285Sscottl} 85237285Sscottl 86237285Sscottl/* update the values in the progress meter */ 87237285Sscottlint 88237285Sscottlprogress_update(progress_t *prog, uint64_t done) 89237285Sscottl{ 90237285Sscottl prog->done = done; 91237285Sscottl prog->percent = (prog->done * 100) / prog->size; 92237285Sscottl prog->now = time(NULL); 93237285Sscottl prog->elapsed = prog->now - prog->start; 94237285Sscottl if (done == 0 || prog->elapsed == 0 || prog->done / prog->elapsed == 0) { 95237285Sscottl prog->eta = 0; 96237285Sscottl } else { 97237285Sscottl prog->eta = prog->size / (prog->done / prog->elapsed) - prog->elapsed; 98237285Sscottl } 99237285Sscottl return 1; 100237285Sscottl} 101237285Sscottl 102237285Sscottl/* update the values in the progress meter */ 103237285Sscottlint 104237285Sscottlprogress_reset_size(progress_t *prog, uint64_t size) 105237285Sscottl{ 106237285Sscottl prog->size = size; 107237285Sscottl return 1; 108237285Sscottl} 109237285Sscottl 110237285Sscottl/* make it look pretty at the end - display done bytes (usually total) */ 111237285Sscottlint 112237285Sscottlprogress_complete(progress_t *prog, uint64_t done) 113237285Sscottl{ 114237285Sscottl progress_update(prog, done); 115237285Sscottl progress_draw(prog); 116237285Sscottl printf("\n"); 117237285Sscottl return 1; 118237285Sscottl} 119237285Sscottl 120237285Sscottl/* draw the progress meter */ 121237285Sscottlint 122237285Sscottlprogress_draw(progress_t *prog) 123237285Sscottl{ 124237285Sscottl#define BAROVERHEAD 45 /* non `*' portion of progress bar */ 125237285Sscottl /* 126237285Sscottl * stars should contain at least 127237285Sscottl * sizeof(buf) - BAROVERHEAD entries 128237285Sscottl */ 129286965Sken#define MIN_BAR_LEN 10 130237285Sscottl static const char stars[] = 131237285Sscottl"*****************************************************************************" 132237285Sscottl"*****************************************************************************" 133237285Sscottl"*****************************************************************************"; 134237285Sscottl unsigned bytesabbrev; 135237285Sscottl unsigned bpsabbrev; 136237285Sscottl int64_t secs; 137237285Sscottl uint64_t bytespersec; 138237285Sscottl uint64_t abbrevsize; 139237285Sscottl int64_t secsleft; 140286965Sken ssize_t barlength; 141237285Sscottl size_t starc; 142237285Sscottl char hours[12]; 143237285Sscottl char buf[256]; 144237285Sscottl int len; 145286965Sken int prefix_len; 146237285Sscottl 147286965Sken prefix_len = strlen(prog->prefix); 148286965Sken barlength = MIN(sizeof(buf) - 1, (unsigned)prog->ttywidth) - 149286965Sken BAROVERHEAD - prefix_len; 150286965Sken if (barlength < MIN_BAR_LEN) { 151286965Sken int tmp_prefix_len; 152286965Sken /* 153286965Sken * In this case the TTY width is too small or the prefix is 154286965Sken * too large for a progress bar. We'll try decreasing the 155286965Sken * prefix length. 156286965Sken */ 157286965Sken barlength = MIN_BAR_LEN; 158286965Sken tmp_prefix_len = MIN(sizeof(buf) - 1,(unsigned)prog->ttywidth) - 159286965Sken BAROVERHEAD - MIN_BAR_LEN; 160286965Sken if (tmp_prefix_len > 0) 161286965Sken prefix_len = tmp_prefix_len; 162286965Sken else 163286965Sken prefix_len = 0; 164286965Sken } 165237285Sscottl starc = (barlength * prog->percent) / 100; 166237285Sscottl abbrevsize = prog->done; 167237285Sscottl for (bytesabbrev = 0; abbrevsize >= 100000 && bytesabbrev < NSUFFIXES; bytesabbrev++) { 168237285Sscottl abbrevsize >>= 10; 169237285Sscottl } 170237285Sscottl if (bytesabbrev == NSUFFIXES) { 171237285Sscottl bytesabbrev--; 172237285Sscottl } 173237285Sscottl bytespersec = 0; 174237285Sscottl if (prog->done > 0) { 175237285Sscottl bytespersec = prog->done; 176237285Sscottl if (prog->elapsed > 0) { 177237285Sscottl bytespersec /= prog->elapsed; 178237285Sscottl } 179237285Sscottl } 180237285Sscottl for (bpsabbrev = 1; bytespersec >= 1024000 && bpsabbrev < NSUFFIXES; bpsabbrev++) { 181237285Sscottl bytespersec >>= 10; 182237285Sscottl } 183237285Sscottl if (prog->done == 0 || prog->elapsed <= 0 || prog->done > prog->size) { 184237285Sscottl secsleft = 0; 185237285Sscottl } else { 186237285Sscottl secsleft = prog->eta; 187237285Sscottl } 188237285Sscottl if ((secs = secsleft / SECSPERHOUR) > 0) { 189237285Sscottl (void) snprintf(hours, sizeof(hours), "%2lld:", (long long)secs); 190237285Sscottl } else { 191237285Sscottl (void) snprintf(hours, sizeof(hours), " "); 192237285Sscottl } 193237285Sscottl secs = secsleft % SECSPERHOUR; 194237285Sscottl len = snprintf(buf, sizeof(buf), 195286965Sken "\r%.*s %3lld%% |%.*s%*s| %5lld %-3s %3lld.%02d %.2sB/s %s%02d:%02d ETA", 196286965Sken prefix_len, (prog->prefix) ? prog->prefix : "", 197237285Sscottl (long long)prog->percent, 198237285Sscottl (int)starc, stars, (int)(barlength - starc), "", 199237285Sscottl (long long)abbrevsize, 200237285Sscottl suffixes[bytesabbrev], 201237285Sscottl (long long)(bytespersec / 1024), 202237285Sscottl (int)((bytespersec % 1024) * 100 / 1024), 203237285Sscottl suffixes[bpsabbrev], 204237285Sscottl hours, 205237285Sscottl (int)secs / 60, (int)secs % 60); 206237285Sscottl return (int)write(STDOUT_FILENO, buf, len); 207237285Sscottl} 208