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