Home | History | Annotate | Line # | Download | only in fsck
progress.c revision 1.2
      1  1.2    atatat /*	$NetBSD: progress.c,v 1.2 2005/05/10 00:39:04 atatat 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  * 3. All advertising materials mentioning features or use of this software
     19  1.1  christos  *    must display the following acknowledgement:
     20  1.1  christos  *	This product includes software developed by the NetBSD
     21  1.1  christos  *	Foundation, Inc. and its contributors.
     22  1.1  christos  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  1.1  christos  *    contributors may be used to endorse or promote products derived
     24  1.1  christos  *    from this software without specific prior written permission.
     25  1.1  christos  *
     26  1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  1.1  christos  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  1.1  christos  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  1.1  christos  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  1.1  christos  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  1.1  christos  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  1.1  christos  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  1.1  christos  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  1.1  christos  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  1.1  christos  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  1.1  christos  * POSSIBILITY OF SUCH DAMAGE.
     37  1.1  christos  */
     38  1.1  christos 
     39  1.1  christos #ifndef SMALL
     40  1.1  christos #include <sys/cdefs.h>
     41  1.2    atatat __RCSID("$NetBSD: progress.c,v 1.2 2005/05/10 00:39:04 atatat Exp $");
     42  1.1  christos 
     43  1.1  christos /*
     44  1.1  christos  * File system independent fsck progress bar routines.
     45  1.1  christos  */
     46  1.1  christos 
     47  1.1  christos #include <sys/param.h>
     48  1.1  christos #include <sys/tty.h>
     49  1.1  christos #include <sys/ioctl.h>
     50  1.1  christos #include <errno.h>
     51  1.1  christos #include <stdio.h>
     52  1.1  christos #include <string.h>
     53  1.1  christos #include <unistd.h>
     54  1.1  christos 
     55  1.1  christos #include "progress.h"
     56  1.1  christos 
     57  1.1  christos static int	ttywidth = 80;
     58  1.1  christos 
     59  1.1  christos static int	progress_onoff;
     60  1.1  christos static off_t	progress_current;
     61  1.1  christos static off_t	progress_total;
     62  1.1  christos 
     63  1.1  christos #define	BUFLEFT		(sizeof(buf) - len)
     64  1.1  christos 
     65  1.1  christos void
     66  1.1  christos progress_switch(int onoff)
     67  1.1  christos {
     68  1.1  christos 	progress_onoff = onoff;
     69  1.1  christos }
     70  1.1  christos 
     71  1.1  christos void
     72  1.1  christos progress_init(off_t total)
     73  1.1  christos {
     74  1.1  christos 	progress_current = 0;
     75  1.1  christos 	progress_total = total;
     76  1.1  christos }
     77  1.1  christos 
     78  1.1  christos void
     79  1.1  christos progress_add(off_t amt)
     80  1.1  christos {
     81  1.1  christos 	progress_current += amt;
     82  1.1  christos }
     83  1.1  christos 
     84  1.1  christos void
     85  1.1  christos progress_bar(const char *dev, const char *label, off_t current, off_t total)
     86  1.1  christos {
     87  1.1  christos 	static int lastpercentage = -1;
     88  1.1  christos 	char buf[256];
     89  1.1  christos 	int len, percentage;
     90  1.1  christos 	int barlength;
     91  1.1  christos 	int i;
     92  1.1  christos 	int lengthextras;
     93  1.1  christos 
     94  1.1  christos #define	BAROVERHEAD	10	/* non-* portion of progress bar */
     95  1.1  christos 
     96  1.1  christos 	/*
     97  1.1  christos 	 * starts should contain at least sizeof(buf) - BAROVERHEAD
     98  1.1  christos 	 * entries.
     99  1.1  christos 	 */
    100  1.1  christos 	static const char stars[] =
    101  1.1  christos "*****************************************************************************"
    102  1.1  christos "*****************************************************************************"
    103  1.1  christos "*****************************************************************************";
    104  1.1  christos 
    105  1.1  christos 	if (progress_onoff == 0)
    106  1.1  christos 		return;
    107  1.1  christos 
    108  1.1  christos 	current += progress_current;
    109  1.1  christos 	total += progress_total;
    110  1.1  christos 
    111  1.1  christos 	len = 0;
    112  1.1  christos 	lengthextras = strlen(dev) + (label != NULL ? strlen(label) : 0);
    113  1.1  christos 	percentage = (current * 100) / total;
    114  1.1  christos 	percentage = MAX(percentage, 0);
    115  1.1  christos 	percentage = MIN(percentage, 100);
    116  1.1  christos 
    117  1.1  christos 	if (percentage == lastpercentage)
    118  1.1  christos 		return;
    119  1.1  christos 	lastpercentage = percentage;
    120  1.1  christos 
    121  1.1  christos 	len += snprintf(buf + len, BUFLEFT, "%s: ", dev);
    122  1.1  christos 	if (label != NULL)
    123  1.1  christos 		len += snprintf(buf + len, BUFLEFT, "%s ", label);
    124  1.1  christos 
    125  1.1  christos 	barlength = MIN(sizeof(buf) - 1, ttywidth) - BAROVERHEAD - lengthextras;
    126  1.1  christos 	if (barlength > 0) {
    127  1.1  christos 		i = barlength * percentage / 100;
    128  1.1  christos 		len += snprintf(buf + len, BUFLEFT,
    129  1.1  christos 		    "|%.*s%*s| ", i, stars, barlength - i, "");
    130  1.1  christos 	}
    131  1.1  christos 	len += snprintf(buf + len, BUFLEFT, "%3d%%\r", percentage);
    132  1.1  christos 	write(fileno(stdout), buf, len);
    133  1.1  christos }
    134  1.1  christos 
    135  1.1  christos void
    136  1.1  christos progress_done(void)
    137  1.1  christos {
    138  1.1  christos 	char buf[256];
    139  1.1  christos 	int len;
    140  1.1  christos 
    141  1.2    atatat 	if (progress_onoff == 0)
    142  1.2    atatat 		return;
    143  1.2    atatat 
    144  1.1  christos 	len = MIN(sizeof(buf) - 2, ttywidth);
    145  1.1  christos 	memset(buf, ' ', len);
    146  1.1  christos 	buf[len] = '\r';
    147  1.1  christos 	buf[len + 1] = '\0';
    148  1.1  christos 	write(fileno(stdout), buf, len + 1);
    149  1.1  christos }
    150  1.1  christos 
    151  1.1  christos void
    152  1.1  christos progress_ttywidth(int a)
    153  1.1  christos {
    154  1.1  christos 	struct winsize winsize;
    155  1.1  christos 	int oerrno = errno;
    156  1.1  christos 
    157  1.1  christos 	if (ioctl(fileno(stdout), TIOCGWINSZ, &winsize) != -1 &&
    158  1.1  christos 	    winsize.ws_col != 0)
    159  1.1  christos 	    	ttywidth = winsize.ws_col;
    160  1.1  christos 	else
    161  1.1  christos 		ttywidth = 80;
    162  1.1  christos 	errno = oerrno;
    163  1.1  christos }
    164  1.1  christos 
    165  1.1  christos #endif /* ! SMALL */
    166