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