util.c revision 1.24 1 /* $NetBSD: util.c,v 1.24 1998/06/04 08:28:36 lukem Exp $ */
2
3 /*
4 * Copyright (c) 1985, 1989, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36 #include <sys/cdefs.h>
37 #ifndef lint
38 __RCSID("$NetBSD: util.c,v 1.24 1998/06/04 08:28:36 lukem Exp $");
39 #endif /* not lint */
40
41 /*
42 * FTP User Program -- Misc support routines
43 */
44 #include <sys/ioctl.h>
45 #include <sys/time.h>
46 #include <arpa/ftp.h>
47
48 #include <ctype.h>
49 #include <err.h>
50 #include <fcntl.h>
51 #include <glob.h>
52 #include <termios.h>
53 #include <signal.h>
54 #include <limits.h>
55 #include <pwd.h>
56 #include <stdio.h>
57 #include <stdlib.h>
58 #include <string.h>
59 #include <time.h>
60 #include <tzfile.h>
61 #include <unistd.h>
62
63 #include "ftp_var.h"
64 #include "pathnames.h"
65
66 /*
67 * Connect to peer server and
68 * auto-login, if possible.
69 */
70 void
71 setpeer(argc, argv)
72 int argc;
73 char *argv[];
74 {
75 char *host;
76 in_port_t port;
77
78 if (connected) {
79 fprintf(ttyout, "Already connected to %s, use close first.\n",
80 hostname);
81 code = -1;
82 return;
83 }
84 if (argc < 2)
85 (void)another(&argc, &argv, "to");
86 if (argc < 2 || argc > 3) {
87 fprintf(ttyout, "usage: %s host-name [port]\n", argv[0]);
88 code = -1;
89 return;
90 }
91 if (gatemode)
92 port = gateport;
93 else
94 port = ftpport;
95 if (argc > 2) {
96 char *ep;
97 long nport;
98
99 nport = strtol(argv[2], &ep, 10);
100 if (nport < 1 || nport > MAX_IN_PORT_T || *ep != '\0') {
101 fprintf(ttyout, "%s: bad port number '%s'.\n",
102 argv[1], argv[2]);
103 fprintf(ttyout, "usage: %s host-name [port]\n",
104 argv[0]);
105 code = -1;
106 return;
107 }
108 port = htons((in_port_t)nport);
109 }
110
111 if (gatemode) {
112 if (gateserver == NULL || *gateserver == '\0')
113 errx(1, "gateserver not defined (shouldn't happen)");
114 host = hookup(gateserver, port);
115 } else
116 host = hookup(argv[1], port);
117
118 if (host) {
119 int overbose;
120
121 if (gatemode) {
122 if (command("PASSERVE %s", argv[1]) != COMPLETE)
123 return;
124 if (verbose)
125 fprintf(ttyout,
126 "Connected via pass-through server %s\n",
127 gateserver);
128 }
129
130 connected = 1;
131 /*
132 * Set up defaults for FTP.
133 */
134 (void)strcpy(typename, "ascii"), type = TYPE_A;
135 curtype = TYPE_A;
136 (void)strcpy(formname, "non-print"), form = FORM_N;
137 (void)strcpy(modename, "stream"), mode = MODE_S;
138 (void)strcpy(structname, "file"), stru = STRU_F;
139 (void)strcpy(bytename, "8"), bytesize = 8;
140 if (autologin)
141 (void)login(argv[1], NULL, NULL);
142
143 overbose = verbose;
144 if (debug == 0)
145 verbose = -1;
146 if (command("SYST") == COMPLETE && overbose) {
147 char *cp, c;
148 c = 0;
149 cp = strchr(reply_string + 4, ' ');
150 if (cp == NULL)
151 cp = strchr(reply_string + 4, '\r');
152 if (cp) {
153 if (cp[-1] == '.')
154 cp--;
155 c = *cp;
156 *cp = '\0';
157 }
158
159 fprintf(ttyout, "Remote system type is %s.\n",
160 reply_string + 4);
161 if (cp)
162 *cp = c;
163 }
164 if (!strncmp(reply_string, "215 UNIX Type: L8", 17)) {
165 if (proxy)
166 unix_proxy = 1;
167 else
168 unix_server = 1;
169 /*
170 * Set type to 0 (not specified by user),
171 * meaning binary by default, but don't bother
172 * telling server. We can use binary
173 * for text files unless changed by the user.
174 */
175 type = 0;
176 (void)strcpy(typename, "binary");
177 if (overbose)
178 fprintf(ttyout,
179 "Using %s mode to transfer files.\n",
180 typename);
181 } else {
182 if (proxy)
183 unix_proxy = 0;
184 else
185 unix_server = 0;
186 if (overbose &&
187 !strncmp(reply_string, "215 TOPS20", 10))
188 fputs(
189 "Remember to set tenex mode when transferring binary files from this machine.\n",
190 ttyout);
191 }
192 verbose = overbose;
193 }
194 }
195
196 /*
197 * login to remote host, using given username & password if supplied
198 */
199 int
200 login(host, user, pass)
201 const char *host;
202 char *user, *pass;
203 {
204 char tmp[80];
205 char *acct;
206 char anonpass[MAXLOGNAME + 1 + MAXHOSTNAMELEN]; /* "user@hostname" */
207 char hostname[MAXHOSTNAMELEN];
208 struct passwd *pw;
209 int n, aflag = 0;
210
211 acct = NULL;
212 if (user == NULL) {
213 if (ruserpass(host, &user, &pass, &acct) < 0) {
214 code = -1;
215 return (0);
216 }
217 }
218
219 /*
220 * Set up arguments for an anonymous FTP session, if necessary.
221 */
222 if ((user == NULL || pass == NULL) && anonftp) {
223 memset(anonpass, 0, sizeof(anonpass));
224 memset(hostname, 0, sizeof(hostname));
225
226 /*
227 * Set up anonymous login password.
228 */
229 if ((user = getlogin()) == NULL) {
230 if ((pw = getpwuid(getuid())) == NULL)
231 user = "anonymous";
232 else
233 user = pw->pw_name;
234 }
235 gethostname(hostname, MAXHOSTNAMELEN);
236 #ifndef DONT_CHEAT_ANONPASS
237 /*
238 * Every anonymous FTP server I've encountered
239 * will accept the string "username@", and will
240 * append the hostname itself. We do this by default
241 * since many servers are picky about not having
242 * a FQDN in the anonymous password. - thorpej (at) netbsd.org
243 */
244 snprintf(anonpass, sizeof(anonpass) - 1, "%s@",
245 user);
246 #else
247 snprintf(anonpass, sizeof(anonpass) - 1, "%s@%s",
248 user, hp->h_name);
249 #endif
250 pass = anonpass;
251 user = "anonymous"; /* as per RFC 1635 */
252 }
253
254 while (user == NULL) {
255 char *myname = getlogin();
256
257 if (myname == NULL && (pw = getpwuid(getuid())) != NULL)
258 myname = pw->pw_name;
259 if (myname)
260 fprintf(ttyout, "Name (%s:%s): ", host, myname);
261 else
262 fprintf(ttyout, "Name (%s): ", host);
263 *tmp = '\0';
264 (void)fgets(tmp, sizeof(tmp) - 1, stdin);
265 tmp[strlen(tmp) - 1] = '\0';
266 if (*tmp == '\0')
267 user = myname;
268 else
269 user = tmp;
270 }
271 n = command("USER %s", user);
272 if (n == CONTINUE) {
273 if (pass == NULL)
274 pass = getpass("Password:");
275 n = command("PASS %s", pass);
276 }
277 if (n == CONTINUE) {
278 aflag++;
279 if (acct == NULL)
280 acct = getpass("Account:");
281 n = command("ACCT %s", acct);
282 }
283 if ((n != COMPLETE) ||
284 (!aflag && acct != NULL && command("ACCT %s", acct) != COMPLETE)) {
285 warnx("Login failed.");
286 return (0);
287 }
288 if (proxy)
289 return (1);
290 connected = -1;
291 for (n = 0; n < macnum; ++n) {
292 if (!strcmp("init", macros[n].mac_name)) {
293 (void)strcpy(line, "$init");
294 makeargv();
295 domacro(margc, margv);
296 break;
297 }
298 }
299 return (1);
300 }
301
302 /*
303 * `another' gets another argument, and stores the new argc and argv.
304 * It reverts to the top level (via main.c's intr()) on EOF/error.
305 *
306 * Returns false if no new arguments have been added.
307 */
308 int
309 another(pargc, pargv, prompt)
310 int *pargc;
311 char ***pargv;
312 const char *prompt;
313 {
314 int len = strlen(line), ret;
315
316 if (len >= sizeof(line) - 3) {
317 fputs("sorry, arguments too long.\n", ttyout);
318 intr();
319 }
320 fprintf(ttyout, "(%s) ", prompt);
321 line[len++] = ' ';
322 if (fgets(&line[len], sizeof(line) - len, stdin) == NULL)
323 intr();
324 len += strlen(&line[len]);
325 if (len > 0 && line[len - 1] == '\n')
326 line[len - 1] = '\0';
327 makeargv();
328 ret = margc > *pargc;
329 *pargc = margc;
330 *pargv = margv;
331 return (ret);
332 }
333
334 /*
335 * glob files given in argv[] from the remote server.
336 * if errbuf isn't NULL, store error messages there instead
337 * of writing to the screen.
338 */
339 char *
340 remglob(argv, doswitch, errbuf)
341 char *argv[];
342 int doswitch;
343 char **errbuf;
344 {
345 char temp[MAXPATHLEN];
346 static char buf[MAXPATHLEN];
347 static FILE *ftemp = NULL;
348 static char **args;
349 int oldverbose, oldhash, fd;
350 char *cp, *mode;
351
352 if (!mflag) {
353 if (!doglob)
354 args = NULL;
355 else {
356 if (ftemp) {
357 (void)fclose(ftemp);
358 ftemp = NULL;
359 }
360 }
361 return (NULL);
362 }
363 if (!doglob) {
364 if (args == NULL)
365 args = argv;
366 if ((cp = *++args) == NULL)
367 args = NULL;
368 return (cp);
369 }
370 if (ftemp == NULL) {
371 (void)snprintf(temp, sizeof(temp), "%s/%s", tmpdir, TMPFILE);
372 if ((fd = mkstemp(temp)) < 0) {
373 warn("unable to create temporary file %s", temp);
374 return (NULL);
375 }
376 close(fd);
377 oldverbose = verbose;
378 verbose = (errbuf != NULL) ? -1 : 0;
379 oldhash = hash;
380 hash = 0;
381 if (doswitch)
382 pswitch(!proxy);
383 for (mode = "w"; *++argv != NULL; mode = "a")
384 recvrequest("NLST", temp, *argv, mode, 0, 0);
385 if ((code / 100) != COMPLETE) {
386 if (errbuf != NULL)
387 *errbuf = reply_string;
388 }
389 if (doswitch)
390 pswitch(!proxy);
391 verbose = oldverbose;
392 hash = oldhash;
393 ftemp = fopen(temp, "r");
394 (void)unlink(temp);
395 if (ftemp == NULL) {
396 if (errbuf == NULL)
397 fputs(
398 "can't find list of remote files, oops.\n",
399 ttyout);
400 else
401 *errbuf =
402 "can't find list of remote files, oops.";
403 return (NULL);
404 }
405 }
406 if (fgets(buf, sizeof(buf), ftemp) == NULL) {
407 (void)fclose(ftemp);
408 ftemp = NULL;
409 return (NULL);
410 }
411 if ((cp = strchr(buf, '\n')) != NULL)
412 *cp = '\0';
413 return (buf);
414 }
415
416 int
417 confirm(cmd, file)
418 const char *cmd, *file;
419 {
420 char line[BUFSIZ];
421
422 if (!interactive || confirmrest)
423 return (1);
424 fprintf(ttyout, "%s %s? ", cmd, file);
425 (void)fflush(ttyout);
426 if (fgets(line, sizeof(line), stdin) == NULL)
427 return (0);
428 switch (tolower(*line)) {
429 case 'n':
430 return (0);
431 case 'p':
432 interactive = 0;
433 fputs("Interactive mode: off.\n", ttyout);
434 break;
435 case 'a':
436 confirmrest = 1;
437 fprintf(ttyout, "Prompting off for duration of %s.\n",
438 cmd);
439 break;
440 }
441 return (1);
442 }
443
444 /*
445 * Glob a local file name specification with
446 * the expectation of a single return value.
447 * Can't control multiple values being expanded
448 * from the expression, we return only the first.
449 */
450 int
451 globulize(cpp)
452 char **cpp;
453 {
454 glob_t gl;
455 int flags;
456
457 if (!doglob)
458 return (1);
459
460 flags = GLOB_BRACE|GLOB_NOCHECK|GLOB_QUOTE|GLOB_TILDE;
461 memset(&gl, 0, sizeof(gl));
462 if (glob(*cpp, flags, NULL, &gl) ||
463 gl.gl_pathc == 0) {
464 warnx("%s: not found", *cpp);
465 globfree(&gl);
466 return (0);
467 }
468 /* XXX: caller should check if *cpp changed, and
469 * free(*cpp) if that is the case
470 */
471 *cpp = strdup(gl.gl_pathv[0]);
472 globfree(&gl);
473 return (1);
474 }
475
476 /*
477 * determine size of remote file
478 */
479 off_t
480 remotesize(file, noisy)
481 const char *file;
482 int noisy;
483 {
484 int overbose;
485 off_t size;
486
487 overbose = verbose;
488 size = -1;
489 if (debug == 0)
490 verbose = -1;
491 if (command("SIZE %s", file) == COMPLETE) {
492 char *cp, *ep;
493
494 cp = strchr(reply_string, ' ');
495 if (cp != NULL) {
496 cp++;
497 #ifdef NO_QUAD
498 size = strtol(cp, &ep, 10);
499 #else
500 size = strtoq(cp, &ep, 10);
501 #endif
502 if (*ep != '\0' && !isspace((unsigned char)*ep))
503 size = -1;
504 }
505 } else if (noisy && debug == 0) {
506 fputs(reply_string, ttyout);
507 putc('\n', ttyout);
508 }
509 verbose = overbose;
510 return (size);
511 }
512
513 /*
514 * determine last modification time (in GMT) of remote file
515 */
516 time_t
517 remotemodtime(file, noisy)
518 const char *file;
519 int noisy;
520 {
521 int overbose;
522 time_t rtime;
523 int ocode;
524
525 overbose = verbose;
526 ocode = code;
527 rtime = -1;
528 if (debug == 0)
529 verbose = -1;
530 if (command("MDTM %s", file) == COMPLETE) {
531 struct tm timebuf;
532 int yy, mo, day, hour, min, sec;
533 sscanf(reply_string, "%*s %04d%02d%02d%02d%02d%02d", &yy, &mo,
534 &day, &hour, &min, &sec);
535 memset(&timebuf, 0, sizeof(timebuf));
536 timebuf.tm_sec = sec;
537 timebuf.tm_min = min;
538 timebuf.tm_hour = hour;
539 timebuf.tm_mday = day;
540 timebuf.tm_mon = mo - 1;
541 timebuf.tm_year = yy - TM_YEAR_BASE;
542 timebuf.tm_isdst = -1;
543 rtime = mktime(&timebuf);
544 if (rtime == -1 && (noisy || debug != 0))
545 fprintf(ttyout, "Can't convert %s to a time.\n",
546 reply_string);
547 else
548 #ifndef __SVR4
549 rtime += timebuf.tm_gmtoff; /* conv. local -> GMT */
550 #else
551 rtime -= timezone;
552 #endif
553 } else if (noisy && debug == 0) {
554 fputs(reply_string, ttyout);
555 putc('\n', ttyout);
556 }
557 verbose = overbose;
558 if (rtime == -1)
559 code = ocode;
560 return (rtime);
561 }
562
563 #ifndef SMALL
564
565 /*
566 * return non-zero if we're the current foreground process
567 */
568 int
569 foregroundproc()
570 {
571 static pid_t pgrp = -1;
572 int ctty_pgrp;
573
574 if (pgrp == -1)
575 pgrp = getpgrp();
576
577 return ((ioctl(fileno(ttyout), TIOCGPGRP, &ctty_pgrp) != -1 &&
578 ctty_pgrp == (int)pgrp));
579 }
580
581
582 static void updateprogressmeter __P((int));
583
584 static void
585 updateprogressmeter(dummy)
586 int dummy;
587 {
588
589 /*
590 * print progress bar only if we are foreground process.
591 */
592 if (foregroundproc())
593 progressmeter(0);
594 }
595 #endif /* SMALL */
596
597 /*
598 * Display a transfer progress bar if progress is non-zero.
599 * SIGALRM is hijacked for use by this function.
600 * - Before the transfer, set filesize to size of file (or -1 if unknown),
601 * and call with flag = -1. This starts the once per second timer,
602 * and a call to updateprogressmeter() upon SIGALRM.
603 * - During the transfer, updateprogressmeter will call progressmeter
604 * with flag = 0
605 * - After the transfer, call with flag = 1
606 */
607 static struct timeval start;
608 static struct timeval lastupdate;
609
610 void
611 progressmeter(flag)
612 int flag;
613 {
614 #ifndef SMALL
615 /*
616 * List of order of magnitude prefixes.
617 * The last is `P', as 2^64 = 16384 Petabytes
618 */
619 static const char prefixes[] = " KMGTP";
620
621 static off_t lastsize;
622 struct timeval now, td, wait;
623 off_t cursize, abbrevsize;
624 double elapsed;
625 int ratio, barlength, i, len, remaining;
626 char buf[256];
627
628 len = 0;
629
630 if (flag == -1) {
631 (void)gettimeofday(&start, NULL);
632 lastupdate = start;
633 lastsize = restart_point;
634 }
635 (void)gettimeofday(&now, NULL);
636 if (!progress || filesize <= 0)
637 return;
638 cursize = bytes + restart_point;
639
640 ratio = cursize * 100 / filesize;
641 ratio = MAX(ratio, 0);
642 ratio = MIN(ratio, 100);
643 len += snprintf(buf + len, sizeof(buf) - len, "\r%3d%% ", ratio);
644
645 barlength = ttywidth - 30;
646 if (barlength > 0) {
647 i = barlength * ratio / 100;
648 len += snprintf(buf + len, sizeof(buf) - len,
649 "|%.*s%*s|", i,
650 "*****************************************************************************"
651 "*****************************************************************************",
652 barlength - i, "");
653 }
654
655 i = 0;
656 abbrevsize = cursize;
657 while (abbrevsize >= 100000 && i < sizeof(prefixes)) {
658 i++;
659 abbrevsize >>= 10;
660 }
661 len += snprintf(buf + len, sizeof(buf) - len,
662 " %5qd %c%c ", (long long)abbrevsize, prefixes[i],
663 prefixes[i] == ' ' ? ' ' : 'B');
664
665 timersub(&now, &lastupdate, &wait);
666 if (cursize > lastsize) {
667 lastupdate = now;
668 lastsize = cursize;
669 if (wait.tv_sec >= STALLTIME) { /* fudge out stalled time */
670 start.tv_sec += wait.tv_sec;
671 start.tv_usec += wait.tv_usec;
672 }
673 wait.tv_sec = 0;
674 }
675
676 timersub(&now, &start, &td);
677 elapsed = td.tv_sec + (td.tv_usec / 1000000.0);
678
679 if (bytes <= 0 || elapsed <= 0.0 || cursize > filesize) {
680 len += snprintf(buf + len, sizeof(buf) - len,
681 " --:-- ETA");
682 } else if (wait.tv_sec >= STALLTIME) {
683 len += snprintf(buf + len, sizeof(buf) - len,
684 " - stalled -");
685 } else {
686 remaining = (int)
687 ((filesize - restart_point) / (bytes / elapsed) - elapsed);
688 if (remaining >= 100 * SECSPERHOUR)
689 len += snprintf(buf + len, sizeof(buf) - len,
690 " --:-- ETA");
691 else {
692 i = remaining / SECSPERHOUR;
693 if (i)
694 len += snprintf(buf + len, sizeof(buf) - len,
695 "%2d:", i);
696 else
697 len += snprintf(buf + len, sizeof(buf) - len,
698 " ");
699 i = remaining % SECSPERHOUR;
700 len += snprintf(buf + len, sizeof(buf) - len,
701 "%02d:%02d ETA", i / 60, i % 60);
702 }
703 }
704 (void)write(fileno(ttyout), buf, len);
705
706 if (flag == -1) {
707 (void)signal(SIGALRM, updateprogressmeter);
708 alarmtimer(1); /* set alarm timer for 1 Hz */
709 } else if (flag == 1) {
710 alarmtimer(0);
711 (void)putc('\n', ttyout);
712 }
713 fflush(ttyout);
714 #endif /* SMALL */
715 }
716
717 /*
718 * Display transfer statistics.
719 * Requires start to be initialised by progressmeter(-1),
720 * direction to be defined by xfer routines, and filesize and bytes
721 * to be updated by xfer routines
722 * If siginfo is nonzero, an ETA is displayed, and the output goes to stderr
723 * instead of ttyout.
724 */
725 void
726 ptransfer(siginfo)
727 int siginfo;
728 {
729 #ifndef SMALL
730 struct timeval now, td, wait;
731 double elapsed;
732 off_t bs;
733 int meg, remaining, hh, len;
734 char buf[100];
735
736 if (!verbose && !siginfo)
737 return;
738
739 (void)gettimeofday(&now, NULL);
740 timersub(&now, &start, &td);
741 elapsed = td.tv_sec + (td.tv_usec / 1000000.0);
742 bs = bytes / (elapsed == 0.0 ? 1 : elapsed);
743 meg = 0;
744 if (bs > (1024 * 1024))
745 meg = 1;
746 len = 0;
747 len += snprintf(buf + len, sizeof(buf) - len,
748 #ifndef NO_QUAD
749 "%qd byte%s %s in ", (long long)bytes,
750 #else
751 "%ld byte%s %s in ", (long)bytes,
752 #endif
753 bytes == 1 ? "" : "s", direction);
754 remaining = (int)elapsed;
755 if (remaining > SECSPERDAY) {
756 int days;
757
758 days = remaining / SECSPERDAY;
759 remaining %= SECSPERDAY;
760 len += snprintf(buf + len, sizeof(buf) - len,
761 "%d day%s ", days, days == 1 ? "" : "s");
762 }
763 hh = remaining / SECSPERHOUR;
764 remaining %= SECSPERHOUR;
765 if (hh)
766 len += snprintf(buf + len, sizeof(buf) - len, "%2d:", hh);
767 len += snprintf(buf + len, sizeof(buf) - len,
768 "%02d:%02d (%.2f %sB/s)", remaining / 60, remaining % 60,
769 bs / (1024.0 * (meg ? 1024.0 : 1.0)),
770 meg ? "M" : "K");
771
772 if (siginfo && bytes > 0 && elapsed > 0.0 && filesize >= 0
773 && bytes + restart_point <= filesize) {
774 remaining = (int)((filesize - restart_point) /
775 (bytes / elapsed) - elapsed);
776 hh = remaining / SECSPERHOUR;
777 remaining %= SECSPERHOUR;
778 len += snprintf(buf + len, sizeof(buf) - len, " ETA: ");
779 if (hh)
780 len += snprintf(buf + len, sizeof(buf) - len, "%2d:",
781 hh);
782 len += snprintf(buf + len, sizeof(buf) - len,
783 "%02d:%02d", remaining / 60, remaining % 60);
784 timersub(&now, &lastupdate, &wait);
785 if (wait.tv_sec >= STALLTIME)
786 len += snprintf(buf + len, sizeof(buf) - len,
787 " (stalled)");
788 }
789 len += snprintf(buf + len, sizeof(buf) - len, "\n");
790 (void)write(siginfo ? STDERR_FILENO : fileno(ttyout), buf, len);
791 #endif /* SMALL */
792 }
793
794 /*
795 * List words in stringlist, vertically arranged
796 */
797 void
798 list_vertical(sl)
799 StringList *sl;
800 {
801 int i, j, w;
802 int columns, width, lines, items;
803 char *p;
804
805 width = items = 0;
806
807 for (i = 0 ; i < sl->sl_cur ; i++) {
808 w = strlen(sl->sl_str[i]);
809 if (w > width)
810 width = w;
811 }
812 width = (width + 8) &~ 7;
813
814 columns = ttywidth / width;
815 if (columns == 0)
816 columns = 1;
817 lines = (sl->sl_cur + columns - 1) / columns;
818 for (i = 0; i < lines; i++) {
819 for (j = 0; j < columns; j++) {
820 p = sl->sl_str[j * lines + i];
821 if (p)
822 fputs(p, ttyout);
823 if (j * lines + i + lines >= sl->sl_cur) {
824 putc('\n', ttyout);
825 break;
826 }
827 w = strlen(p);
828 while (w < width) {
829 w = (w + 8) &~ 7;
830 (void)putc('\t', ttyout);
831 }
832 }
833 }
834 }
835
836 /*
837 * Update the global ttywidth value, using TIOCGWINSZ.
838 */
839 void
840 setttywidth(a)
841 int a;
842 {
843 struct winsize winsize;
844
845 if (ioctl(fileno(ttyout), TIOCGWINSZ, &winsize) != -1)
846 ttywidth = winsize.ws_col;
847 else
848 ttywidth = 80;
849 }
850
851 /*
852 * Set the SIGALRM interval timer for wait seconds, 0 to disable.
853 */
854 void
855 alarmtimer(wait)
856 int wait;
857 {
858 struct itimerval itv;
859
860 itv.it_value.tv_sec = wait;
861 itv.it_value.tv_usec = 0;
862 itv.it_interval = itv.it_value;
863 setitimer(ITIMER_REAL, &itv, NULL);
864 }
865
866 /*
867 * Setup or cleanup EditLine structures
868 */
869 #ifndef SMALL
870 void
871 controlediting()
872 {
873 if (editing && el == NULL && hist == NULL) {
874 HistEvent ev;
875
876 el = el_init(__progname, stdin, ttyout, stderr);
877 /* init editline */
878 hist = history_init(); /* init the builtin history */
879 history(hist, &ev, H_SETSIZE, 100);/* remember 100 events */
880 el_set(el, EL_HIST, history, hist); /* use history */
881
882 el_set(el, EL_EDITOR, "emacs"); /* default editor is emacs */
883 el_set(el, EL_PROMPT, prompt); /* set the prompt function */
884
885 /* add local file completion, bind to TAB */
886 el_set(el, EL_ADDFN, "ftp-complete",
887 "Context sensitive argument completion",
888 complete);
889 el_set(el, EL_BIND, "^I", "ftp-complete", NULL);
890
891 el_source(el, NULL); /* read ~/.editrc */
892 el_set(el, EL_SIGNAL, 1);
893 } else if (!editing) {
894 if (hist) {
895 history_end(hist);
896 hist = NULL;
897 }
898 if (el) {
899 el_end(el);
900 el = NULL;
901 }
902 }
903 }
904 #endif /* !SMALL */
905