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