Home | History | Annotate | Line # | Download | only in fsck
progress.c revision 1.3.18.1
      1  1.3.18.1       mjf /*	$NetBSD: progress.c,v 1.3.18.1 2008/06/02 13:21:21 mjf Exp $	*/
      2       1.1  christos 
      3       1.1  christos /*-
      4       1.1  christos  * Copyright (c) 1997-2004 The NetBSD Foundation, Inc.
      5       1.1  christos  * All rights reserved.
      6       1.1  christos  *
      7       1.1  christos  * This code is derived from software contributed to The NetBSD Foundation
      8       1.1  christos  * by Luke Mewburn; by Chris Gilbert; and by Jason R. Thorpe.
      9       1.1  christos  *
     10       1.1  christos  * Redistribution and use in source and binary forms, with or without
     11       1.1  christos  * modification, are permitted provided that the following conditions
     12       1.1  christos  * are met:
     13       1.1  christos  * 1. Redistributions of source code must retain the above copyright
     14       1.1  christos  *    notice, this list of conditions and the following disclaimer.
     15       1.1  christos  * 2. Redistributions in binary form must reproduce the above copyright
     16       1.1  christos  *    notice, this list of conditions and the following disclaimer in the
     17       1.1  christos  *    documentation and/or other materials provided with the distribution.
     18       1.1  christos  *
     19       1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20       1.1  christos  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21       1.1  christos  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22       1.1  christos  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23       1.1  christos  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24       1.1  christos  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25       1.1  christos  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26       1.1  christos  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27       1.1  christos  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28       1.1  christos  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29       1.1  christos  * POSSIBILITY OF SUCH DAMAGE.
     30       1.1  christos  */
     31       1.1  christos 
     32       1.1  christos #ifndef SMALL
     33       1.1  christos #include <sys/cdefs.h>
     34  1.3.18.1       mjf __RCSID("$NetBSD: progress.c,v 1.3.18.1 2008/06/02 13:21:21 mjf Exp $");
     35       1.1  christos 
     36       1.1  christos /*
     37       1.1  christos  * File system independent fsck progress bar routines.
     38       1.1  christos  */
     39       1.1  christos 
     40       1.1  christos #include <sys/param.h>
     41       1.1  christos #include <sys/tty.h>
     42       1.1  christos #include <sys/ioctl.h>
     43       1.1  christos #include <errno.h>
     44       1.1  christos #include <stdio.h>
     45       1.1  christos #include <string.h>
     46       1.1  christos #include <unistd.h>
     47       1.1  christos 
     48       1.1  christos #include "progress.h"
     49       1.1  christos 
     50       1.1  christos static int	ttywidth = 80;
     51       1.1  christos 
     52       1.1  christos static int	progress_onoff;
     53       1.3       apb static int	progress_lowlim;
     54       1.3       apb static int	progress_highlim;
     55       1.1  christos 
     56       1.1  christos #define	BUFLEFT		(sizeof(buf) - len)
     57       1.1  christos 
     58       1.1  christos void
     59       1.1  christos progress_switch(int onoff)
     60       1.1  christos {
     61       1.1  christos 	progress_onoff = onoff;
     62       1.1  christos }
     63       1.1  christos 
     64       1.1  christos void
     65       1.3       apb progress_init(void)
     66       1.1  christos {
     67       1.3       apb 	progress_setrange(0, 100);
     68       1.1  christos }
     69       1.1  christos 
     70       1.3       apb /* Set both low and high limit. */
     71       1.1  christos void
     72       1.3       apb progress_setrange(int lowlim, int highlim)
     73       1.1  christos {
     74       1.3       apb 	progress_lowlim = lowlim;
     75       1.3       apb 	progress_highlim = highlim;
     76       1.1  christos }
     77       1.1  christos 
     78       1.3       apb /* Previous high limit becomes new low limit; set new high limit. */
     79       1.3       apb void
     80       1.3       apb progress_sethighlim(int highlim)
     81       1.3       apb {
     82       1.3       apb 	progress_setrange(progress_highlim, highlim);
     83       1.3       apb }
     84       1.3       apb 
     85       1.3       apb /*
     86       1.3       apb  * Display a progress bar, assuming that current/total represents a
     87       1.3       apb  * percentage in the range [progress_lowlim .. progress_highlim].
     88       1.3       apb  */
     89       1.1  christos void
     90       1.1  christos progress_bar(const char *dev, const char *label, off_t current, off_t total)
     91       1.1  christos {
     92       1.1  christos 	static int lastpercentage = -1;
     93       1.1  christos 	char buf[256];
     94       1.1  christos 	int len, percentage;
     95       1.1  christos 	int barlength;
     96       1.1  christos 	int i;
     97       1.1  christos 	int lengthextras;
     98       1.1  christos 
     99       1.1  christos #define	BAROVERHEAD	10	/* non-* portion of progress bar */
    100       1.1  christos 
    101       1.1  christos 	/*
    102       1.3       apb 	 * stars should contain at least sizeof(buf) - BAROVERHEAD
    103       1.1  christos 	 * entries.
    104       1.1  christos 	 */
    105       1.1  christos 	static const char stars[] =
    106       1.1  christos "*****************************************************************************"
    107       1.1  christos "*****************************************************************************"
    108       1.1  christos "*****************************************************************************";
    109       1.1  christos 
    110       1.1  christos 	if (progress_onoff == 0)
    111       1.1  christos 		return;
    112       1.1  christos 
    113       1.1  christos 	len = 0;
    114       1.1  christos 	lengthextras = strlen(dev) + (label != NULL ? strlen(label) : 0);
    115       1.3       apb 	percentage = progress_lowlim +
    116       1.3       apb 		(current * (progress_highlim - progress_lowlim)) / total;
    117       1.1  christos 	percentage = MAX(percentage, 0);
    118       1.1  christos 	percentage = MIN(percentage, 100);
    119       1.1  christos 
    120       1.1  christos 	if (percentage == lastpercentage)
    121       1.1  christos 		return;
    122       1.1  christos 	lastpercentage = percentage;
    123       1.1  christos 
    124       1.1  christos 	len += snprintf(buf + len, BUFLEFT, "%s: ", dev);
    125       1.1  christos 	if (label != NULL)
    126       1.1  christos 		len += snprintf(buf + len, BUFLEFT, "%s ", label);
    127       1.1  christos 
    128       1.1  christos 	barlength = MIN(sizeof(buf) - 1, ttywidth) - BAROVERHEAD - lengthextras;
    129       1.1  christos 	if (barlength > 0) {
    130       1.1  christos 		i = barlength * percentage / 100;
    131       1.1  christos 		len += snprintf(buf + len, BUFLEFT,
    132       1.1  christos 		    "|%.*s%*s| ", i, stars, barlength - i, "");
    133       1.1  christos 	}
    134       1.1  christos 	len += snprintf(buf + len, BUFLEFT, "%3d%%\r", percentage);
    135       1.1  christos 	write(fileno(stdout), buf, len);
    136       1.1  christos }
    137       1.1  christos 
    138       1.1  christos void
    139       1.1  christos progress_done(void)
    140       1.1  christos {
    141       1.1  christos 	char buf[256];
    142       1.1  christos 	int len;
    143       1.1  christos 
    144       1.2    atatat 	if (progress_onoff == 0)
    145       1.2    atatat 		return;
    146       1.2    atatat 
    147       1.1  christos 	len = MIN(sizeof(buf) - 2, ttywidth);
    148       1.1  christos 	memset(buf, ' ', len);
    149       1.1  christos 	buf[len] = '\r';
    150       1.1  christos 	buf[len + 1] = '\0';
    151       1.1  christos 	write(fileno(stdout), buf, len + 1);
    152       1.1  christos }
    153       1.1  christos 
    154       1.1  christos void
    155       1.1  christos progress_ttywidth(int a)
    156       1.1  christos {
    157       1.1  christos 	struct winsize winsize;
    158       1.1  christos 	int oerrno = errno;
    159       1.1  christos 
    160       1.1  christos 	if (ioctl(fileno(stdout), TIOCGWINSZ, &winsize) != -1 &&
    161       1.1  christos 	    winsize.ws_col != 0)
    162       1.1  christos 	    	ttywidth = winsize.ws_col;
    163       1.1  christos 	else
    164       1.1  christos 		ttywidth = 80;
    165       1.1  christos 	errno = oerrno;
    166       1.1  christos }
    167       1.1  christos 
    168       1.1  christos #endif /* ! SMALL */
    169