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