Home | History | Annotate | Line # | Download | only in ftp
util.c revision 1.37
      1 /*	$NetBSD: util.c,v 1.37 1998/12/29 14:27:59 lukem Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1998 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
      9  * NASA Ames Research Center.
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions and the following disclaimer.
     16  * 2. Redistributions in binary form must reproduce the above copyright
     17  *    notice, this list of conditions and the following disclaimer in the
     18  *    documentation and/or other materials provided with the distribution.
     19  * 3. All advertising materials mentioning features or use of this software
     20  *    must display the following acknowledgement:
     21  *	This product includes software developed by the NetBSD
     22  *	Foundation, Inc. and its contributors.
     23  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24  *    contributors may be used to endorse or promote products derived
     25  *    from this software without specific prior written permission.
     26  *
     27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37  * POSSIBILITY OF SUCH DAMAGE.
     38  */
     39 
     40 /*
     41  * Copyright (c) 1985, 1989, 1993, 1994
     42  *	The Regents of the University of California.  All rights reserved.
     43  *
     44  * Redistribution and use in source and binary forms, with or without
     45  * modification, are permitted provided that the following conditions
     46  * are met:
     47  * 1. Redistributions of source code must retain the above copyright
     48  *    notice, this list of conditions and the following disclaimer.
     49  * 2. Redistributions in binary form must reproduce the above copyright
     50  *    notice, this list of conditions and the following disclaimer in the
     51  *    documentation and/or other materials provided with the distribution.
     52  * 3. All advertising materials mentioning features or use of this software
     53  *    must display the following acknowledgement:
     54  *	This product includes software developed by the University of
     55  *	California, Berkeley and its contributors.
     56  * 4. Neither the name of the University nor the names of its contributors
     57  *    may be used to endorse or promote products derived from this software
     58  *    without specific prior written permission.
     59  *
     60  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     61  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     62  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     63  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     64  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     65  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     66  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     67  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     68  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     69  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     70  * SUCH DAMAGE.
     71  */
     72 
     73 #include <sys/cdefs.h>
     74 #ifndef lint
     75 __RCSID("$NetBSD: util.c,v 1.37 1998/12/29 14:27:59 lukem Exp $");
     76 #endif /* not lint */
     77 
     78 /*
     79  * FTP User Program -- Misc support routines
     80  */
     81 #include <sys/types.h>
     82 #include <sys/socket.h>
     83 #include <sys/ioctl.h>
     84 #include <sys/time.h>
     85 #include <arpa/ftp.h>
     86 
     87 #include <ctype.h>
     88 #include <err.h>
     89 #include <fcntl.h>
     90 #include <glob.h>
     91 #include <termios.h>
     92 #include <signal.h>
     93 #include <limits.h>
     94 #include <pwd.h>
     95 #include <stdio.h>
     96 #include <stdlib.h>
     97 #include <string.h>
     98 #include <time.h>
     99 #include <tzfile.h>
    100 #include <unistd.h>
    101 
    102 #include "ftp_var.h"
    103 #include "pathnames.h"
    104 
    105 #ifndef HAVE_TIMEGM
    106 static time_t	sub_mkgmt __P((struct tm *tm));
    107 #endif
    108 
    109 /*
    110  * Connect to peer server and
    111  * auto-login, if possible.
    112  */
    113 void
    114 setpeer(argc, argv)
    115 	int argc;
    116 	char *argv[];
    117 {
    118 	char *host;
    119 	in_port_t port;
    120 
    121 	if (connected) {
    122 		fprintf(ttyout, "Already connected to %s, use close first.\n",
    123 		    hostname);
    124 		code = -1;
    125 		return;
    126 	}
    127 	if (argc < 2)
    128 		(void)another(&argc, &argv, "to");
    129 	if (argc < 2 || argc > 3) {
    130 		fprintf(ttyout, "usage: %s host-name [port]\n", argv[0]);
    131 		code = -1;
    132 		return;
    133 	}
    134 	if (gatemode)
    135 		port = gateport;
    136 	else
    137 		port = ftpport;
    138 	if (argc > 2) {
    139 		char *ep;
    140 		long nport;
    141 
    142 		nport = strtol(argv[2], &ep, 10);
    143 		if (nport < 1 || nport > MAX_IN_PORT_T || *ep != '\0') {
    144 			fprintf(ttyout, "%s: bad port number '%s'.\n",
    145 			    argv[1], argv[2]);
    146 			fprintf(ttyout, "usage: %s host-name [port]\n",
    147 			    argv[0]);
    148 			code = -1;
    149 			return;
    150 		}
    151 		port = htons((in_port_t)nport);
    152 	}
    153 
    154 	if (gatemode) {
    155 		if (gateserver == NULL || *gateserver == '\0')
    156 			errx(1, "gateserver not defined (shouldn't happen)");
    157 		host = hookup(gateserver, port);
    158 	} else
    159 		host = hookup(argv[1], port);
    160 
    161 	if (host) {
    162 		int overbose;
    163 
    164 		if (gatemode) {
    165 			if (command("PASSERVE %s", argv[1]) != COMPLETE)
    166 				return;
    167 			if (verbose)
    168 				fprintf(ttyout,
    169 				    "Connected via pass-through server %s\n",
    170 				    gateserver);
    171 		}
    172 
    173 		connected = 1;
    174 		/*
    175 		 * Set up defaults for FTP.
    176 		 */
    177 		(void)strcpy(typename, "ascii"), type = TYPE_A;
    178 		curtype = TYPE_A;
    179 		(void)strcpy(formname, "non-print"), form = FORM_N;
    180 		(void)strcpy(modename, "stream"), mode = MODE_S;
    181 		(void)strcpy(structname, "file"), stru = STRU_F;
    182 		(void)strcpy(bytename, "8"), bytesize = 8;
    183 		if (autologin)
    184 			(void)ftp_login(argv[1], NULL, NULL);
    185 
    186 		overbose = verbose;
    187 		if (debug == 0)
    188 			verbose = -1;
    189 		if (command("SYST") == COMPLETE && overbose) {
    190 			char *cp, c;
    191 			c = 0;
    192 			cp = strchr(reply_string + 4, ' ');
    193 			if (cp == NULL)
    194 				cp = strchr(reply_string + 4, '\r');
    195 			if (cp) {
    196 				if (cp[-1] == '.')
    197 					cp--;
    198 				c = *cp;
    199 				*cp = '\0';
    200 			}
    201 
    202 			fprintf(ttyout, "Remote system type is %s.\n",
    203 			    reply_string + 4);
    204 			if (cp)
    205 				*cp = c;
    206 		}
    207 		if (!strncmp(reply_string, "215 UNIX Type: L8", 17)) {
    208 			if (proxy)
    209 				unix_proxy = 1;
    210 			else
    211 				unix_server = 1;
    212 			/*
    213 			 * Set type to 0 (not specified by user),
    214 			 * meaning binary by default, but don't bother
    215 			 * telling server.  We can use binary
    216 			 * for text files unless changed by the user.
    217 			 */
    218 			type = 0;
    219 			(void)strcpy(typename, "binary");
    220 			if (overbose)
    221 			    fprintf(ttyout,
    222 				"Using %s mode to transfer files.\n",
    223 				typename);
    224 		} else {
    225 			if (proxy)
    226 				unix_proxy = 0;
    227 			else
    228 				unix_server = 0;
    229 			if (overbose &&
    230 			    !strncmp(reply_string, "215 TOPS20", 10))
    231 				fputs(
    232 "Remember to set tenex mode when transferring binary files from this machine.\n",
    233 				    ttyout);
    234 		}
    235 		verbose = overbose;
    236 	}
    237 }
    238 
    239 /*
    240  * login to remote host, using given username & password if supplied
    241  */
    242 int
    243 ftp_login(host, user, pass)
    244 	const char *host;
    245 	const char *user, *pass;
    246 {
    247 	char tmp[80];
    248 	const char *acct;
    249 	char anonpass[MAXLOGNAME + 2]; /* "user@" */
    250 	struct passwd *pw;
    251 	int n, aflag = 0;
    252 
    253 	acct = NULL;
    254 	if (user == NULL) {
    255 		if (ruserpass(host, &user, &pass, &acct) < 0) {
    256 			code = -1;
    257 			return (0);
    258 		}
    259 	}
    260 
    261 	/*
    262 	 * Set up arguments for an anonymous FTP session, if necessary.
    263 	 */
    264 	if ((user == NULL || pass == NULL) && anonftp) {
    265 		memset(anonpass, 0, sizeof(anonpass));
    266 
    267 		/*
    268 		 * Set up anonymous login password.
    269 		 */
    270 		if ((pass = getenv("FTPANONPASS")) == NULL) {
    271 			if ((pass = getlogin()) == NULL) {
    272 				if ((pw = getpwuid(getuid())) == NULL)
    273 					pass = "anonymous";
    274 				else
    275 					pass = pw->pw_name;
    276 			}
    277 			/*
    278 			 * Every anonymous FTP server I've encountered
    279 			 * will accept the string "username@", and will
    280 			 * append the hostname itself.  We do this by default
    281 			 * since many servers are picky about not having
    282 			 * a FQDN in the anonymous password.
    283 			 * - thorpej (at) netbsd.org
    284 			 */
    285 			snprintf(anonpass, sizeof(anonpass) - 1, "%s@", pass);
    286 			pass = anonpass;
    287 		}
    288 		user = "anonymous";	/* as per RFC 1635 */
    289 	}
    290 
    291 	while (user == NULL) {
    292 		const char *myname = getlogin();
    293 
    294 		if (myname == NULL && (pw = getpwuid(getuid())) != NULL)
    295 			myname = pw->pw_name;
    296 		if (myname)
    297 			fprintf(ttyout, "Name (%s:%s): ", host, myname);
    298 		else
    299 			fprintf(ttyout, "Name (%s): ", host);
    300 		*tmp = '\0';
    301 		(void)fgets(tmp, sizeof(tmp) - 1, stdin);
    302 		tmp[strlen(tmp) - 1] = '\0';
    303 		if (*tmp == '\0')
    304 			user = myname;
    305 		else
    306 			user = tmp;
    307 	}
    308 	n = command("USER %s", user);
    309 	if (n == CONTINUE) {
    310 		if (pass == NULL)
    311 			pass = getpass("Password:");
    312 		n = command("PASS %s", pass);
    313 	}
    314 	if (n == CONTINUE) {
    315 		aflag++;
    316 		if (acct == NULL)
    317 			acct = getpass("Account:");
    318 		n = command("ACCT %s", acct);
    319 	}
    320 	if ((n != COMPLETE) ||
    321 	    (!aflag && acct != NULL && command("ACCT %s", acct) != COMPLETE)) {
    322 		warnx("Login failed.");
    323 		return (0);
    324 	}
    325 	if (proxy)
    326 		return (1);
    327 	connected = -1;
    328 	for (n = 0; n < macnum; ++n) {
    329 		if (!strcmp("init", macros[n].mac_name)) {
    330 			(void)strcpy(line, "$init");
    331 			makeargv();
    332 			domacro(margc, margv);
    333 			break;
    334 		}
    335 	}
    336 	return (1);
    337 }
    338 
    339 /*
    340  * `another' gets another argument, and stores the new argc and argv.
    341  * It reverts to the top level (via main.c's intr()) on EOF/error.
    342  *
    343  * Returns false if no new arguments have been added.
    344  */
    345 int
    346 another(pargc, pargv, prompt)
    347 	int *pargc;
    348 	char ***pargv;
    349 	const char *prompt;
    350 {
    351 	int len = strlen(line), ret;
    352 
    353 	if (len >= sizeof(line) - 3) {
    354 		fputs("sorry, arguments too long.\n", ttyout);
    355 		intr();
    356 	}
    357 	fprintf(ttyout, "(%s) ", prompt);
    358 	line[len++] = ' ';
    359 	if (fgets(&line[len], sizeof(line) - len, stdin) == NULL)
    360 		intr();
    361 	len += strlen(&line[len]);
    362 	if (len > 0 && line[len - 1] == '\n')
    363 		line[len - 1] = '\0';
    364 	makeargv();
    365 	ret = margc > *pargc;
    366 	*pargc = margc;
    367 	*pargv = margv;
    368 	return (ret);
    369 }
    370 
    371 /*
    372  * glob files given in argv[] from the remote server.
    373  * if errbuf isn't NULL, store error messages there instead
    374  * of writing to the screen.
    375  */
    376 char *
    377 remglob(argv, doswitch, errbuf)
    378         char *argv[];
    379         int doswitch;
    380 	char **errbuf;
    381 {
    382         char temp[MAXPATHLEN];
    383         static char buf[MAXPATHLEN];
    384         static FILE *ftemp = NULL;
    385         static char **args;
    386         int oldverbose, oldhash, fd;
    387         char *cp, *mode;
    388 
    389         if (!mflag) {
    390                 if (!doglob)
    391                         args = NULL;
    392                 else {
    393                         if (ftemp) {
    394                                 (void)fclose(ftemp);
    395                                 ftemp = NULL;
    396                         }
    397                 }
    398                 return (NULL);
    399         }
    400         if (!doglob) {
    401                 if (args == NULL)
    402                         args = argv;
    403                 if ((cp = *++args) == NULL)
    404                         args = NULL;
    405                 return (cp);
    406         }
    407         if (ftemp == NULL) {
    408                 (void)snprintf(temp, sizeof(temp), "%s/%s", tmpdir, TMPFILE);
    409                 if ((fd = mkstemp(temp)) < 0) {
    410                         warn("unable to create temporary file %s", temp);
    411                         return (NULL);
    412                 }
    413                 close(fd);
    414                 oldverbose = verbose;
    415 		verbose = (errbuf != NULL) ? -1 : 0;
    416                 oldhash = hash;
    417                 hash = 0;
    418                 if (doswitch)
    419                         pswitch(!proxy);
    420                 for (mode = "w"; *++argv != NULL; mode = "a")
    421                         recvrequest("NLST", temp, *argv, mode, 0, 0);
    422 		if ((code / 100) != COMPLETE) {
    423 			if (errbuf != NULL)
    424 				*errbuf = reply_string;
    425 		}
    426                 if (doswitch)
    427                         pswitch(!proxy);
    428                 verbose = oldverbose;
    429 		hash = oldhash;
    430                 ftemp = fopen(temp, "r");
    431                 (void)unlink(temp);
    432                 if (ftemp == NULL) {
    433 			if (errbuf == NULL)
    434 				fputs(
    435 				    "can't find list of remote files, oops.\n",
    436 				    ttyout);
    437 			else
    438 				*errbuf =
    439 				    "can't find list of remote files, oops.";
    440                         return (NULL);
    441                 }
    442         }
    443         if (fgets(buf, sizeof(buf), ftemp) == NULL) {
    444                 (void)fclose(ftemp);
    445 		ftemp = NULL;
    446                 return (NULL);
    447         }
    448         if ((cp = strchr(buf, '\n')) != NULL)
    449                 *cp = '\0';
    450         return (buf);
    451 }
    452 
    453 int
    454 confirm(cmd, file)
    455 	const char *cmd, *file;
    456 {
    457 	char line[BUFSIZ];
    458 
    459 	if (!interactive || confirmrest)
    460 		return (1);
    461 	fprintf(ttyout, "%s %s? ", cmd, file);
    462 	(void)fflush(ttyout);
    463 	if (fgets(line, sizeof(line), stdin) == NULL)
    464 		return (0);
    465 	switch (tolower(*line)) {
    466 		case 'n':
    467 			return (0);
    468 		case 'p':
    469 			interactive = 0;
    470 			fputs("Interactive mode: off.\n", ttyout);
    471 			break;
    472 		case 'a':
    473 			confirmrest = 1;
    474 			fprintf(ttyout, "Prompting off for duration of %s.\n",
    475 			    cmd);
    476 			break;
    477 	}
    478 	return (1);
    479 }
    480 
    481 /*
    482  * Glob a local file name specification with
    483  * the expectation of a single return value.
    484  * Can't control multiple values being expanded
    485  * from the expression, we return only the first.
    486  */
    487 int
    488 globulize(cpp)
    489 	char **cpp;
    490 {
    491 	glob_t gl;
    492 	int flags;
    493 
    494 	if (!doglob)
    495 		return (1);
    496 
    497 	flags = GLOB_BRACE|GLOB_NOCHECK|GLOB_TILDE;
    498 	memset(&gl, 0, sizeof(gl));
    499 	if (glob(*cpp, flags, NULL, &gl) ||
    500 	    gl.gl_pathc == 0) {
    501 		warnx("%s: not found", *cpp);
    502 		globfree(&gl);
    503 		return (0);
    504 	}
    505 		/* XXX: caller should check if *cpp changed, and
    506 		 *	free(*cpp) if that is the case
    507 		 */
    508 	*cpp = xstrdup(gl.gl_pathv[0]);
    509 	globfree(&gl);
    510 	return (1);
    511 }
    512 
    513 /*
    514  * determine size of remote file
    515  */
    516 off_t
    517 remotesize(file, noisy)
    518 	const char *file;
    519 	int noisy;
    520 {
    521 	int overbose;
    522 	off_t size;
    523 
    524 	overbose = verbose;
    525 	size = -1;
    526 	if (debug == 0)
    527 		verbose = -1;
    528 	if (command("SIZE %s", file) == COMPLETE) {
    529 		char *cp, *ep;
    530 
    531 		cp = strchr(reply_string, ' ');
    532 		if (cp != NULL) {
    533 			cp++;
    534 #ifndef NO_QUAD
    535 			size = strtoq(cp, &ep, 10);
    536 #else
    537 			size = strtol(cp, &ep, 10);
    538 #endif
    539 			if (*ep != '\0' && !isspace((unsigned char)*ep))
    540 				size = -1;
    541 		}
    542 	} else if (noisy && debug == 0) {
    543 		fputs(reply_string, ttyout);
    544 		putc('\n', ttyout);
    545 	}
    546 	verbose = overbose;
    547 	return (size);
    548 }
    549 
    550 /*
    551  * determine last modification time (in GMT) of remote file
    552  */
    553 time_t
    554 remotemodtime(file, noisy)
    555 	const char *file;
    556 	int noisy;
    557 {
    558 	int overbose;
    559 	time_t rtime;
    560 	int ocode;
    561 
    562 	overbose = verbose;
    563 	ocode = code;
    564 	rtime = -1;
    565 	if (debug == 0)
    566 		verbose = -1;
    567 	if (command("MDTM %s", file) == COMPLETE) {
    568 		struct tm timebuf;
    569 		int yy, mo, day, hour, min, sec;
    570 		sscanf(reply_string, "%*s %04d%02d%02d%02d%02d%02d", &yy, &mo,
    571 			&day, &hour, &min, &sec);
    572 		memset(&timebuf, 0, sizeof(timebuf));
    573 		timebuf.tm_sec = sec;
    574 		timebuf.tm_min = min;
    575 		timebuf.tm_hour = hour;
    576 		timebuf.tm_mday = day;
    577 		timebuf.tm_mon = mo - 1;
    578 		timebuf.tm_year = yy - TM_YEAR_BASE;
    579 		timebuf.tm_isdst = -1;
    580 		rtime = mkgmtime(&timebuf);
    581 		if (rtime == -1 && (noisy || debug != 0))
    582 			fprintf(ttyout, "Can't convert %s to a time.\n",
    583 			    reply_string);
    584 	} else if (noisy && debug == 0) {
    585 		fputs(reply_string, ttyout);
    586 		putc('\n', ttyout);
    587 	}
    588 	verbose = overbose;
    589 	if (rtime == -1)
    590 		code = ocode;
    591 	return (rtime);
    592 }
    593 
    594 /*
    595  * UTC version of mktime(3)
    596  */
    597 #ifdef HAVE_TIMEGM
    598 time_t
    599 mkgmtime(tm)
    600 	struct tm *tm;
    601 {
    602 
    603 	/* This is very clean, but not portable at all. */
    604 	return (timegm(tm));
    605 }
    606 
    607 #else	/* not HAVE_TIMEGM */
    608 
    609 /*
    610  * This code is not portable, but works on most Unix-like systems.
    611  * If the local timezone has no summer time, using mktime(3) function
    612  * and adjusting offset would be usable (adjusting leap seconds
    613  * is still required, though), but the assumption is not always true.
    614  *
    615  * Anyway, no portable and correct implementation of UTC to time_t
    616  * conversion exists....
    617  */
    618 
    619 static time_t
    620 sub_mkgmt(tm)
    621 	struct tm *tm;
    622 {
    623 	int y, nleapdays;
    624 	time_t t;
    625 	/* days before the month */
    626 	static const unsigned short moff[12] = {
    627 		0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
    628 	};
    629 
    630 	/*
    631 	 * XXX This code assumes the given time to be normalized.
    632 	 * Normalizing here is impossible in case the given time is a leap
    633 	 * second but the local time library is ignorant of leap seconds.
    634 	 */
    635 
    636 	/* minimal sanity checking not to access outside of the array */
    637 	if ((unsigned) tm->tm_mon >= 12)
    638 		return (time_t) -1;
    639 	if (tm->tm_year < EPOCH_YEAR - TM_YEAR_BASE)
    640 		return (time_t) -1;
    641 
    642 	y = tm->tm_year + TM_YEAR_BASE - (tm->tm_mon < 2);
    643 	nleapdays = y / 4 - y / 100 + y / 400 -
    644 	    ((EPOCH_YEAR-1) / 4 - (EPOCH_YEAR-1) / 100 + (EPOCH_YEAR-1) / 400);
    645 	t = ((((time_t) (tm->tm_year - (EPOCH_YEAR - TM_YEAR_BASE)) * 365 +
    646 			moff[tm->tm_mon] + tm->tm_mday - 1 + nleapdays) * 24 +
    647 		tm->tm_hour) * 60 + tm->tm_min) * 60 + tm->tm_sec;
    648 
    649 	return (t < 0 ? (time_t) -1 : t);
    650 }
    651 
    652 time_t
    653 mkgmtime(tm)
    654 	struct tm *tm;
    655 {
    656 	time_t t, t2;
    657 	struct tm *tm2;
    658 	int sec;
    659 
    660 	/* Do the first guess. */
    661 	if ((t = sub_mkgmt(tm)) == (time_t) -1)
    662 		return (time_t) -1;
    663 
    664 	/* save value in case *tm is overwritten by gmtime() */
    665 	sec = tm->tm_sec;
    666 
    667 	tm2 = gmtime(&t);
    668 	if ((t2 = sub_mkgmt(tm2)) == (time_t) -1)
    669 		return (time_t) -1;
    670 
    671 	if (t2 < t || tm2->tm_sec != sec) {
    672 		/*
    673 		 * Adjust for leap seconds.
    674 		 *
    675 		 *     real time_t time
    676 		 *           |
    677 		 *          tm
    678 		 *         /	... (a) first sub_mkgmt() conversion
    679 		 *       t
    680 		 *       |
    681 		 *      tm2
    682 		 *     /	... (b) second sub_mkgmt() conversion
    683 		 *   t2
    684 		 *			--->time
    685 		 */
    686 		/*
    687 		 * Do the second guess, assuming (a) and (b) are almost equal.
    688 		 */
    689 		t += t - t2;
    690 		tm2 = gmtime(&t);
    691 
    692 		/*
    693 		 * Either (a) or (b), may include one or two extra
    694 		 * leap seconds.  Try t, t + 2, t - 2, t + 1, and t - 1.
    695 		 */
    696 		if (tm2->tm_sec == sec
    697 		    || (t += 2, tm2 = gmtime(&t), tm2->tm_sec == sec)
    698 		    || (t -= 4, tm2 = gmtime(&t), tm2->tm_sec == sec)
    699 		    || (t += 3, tm2 = gmtime(&t), tm2->tm_sec == sec)
    700 		    || (t -= 2, tm2 = gmtime(&t), tm2->tm_sec == sec))
    701 			;	/* found */
    702 		else {
    703 			/*
    704 			 * Not found.
    705 			 */
    706 			if (sec >= 60)
    707 				/*
    708 				 * The given time is a leap second
    709 				 * (sec 60 or 61), but the time library
    710 				 * is ignorant of the leap second.
    711 				 */
    712 				;	/* treat sec 60 as 59,
    713 					   sec 61 as 0 of the next minute */
    714 			else
    715 				/* The given time may not be normalized. */
    716 				t++;	/* restore t */
    717 		}
    718 	}
    719 
    720 	return (t < 0 ? (time_t) -1 : t);
    721 }
    722 #endif	/* not HAVE_TIMEGM */
    723 
    724 #ifndef	SMALL
    725 
    726 /*
    727  * return non-zero if we're the current foreground process
    728  */
    729 int
    730 foregroundproc()
    731 {
    732 	static pid_t pgrp = -1;
    733 	int ctty_pgrp;
    734 
    735 	if (pgrp == -1)
    736 		pgrp = getpgrp();
    737 
    738 	return ((ioctl(fileno(ttyout), TIOCGPGRP, &ctty_pgrp) != -1 &&
    739 	    ctty_pgrp == (int)pgrp));
    740 }
    741 
    742 
    743 static void updateprogressmeter __P((int));
    744 
    745 static void
    746 updateprogressmeter(dummy)
    747 	int dummy;
    748 {
    749 
    750 	/*
    751 	 * print progress bar only if we are foreground process.
    752 	 */
    753 	if (foregroundproc())
    754 		progressmeter(0);
    755 }
    756 #endif	/* SMALL */
    757 
    758 /*
    759  * Display a transfer progress bar if progress is non-zero.
    760  * SIGALRM is hijacked for use by this function.
    761  * - Before the transfer, set filesize to size of file (or -1 if unknown),
    762  *   and call with flag = -1. This starts the once per second timer,
    763  *   and a call to updateprogressmeter() upon SIGALRM.
    764  * - During the transfer, updateprogressmeter will call progressmeter
    765  *   with flag = 0
    766  * - After the transfer, call with flag = 1
    767  */
    768 static struct timeval start;
    769 static struct timeval lastupdate;
    770 
    771 void
    772 progressmeter(flag)
    773 	int flag;
    774 {
    775 #ifndef	SMALL
    776 	/*
    777 	 * List of order of magnitude prefixes.
    778 	 * The last is `P', as 2^64 = 16384 Petabytes
    779 	 */
    780 	static const char prefixes[] = "BKMGTP";
    781 
    782 	static off_t lastsize;
    783 	struct timeval now, td, wait;
    784 	off_t cursize, abbrevsize, bytespersec;
    785 	double elapsed;
    786 	int ratio, barlength, i, len, remaining;
    787 	char buf[256];
    788 
    789 	len = 0;
    790 
    791 	if (flag == -1) {
    792 		(void)gettimeofday(&start, NULL);
    793 		lastupdate = start;
    794 		lastsize = restart_point;
    795 	}
    796 	(void)gettimeofday(&now, NULL);
    797 	if (!progress || filesize <= 0)
    798 		return;
    799 	cursize = bytes + restart_point;
    800 
    801 	ratio = (int)((double)cursize * 100.0 / (double)filesize);
    802 	ratio = MAX(ratio, 0);
    803 	ratio = MIN(ratio, 100);
    804 	len += snprintf(buf + len, sizeof(buf) - len, "\r%3d%% ", ratio);
    805 
    806 	barlength = ttywidth - 42;
    807 	if (barlength > 0) {
    808 		i = barlength * ratio / 100;
    809 		len += snprintf(buf + len, sizeof(buf) - len,
    810 		    "|%.*s%*s|", i,
    811 "*****************************************************************************"
    812 "*****************************************************************************",
    813 		    barlength - i, "");
    814 	}
    815 
    816 	abbrevsize = cursize;
    817 	for (i = 0; abbrevsize >= 100000 && i < sizeof(prefixes); i++)
    818 		abbrevsize >>= 10;
    819 	len += snprintf(buf + len, sizeof(buf) - len,
    820 #ifndef NO_QUAD
    821 	    " %5qd %c%c ", (long long)abbrevsize,
    822 #else
    823 	    " %5ld %c%c ", (long)abbrevsize,
    824 #endif
    825 	    i == 0 ? ' ' : prefixes[i],
    826 	    i == 0 ? ' ' : 'B');
    827 
    828 	timersub(&now, &lastupdate, &wait);
    829 	if (cursize > lastsize) {
    830 		lastupdate = now;
    831 		lastsize = cursize;
    832 		if (wait.tv_sec >= STALLTIME) {	/* fudge out stalled time */
    833 			start.tv_sec += wait.tv_sec;
    834 			start.tv_usec += wait.tv_usec;
    835 		}
    836 		wait.tv_sec = 0;
    837 	}
    838 
    839 	timersub(&now, &start, &td);
    840 	elapsed = td.tv_sec + (td.tv_usec / 1000000.0);
    841 
    842 	bytespersec = 0;
    843 	if (bytes > 0) {
    844 		bytespersec = bytes;
    845 		if (elapsed > 0.0)
    846 			bytespersec /= elapsed;
    847 	}
    848 	for (i = 0; bytespersec >= 100000 && i < sizeof(prefixes); i++)
    849 		bytespersec >>= 10;
    850 	len += snprintf(buf + len, sizeof(buf) - len,
    851 #ifndef NO_QUAD
    852 	    " %5qd %c%s ", (long long)bytespersec,
    853 #else
    854 	    " %5ld %c%s ", (long)bytespersec,
    855 #endif
    856 	    prefixes[i],
    857 	    i == 0 ? "/s " : "B/s");
    858 
    859 	if (bytes <= 0 || elapsed <= 0.0 || cursize > filesize) {
    860 		len += snprintf(buf + len, sizeof(buf) - len,
    861 		    "   --:-- ETA");
    862 	} else if (wait.tv_sec >= STALLTIME) {
    863 		len += snprintf(buf + len, sizeof(buf) - len,
    864 		    " - stalled -");
    865 	} else {
    866 		remaining = (int)
    867 		    ((filesize - restart_point) / (bytes / elapsed) - elapsed);
    868 		if (remaining >= 100 * SECSPERHOUR)
    869 			len += snprintf(buf + len, sizeof(buf) - len,
    870 			    "   --:-- ETA");
    871 		else {
    872 			i = remaining / SECSPERHOUR;
    873 			if (i)
    874 				len += snprintf(buf + len, sizeof(buf) - len,
    875 				    "%2d:", i);
    876 			else
    877 				len += snprintf(buf + len, sizeof(buf) - len,
    878 				    "   ");
    879 			i = remaining % SECSPERHOUR;
    880 			len += snprintf(buf + len, sizeof(buf) - len,
    881 			    "%02d:%02d ETA", i / 60, i % 60);
    882 		}
    883 	}
    884 	(void)write(fileno(ttyout), buf, len);
    885 
    886 	if (flag == -1) {
    887 		(void)xsignal(SIGALRM, updateprogressmeter);
    888 		alarmtimer(1);		/* set alarm timer for 1 Hz */
    889 	} else if (flag == 1) {
    890 		(void)xsignal(SIGALRM, SIG_DFL);
    891 		alarmtimer(0);
    892 		(void)putc('\n', ttyout);
    893 	}
    894 	fflush(ttyout);
    895 #endif	/* SMALL */
    896 }
    897 
    898 /*
    899  * Display transfer statistics.
    900  * Requires start to be initialised by progressmeter(-1),
    901  * direction to be defined by xfer routines, and filesize and bytes
    902  * to be updated by xfer routines
    903  * If siginfo is nonzero, an ETA is displayed, and the output goes to stderr
    904  * instead of ttyout.
    905  */
    906 void
    907 ptransfer(siginfo)
    908 	int siginfo;
    909 {
    910 #ifndef	SMALL
    911 	struct timeval now, td, wait;
    912 	double elapsed;
    913 	off_t bytespersec;
    914 	int meg, remaining, hh, len;
    915 	char buf[100];
    916 
    917 	if (!verbose && !siginfo)
    918 		return;
    919 
    920 	(void)gettimeofday(&now, NULL);
    921 	timersub(&now, &start, &td);
    922 	elapsed = td.tv_sec + (td.tv_usec / 1000000.0);
    923 	bytespersec = 0;
    924 	meg = 0;
    925 	if (bytes > 0) {
    926 		bytespersec = bytes;
    927 		if (elapsed > 0.0)
    928 			bytespersec /= elapsed;
    929 		if (bytespersec > (1024 * 1024))
    930 			meg = 1;
    931 	}
    932 	len = 0;
    933 	len += snprintf(buf + len, sizeof(buf) - len,
    934 #ifndef NO_QUAD
    935 	    "%qd byte%s %s in ", (long long)bytes,
    936 #else
    937 	    "%ld byte%s %s in ", (long)bytes,
    938 #endif
    939 	    bytes == 1 ? "" : "s", direction);
    940 	remaining = (int)elapsed;
    941 	if (remaining > SECSPERDAY) {
    942 		int days;
    943 
    944 		days = remaining / SECSPERDAY;
    945 		remaining %= SECSPERDAY;
    946 		len += snprintf(buf + len, sizeof(buf) - len,
    947 		    "%d day%s ", days, days == 1 ? "" : "s");
    948 	}
    949 	hh = remaining / SECSPERHOUR;
    950 	remaining %= SECSPERHOUR;
    951 	if (hh)
    952 		len += snprintf(buf + len, sizeof(buf) - len, "%2d:", hh);
    953 	len += snprintf(buf + len, sizeof(buf) - len,
    954 	    "%02d:%02d (%.2f %sB/s)", remaining / 60, remaining % 60,
    955 	    bytespersec / (1024.0 * (meg ? 1024.0 : 1.0)),
    956 	    meg ? "M" : "K");
    957 
    958 	if (siginfo && bytes > 0 && elapsed > 0.0 && filesize >= 0
    959 	    && bytes + restart_point <= filesize) {
    960 		remaining = (int)((filesize - restart_point) /
    961 				  (bytes / elapsed) - elapsed);
    962 		hh = remaining / SECSPERHOUR;
    963 		remaining %= SECSPERHOUR;
    964 		len += snprintf(buf + len, sizeof(buf) - len, "  ETA: ");
    965 		if (hh)
    966 			len += snprintf(buf + len, sizeof(buf) - len, "%2d:",
    967 			    hh);
    968 		len += snprintf(buf + len, sizeof(buf) - len,
    969 		    "%02d:%02d", remaining / 60, remaining % 60);
    970 		timersub(&now, &lastupdate, &wait);
    971 		if (wait.tv_sec >= STALLTIME)
    972 			len += snprintf(buf + len, sizeof(buf) - len,
    973 			    "  (stalled)");
    974 	}
    975 	len += snprintf(buf + len, sizeof(buf) - len, "\n");
    976 	(void)write(siginfo ? STDERR_FILENO : fileno(ttyout), buf, len);
    977 #endif	/* SMALL */
    978 }
    979 
    980 /*
    981  * List words in stringlist, vertically arranged
    982  */
    983 void
    984 list_vertical(sl)
    985 	StringList *sl;
    986 {
    987 	int i, j, w;
    988 	int columns, width, lines, items;
    989 	char *p;
    990 
    991 	width = items = 0;
    992 
    993 	for (i = 0 ; i < sl->sl_cur ; i++) {
    994 		w = strlen(sl->sl_str[i]);
    995 		if (w > width)
    996 			width = w;
    997 	}
    998 	width = (width + 8) &~ 7;
    999 
   1000 	columns = ttywidth / width;
   1001 	if (columns == 0)
   1002 		columns = 1;
   1003 	lines = (sl->sl_cur + columns - 1) / columns;
   1004 	for (i = 0; i < lines; i++) {
   1005 		for (j = 0; j < columns; j++) {
   1006 			p = sl->sl_str[j * lines + i];
   1007 			if (p)
   1008 				fputs(p, ttyout);
   1009 			if (j * lines + i + lines >= sl->sl_cur) {
   1010 				putc('\n', ttyout);
   1011 				break;
   1012 			}
   1013 			w = strlen(p);
   1014 			while (w < width) {
   1015 				w = (w + 8) &~ 7;
   1016 				(void)putc('\t', ttyout);
   1017 			}
   1018 		}
   1019 	}
   1020 }
   1021 
   1022 /*
   1023  * Update the global ttywidth value, using TIOCGWINSZ.
   1024  */
   1025 void
   1026 setttywidth(a)
   1027 	int a;
   1028 {
   1029 	struct winsize winsize;
   1030 
   1031 	if (ioctl(fileno(ttyout), TIOCGWINSZ, &winsize) != -1 &&
   1032 	    winsize.ws_col != 0)
   1033 		ttywidth = winsize.ws_col;
   1034 	else
   1035 		ttywidth = 80;
   1036 }
   1037 
   1038 /*
   1039  * Set the SIGALRM interval timer for wait seconds, 0 to disable.
   1040  */
   1041 void
   1042 alarmtimer(wait)
   1043 	int wait;
   1044 {
   1045 	struct itimerval itv;
   1046 
   1047 	itv.it_value.tv_sec = wait;
   1048 	itv.it_value.tv_usec = 0;
   1049 	itv.it_interval = itv.it_value;
   1050 	setitimer(ITIMER_REAL, &itv, NULL);
   1051 }
   1052 
   1053 /*
   1054  * Setup or cleanup EditLine structures
   1055  */
   1056 #ifndef SMALL
   1057 void
   1058 controlediting()
   1059 {
   1060 	if (editing && el == NULL && hist == NULL) {
   1061 		HistEvent ev;
   1062 		int editmode;
   1063 
   1064 		el = el_init(__progname, stdin, ttyout, stderr);
   1065 		/* init editline */
   1066 		hist = history_init();		/* init the builtin history */
   1067 		history(hist, &ev, H_SETSIZE, 100);/* remember 100 events */
   1068 		el_set(el, EL_HIST, history, hist);	/* use history */
   1069 
   1070 		el_set(el, EL_EDITOR, "emacs");	/* default editor is emacs */
   1071 		el_set(el, EL_PROMPT, prompt);	/* set the prompt function */
   1072 
   1073 		/* add local file completion, bind to TAB */
   1074 		el_set(el, EL_ADDFN, "ftp-complete",
   1075 		    "Context sensitive argument completion",
   1076 		    complete);
   1077 		el_set(el, EL_BIND, "^I", "ftp-complete", NULL);
   1078 		el_source(el, NULL);	/* read ~/.editrc */
   1079 		if ((el_get(el, EL_EDITMODE, &editmode) != -1) && editmode == 0)
   1080 			editing = 0;	/* the user doesn't want editing,
   1081 					 * so disable, and let statement
   1082 					 * below cleanup */
   1083 		else
   1084 			el_set(el, EL_SIGNAL, 1);
   1085 	}
   1086 	if (!editing) {
   1087 		if (hist) {
   1088 			history_end(hist);
   1089 			hist = NULL;
   1090 		}
   1091 		if (el) {
   1092 			el_end(el);
   1093 			el = NULL;
   1094 		}
   1095 	}
   1096 }
   1097 #endif /* !SMALL */
   1098 
   1099 /*
   1100  * Parse the specified socket buffer size.
   1101  */
   1102 int
   1103 getsockbufsize(arg)
   1104 	const char *arg;
   1105 {
   1106 	char *cp;
   1107 	int val;
   1108 
   1109 	if (!isdigit((unsigned char)arg[0]))
   1110 		return (-1);
   1111 
   1112 	val = strtol(arg, &cp, 10);
   1113 	if (cp != NULL) {
   1114 		if (cp[1] != '\0')
   1115 			 return (-1);
   1116 		if (cp[0] == 'k')
   1117 			val *= 1024;
   1118 		if (cp[0] == 'm')
   1119 			val *= 1024 * 1024;
   1120 	}
   1121 
   1122 	if (val < 0)
   1123 		return (-1);
   1124 
   1125 	return (val);
   1126 }
   1127 
   1128 /*
   1129  * Set up socket buffer sizes before a connection is made.
   1130  */
   1131 void
   1132 setupsockbufsize(sock)
   1133 	int sock;
   1134 {
   1135 	static int sndbuf_default, rcvbuf_default;
   1136 	int len, size;
   1137 
   1138 	/*
   1139 	 * Get the default socket buffer sizes if we don't already
   1140 	 * have them.  It doesn't matter which socket we do this
   1141 	 * to, because on the first call no socket buffer sizes
   1142 	 * will have been modified, so we are guaranteed to get
   1143 	 * the system defaults.
   1144 	 */
   1145 	if (sndbuf_default == 0) {
   1146 		len = sizeof(sndbuf_default);
   1147 		if (getsockopt(sock, SOL_SOCKET, SO_SNDBUF, &sndbuf_default,
   1148 		    &len) < 0)
   1149 			err(1, "unable to get default sndbuf size");
   1150 		len = sizeof(rcvbuf_default);
   1151 		if (getsockopt(sock, SOL_SOCKET, SO_RCVBUF, &rcvbuf_default,
   1152 		    &len) < 0)
   1153 			err(1, "unable to get default rcvbuf size");
   1154 
   1155 	}
   1156 
   1157 	size = sndbuf_size ? sndbuf_size : sndbuf_default;
   1158 	if (setsockopt(sock, SOL_SOCKET, SO_SNDBUF, &size, sizeof(size)) < 0)
   1159 		warn("unable to set sndbuf size %d", size);
   1160 
   1161 	size = rcvbuf_size ? rcvbuf_size : rcvbuf_default;
   1162 	if (setsockopt(sock, SOL_SOCKET, SO_RCVBUF, &size, sizeof(size)) < 0)
   1163 		warn("unable to set rcvbuf size %d", size);
   1164 }
   1165 
   1166 /*
   1167  * If the socket buffer sizes were not set manually (i.e. came from a
   1168  * configuration file), reset them so the right thing will happen on
   1169  * subsequent connections.
   1170  */
   1171 void
   1172 resetsockbufsize()
   1173 {
   1174 
   1175 	if (sndbuf_manual == 0)
   1176 		sndbuf_size = 0;
   1177 	if (rcvbuf_manual == 0)
   1178 		rcvbuf_size = 0;
   1179 }
   1180 
   1181 /*
   1182  * Internal version of connect(2); sets socket buffer sizes first.
   1183  */
   1184 int
   1185 xconnect(sock, name, namelen)
   1186 	int sock;
   1187 	const struct sockaddr *name;
   1188 	int namelen;
   1189 {
   1190 
   1191 	setupsockbufsize(sock);
   1192 	return (connect(sock, name, namelen));
   1193 }
   1194 
   1195 /*
   1196  * Internal version of listen(2); sets socket buffer sizes first.
   1197  */
   1198 int
   1199 xlisten(sock, backlog)
   1200 	int sock, backlog;
   1201 {
   1202 
   1203 	setupsockbufsize(sock);
   1204 	return (listen(sock, backlog));
   1205 }
   1206 
   1207 void *
   1208 xmalloc(size)
   1209 	size_t size;
   1210 {
   1211 	void *p;
   1212 
   1213 	p = malloc(size);
   1214 	if (p == NULL)
   1215 		err(1, "Unable to allocate %ld bytes of memory", (long)size);
   1216 	return (p);
   1217 }
   1218 
   1219 char *
   1220 xstrdup(str)
   1221 	const char *str;
   1222 {
   1223 	char *s;
   1224 
   1225 	if (str == NULL)
   1226 		errx(1, "xstrdup() called with NULL argument");
   1227 	s = strdup(str);
   1228 	if (s == NULL)
   1229 		err(1, "Unable to allocate memory for string copy");
   1230 	return (s);
   1231 }
   1232 
   1233 sig_t
   1234 xsignal(sig, func)
   1235 	int sig;
   1236 	void (*func) __P((int));
   1237 {
   1238 	struct sigaction act, oact;
   1239 
   1240 	act.sa_handler = func;
   1241 	sigemptyset(&act.sa_mask);
   1242 	act.sa_flags = SA_RESTART;
   1243 	if (sigaction(sig, &act, &oact) < 0)
   1244 		return (SIG_ERR);
   1245 	return (oact.sa_handler);
   1246 }
   1247