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