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