Home | History | Annotate | Line # | Download | only in ftp
util.c revision 1.10
      1 /*	$NetBSD: util.c,v 1.10 1997/07/20 09:46:03 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.10 1997/07/20 09:46:03 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 		sscanf(reply_string, "%*s %qd", &size);
    458 	else if (noisy && debug == 0)
    459 		puts(reply_string);
    460 	verbose = overbose;
    461 	return (size);
    462 }
    463 
    464 /*
    465  * determine last modification time (in GMT) of remote file
    466  */
    467 time_t
    468 remotemodtime(file, noisy)
    469 	const char *file;
    470 	int noisy;
    471 {
    472 	int overbose;
    473 	time_t rtime;
    474 
    475 	overbose = verbose;
    476 	rtime = -1;
    477 	if (debug == 0)
    478 		verbose = -1;
    479 	if (command("MDTM %s", file) == COMPLETE) {
    480 		struct tm timebuf;
    481 		int yy, mo, day, hour, min, sec;
    482 		sscanf(reply_string, "%*s %04d%02d%02d%02d%02d%02d", &yy, &mo,
    483 			&day, &hour, &min, &sec);
    484 		memset(&timebuf, 0, sizeof(timebuf));
    485 		timebuf.tm_sec = sec;
    486 		timebuf.tm_min = min;
    487 		timebuf.tm_hour = hour;
    488 		timebuf.tm_mday = day;
    489 		timebuf.tm_mon = mo - 1;
    490 		timebuf.tm_year = yy - 1900;
    491 		timebuf.tm_isdst = -1;
    492 		rtime = mktime(&timebuf);
    493 		if (rtime == -1 && (noisy || debug != 0))
    494 			printf("Can't convert %s to a time.\n", reply_string);
    495 		else
    496 			rtime += timebuf.tm_gmtoff;	/* conv. local -> GMT */
    497 	} else if (noisy && debug == 0)
    498 		puts(reply_string);
    499 	verbose = overbose;
    500 	return (rtime);
    501 }
    502 
    503 void updateprogressmeter __P((int));
    504 
    505 void
    506 updateprogressmeter(dummy)
    507 	int dummy;
    508 {
    509 	static pid_t pgrp = -1;
    510 	int ctty_pgrp;
    511 
    512 	if (pgrp == -1)
    513 		pgrp = getpgrp();
    514 
    515 	/*
    516 	 * print progress bar only if we are foreground process.
    517 	 */
    518 	if (ioctl(STDOUT_FILENO, TIOCGPGRP, &ctty_pgrp) != -1 &&
    519 	    ctty_pgrp == (int)pgrp)
    520 		progressmeter(0);
    521 }
    522 
    523 /*
    524  * Display a transfer progress bar if progress is non-zero.
    525  * SIGALRM is hijacked for use by this function.
    526  * - Before the transfer, set filesize to size of file (or -1 if unknown),
    527  *   and call with flag = -1. This starts the once per second timer,
    528  *   and a call to updateprogressmeter() upon SIGALRM.
    529  * - During the transfer, updateprogressmeter will call progressmeter
    530  *   with flag = 0
    531  * - After the transfer, call with flag = 1
    532  */
    533 static struct timeval start;
    534 
    535 void
    536 progressmeter(flag)
    537 	int flag;
    538 {
    539 	/*
    540 	 * List of order of magnitude prefixes.
    541 	 * The last is `P', as 2^64 = 16384 Petabytes
    542 	 */
    543 	static const char prefixes[] = " KMGTP";
    544 
    545 	static struct timeval lastupdate;
    546 	static off_t lastsize;
    547 	struct timeval now, td, wait;
    548 	off_t cursize, abbrevsize;
    549 	double elapsed;
    550 	int ratio, barlength, i, remaining;
    551 	char buf[256];
    552 
    553 	if (flag == -1) {
    554 		(void)gettimeofday(&start, (struct timezone *)0);
    555 		lastupdate = start;
    556 		lastsize = restart_point;
    557 	}
    558 	(void)gettimeofday(&now, (struct timezone *)0);
    559 	if (!progress || filesize <= 0)
    560 		return;
    561 	cursize = bytes + restart_point;
    562 
    563 	ratio = cursize * 100 / filesize;
    564 	ratio = MAX(ratio, 0);
    565 	ratio = MIN(ratio, 100);
    566 	snprintf(buf, sizeof(buf), "\r%3d%% ", ratio);
    567 
    568 	barlength = ttywidth - 30;
    569 	if (barlength > 0) {
    570 		i = barlength * ratio / 100;
    571 		snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
    572 		    "|%.*s%*s|", i,
    573 "*****************************************************************************"
    574 "*****************************************************************************",
    575 		    barlength - i, "");
    576 	}
    577 
    578 	i = 0;
    579 	abbrevsize = cursize;
    580 	while (abbrevsize >= 100000 && i < sizeof(prefixes)) {
    581 		i++;
    582 		abbrevsize >>= 10;
    583 	}
    584 	snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
    585 	    " %5qd %c%c ", abbrevsize, prefixes[i],
    586 	    prefixes[i] == ' ' ? ' ' : 'B');
    587 
    588 	timersub(&now, &lastupdate, &wait);
    589 	if (cursize > lastsize) {
    590 		lastupdate = now;
    591 		lastsize = cursize;
    592 		if (wait.tv_sec >= STALLTIME) {	/* fudge out stalled time */
    593 			start.tv_sec += wait.tv_sec;
    594 			start.tv_usec += wait.tv_usec;
    595 		}
    596 		wait.tv_sec = 0;
    597 	}
    598 
    599 	timersub(&now, &start, &td);
    600 	elapsed = td.tv_sec + (td.tv_usec / 1000000.0);
    601 
    602 	if (bytes <= 0 || elapsed <= 0.0 || cursize > filesize) {
    603 		snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
    604 		    "   --:-- ETA");
    605 	} else if (wait.tv_sec >= STALLTIME) {
    606 		snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
    607 		    " - stalled -");
    608 	} else {
    609 		remaining = (int)((filesize - restart_point) /
    610 				  (bytes / elapsed) - elapsed);
    611 		i = remaining / 3600;
    612 		if (i)
    613 			snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
    614 			    "%2d:", i);
    615 		else
    616 			snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
    617 			    "   ");
    618 		i = remaining % 3600;
    619 		snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
    620 		    "%02d:%02d ETA", i / 60, i % 60);
    621 	}
    622 	(void)write(STDOUT_FILENO, buf, strlen(buf));
    623 
    624 	if (flag == -1) {
    625 		(void)signal(SIGALRM, updateprogressmeter);
    626 		alarmtimer(1);		/* set alarm timer for 1 Hz */
    627 	} else if (flag == 1) {
    628 		alarmtimer(0);
    629 		(void)putchar('\n');
    630 	}
    631 	fflush(stdout);
    632 }
    633 
    634 /*
    635  * Display transfer statistics.
    636  * Requires start to be initialised by progressmeter(-1),
    637  * direction to be defined by xfer routines, and filesize and bytes
    638  * to be updated by xfer routines
    639  * If siginfo is nonzero, an ETA is displayed, and the output goes to STDERR
    640  * instead of STDOUT.
    641  */
    642 void
    643 ptransfer(siginfo)
    644 	int siginfo;
    645 {
    646 	struct timeval now, td;
    647 	double elapsed;
    648 	off_t bs;
    649 	int meg, remaining, hh;
    650 	char buf[100];
    651 
    652 	if (!verbose && !siginfo)
    653 		return;
    654 
    655 	(void)gettimeofday(&now, (struct timezone *)0);
    656 	timersub(&now, &start, &td);
    657 	elapsed = td.tv_sec + (td.tv_usec / 1000000.0);
    658 	bs = bytes / (elapsed == 0.0 ? 1 : elapsed);
    659 	meg = 0;
    660 	if (bs > (1024 * 1024))
    661 		meg = 1;
    662 	(void)snprintf(buf, sizeof(buf),
    663 	    "%qd byte%s %s in %.2f seconds (%.2f %sB/s)\n",
    664 	    bytes, bytes == 1 ? "" : "s", direction, elapsed,
    665 	    bs / (1024.0 * (meg ? 1024.0 : 1.0)), meg ? "M" : "K");
    666 	if (siginfo && bytes > 0 && elapsed > 0.0 && filesize >= 0
    667 	    && bytes + restart_point <= filesize) {
    668 		remaining = (int)((filesize - restart_point) /
    669 				  (bytes / elapsed) - elapsed);
    670 		hh = remaining / 3600;
    671 		remaining %= 3600;
    672 			/* "buf+len(buf) -1" to overwrite \n */
    673 		snprintf(buf + strlen(buf) - 1, sizeof(buf) - strlen(buf),
    674 		    "  ETA: %02d:%02d:%02d\n", hh, remaining / 60,
    675 		    remaining % 60);
    676 	}
    677 	(void)write(siginfo ? STDERR_FILENO : STDOUT_FILENO, buf, strlen(buf));
    678 }
    679 
    680 /*
    681  * List words in stringlist, vertically arranged
    682  */
    683 void
    684 list_vertical(sl)
    685 	StringList *sl;
    686 {
    687 	int i, j, w;
    688 	int columns, width, lines, items;
    689 	char *p;
    690 
    691 	width = items = 0;
    692 
    693 	for (i = 0 ; i < sl->sl_cur ; i++) {
    694 		w = strlen(sl->sl_str[i]);
    695 		if (w > width)
    696 			width = w;
    697 	}
    698 	width = (width + 8) &~ 7;
    699 
    700 	columns = ttywidth / width;
    701 	if (columns == 0)
    702 		columns = 1;
    703 	lines = (sl->sl_cur + columns - 1) / columns;
    704 	for (i = 0; i < lines; i++) {
    705 		for (j = 0; j < columns; j++) {
    706 			p = sl->sl_str[j * lines + i];
    707 			if (p)
    708 				fputs(p, stdout);
    709 			if (j * lines + i + lines >= sl->sl_cur) {
    710 				putchar('\n');
    711 				break;
    712 			}
    713 			w = strlen(p);
    714 			while (w < width) {
    715 				w = (w + 8) &~ 7;
    716 				(void)putchar('\t');
    717 			}
    718 		}
    719 	}
    720 }
    721 
    722 /*
    723  * Update the global ttywidth value, using TIOCGWINSZ.
    724  */
    725 void
    726 setttywidth(a)
    727 	int a;
    728 {
    729 	struct winsize winsize;
    730 
    731 	if (ioctl(fileno(stdout), TIOCGWINSZ, &winsize) != -1)
    732 		ttywidth = winsize.ws_col;
    733 	else
    734 		ttywidth = 80;
    735 }
    736 
    737 /*
    738  * Set the SIGALRM interval timer for wait seconds, 0 to disable.
    739  */
    740 void
    741 alarmtimer(wait)
    742 	int wait;
    743 {
    744 	struct itimerval itv;
    745 
    746 	itv.it_value.tv_sec = wait;
    747 	itv.it_value.tv_usec = 0;
    748 	itv.it_interval = itv.it_value;
    749 	setitimer(ITIMER_REAL, &itv, NULL);
    750 }
    751 
    752 /*
    753  * Setup or cleanup EditLine structures
    754  */
    755 #ifndef SMALL
    756 void
    757 controlediting()
    758 {
    759 	if (editing && el == NULL && hist == NULL) {
    760 		el = el_init(__progname, stdin, stdout); /* init editline */
    761 		hist = history_init();		/* init the builtin history */
    762 		history(hist, H_EVENT, 100);	/* remember 100 events */
    763 		el_set(el, EL_HIST, history, hist);	/* use history */
    764 
    765 		el_set(el, EL_EDITOR, "emacs");	/* default editor is emacs */
    766 		el_set(el, EL_PROMPT, prompt);	/* set the prompt function */
    767 
    768 		/* add local file completion, bind to TAB */
    769 		el_set(el, EL_ADDFN, "ftp-complete",
    770 		    "Context sensitive argument completion",
    771 		    complete);
    772 		el_set(el, EL_BIND, "^I", "ftp-complete", NULL);
    773 
    774 		el_source(el, NULL);	/* read ~/.editrc */
    775 		el_set(el, EL_SIGNAL, 1);
    776 	} else if (!editing) {
    777 		if (hist) {
    778 			history_end(hist);
    779 			hist = NULL;
    780 		}
    781 		if (el) {
    782 			el_end(el);
    783 			el = NULL;
    784 		}
    785 	}
    786 }
    787 #endif /* !SMALL */
    788