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