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