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