progress.c revision 1.3 1 1.3 apb /* $NetBSD: progress.c,v 1.3 2006/11/14 21:01:46 apb 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.3 apb __RCSID("$NetBSD: progress.c,v 1.3 2006/11/14 21:01:46 apb 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.3 apb static int progress_lowlim;
61 1.3 apb static int progress_highlim;
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.3 apb progress_init(void)
73 1.1 christos {
74 1.3 apb progress_setrange(0, 100);
75 1.1 christos }
76 1.1 christos
77 1.3 apb /* Set both low and high limit. */
78 1.1 christos void
79 1.3 apb progress_setrange(int lowlim, int highlim)
80 1.1 christos {
81 1.3 apb progress_lowlim = lowlim;
82 1.3 apb progress_highlim = highlim;
83 1.1 christos }
84 1.1 christos
85 1.3 apb /* Previous high limit becomes new low limit; set new high limit. */
86 1.3 apb void
87 1.3 apb progress_sethighlim(int highlim)
88 1.3 apb {
89 1.3 apb progress_setrange(progress_highlim, highlim);
90 1.3 apb }
91 1.3 apb
92 1.3 apb /*
93 1.3 apb * Display a progress bar, assuming that current/total represents a
94 1.3 apb * percentage in the range [progress_lowlim .. progress_highlim].
95 1.3 apb */
96 1.1 christos void
97 1.1 christos progress_bar(const char *dev, const char *label, off_t current, off_t total)
98 1.1 christos {
99 1.1 christos static int lastpercentage = -1;
100 1.1 christos char buf[256];
101 1.1 christos int len, percentage;
102 1.1 christos int barlength;
103 1.1 christos int i;
104 1.1 christos int lengthextras;
105 1.1 christos
106 1.1 christos #define BAROVERHEAD 10 /* non-* portion of progress bar */
107 1.1 christos
108 1.1 christos /*
109 1.3 apb * stars should contain at least sizeof(buf) - BAROVERHEAD
110 1.1 christos * entries.
111 1.1 christos */
112 1.1 christos static const char stars[] =
113 1.1 christos "*****************************************************************************"
114 1.1 christos "*****************************************************************************"
115 1.1 christos "*****************************************************************************";
116 1.1 christos
117 1.1 christos if (progress_onoff == 0)
118 1.1 christos return;
119 1.1 christos
120 1.1 christos len = 0;
121 1.1 christos lengthextras = strlen(dev) + (label != NULL ? strlen(label) : 0);
122 1.3 apb percentage = progress_lowlim +
123 1.3 apb (current * (progress_highlim - progress_lowlim)) / total;
124 1.1 christos percentage = MAX(percentage, 0);
125 1.1 christos percentage = MIN(percentage, 100);
126 1.1 christos
127 1.1 christos if (percentage == lastpercentage)
128 1.1 christos return;
129 1.1 christos lastpercentage = percentage;
130 1.1 christos
131 1.1 christos len += snprintf(buf + len, BUFLEFT, "%s: ", dev);
132 1.1 christos if (label != NULL)
133 1.1 christos len += snprintf(buf + len, BUFLEFT, "%s ", label);
134 1.1 christos
135 1.1 christos barlength = MIN(sizeof(buf) - 1, ttywidth) - BAROVERHEAD - lengthextras;
136 1.1 christos if (barlength > 0) {
137 1.1 christos i = barlength * percentage / 100;
138 1.1 christos len += snprintf(buf + len, BUFLEFT,
139 1.1 christos "|%.*s%*s| ", i, stars, barlength - i, "");
140 1.1 christos }
141 1.1 christos len += snprintf(buf + len, BUFLEFT, "%3d%%\r", percentage);
142 1.1 christos write(fileno(stdout), buf, len);
143 1.1 christos }
144 1.1 christos
145 1.1 christos void
146 1.1 christos progress_done(void)
147 1.1 christos {
148 1.1 christos char buf[256];
149 1.1 christos int len;
150 1.1 christos
151 1.2 atatat if (progress_onoff == 0)
152 1.2 atatat return;
153 1.2 atatat
154 1.1 christos len = MIN(sizeof(buf) - 2, ttywidth);
155 1.1 christos memset(buf, ' ', len);
156 1.1 christos buf[len] = '\r';
157 1.1 christos buf[len + 1] = '\0';
158 1.1 christos write(fileno(stdout), buf, len + 1);
159 1.1 christos }
160 1.1 christos
161 1.1 christos void
162 1.1 christos progress_ttywidth(int a)
163 1.1 christos {
164 1.1 christos struct winsize winsize;
165 1.1 christos int oerrno = errno;
166 1.1 christos
167 1.1 christos if (ioctl(fileno(stdout), TIOCGWINSZ, &winsize) != -1 &&
168 1.1 christos winsize.ws_col != 0)
169 1.1 christos ttywidth = winsize.ws_col;
170 1.1 christos else
171 1.1 christos ttywidth = 80;
172 1.1 christos errno = oerrno;
173 1.1 christos }
174 1.1 christos
175 1.1 christos #endif /* ! SMALL */
176