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