]> andersk Git - openssh.git/blame - progressmeter.c
- (djm) Bug #501: gai_strerror should return char*;
[openssh.git] / progressmeter.c
CommitLineData
c9c38b73 1/*
2 * Copyright (c) 1999 Theo de Raadt. All rights reserved.
3 * Copyright (c) 1999 Aaron Campbell. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26/*
27 * Parts from:
28 *
29 * Copyright (c) 1983, 1990, 1992, 1993, 1995
30 * The Regents of the University of California. All rights reserved.
31 *
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
34 * are met:
35 * 1. Redistributions of source code must retain the above copyright
36 * notice, this list of conditions and the following disclaimer.
37 * 2. Redistributions in binary form must reproduce the above copyright
38 * notice, this list of conditions and the following disclaimer in the
39 * documentation and/or other materials provided with the distribution.
40 * 3. All advertising materials mentioning features or use of this software
41 * must display the following acknowledgement:
42 * This product includes software developed by the University of
43 * California, Berkeley and its contributors.
44 * 4. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
47 *
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 * SUCH DAMAGE.
59 *
60 */
61
62#include "includes.h"
b7fd001f 63RCSID("$OpenBSD: progressmeter.c,v 1.2 2003/01/12 16:57:02 markus Exp $");
c9c38b73 64
65#include <libgen.h>
66
67#include "atomicio.h"
b7fd001f 68#include "progressmeter.h"
c9c38b73 69
70/* Number of seconds before xfer considered "stalled". */
71#define STALLTIME 5
72/* alarm() interval for updating progress meter. */
73#define PROGRESSTIME 1
74
75/* Signal handler used for updating the progress meter. */
76static void update_progress_meter(int);
77
78/* Returns non-zero if we are the foreground process. */
79static int foregroundproc(void);
80
81/* Returns width of the terminal (for progress meter calculations). */
82static int get_tty_width(void);
83
84/* Visual statistics about files as they are transferred. */
b7fd001f 85static void draw_progress_meter(void);
c9c38b73 86
87/* Time a transfer started. */
88static struct timeval start;
89
90/* Number of bytes of current file transferred so far. */
91static volatile off_t *statbytes;
92
93/* Total size of current file. */
94static off_t totalbytes;
95
96/* Name of current file being transferred. */
97static char *curfile;
98
99/* Time of last update. */
100static struct timeval lastupdate;
101
102/* Size at the time of the last update. */
103static off_t lastsize;
104
105void
106start_progress_meter(char *file, off_t filesize, off_t *counter)
107{
108 if ((curfile = basename(file)) == NULL)
109 curfile = file;
110
111 totalbytes = filesize;
112 statbytes = counter;
113 (void) gettimeofday(&start, (struct timezone *) 0);
114 lastupdate = start;
115 lastsize = 0;
116
117 draw_progress_meter();
118 signal(SIGALRM, update_progress_meter);
119 alarm(PROGRESSTIME);
120}
121
122void
123stop_progress_meter()
124{
125 alarm(0);
126 draw_progress_meter();
127 atomicio(write, fileno(stdout), "\n", 1);
128}
129
130static void
131update_progress_meter(int ignore)
132{
133 int save_errno = errno;
134
135 draw_progress_meter();
136 signal(SIGALRM, update_progress_meter);
137 alarm(PROGRESSTIME);
138 errno = save_errno;
139}
140
141static int
142foregroundproc(void)
143{
144 static pid_t pgrp = -1;
145 int ctty_pgrp;
146
147 if (pgrp == -1)
148 pgrp = getpgrp();
149
150 return ((ioctl(STDOUT_FILENO, TIOCGPGRP, &ctty_pgrp) != -1 &&
151 ctty_pgrp == pgrp));
152}
153
154static void
155draw_progress_meter()
156{
157 static const char spaces[] = " "
158 " "
159 " "
160 " "
161 " "
162 " ";
163 static const char prefixes[] = " KMGTP";
164 struct timeval now, td, wait;
165 off_t cursize, abbrevsize, bytespersec;
166 double elapsed;
167 int ratio, remaining, i, ai, bi, nspaces;
168 char buf[512];
169
170 if (foregroundproc() == 0)
171 return;
172
173 (void) gettimeofday(&now, (struct timezone *) 0);
174 cursize = *statbytes;
175 if (totalbytes != 0) {
176 ratio = 100.0 * cursize / totalbytes;
177 ratio = MAX(ratio, 0);
178 ratio = MIN(ratio, 100);
179 } else
180 ratio = 100;
181
182 abbrevsize = cursize;
183 for (ai = 0; abbrevsize >= 10000 && ai < sizeof(prefixes); ai++)
184 abbrevsize >>= 10;
185
186 timersub(&now, &lastupdate, &wait);
187 if (cursize > lastsize) {
188 lastupdate = now;
189 lastsize = cursize;
190 wait.tv_sec = 0;
191 }
192 timersub(&now, &start, &td);
193 elapsed = td.tv_sec + (td.tv_usec / 1000000.0);
194
195 bytespersec = 0;
196 if (cursize > 0) {
197 bytespersec = cursize;
198 if (elapsed > 0.0)
199 bytespersec /= elapsed;
200 }
201 for (bi = 1; bytespersec >= 1024000 && bi < sizeof(prefixes); bi++)
202 bytespersec >>= 10;
203
204 nspaces = MIN(get_tty_width() - 79, sizeof(spaces) - 1);
205
115dc20f 206#ifdef HAVE_LONG_LONG_INT
c9c38b73 207 snprintf(buf, sizeof(buf),
208 "\r%-45.45s%.*s%3d%% %4lld%c%c %3lld.%01d%cB/s",
209 curfile,
210 nspaces,
211 spaces,
212 ratio,
213 (long long)abbrevsize,
214 prefixes[ai],
215 ai == 0 ? ' ' : 'B',
216 (long long)(bytespersec / 1024),
217 (int)((bytespersec % 1024) * 10 / 1024),
218 prefixes[bi]
219 );
115dc20f 220#else
221 /* XXX: Handle integer overflow? */
222 snprintf(buf, sizeof(buf),
223 "\r%-45.45s%.*s%3d%% %4lu%c%c %3lu.%01d%cB/s",
224 curfile,
225 nspaces,
226 spaces,
227 ratio,
228 (u_long)abbrevsize,
229 prefixes[ai],
230 ai == 0 ? ' ' : 'B',
231 (u_long)(bytespersec / 1024),
232 (int)((bytespersec % 1024) * 10 / 1024),
233 prefixes[bi]
234 );
235#endif
c9c38b73 236
237 if (cursize <= 0 || elapsed <= 0.0 || cursize > totalbytes) {
238 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
239 " --:-- ETA");
240 } else if (wait.tv_sec >= STALLTIME) {
241 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
242 " - stalled -");
243 } else {
244 if (cursize != totalbytes)
245 remaining = (int)(totalbytes / (cursize / elapsed) -
246 elapsed);
247 else
248 remaining = elapsed;
249
250 i = remaining / 3600;
251 if (i)
252 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
253 "%2d:", i);
254 else
255 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
256 " ");
257 i = remaining % 3600;
258 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
259 "%02d:%02d%s", i / 60, i % 60,
260 (cursize != totalbytes) ? " ETA" : " ");
261 }
262 atomicio(write, fileno(stdout), buf, strlen(buf));
263}
264
265static int
266get_tty_width(void)
267{
268 struct winsize winsize;
269
270 if (ioctl(fileno(stdout), TIOCGWINSZ, &winsize) != -1)
271 return (winsize.ws_col ? winsize.ws_col : 80);
272 else
273 return (80);
274}
This page took 0.116449 seconds and 5 git commands to generate.