util.c revision 1.2 1 1.2 thorpej /* $NetBSD: util.c,v 1.2 1997/01/30 03:36:26 thorpej Exp $ */
2 1.1 lukem
3 1.1 lukem /*
4 1.1 lukem * Copyright (c) 1985, 1989, 1993, 1994
5 1.1 lukem * The Regents of the University of California. All rights reserved.
6 1.1 lukem *
7 1.1 lukem * Redistribution and use in source and binary forms, with or without
8 1.1 lukem * modification, are permitted provided that the following conditions
9 1.1 lukem * are met:
10 1.1 lukem * 1. Redistributions of source code must retain the above copyright
11 1.1 lukem * notice, this list of conditions and the following disclaimer.
12 1.1 lukem * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 lukem * notice, this list of conditions and the following disclaimer in the
14 1.1 lukem * documentation and/or other materials provided with the distribution.
15 1.1 lukem * 3. All advertising materials mentioning features or use of this software
16 1.1 lukem * must display the following acknowledgement:
17 1.1 lukem * This product includes software developed by the University of
18 1.1 lukem * California, Berkeley and its contributors.
19 1.1 lukem * 4. Neither the name of the University nor the names of its contributors
20 1.1 lukem * may be used to endorse or promote products derived from this software
21 1.1 lukem * without specific prior written permission.
22 1.1 lukem *
23 1.1 lukem * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 1.1 lukem * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 1.1 lukem * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 1.1 lukem * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 1.1 lukem * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 1.1 lukem * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 1.1 lukem * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 1.1 lukem * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 1.1 lukem * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 1.1 lukem * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 1.1 lukem * SUCH DAMAGE.
34 1.1 lukem */
35 1.1 lukem
36 1.1 lukem #ifndef lint
37 1.2 thorpej static char rcsid[] = "$NetBSD: util.c,v 1.2 1997/01/30 03:36:26 thorpej Exp $";
38 1.1 lukem #endif /* not lint */
39 1.1 lukem
40 1.1 lukem /*
41 1.1 lukem * FTP User Program -- Misc support routines
42 1.1 lukem */
43 1.1 lukem #include <sys/ioctl.h>
44 1.1 lukem #include <sys/time.h>
45 1.1 lukem #include <arpa/ftp.h>
46 1.1 lukem
47 1.1 lukem #include <ctype.h>
48 1.1 lukem #include <err.h>
49 1.1 lukem #include <fcntl.h>
50 1.1 lukem #include <glob.h>
51 1.1 lukem #include <stdio.h>
52 1.1 lukem #include <string.h>
53 1.1 lukem #include <time.h>
54 1.1 lukem #include <unistd.h>
55 1.1 lukem
56 1.1 lukem #include "ftp_var.h"
57 1.1 lukem #include "pathnames.h"
58 1.1 lukem
59 1.1 lukem /*
60 1.1 lukem * Connect to peer server and
61 1.1 lukem * auto-login, if possible.
62 1.1 lukem */
63 1.1 lukem void
64 1.1 lukem setpeer(argc, argv)
65 1.1 lukem int argc;
66 1.1 lukem char *argv[];
67 1.1 lukem {
68 1.1 lukem char *host;
69 1.1 lukem short port;
70 1.1 lukem
71 1.1 lukem if (connected) {
72 1.1 lukem printf("Already connected to %s, use close first.\n",
73 1.1 lukem hostname);
74 1.1 lukem code = -1;
75 1.1 lukem return;
76 1.1 lukem }
77 1.1 lukem if (argc < 2)
78 1.1 lukem (void) another(&argc, &argv, "to");
79 1.1 lukem if (argc < 2 || argc > 3) {
80 1.1 lukem printf("usage: %s host-name [port]\n", argv[0]);
81 1.1 lukem code = -1;
82 1.1 lukem return;
83 1.1 lukem }
84 1.1 lukem port = ftpport;
85 1.1 lukem if (argc > 2) {
86 1.1 lukem port = atoi(argv[2]);
87 1.1 lukem if (port <= 0) {
88 1.1 lukem printf("%s: bad port number-- %s\n", argv[1], argv[2]);
89 1.1 lukem printf ("usage: %s host-name [port]\n", argv[0]);
90 1.1 lukem code = -1;
91 1.1 lukem return;
92 1.1 lukem }
93 1.1 lukem port = htons(port);
94 1.1 lukem }
95 1.1 lukem host = hookup(argv[1], port);
96 1.1 lukem if (host) {
97 1.1 lukem int overbose;
98 1.1 lukem
99 1.1 lukem connected = 1;
100 1.1 lukem /*
101 1.1 lukem * Set up defaults for FTP.
102 1.1 lukem */
103 1.1 lukem (void) strcpy(typename, "ascii"), type = TYPE_A;
104 1.1 lukem curtype = TYPE_A;
105 1.1 lukem (void) strcpy(formname, "non-print"), form = FORM_N;
106 1.1 lukem (void) strcpy(modename, "stream"), mode = MODE_S;
107 1.1 lukem (void) strcpy(structname, "file"), stru = STRU_F;
108 1.1 lukem (void) strcpy(bytename, "8"), bytesize = 8;
109 1.1 lukem if (autologin)
110 1.1 lukem (void) login(argv[1]);
111 1.1 lukem
112 1.1 lukem overbose = verbose;
113 1.1 lukem if (debug == 0)
114 1.1 lukem verbose = -1;
115 1.1 lukem if (command("SYST") == COMPLETE && overbose) {
116 1.1 lukem char *cp, c;
117 1.1 lukem c = 0;
118 1.1 lukem cp = strchr(reply_string+4, ' ');
119 1.1 lukem if (cp == NULL)
120 1.1 lukem cp = strchr(reply_string+4, '\r');
121 1.1 lukem if (cp) {
122 1.1 lukem if (cp[-1] == '.')
123 1.1 lukem cp--;
124 1.1 lukem c = *cp;
125 1.1 lukem *cp = '\0';
126 1.1 lukem }
127 1.1 lukem
128 1.1 lukem printf("Remote system type is %s.\n",
129 1.1 lukem reply_string+4);
130 1.1 lukem if (cp)
131 1.1 lukem *cp = c;
132 1.1 lukem }
133 1.1 lukem if (!strncmp(reply_string, "215 UNIX Type: L8", 17)) {
134 1.1 lukem if (proxy)
135 1.1 lukem unix_proxy = 1;
136 1.1 lukem else
137 1.1 lukem unix_server = 1;
138 1.1 lukem /*
139 1.1 lukem * Set type to 0 (not specified by user),
140 1.1 lukem * meaning binary by default, but don't bother
141 1.1 lukem * telling server. We can use binary
142 1.1 lukem * for text files unless changed by the user.
143 1.1 lukem */
144 1.1 lukem type = 0;
145 1.1 lukem (void) strcpy(typename, "binary");
146 1.1 lukem if (overbose)
147 1.1 lukem printf("Using %s mode to transfer files.\n",
148 1.1 lukem typename);
149 1.1 lukem } else {
150 1.1 lukem if (proxy)
151 1.1 lukem unix_proxy = 0;
152 1.1 lukem else
153 1.1 lukem unix_server = 0;
154 1.1 lukem if (overbose &&
155 1.1 lukem !strncmp(reply_string, "215 TOPS20", 10))
156 1.1 lukem printf("Remember to set tenex mode when "
157 1.1 lukem "transferring binary files from this "
158 1.1 lukem "machine.\n");
159 1.1 lukem }
160 1.1 lukem verbose = overbose;
161 1.1 lukem }
162 1.1 lukem }
163 1.1 lukem
164 1.1 lukem /*
165 1.1 lukem * `Another' gets another argument, and stores the new argc and argv.
166 1.1 lukem * It reverts to the top level (via main.c's intr()) on EOF/error.
167 1.1 lukem *
168 1.1 lukem * Returns false if no new arguments have been added.
169 1.1 lukem */
170 1.1 lukem int
171 1.1 lukem another(pargc, pargv, prompt)
172 1.1 lukem int *pargc;
173 1.1 lukem char ***pargv;
174 1.1 lukem const char *prompt;
175 1.1 lukem {
176 1.1 lukem int len = strlen(line), ret;
177 1.1 lukem
178 1.1 lukem if (len >= sizeof(line) - 3) {
179 1.1 lukem printf("sorry, arguments too long\n");
180 1.1 lukem intr();
181 1.1 lukem }
182 1.1 lukem printf("(%s) ", prompt);
183 1.1 lukem line[len++] = ' ';
184 1.1 lukem if (fgets(&line[len], sizeof(line) - len, stdin) == NULL)
185 1.1 lukem intr();
186 1.1 lukem len += strlen(&line[len]);
187 1.1 lukem if (len > 0 && line[len - 1] == '\n')
188 1.1 lukem line[len - 1] = '\0';
189 1.1 lukem makeargv();
190 1.1 lukem ret = margc > *pargc;
191 1.1 lukem *pargc = margc;
192 1.1 lukem *pargv = margv;
193 1.1 lukem return (ret);
194 1.1 lukem }
195 1.1 lukem
196 1.1 lukem char *
197 1.1 lukem remglob(argv, doswitch)
198 1.1 lukem char *argv[];
199 1.1 lukem int doswitch;
200 1.1 lukem {
201 1.1 lukem char temp[MAXPATHLEN];
202 1.1 lukem static char buf[MAXPATHLEN];
203 1.1 lukem static FILE *ftemp = NULL;
204 1.1 lukem static char **args;
205 1.1 lukem int oldverbose, oldhash, fd;
206 1.1 lukem char *cp, *mode;
207 1.1 lukem
208 1.1 lukem if (!mflag) {
209 1.1 lukem if (!doglob) {
210 1.1 lukem args = NULL;
211 1.1 lukem }
212 1.1 lukem else {
213 1.1 lukem if (ftemp) {
214 1.1 lukem (void) fclose(ftemp);
215 1.1 lukem ftemp = NULL;
216 1.1 lukem }
217 1.1 lukem }
218 1.1 lukem return (NULL);
219 1.1 lukem }
220 1.1 lukem if (!doglob) {
221 1.1 lukem if (args == NULL)
222 1.1 lukem args = argv;
223 1.1 lukem if ((cp = *++args) == NULL)
224 1.1 lukem args = NULL;
225 1.1 lukem return (cp);
226 1.1 lukem }
227 1.1 lukem if (ftemp == NULL) {
228 1.1 lukem (void) snprintf(temp, sizeof(temp), "%s%s", _PATH_TMP, TMPFILE)
229 1.1 lukem ;
230 1.1 lukem fd = mkstemp(temp);
231 1.1 lukem if (fd < 0) {
232 1.1 lukem warn("unable to create temporary file %s", temp);
233 1.1 lukem return (NULL);
234 1.1 lukem }
235 1.1 lukem close(fd);
236 1.1 lukem oldverbose = verbose, verbose = 0;
237 1.1 lukem oldhash = hash, hash = 0;
238 1.1 lukem if (doswitch) {
239 1.1 lukem pswitch(!proxy);
240 1.1 lukem }
241 1.1 lukem for (mode = "w"; *++argv != NULL; mode = "a")
242 1.1 lukem recvrequest ("NLST", temp, *argv, mode, 0);
243 1.1 lukem if (doswitch) {
244 1.1 lukem pswitch(!proxy);
245 1.1 lukem }
246 1.1 lukem verbose = oldverbose; hash = oldhash;
247 1.1 lukem ftemp = fopen(temp, "r");
248 1.1 lukem (void) unlink(temp);
249 1.1 lukem if (ftemp == NULL) {
250 1.1 lukem printf("can't find list of remote files, oops\n");
251 1.1 lukem return (NULL);
252 1.1 lukem }
253 1.1 lukem }
254 1.1 lukem if (fgets(buf, sizeof (buf), ftemp) == NULL) {
255 1.1 lukem (void) fclose(ftemp), ftemp = NULL;
256 1.1 lukem return (NULL);
257 1.1 lukem }
258 1.1 lukem if ((cp = strchr(buf, '\n')) != NULL)
259 1.1 lukem *cp = '\0';
260 1.1 lukem return (buf);
261 1.1 lukem }
262 1.1 lukem
263 1.1 lukem int
264 1.1 lukem confirm(cmd, file)
265 1.1 lukem const char *cmd, *file;
266 1.1 lukem {
267 1.1 lukem char line[BUFSIZ];
268 1.1 lukem
269 1.1 lukem if (!interactive || confirmrest)
270 1.1 lukem return (1);
271 1.1 lukem printf("%s %s? ", cmd, file);
272 1.1 lukem (void) fflush(stdout);
273 1.1 lukem if (fgets(line, sizeof(line), stdin) == NULL)
274 1.1 lukem return (0);
275 1.1 lukem switch (tolower(*line)) {
276 1.1 lukem case 'n':
277 1.1 lukem return (0);
278 1.1 lukem case 'p':
279 1.1 lukem interactive = 0;
280 1.1 lukem printf("Interactive mode: off\n");
281 1.1 lukem break;
282 1.1 lukem case 'a':
283 1.1 lukem confirmrest = 1;
284 1.1 lukem printf("Prompting off for duration of %s\n", cmd);
285 1.1 lukem break;
286 1.1 lukem }
287 1.1 lukem return (1);
288 1.1 lukem }
289 1.1 lukem
290 1.1 lukem /*
291 1.1 lukem * Glob a local file name specification with
292 1.1 lukem * the expectation of a single return value.
293 1.1 lukem * Can't control multiple values being expanded
294 1.1 lukem * from the expression, we return only the first.
295 1.1 lukem */
296 1.1 lukem int
297 1.1 lukem globulize(cpp)
298 1.1 lukem char **cpp;
299 1.1 lukem {
300 1.1 lukem glob_t gl;
301 1.1 lukem int flags;
302 1.1 lukem
303 1.1 lukem if (!doglob)
304 1.1 lukem return (1);
305 1.1 lukem
306 1.1 lukem flags = GLOB_BRACE|GLOB_NOCHECK|GLOB_QUOTE|GLOB_TILDE;
307 1.1 lukem memset(&gl, 0, sizeof(gl));
308 1.1 lukem if (glob(*cpp, flags, NULL, &gl) ||
309 1.1 lukem gl.gl_pathc == 0) {
310 1.1 lukem warnx("%s: not found", *cpp);
311 1.1 lukem globfree(&gl);
312 1.1 lukem return (0);
313 1.1 lukem }
314 1.1 lukem *cpp = strdup(gl.gl_pathv[0]); /* XXX - wasted memory */
315 1.1 lukem globfree(&gl);
316 1.1 lukem return (1);
317 1.1 lukem }
318 1.1 lukem
319 1.1 lukem /*
320 1.1 lukem * determine size of remote file
321 1.1 lukem */
322 1.1 lukem off_t
323 1.1 lukem remotesize(file, noisy)
324 1.1 lukem const char *file;
325 1.1 lukem int noisy;
326 1.1 lukem {
327 1.1 lukem int overbose;
328 1.1 lukem off_t size;
329 1.1 lukem
330 1.1 lukem overbose = verbose;
331 1.1 lukem size = -1;
332 1.1 lukem if (debug == 0)
333 1.1 lukem verbose = -1;
334 1.1 lukem if (command("SIZE %s", file) == COMPLETE)
335 1.1 lukem sscanf(reply_string, "%*s %qd", &size);
336 1.1 lukem else if (noisy && debug == 0)
337 1.1 lukem printf("%s\n", reply_string);
338 1.1 lukem verbose = overbose;
339 1.1 lukem return (size);
340 1.1 lukem }
341 1.1 lukem
342 1.1 lukem /*
343 1.1 lukem * determine last modification time (in GMT) of remote file
344 1.1 lukem */
345 1.1 lukem time_t
346 1.1 lukem remotemodtime(file, noisy)
347 1.1 lukem const char *file;
348 1.1 lukem int noisy;
349 1.1 lukem {
350 1.1 lukem int overbose;
351 1.1 lukem time_t rtime;
352 1.1 lukem
353 1.1 lukem overbose = verbose;
354 1.1 lukem rtime = -1;
355 1.1 lukem if (debug == 0)
356 1.1 lukem verbose = -1;
357 1.1 lukem if (command("MDTM %s", file) == COMPLETE) {
358 1.1 lukem struct tm timebuf;
359 1.1 lukem int yy, mo, day, hour, min, sec;
360 1.1 lukem sscanf(reply_string, "%*s %04d%02d%02d%02d%02d%02d", &yy, &mo,
361 1.1 lukem &day, &hour, &min, &sec);
362 1.1 lukem memset(&timebuf, 0, sizeof(timebuf));
363 1.1 lukem timebuf.tm_sec = sec;
364 1.1 lukem timebuf.tm_min = min;
365 1.1 lukem timebuf.tm_hour = hour;
366 1.1 lukem timebuf.tm_mday = day;
367 1.1 lukem timebuf.tm_mon = mo - 1;
368 1.1 lukem timebuf.tm_year = yy - 1900;
369 1.1 lukem timebuf.tm_isdst = -1;
370 1.1 lukem rtime = mktime(&timebuf);
371 1.1 lukem if (rtime == -1 && (noisy || debug != 0))
372 1.1 lukem printf("Can't convert %s to a time\n", reply_string);
373 1.1 lukem else
374 1.1 lukem rtime += timebuf.tm_gmtoff; /* conv. local -> GMT */
375 1.1 lukem } else if (noisy && debug == 0)
376 1.1 lukem printf("%s\n", reply_string);
377 1.1 lukem verbose = overbose;
378 1.1 lukem return (rtime);
379 1.1 lukem }
380 1.1 lukem
381 1.1 lukem /*
382 1.1 lukem * Display a transfer progress bar if progress is non-zero.
383 1.1 lukem * - Before transfer, set filesize to size of file (-1 = unknown),
384 1.1 lukem * and call with flag < 0
385 1.1 lukem * - During transfer, update bytes and call with flag = 0
386 1.1 lukem * - After transfer, call with flag > 1
387 1.1 lukem */
388 1.1 lukem static struct timeval start;
389 1.1 lukem
390 1.1 lukem void
391 1.1 lukem progressmeter(flag)
392 1.1 lukem int flag;
393 1.1 lukem {
394 1.1 lukem static struct timeval before;
395 1.1 lukem static int ttywidth;
396 1.1 lukem struct winsize winsize;
397 1.1 lukem struct timeval now, td;
398 1.1 lukem off_t cursize, abbrevsize;
399 1.1 lukem double elapsed;
400 1.1 lukem int ratio, barlength, i, remaining;
401 1.1 lukem char prefixes[] = " KMGTP"; /* `P' because 2^64 = 16384 Petabytes */
402 1.1 lukem
403 1.1 lukem if (flag < 0)
404 1.1 lukem (void) gettimeofday(&start, (struct timezone *)0);
405 1.2 thorpej if (!progress || filesize <= 0)
406 1.1 lukem return;
407 1.1 lukem if (flag < 0) {
408 1.1 lukem before.tv_sec = -1;
409 1.1 lukem if (ioctl(fileno(stdin), TIOCGWINSZ, &winsize) < 0)
410 1.1 lukem ttywidth = 80;
411 1.1 lukem else
412 1.1 lukem ttywidth = winsize.ws_col;
413 1.1 lukem } else if (flag == 0) {
414 1.1 lukem (void) gettimeofday(&now, (struct timezone *)0);
415 1.1 lukem if (now.tv_sec <= before.tv_sec)
416 1.1 lukem return;
417 1.1 lukem }
418 1.1 lukem before = now;
419 1.1 lukem cursize = bytes + restart_point;
420 1.1 lukem
421 1.1 lukem ratio = cursize * 100 / filesize;
422 1.1 lukem ratio = MAX(ratio, 0);
423 1.1 lukem ratio = MIN(ratio, 100);
424 1.1 lukem printf("\r%3d%% ", ratio);
425 1.1 lukem
426 1.1 lukem barlength = ttywidth - 30;
427 1.1 lukem if (barlength > 0) {
428 1.1 lukem i = barlength * ratio / 100;
429 1.1 lukem printf("|%.*s%*s|", i,
430 1.1 lukem "*****************************************************************************"
431 1.1 lukem "*****************************************************************************",
432 1.1 lukem barlength - i, "");
433 1.1 lukem }
434 1.1 lukem
435 1.1 lukem i = 0;
436 1.1 lukem abbrevsize = cursize;
437 1.1 lukem while (abbrevsize >= 100000 && i < sizeof(prefixes)) {
438 1.1 lukem i++;
439 1.1 lukem abbrevsize >>= 10;
440 1.1 lukem }
441 1.1 lukem printf(" %5qd %c%c ", abbrevsize, prefixes[i],
442 1.1 lukem prefixes[i] == ' ' ? ' ' : 'B');
443 1.1 lukem
444 1.1 lukem timersub(&now, &start, &td);
445 1.1 lukem elapsed = td.tv_sec + (td.tv_usec / 1000000.0);
446 1.1 lukem if (bytes <= 0 || elapsed <= 0.0) {
447 1.1 lukem printf(" --:--");
448 1.1 lukem } else {
449 1.1 lukem remaining = (int)((filesize - restart_point) /
450 1.1 lukem (bytes / elapsed) - elapsed);
451 1.1 lukem i = remaining / 3600;
452 1.1 lukem if (i)
453 1.1 lukem printf("%2d:", i);
454 1.1 lukem else
455 1.1 lukem printf(" ");
456 1.1 lukem i = remaining % 3600;
457 1.1 lukem printf("%02d:%02d", i / 60, i % 60);
458 1.1 lukem }
459 1.1 lukem printf(" ETA");
460 1.1 lukem
461 1.1 lukem if (flag > 0)
462 1.1 lukem (void) putchar('\n');
463 1.1 lukem fflush(stdout);
464 1.1 lukem }
465 1.1 lukem
466 1.1 lukem /*
467 1.1 lukem * Display transfer statistics.
468 1.1 lukem * Requires start to be initialised by progressmeter(-1),
469 1.1 lukem * direction to be defined by xfer routines, and filesize and bytes
470 1.1 lukem * to be updated by xfer routines
471 1.1 lukem * If siginfo is nonzero, an ETA is displayed, and the output goes to STDERR
472 1.1 lukem * instead of STDOUT.
473 1.1 lukem */
474 1.1 lukem void
475 1.1 lukem ptransfer(siginfo)
476 1.1 lukem int siginfo;
477 1.1 lukem {
478 1.1 lukem struct timeval now, td;
479 1.1 lukem double elapsed;
480 1.1 lukem off_t bs;
481 1.1 lukem int meg, remaining, hh;
482 1.1 lukem char buf[100];
483 1.1 lukem
484 1.1 lukem if (!verbose && !siginfo)
485 1.1 lukem return;
486 1.1 lukem
487 1.1 lukem (void) gettimeofday(&now, (struct timezone *)0);
488 1.1 lukem timersub(&now, &start, &td);
489 1.1 lukem elapsed = td.tv_sec + (td.tv_usec / 1000000.0);
490 1.1 lukem bs = bytes / (elapsed == 0.0 ? 1 : elapsed);
491 1.1 lukem meg = 0;
492 1.1 lukem if (bs > (1024 * 1024))
493 1.1 lukem meg = 1;
494 1.1 lukem (void)snprintf(buf, sizeof(buf),
495 1.1 lukem "%qd byte%s %s in %.2f seconds (%.2f %sB/s)\n",
496 1.1 lukem bytes, bytes == 1 ? "" : "s", direction, elapsed,
497 1.1 lukem bs / (1024.0 * (meg ? 1024.0 : 1.0)), meg ? "M" : "K");
498 1.1 lukem if (siginfo && bytes > 0 && elapsed > 0.0) {
499 1.1 lukem remaining = (int)((filesize - restart_point) /
500 1.1 lukem (bytes / elapsed) - elapsed);
501 1.1 lukem hh = remaining / 3600;
502 1.1 lukem remaining %= 3600;
503 1.1 lukem snprintf(buf + strlen(buf) - 1, sizeof(buf) - strlen(buf),
504 1.1 lukem " ETA: %02d:%02d:%02d\n", hh, remaining / 60,
505 1.1 lukem remaining % 60);
506 1.1 lukem }
507 1.1 lukem (void)write(siginfo ? STDERR_FILENO : STDOUT_FILENO, buf, strlen(buf));
508 1.1 lukem }
509 1.1 lukem
510 1.1 lukem /*
511 1.1 lukem * List words in stringlist, vertically arranged
512 1.1 lukem */
513 1.1 lukem void
514 1.1 lukem list_vertical(sl)
515 1.1 lukem StringList *sl;
516 1.1 lukem {
517 1.1 lukem static int ttywidth;
518 1.1 lukem struct winsize winsize;
519 1.1 lukem int i, j, w;
520 1.1 lukem int columns, width, lines, items;
521 1.1 lukem char *p;
522 1.1 lukem
523 1.1 lukem width = items = 0;
524 1.1 lukem if (fromatty && ioctl(fileno(stdin), TIOCGWINSZ, &winsize) != -1)
525 1.1 lukem ttywidth = winsize.ws_col;
526 1.1 lukem else
527 1.1 lukem ttywidth = 80;
528 1.1 lukem
529 1.1 lukem for (i = 0 ; i < sl->sl_cur ; i++) {
530 1.1 lukem w = strlen(sl->sl_str[i]);
531 1.1 lukem if (w > width)
532 1.1 lukem width = w;
533 1.1 lukem }
534 1.1 lukem width = (width + 8) &~ 7;
535 1.1 lukem
536 1.1 lukem columns = ttywidth / width;
537 1.1 lukem if (columns == 0)
538 1.1 lukem columns = 1;
539 1.1 lukem lines = (sl->sl_cur + columns - 1) / columns;
540 1.1 lukem for (i = 0; i < lines; i++) {
541 1.1 lukem for (j = 0; j < columns; j++) {
542 1.1 lukem p = sl->sl_str[j * lines + i];
543 1.1 lukem if (p)
544 1.1 lukem printf("%s", p);
545 1.1 lukem if (j * lines + i + lines >= sl->sl_cur) {
546 1.1 lukem printf("\n");
547 1.1 lukem break;
548 1.1 lukem }
549 1.1 lukem w = strlen(p);
550 1.1 lukem while (w < width) {
551 1.1 lukem w = (w + 8) &~ 7;
552 1.1 lukem (void) putchar('\t');
553 1.1 lukem }
554 1.1 lukem }
555 1.1 lukem }
556 1.1 lukem }
557