Annotation of src/usr.bin/ftp/util.c, Revision 1.9
1.9 ! millert 1: /* $OpenBSD: util.c,v 1.8 1997/05/05 20:49:51 jkatz 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.9 ! millert 38: static char rcsid[] = "$OpenBSD: util.c,v 1.8 1997/05/05 20:49:51 jkatz 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:
535: void
536: updateprogressmeter()
537: {
538:
539: progressmeter(0);
540: }
541:
542: /*
543: * Display a transfer progress bar if progress is non-zero.
544: * SIGALRM is hijacked for use by this function.
545: * - Before the transfer, set filesize to size of file (or -1 if unknown),
546: * and call with flag = -1. This starts the once per second timer,
547: * and a call to updateprogressmeter() upon SIGALRM.
548: * - During the transfer, updateprogressmeter will call progressmeter
549: * with flag = 0
550: * - After the transfer, call with flag = 1
551: */
552: static struct timeval start;
553:
554: void
555: progressmeter(flag)
556: int flag;
557: {
558: /*
559: * List of order of magnitude prefixes.
560: * The last is `P', as 2^64 = 16384 Petabytes
561: */
562: static const char prefixes[] = " KMGTP";
563:
564: static struct timeval lastupdate;
565: static off_t lastsize;
566: struct timeval now, td, wait;
567: off_t cursize, abbrevsize;
568: double elapsed;
569: int ratio, barlength, i, remaining;
570: char buf[256];
571:
572: if (flag == -1) {
1.2 millert 573: (void)gettimeofday(&start, (struct timezone *)0);
1.1 millert 574: lastupdate = start;
575: lastsize = restart_point;
576: }
1.2 millert 577: (void)gettimeofday(&now, (struct timezone *)0);
1.1 millert 578: if (!progress || filesize <= 0)
579: return;
580: cursize = bytes + restart_point;
581:
582: ratio = cursize * 100 / filesize;
583: ratio = MAX(ratio, 0);
584: ratio = MIN(ratio, 100);
585: snprintf(buf, sizeof(buf), "\r%3d%% ", ratio);
586:
587: barlength = ttywidth - 30;
588: if (barlength > 0) {
589: i = barlength * ratio / 100;
590: snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
591: "|%.*s%*s|", i,
592: "*****************************************************************************"
593: "*****************************************************************************",
594: barlength - i, "");
595: }
596:
597: i = 0;
598: abbrevsize = cursize;
599: while (abbrevsize >= 100000 && i < sizeof(prefixes)) {
600: i++;
601: abbrevsize >>= 10;
602: }
603: snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
604: " %5qd %c%c ", abbrevsize, prefixes[i],
605: prefixes[i] == ' ' ? ' ' : 'B');
606:
607: timersub(&now, &lastupdate, &wait);
608: if (cursize > lastsize) {
609: lastupdate = now;
610: lastsize = cursize;
611: if (wait.tv_sec >= STALLTIME) { /* fudge out stalled time */
612: start.tv_sec += wait.tv_sec;
613: start.tv_usec += wait.tv_usec;
614: }
615: wait.tv_sec = 0;
616: }
617:
618: timersub(&now, &start, &td);
619: elapsed = td.tv_sec + (td.tv_usec / 1000000.0);
620:
621: if (bytes <= 0 || elapsed <= 0.0) {
622: snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
623: " --:-- ETA");
624: } else if (wait.tv_sec >= STALLTIME) {
625: snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
626: " - stalled -");
627: } else {
628: remaining = (int)((filesize - restart_point) /
629: (bytes / elapsed) - elapsed);
630: i = remaining / 3600;
631: if (i)
632: snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
633: "%2d:", i);
634: else
635: snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
636: " ");
637: i = remaining % 3600;
638: snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
639: "%02d:%02d ETA", i / 60, i % 60);
640: }
1.7 deraadt 641: (void)write(fileno(ttyout), buf, strlen(buf));
1.1 millert 642:
643: if (flag == -1) {
1.2 millert 644: (void)signal(SIGALRM, updateprogressmeter);
1.1 millert 645: alarmtimer(1); /* set alarm timer for 1 Hz */
646: } else if (flag == 1) {
647: alarmtimer(0);
1.7 deraadt 648: (void)putc('\n', ttyout);
1.1 millert 649: }
1.7 deraadt 650: fflush(ttyout);
1.1 millert 651: }
652:
653: /*
654: * Display transfer statistics.
655: * Requires start to be initialised by progressmeter(-1),
656: * direction to be defined by xfer routines, and filesize and bytes
657: * to be updated by xfer routines
658: * If siginfo is nonzero, an ETA is displayed, and the output goes to STDERR
1.7 deraadt 659: * instead of TTYOUT.
1.1 millert 660: */
661: void
662: ptransfer(siginfo)
663: int siginfo;
664: {
665: struct timeval now, td;
666: double elapsed;
667: off_t bs;
668: int meg, remaining, hh;
669: char buf[100];
670:
671: if (!verbose && !siginfo)
672: return;
673:
1.2 millert 674: (void)gettimeofday(&now, (struct timezone *)0);
1.1 millert 675: timersub(&now, &start, &td);
676: elapsed = td.tv_sec + (td.tv_usec / 1000000.0);
677: bs = bytes / (elapsed == 0.0 ? 1 : elapsed);
678: meg = 0;
679: if (bs > (1024 * 1024))
680: meg = 1;
681: (void)snprintf(buf, sizeof(buf),
682: "%qd byte%s %s in %.2f seconds (%.2f %sB/s)\n",
683: bytes, bytes == 1 ? "" : "s", direction, elapsed,
684: bs / (1024.0 * (meg ? 1024.0 : 1.0)), meg ? "M" : "K");
685: if (siginfo && bytes > 0 && elapsed > 0.0 && filesize >= 0) {
686: remaining = (int)((filesize - restart_point) /
687: (bytes / elapsed) - elapsed);
688: hh = remaining / 3600;
689: remaining %= 3600;
690: snprintf(buf + strlen(buf) - 1, sizeof(buf) - strlen(buf),
691: " ETA: %02d:%02d:%02d\n", hh, remaining / 60,
692: remaining % 60);
693: }
1.7 deraadt 694: (void)write(siginfo ? STDERR_FILENO : fileno(ttyout), buf, strlen(buf));
1.1 millert 695: }
696:
697: /*
698: * List words in stringlist, vertically arranged
699: */
700: void
701: list_vertical(sl)
702: StringList *sl;
703: {
704: int i, j, w;
705: int columns, width, lines, items;
706: char *p;
707:
708: width = items = 0;
709:
710: for (i = 0 ; i < sl->sl_cur ; i++) {
711: w = strlen(sl->sl_str[i]);
712: if (w > width)
713: width = w;
714: }
715: width = (width + 8) &~ 7;
716:
717: columns = ttywidth / width;
718: if (columns == 0)
719: columns = 1;
720: lines = (sl->sl_cur + columns - 1) / columns;
721: for (i = 0; i < lines; i++) {
722: for (j = 0; j < columns; j++) {
723: p = sl->sl_str[j * lines + i];
724: if (p)
1.7 deraadt 725: fputs(p, ttyout);
1.1 millert 726: if (j * lines + i + lines >= sl->sl_cur) {
1.7 deraadt 727: putc('\n', ttyout);
1.1 millert 728: break;
729: }
730: w = strlen(p);
731: while (w < width) {
732: w = (w + 8) &~ 7;
1.7 deraadt 733: (void)putc('\t', ttyout);
1.1 millert 734: }
735: }
736: }
737: }
738:
739: /*
740: * Update the global ttywidth value, using TIOCGWINSZ.
741: */
742: void
743: setttywidth(a)
744: int a;
745: {
746: struct winsize winsize;
747:
1.7 deraadt 748: if (ioctl(fileno(ttyout), TIOCGWINSZ, &winsize) != -1)
1.1 millert 749: ttywidth = winsize.ws_col;
750: else
751: ttywidth = 80;
752: }
753:
754: /*
755: * Set the SIGALRM interval timer for wait seconds, 0 to disable.
756: */
757: void
758: alarmtimer(wait)
759: int wait;
760: {
761: struct itimerval itv;
762:
763: itv.it_value.tv_sec = wait;
764: itv.it_value.tv_usec = 0;
765: itv.it_interval = itv.it_value;
766: setitimer(ITIMER_REAL, &itv, NULL);
767: }
1.5 millert 768:
769: /*
770: * Setup or cleanup EditLine structures
771: */
772: #ifndef SMALL
773: void
774: controlediting()
775: {
776: if (editing && el == NULL && hist == NULL) {
1.7 deraadt 777: el = el_init(__progname, stdin, ttyout); /* init editline */
1.5 millert 778: hist = history_init(); /* init the builtin history */
779: history(hist, H_EVENT, 100); /* remember 100 events */
780: el_set(el, EL_HIST, history, hist); /* use history */
781:
782: el_set(el, EL_EDITOR, "emacs"); /* default editor is emacs */
783: el_set(el, EL_PROMPT, prompt); /* set the prompt function */
784:
785: /* add local file completion, bind to TAB */
786: el_set(el, EL_ADDFN, "ftp-complete",
787: "Context sensitive argument completion",
788: complete);
789: el_set(el, EL_BIND, "^I", "ftp-complete", NULL);
790:
791: el_source(el, NULL); /* read ~/.editrc */
792: el_set(el, EL_SIGNAL, 1);
793: } else if (!editing) {
794: if (hist) {
795: history_end(hist);
796: hist = NULL;
797: }
798: if (el) {
799: el_end(el);
800: el = NULL;
801: }
802: }
803: }
804: #endif /* !SMALL */