Home | History | Annotate | Line # | Download | only in ftp
cmds.c revision 1.91
      1 /*	$NetBSD: cmds.c,v 1.91 2000/10/11 14:46:03 is Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1996-2000 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Luke Mewburn.
      9  *
     10  * This code is derived from software contributed to The NetBSD Foundation
     11  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
     12  * NASA Ames Research Center.
     13  *
     14  * Redistribution and use in source and binary forms, with or without
     15  * modification, are permitted provided that the following conditions
     16  * are met:
     17  * 1. Redistributions of source code must retain the above copyright
     18  *    notice, this list of conditions and the following disclaimer.
     19  * 2. Redistributions in binary form must reproduce the above copyright
     20  *    notice, this list of conditions and the following disclaimer in the
     21  *    documentation and/or other materials provided with the distribution.
     22  * 3. All advertising materials mentioning features or use of this software
     23  *    must display the following acknowledgement:
     24  *	This product includes software developed by the NetBSD
     25  *	Foundation, Inc. and its contributors.
     26  * 4. Neither the name of The NetBSD Foundation nor the names of its
     27  *    contributors may be used to endorse or promote products derived
     28  *    from this software without specific prior written permission.
     29  *
     30  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     31  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     32  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     33  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     34  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     35  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     36  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     37  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     38  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     39  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     40  * POSSIBILITY OF SUCH DAMAGE.
     41  */
     42 
     43 /*
     44  * Copyright (c) 1985, 1989, 1993, 1994
     45  *	The Regents of the University of California.  All rights reserved.
     46  *
     47  * Redistribution and use in source and binary forms, with or without
     48  * modification, are permitted provided that the following conditions
     49  * are met:
     50  * 1. Redistributions of source code must retain the above copyright
     51  *    notice, this list of conditions and the following disclaimer.
     52  * 2. Redistributions in binary form must reproduce the above copyright
     53  *    notice, this list of conditions and the following disclaimer in the
     54  *    documentation and/or other materials provided with the distribution.
     55  * 3. All advertising materials mentioning features or use of this software
     56  *    must display the following acknowledgement:
     57  *	This product includes software developed by the University of
     58  *	California, Berkeley and its contributors.
     59  * 4. Neither the name of the University nor the names of its contributors
     60  *    may be used to endorse or promote products derived from this software
     61  *    without specific prior written permission.
     62  *
     63  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     64  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     65  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     66  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     67  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     68  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     69  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     70  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     71  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     72  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     73  * SUCH DAMAGE.
     74  */
     75 
     76 /*
     77  * Copyright (C) 1997 and 1998 WIDE Project.
     78  * All rights reserved.
     79  *
     80  * Redistribution and use in source and binary forms, with or without
     81  * modification, are permitted provided that the following conditions
     82  * are met:
     83  * 1. Redistributions of source code must retain the above copyright
     84  *    notice, this list of conditions and the following disclaimer.
     85  * 2. Redistributions in binary form must reproduce the above copyright
     86  *    notice, this list of conditions and the following disclaimer in the
     87  *    documentation and/or other materials provided with the distribution.
     88  * 3. Neither the name of the project nor the names of its contributors
     89  *    may be used to endorse or promote products derived from this software
     90  *    without specific prior written permission.
     91  *
     92  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
     93  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     94  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     95  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
     96  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     97  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     98  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     99  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
    100  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
    101  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
    102  * SUCH DAMAGE.
    103  */
    104 
    105 #include <sys/cdefs.h>
    106 #ifndef lint
    107 #if 0
    108 static char sccsid[] = "@(#)cmds.c	8.6 (Berkeley) 10/9/94";
    109 #else
    110 __RCSID("$NetBSD: cmds.c,v 1.91 2000/10/11 14:46:03 is Exp $");
    111 #endif
    112 #endif /* not lint */
    113 
    114 /*
    115  * FTP User Program -- Command Routines.
    116  */
    117 #include <sys/types.h>
    118 #include <sys/socket.h>
    119 #include <sys/stat.h>
    120 #include <sys/wait.h>
    121 #include <arpa/ftp.h>
    122 
    123 #include <ctype.h>
    124 #include <err.h>
    125 #include <glob.h>
    126 #include <limits.h>
    127 #include <netdb.h>
    128 #include <paths.h>
    129 #include <stdio.h>
    130 #include <stdlib.h>
    131 #include <string.h>
    132 #include <time.h>
    133 #include <unistd.h>
    134 #include <util.h>
    135 
    136 #include "ftp_var.h"
    137 #include "version.h"
    138 
    139 struct	types {
    140 	char	*t_name;
    141 	char	*t_mode;
    142 	int	t_type;
    143 	char	*t_arg;
    144 } types[] = {
    145 	{ "ascii",	"A",	TYPE_A,	0 },
    146 	{ "binary",	"I",	TYPE_I,	0 },
    147 	{ "image",	"I",	TYPE_I,	0 },
    148 	{ "ebcdic",	"E",	TYPE_E,	0 },
    149 	{ "tenex",	"L",	TYPE_L,	bytename },
    150 	{ NULL }
    151 };
    152 
    153 sigjmp_buf	 jabort;
    154 char		*mname;
    155 
    156 static int	confirm(const char *, const char *);
    157 
    158 static int
    159 confirm(const char *cmd, const char *file)
    160 {
    161 	char line[BUFSIZ];
    162 
    163 	if (!interactive || confirmrest)
    164 		return (1);
    165 	while (1) {
    166 		fprintf(ttyout, "%s %s [anpqy?]? ", cmd, file);
    167 		(void)fflush(ttyout);
    168 		if (fgets(line, sizeof(line), stdin) == NULL) {
    169 			mflag = 0;
    170 			fprintf(ttyout, "\nEOF received; %s aborted\n", mname);
    171 			clearerr(stdin);
    172 			return (0);
    173 		}
    174 		switch (tolower(*line)) {
    175 			case 'a':
    176 				confirmrest = 1;
    177 				fprintf(ttyout,
    178 				    "Prompting off for duration of %s.\n", cmd);
    179 				break;
    180 			case 'p':
    181 				interactive = 0;
    182 				fputs("Interactive mode: off.\n", ttyout);
    183 				break;
    184 			case 'q':
    185 				mflag = 0;
    186 				fprintf(ttyout, "%s aborted.\n", mname);
    187 				/* FALLTHROUGH */
    188 			case 'n':
    189 				return (0);
    190 			case '?':
    191 				fprintf(ttyout,
    192 				    "  confirmation options:\n"
    193 				    "\ta  answer `yes' for the duration of %s\n"
    194 				    "\tn  answer `no' for this file\n"
    195 				    "\tp  turn off `prompt' mode\n"
    196 				    "\tq  stop the current %s\n"
    197 				    "\ty  answer `yes' for this file\n"
    198 				    "\t?  this help list\n",
    199 				    cmd, cmd);
    200 				continue;	/* back to while(1) */
    201 		}
    202 		return (1);
    203 	}
    204 	/* NOTREACHED */
    205 }
    206 
    207 /*
    208  * Set transfer type.
    209  */
    210 void
    211 settype(int argc, char *argv[])
    212 {
    213 	struct types *p;
    214 	int comret;
    215 
    216 	if (argc == 0 || argc > 2) {
    217 		char *sep;
    218 
    219 		fprintf(ttyout, "usage: %s [", argv[0]);
    220 		sep = " ";
    221 		for (p = types; p->t_name; p++) {
    222 			fprintf(ttyout, "%s%s", sep, p->t_name);
    223 			sep = " | ";
    224 		}
    225 		fputs(" ]\n", ttyout);
    226 		code = -1;
    227 		return;
    228 	}
    229 	if (argc < 2) {
    230 		fprintf(ttyout, "Using %s mode to transfer files.\n", typename);
    231 		code = 0;
    232 		return;
    233 	}
    234 	for (p = types; p->t_name; p++)
    235 		if (strcmp(argv[1], p->t_name) == 0)
    236 			break;
    237 	if (p->t_name == 0) {
    238 		fprintf(ttyout, "%s: unknown mode.\n", argv[1]);
    239 		code = -1;
    240 		return;
    241 	}
    242 	if ((p->t_arg != NULL) && (*(p->t_arg) != '\0'))
    243 		comret = command("TYPE %s %s", p->t_mode, p->t_arg);
    244 	else
    245 		comret = command("TYPE %s", p->t_mode);
    246 	if (comret == COMPLETE) {
    247 		(void)strlcpy(typename, p->t_name, sizeof(typename));
    248 		curtype = type = p->t_type;
    249 	}
    250 }
    251 
    252 /*
    253  * Internal form of settype; changes current type in use with server
    254  * without changing our notion of the type for data transfers.
    255  * Used to change to and from ascii for listings.
    256  */
    257 void
    258 changetype(int newtype, int show)
    259 {
    260 	struct types *p;
    261 	int comret, oldverbose = verbose;
    262 
    263 	if (newtype == 0)
    264 		newtype = TYPE_I;
    265 	if (newtype == curtype)
    266 		return;
    267 	if (debug == 0 && show == 0)
    268 		verbose = 0;
    269 	for (p = types; p->t_name; p++)
    270 		if (newtype == p->t_type)
    271 			break;
    272 	if (p->t_name == 0) {
    273 		warnx("internal error: unknown type %d.", newtype);
    274 		return;
    275 	}
    276 	if (newtype == TYPE_L && bytename[0] != '\0')
    277 		comret = command("TYPE %s %s", p->t_mode, bytename);
    278 	else
    279 		comret = command("TYPE %s", p->t_mode);
    280 	if (comret == COMPLETE)
    281 		curtype = newtype;
    282 	verbose = oldverbose;
    283 }
    284 
    285 char *stype[] = {
    286 	"type",
    287 	"",
    288 	0
    289 };
    290 
    291 /*
    292  * Set binary transfer type.
    293  */
    294 /*VARARGS*/
    295 void
    296 setbinary(int argc, char *argv[])
    297 {
    298 
    299 	if (argc == 0) {
    300 		fprintf(ttyout, "usage: %s\n", argv[0]);
    301 		code = -1;
    302 		return;
    303 	}
    304 	stype[1] = "binary";
    305 	settype(2, stype);
    306 }
    307 
    308 /*
    309  * Set ascii transfer type.
    310  */
    311 /*VARARGS*/
    312 void
    313 setascii(int argc, char *argv[])
    314 {
    315 
    316 	if (argc == 0) {
    317 		fprintf(ttyout, "usage: %s\n", argv[0]);
    318 		code = -1;
    319 		return;
    320 	}
    321 	stype[1] = "ascii";
    322 	settype(2, stype);
    323 }
    324 
    325 /*
    326  * Set tenex transfer type.
    327  */
    328 /*VARARGS*/
    329 void
    330 settenex(int argc, char *argv[])
    331 {
    332 
    333 	if (argc == 0) {
    334 		fprintf(ttyout, "usage: %s\n", argv[0]);
    335 		code = -1;
    336 		return;
    337 	}
    338 	stype[1] = "tenex";
    339 	settype(2, stype);
    340 }
    341 
    342 /*
    343  * Set file transfer mode.
    344  */
    345 /*ARGSUSED*/
    346 void
    347 setftmode(int argc, char *argv[])
    348 {
    349 
    350 	if (argc != 2) {
    351 		fprintf(ttyout, "usage: %s mode-name\n", argv[0]);
    352 		code = -1;
    353 		return;
    354 	}
    355 	fprintf(ttyout, "We only support %s mode, sorry.\n", modename);
    356 	code = -1;
    357 }
    358 
    359 /*
    360  * Set file transfer format.
    361  */
    362 /*ARGSUSED*/
    363 void
    364 setform(int argc, char *argv[])
    365 {
    366 
    367 	if (argc != 2) {
    368 		fprintf(ttyout, "usage: %s format\n", argv[0]);
    369 		code = -1;
    370 		return;
    371 	}
    372 	fprintf(ttyout, "We only support %s format, sorry.\n", formname);
    373 	code = -1;
    374 }
    375 
    376 /*
    377  * Set file transfer structure.
    378  */
    379 /*ARGSUSED*/
    380 void
    381 setstruct(int argc, char *argv[])
    382 {
    383 
    384 	if (argc != 2) {
    385 		fprintf(ttyout, "usage: %s struct-mode\n", argv[0]);
    386 		code = -1;
    387 		return;
    388 	}
    389 	fprintf(ttyout, "We only support %s structure, sorry.\n", structname);
    390 	code = -1;
    391 }
    392 
    393 /*
    394  * Send a single file.
    395  */
    396 void
    397 put(int argc, char *argv[])
    398 {
    399 	char *cmd;
    400 	int loc = 0;
    401 	char *locfile, *remfile;
    402 
    403 	if (argc == 2) {
    404 		argc++;
    405 		argv[2] = argv[1];
    406 		loc++;
    407 	}
    408 	if (argc == 0 || (argc == 1 && !another(&argc, &argv, "local-file")))
    409 		goto usage;
    410 	if ((argc < 3 && !another(&argc, &argv, "remote-file")) || argc > 3) {
    411  usage:
    412 		fprintf(ttyout, "usage: %s local-file [remote-file]\n",
    413 		    argv[0]);
    414 		code = -1;
    415 		return;
    416 	}
    417 	if ((locfile = globulize(argv[1])) == NULL) {
    418 		code = -1;
    419 		return;
    420 	}
    421 	remfile = argv[2];
    422 	if (loc)	/* If argv[2] is a copy of the old argv[1], update it */
    423 		remfile = locfile;
    424 	cmd = (argv[0][0] == 'a') ? "APPE" : ((sunique) ? "STOU" : "STOR");
    425 	if (loc && ntflag)
    426 		remfile = dotrans(remfile);
    427 	if (loc && mapflag)
    428 		remfile = domap(remfile);
    429 	sendrequest(cmd, locfile, remfile,
    430 	    locfile != argv[1] || remfile != argv[2]);
    431 	free(locfile);
    432 }
    433 
    434 /*
    435  * Send multiple files.
    436  */
    437 void
    438 mput(int argc, char *argv[])
    439 {
    440 	int i;
    441 	sigfunc oldintr;
    442 	int ointer;
    443 	char *tp;
    444 
    445 	if (argc == 0 || (argc == 1 && !another(&argc, &argv, "local-files"))) {
    446 		fprintf(ttyout, "usage: %s local-files\n", argv[0]);
    447 		code = -1;
    448 		return;
    449 	}
    450 	mname = argv[0];
    451 	mflag = 1;
    452 	oldintr = xsignal(SIGINT, mintr);
    453 	if (sigsetjmp(jabort, 1))
    454 		mabort();
    455 	if (proxy) {
    456 		char *cp;
    457 
    458 		while ((cp = remglob(argv, 0, NULL)) != NULL) {
    459 			if (*cp == '\0' || !connected) {
    460 				mflag = 0;
    461 				continue;
    462 			}
    463 			if (mflag && confirm(argv[0], cp)) {
    464 				tp = cp;
    465 				if (mcase)
    466 					tp = docase(tp);
    467 				if (ntflag)
    468 					tp = dotrans(tp);
    469 				if (mapflag)
    470 					tp = domap(tp);
    471 				sendrequest((sunique) ? "STOU" : "STOR",
    472 				    cp, tp, cp != tp || !interactive);
    473 				if (!mflag && fromatty) {
    474 					ointer = interactive;
    475 					interactive = 1;
    476 					if (confirm("Continue with", "mput")) {
    477 						mflag++;
    478 					}
    479 					interactive = ointer;
    480 				}
    481 			}
    482 		}
    483 		goto cleanupmput;
    484 	}
    485 	for (i = 1; i < argc && connected; i++) {
    486 		char **cpp;
    487 		glob_t gl;
    488 		int flags;
    489 
    490 		if (!doglob) {
    491 			if (mflag && confirm(argv[0], argv[i])) {
    492 				tp = (ntflag) ? dotrans(argv[i]) : argv[i];
    493 				tp = (mapflag) ? domap(tp) : tp;
    494 				sendrequest((sunique) ? "STOU" : "STOR",
    495 				    argv[i], tp, tp != argv[i] || !interactive);
    496 				if (!mflag && fromatty) {
    497 					ointer = interactive;
    498 					interactive = 1;
    499 					if (confirm("Continue with", "mput")) {
    500 						mflag++;
    501 					}
    502 					interactive = ointer;
    503 				}
    504 			}
    505 			continue;
    506 		}
    507 
    508 		memset(&gl, 0, sizeof(gl));
    509 		flags = GLOB_BRACE|GLOB_NOCHECK|GLOB_TILDE;
    510 		if (glob(argv[i], flags, NULL, &gl) || gl.gl_pathc == 0) {
    511 			warnx("%s: not found", argv[i]);
    512 			globfree(&gl);
    513 			continue;
    514 		}
    515 		for (cpp = gl.gl_pathv; cpp && *cpp != NULL && connected;
    516 		    cpp++) {
    517 			if (mflag && confirm(argv[0], *cpp)) {
    518 				tp = (ntflag) ? dotrans(*cpp) : *cpp;
    519 				tp = (mapflag) ? domap(tp) : tp;
    520 				sendrequest((sunique) ? "STOU" : "STOR",
    521 				    *cpp, tp, *cpp != tp || !interactive);
    522 				if (!mflag && fromatty) {
    523 					ointer = interactive;
    524 					interactive = 1;
    525 					if (confirm("Continue with", "mput")) {
    526 						mflag++;
    527 					}
    528 					interactive = ointer;
    529 				}
    530 			}
    531 		}
    532 		globfree(&gl);
    533 	}
    534  cleanupmput:
    535 	(void)xsignal(SIGINT, oldintr);
    536 	mflag = 0;
    537 }
    538 
    539 void
    540 reget(int argc, char *argv[])
    541 {
    542 
    543 	(void)getit(argc, argv, 1, "r+w");
    544 }
    545 
    546 void
    547 get(int argc, char *argv[])
    548 {
    549 
    550 	(void)getit(argc, argv, 0, restart_point ? "r+w" : "w" );
    551 }
    552 
    553 /*
    554  * Receive one file.
    555  */
    556 int
    557 getit(int argc, char *argv[], int restartit, const char *mode)
    558 {
    559 	int loc = 0;
    560 	int rval = 0;
    561 	char *remfile, *locfile, *olocfile;
    562 
    563 	if (argc == 2) {
    564 		argc++;
    565 		argv[2] = argv[1];
    566 		loc++;
    567 	}
    568 	if (argc == 0 || (argc == 1 && !another(&argc, &argv, "remote-file")))
    569 		goto usage;
    570 	if ((argc < 3 && !another(&argc, &argv, "local-file")) || argc > 3) {
    571  usage:
    572 		fprintf(ttyout, "usage: %s remote-file [local-file]\n",
    573 		    argv[0]);
    574 		code = -1;
    575 		return (0);
    576 	}
    577 	remfile = argv[1];
    578 	if ((olocfile = globulize(argv[2])) == NULL) {
    579 		code = -1;
    580 		return (0);
    581 	}
    582 	locfile = olocfile;
    583 	if (loc && mcase)
    584 		locfile = docase(locfile);
    585 	if (loc && ntflag)
    586 		locfile = dotrans(locfile);
    587 	if (loc && mapflag)
    588 		locfile = domap(locfile);
    589 	if (restartit) {
    590 		struct stat stbuf;
    591 		int ret;
    592 
    593 		if (! features[FEAT_REST_STREAM]) {
    594 			fprintf(ttyout,
    595 			    "Restart is not supported by the remote server.\n");
    596 			return (0);
    597 		}
    598 		ret = stat(locfile, &stbuf);
    599 		if (restartit == 1) {
    600 			if (ret < 0) {
    601 				warn("local: %s", locfile);
    602 				goto freegetit;
    603 			}
    604 			restart_point = stbuf.st_size;
    605 		} else {
    606 			if (ret == 0) {
    607 				time_t mtime;
    608 
    609 				mtime = remotemodtime(argv[1], 0);
    610 				if (mtime == -1)
    611 					goto freegetit;
    612 				if (stbuf.st_mtime >= mtime) {
    613 					rval = 1;
    614 					goto freegetit;
    615 				}
    616 			}
    617 		}
    618 	}
    619 
    620 	recvrequest("RETR", locfile, remfile, mode,
    621 	    remfile != argv[1] || locfile != argv[2], loc);
    622 	restart_point = 0;
    623  freegetit:
    624 	(void)free(olocfile);
    625 	return (rval);
    626 }
    627 
    628 /* ARGSUSED */
    629 void
    630 mintr(int signo)
    631 {
    632 
    633 	alarmtimer(0);
    634 	if (fromatty)
    635 		write(fileno(ttyout), "\n", 1);
    636 	siglongjmp(jabort, 1);
    637 }
    638 
    639 void
    640 mabort(void)
    641 {
    642 	int ointer, oconf;
    643 
    644 	if (mflag && fromatty) {
    645 		ointer = interactive;
    646 		oconf = confirmrest;
    647 		interactive = 1;
    648 		confirmrest = 0;
    649 		if (confirm("Continue with", mname)) {
    650 			interactive = ointer;
    651 			confirmrest = oconf;
    652 			return;
    653 		}
    654 		interactive = ointer;
    655 		confirmrest = oconf;
    656 	}
    657 	mflag = 0;
    658 }
    659 
    660 /*
    661  * Get multiple files.
    662  */
    663 void
    664 mget(int argc, char *argv[])
    665 {
    666 	sigfunc oldintr;
    667 	int ch, ointer;
    668 	char *cp, *tp, *tp2, tmpbuf[MAXPATHLEN];
    669 
    670 	if (argc == 0 ||
    671 	    (argc == 1 && !another(&argc, &argv, "remote-files"))) {
    672 		fprintf(ttyout, "usage: %s remote-files\n", argv[0]);
    673 		code = -1;
    674 		return;
    675 	}
    676 	mname = argv[0];
    677 	mflag = 1;
    678 	oldintr = xsignal(SIGINT, mintr);
    679 	if (sigsetjmp(jabort, 1))
    680 		mabort();
    681 	while ((cp = remglob(argv, proxy, NULL)) != NULL) {
    682 		if (*cp == '\0' || !connected) {
    683 			mflag = 0;
    684 			continue;
    685 		}
    686 		if (mflag && confirm(argv[0], cp)) {
    687 			tp = cp;
    688 			if (mcase) {
    689 				for (tp2 = tmpbuf; (ch = *tp++) != 0; )
    690 					*tp2++ = isupper(ch) ? tolower(ch) : ch;
    691 				*tp2 = '\0';
    692 				tp = tmpbuf;
    693 			}
    694 			if (ntflag) {
    695 				tp = dotrans(tp);
    696 			}
    697 			if (mapflag) {
    698 				tp = domap(tp);
    699 			}
    700 			recvrequest("RETR", tp, cp, "w",
    701 			    tp != cp || !interactive, 1);
    702 			if (!mflag && fromatty) {
    703 				ointer = interactive;
    704 				interactive = 1;
    705 				if (confirm("Continue with", "mget")) {
    706 					mflag++;
    707 				}
    708 				interactive = ointer;
    709 			}
    710 		}
    711 	}
    712 	(void)xsignal(SIGINT, oldintr);
    713 	mflag = 0;
    714 }
    715 
    716 /*
    717  * Read list of filenames from a local file and get those
    718  */
    719 void
    720 fget(int argc, char *argv[])
    721 {
    722 	char	*buf, *mode;
    723 	FILE	*fp;
    724 
    725 	if (argc != 2) {
    726 		fprintf(ttyout, "usage: %s localfile\n", argv[0]);
    727 		code = -1;
    728 		return;
    729 	}
    730 
    731 	fp = fopen(argv[1], "r");
    732 	if (fp == NULL) {
    733 		fprintf(ttyout, "Cannot open source file %s\n", argv[1]);
    734 		code = -1;
    735 		return;
    736 	}
    737 
    738 	argv[0] = "get";
    739 	mode = restart_point ? "r+w" : "w";
    740 
    741 	for (;
    742 	    (buf = fparseln(fp, NULL, NULL, "\0\0\0", 0)) != NULL;
    743 	    free(buf)) {
    744 		if (buf[0] == '\0')
    745 			continue;
    746 		argv[1] = buf;
    747 		(void)getit(argc, argv, 0, mode);
    748 	}
    749 	fclose(fp);
    750 }
    751 
    752 char *
    753 onoff(int bool)
    754 {
    755 
    756 	return (bool ? "on" : "off");
    757 }
    758 
    759 /*
    760  * Show status.
    761  */
    762 /*ARGSUSED*/
    763 void
    764 status(int argc, char *argv[])
    765 {
    766 	int i;
    767 
    768 	if (argc == 0) {
    769 		fprintf(ttyout, "usage: %s\n", argv[0]);
    770 		code = -1;
    771 		return;
    772 	}
    773 	if (connected)
    774 		fprintf(ttyout, "Connected %sto %s.\n",
    775 		    connected == -1 ? "and logged in" : "", hostname);
    776 	else
    777 		fputs("Not connected.\n", ttyout);
    778 	if (!proxy) {
    779 		pswitch(1);
    780 		if (connected) {
    781 			fprintf(ttyout, "Connected for proxy commands to %s.\n",
    782 			    hostname);
    783 		}
    784 		else {
    785 			fputs("No proxy connection.\n", ttyout);
    786 		}
    787 		pswitch(0);
    788 	}
    789 	fprintf(ttyout, "Gate ftp: %s, server %s, port %s.\n", onoff(gatemode),
    790 	    *gateserver ? gateserver : "(none)", gateport);
    791 	fprintf(ttyout, "Passive mode: %s; fallback to active mode: %s.\n",
    792 	    onoff(passivemode), onoff(activefallback));
    793 	fprintf(ttyout, "Mode: %s; Type: %s; Form: %s; Structure: %s.\n",
    794 	    modename, typename, formname, structname);
    795 	fprintf(ttyout, "Verbose: %s; Bell: %s; Prompting: %s; Globbing: %s.\n",
    796 	    onoff(verbose), onoff(bell), onoff(interactive), onoff(doglob));
    797 	fprintf(ttyout, "Store unique: %s; Receive unique: %s.\n",
    798 	    onoff(sunique), onoff(runique));
    799 	fprintf(ttyout, "Preserve modification times: %s.\n", onoff(preserve));
    800 	fprintf(ttyout, "Case: %s; CR stripping: %s.\n", onoff(mcase),
    801 	    onoff(crflag));
    802 	if (ntflag) {
    803 		fprintf(ttyout, "Ntrans: (in) %s (out) %s\n", ntin, ntout);
    804 	}
    805 	else {
    806 		fputs("Ntrans: off.\n", ttyout);
    807 	}
    808 	if (mapflag) {
    809 		fprintf(ttyout, "Nmap: (in) %s (out) %s\n", mapin, mapout);
    810 	}
    811 	else {
    812 		fputs("Nmap: off.\n", ttyout);
    813 	}
    814 	fprintf(ttyout,
    815 	    "Hash mark printing: %s; Mark count: %d; Progress bar: %s.\n",
    816 	    onoff(hash), mark, onoff(progress));
    817 	fprintf(ttyout,
    818 	    "Get transfer rate throttle: %s; maximum: %d; increment %d.\n",
    819 	    onoff(rate_get), rate_get, rate_get_incr);
    820 	fprintf(ttyout,
    821 	    "Put transfer rate throttle: %s; maximum: %d; increment %d.\n",
    822 	    onoff(rate_put), rate_put, rate_put_incr);
    823 	fprintf(ttyout,
    824 	    "Socket buffer sizes: send %d, receive %d.\n",
    825 	    sndbuf_size, rcvbuf_size);
    826 	fprintf(ttyout, "Use of PORT cmds: %s.\n", onoff(sendport));
    827 	fprintf(ttyout, "Use of EPSV/EPRT cmds for IPv4: %s%s.\n", onoff(epsv4),
    828 	    epsv4bad ? " (disabled for this connection)" : "");
    829 	fprintf(ttyout, "Command line editing: %s.\n",
    830 #ifdef NO_EDITCOMPLETE
    831 	    "support not compiled in"
    832 #else	/* !def NO_EDITCOMPLETE */
    833 	    onoff(editing)
    834 #endif	/* !def NO_EDITCOMPLETE */
    835 	    );
    836 	fprintf(ttyout, "Version: %s %s\n", FTP_PRODUCT, FTP_VERSION);
    837 	if (macnum > 0) {
    838 		fputs("Macros:\n", ttyout);
    839 		for (i=0; i<macnum; i++) {
    840 			fprintf(ttyout, "\t%s\n", macros[i].mac_name);
    841 		}
    842 	}
    843 	code = 0;
    844 }
    845 
    846 /*
    847  * Toggle a variable
    848  */
    849 int
    850 togglevar(int argc, char *argv[], int *var, const char *mesg)
    851 {
    852 	if (argc == 1) {
    853 		*var = !*var;
    854 	} else if (argc == 2 && strcasecmp(argv[1], "on") == 0) {
    855 		*var = 1;
    856 	} else if (argc == 2 && strcasecmp(argv[1], "off") == 0) {
    857 		*var = 0;
    858 	} else {
    859 		fprintf(ttyout, "usage: %s [ on | off ]\n", argv[0]);
    860 		return (-1);
    861 	}
    862 	if (mesg)
    863 		fprintf(ttyout, "%s %s.\n", mesg, onoff(*var));
    864 	return (*var);
    865 }
    866 
    867 /*
    868  * Set beep on cmd completed mode.
    869  */
    870 /*VARARGS*/
    871 void
    872 setbell(int argc, char *argv[])
    873 {
    874 
    875 	code = togglevar(argc, argv, &bell, "Bell mode");
    876 }
    877 
    878 /*
    879  * Set command line editing
    880  */
    881 /*VARARGS*/
    882 void
    883 setedit(int argc, char *argv[])
    884 {
    885 
    886 #ifdef NO_EDITCOMPLETE
    887 	if (argc == 0) {
    888 		fprintf(ttyout, "usage: %s\n", argv[0]);
    889 		code = -1;
    890 		return;
    891 	}
    892 	if (verbose)
    893 		fputs("Editing support not compiled in; ignoring command.\n",
    894 		    ttyout);
    895 #else	/* !def NO_EDITCOMPLETE */
    896 	code = togglevar(argc, argv, &editing, "Editing mode");
    897 	controlediting();
    898 #endif	/* !def NO_EDITCOMPLETE */
    899 }
    900 
    901 /*
    902  * Turn on packet tracing.
    903  */
    904 /*VARARGS*/
    905 void
    906 settrace(int argc, char *argv[])
    907 {
    908 
    909 	code = togglevar(argc, argv, &trace, "Packet tracing");
    910 }
    911 
    912 /*
    913  * Toggle hash mark printing during transfers, or set hash mark bytecount.
    914  */
    915 /*VARARGS*/
    916 void
    917 sethash(int argc, char *argv[])
    918 {
    919 	if (argc == 1)
    920 		hash = !hash;
    921 	else if (argc != 2) {
    922 		fprintf(ttyout, "usage: %s [ on | off | bytecount ]\n",
    923 		    argv[0]);
    924 		code = -1;
    925 		return;
    926 	} else if (strcasecmp(argv[1], "on") == 0)
    927 		hash = 1;
    928 	else if (strcasecmp(argv[1], "off") == 0)
    929 		hash = 0;
    930 	else {
    931 		int nmark;
    932 
    933 		nmark = strsuftoi(argv[1]);
    934 		if (nmark < 1) {
    935 			fprintf(ttyout, "mark: bad bytecount value `%s'.\n",
    936 			    argv[1]);
    937 			code = -1;
    938 			return;
    939 		}
    940 		mark = nmark;
    941 		hash = 1;
    942 	}
    943 	fprintf(ttyout, "Hash mark printing %s", onoff(hash));
    944 	if (hash)
    945 		fprintf(ttyout, " (%d bytes/hash mark)", mark);
    946 	fputs(".\n", ttyout);
    947 	if (hash)
    948 		progress = 0;
    949 	code = hash;
    950 }
    951 
    952 /*
    953  * Turn on printing of server echo's.
    954  */
    955 /*VARARGS*/
    956 void
    957 setverbose(int argc, char *argv[])
    958 {
    959 
    960 	code = togglevar(argc, argv, &verbose, "Verbose mode");
    961 }
    962 
    963 /*
    964  * Toggle PORT/LPRT cmd use before each data connection.
    965  */
    966 /*VARARGS*/
    967 void
    968 setport(int argc, char *argv[])
    969 {
    970 
    971 	code = togglevar(argc, argv, &sendport, "Use of PORT/LPRT cmds");
    972 }
    973 
    974 /*
    975  * Toggle transfer progress bar.
    976  */
    977 /*VARARGS*/
    978 void
    979 setprogress(int argc, char *argv[])
    980 {
    981 
    982 	code = togglevar(argc, argv, &progress, "Progress bar");
    983 	if (progress)
    984 		hash = 0;
    985 }
    986 
    987 /*
    988  * Turn on interactive prompting during mget, mput, and mdelete.
    989  */
    990 /*VARARGS*/
    991 void
    992 setprompt(int argc, char *argv[])
    993 {
    994 
    995 	code = togglevar(argc, argv, &interactive, "Interactive mode");
    996 }
    997 
    998 /*
    999  * Toggle gate-ftp mode, or set gate-ftp server
   1000  */
   1001 /*VARARGS*/
   1002 void
   1003 setgate(int argc, char *argv[])
   1004 {
   1005 	static char gsbuf[MAXHOSTNAMELEN];
   1006 
   1007 	if (argc == 0 || argc > 3) {
   1008 		fprintf(ttyout,
   1009 		    "usage: %s [ on | off | gateserver [port] ]\n", argv[0]);
   1010 		code = -1;
   1011 		return;
   1012 	} else if (argc < 2) {
   1013 		gatemode = !gatemode;
   1014 	} else {
   1015 		if (argc == 2 && strcasecmp(argv[1], "on") == 0)
   1016 			gatemode = 1;
   1017 		else if (argc == 2 && strcasecmp(argv[1], "off") == 0)
   1018 			gatemode = 0;
   1019 		else {
   1020 			if (argc == 3)
   1021 				gateport = strdup(argv[2]);
   1022 			(void)strlcpy(gsbuf, argv[1], sizeof(gsbuf));
   1023 			gateserver = gsbuf;
   1024 			gatemode = 1;
   1025 		}
   1026 	}
   1027 	if (gatemode && (gateserver == NULL || *gateserver == '\0')) {
   1028 		fprintf(ttyout,
   1029 		    "Disabling gate-ftp mode - no gate-ftp server defined.\n");
   1030 		gatemode = 0;
   1031 	} else {
   1032 		fprintf(ttyout, "Gate ftp: %s, server %s, port %s.\n",
   1033 		    onoff(gatemode), *gateserver ? gateserver : "(none)",
   1034 		    gateport);
   1035 	}
   1036 	code = gatemode;
   1037 }
   1038 
   1039 /*
   1040  * Toggle metacharacter interpretation on local file names.
   1041  */
   1042 /*VARARGS*/
   1043 void
   1044 setglob(int argc, char *argv[])
   1045 {
   1046 
   1047 	code = togglevar(argc, argv, &doglob, "Globbing");
   1048 }
   1049 
   1050 /*
   1051  * Toggle preserving modification times on retrieved files.
   1052  */
   1053 /*VARARGS*/
   1054 void
   1055 setpreserve(int argc, char *argv[])
   1056 {
   1057 
   1058 	code = togglevar(argc, argv, &preserve, "Preserve modification times");
   1059 }
   1060 
   1061 /*
   1062  * Set debugging mode on/off and/or set level of debugging.
   1063  */
   1064 /*VARARGS*/
   1065 void
   1066 setdebug(int argc, char *argv[])
   1067 {
   1068 	if (argc == 0 || argc > 2) {
   1069 		fprintf(ttyout, "usage: %s [ on | off | debuglevel ]\n",
   1070 		    argv[0]);
   1071 		code = -1;
   1072 		return;
   1073 	} else if (argc == 2) {
   1074 		if (strcasecmp(argv[1], "on") == 0)
   1075 			debug = 1;
   1076 		else if (strcasecmp(argv[1], "off") == 0)
   1077 			debug = 0;
   1078 		else {
   1079 			int val;
   1080 
   1081 			val = strsuftoi(argv[1]);
   1082 			if (val < 0) {
   1083 				fprintf(ttyout, "%s: bad debugging value.\n",
   1084 				    argv[1]);
   1085 				code = -1;
   1086 				return;
   1087 			}
   1088 			debug = val;
   1089 		}
   1090 	} else
   1091 		debug = !debug;
   1092 	if (debug)
   1093 		options |= SO_DEBUG;
   1094 	else
   1095 		options &= ~SO_DEBUG;
   1096 	fprintf(ttyout, "Debugging %s (debug=%d).\n", onoff(debug), debug);
   1097 	code = debug > 0;
   1098 }
   1099 
   1100 /*
   1101  * Set current working directory on remote machine.
   1102  */
   1103 void
   1104 cd(int argc, char *argv[])
   1105 {
   1106 	int r;
   1107 
   1108 	if (argc == 0 || argc > 2 ||
   1109 	    (argc == 1 && !another(&argc, &argv, "remote-directory"))) {
   1110 		fprintf(ttyout, "usage: %s remote-directory\n", argv[0]);
   1111 		code = -1;
   1112 		return;
   1113 	}
   1114 	r = command("CWD %s", argv[1]);
   1115 	if (r == ERROR && code == 500) {
   1116 		if (verbose)
   1117 			fputs("CWD command not recognized, trying XCWD.\n",
   1118 			    ttyout);
   1119 		r = command("XCWD %s", argv[1]);
   1120 	}
   1121 	if (r == COMPLETE) {
   1122 		dirchange = 1;
   1123 		updateremotepwd();
   1124 	}
   1125 }
   1126 
   1127 /*
   1128  * Set current working directory on local machine.
   1129  */
   1130 void
   1131 lcd(int argc, char *argv[])
   1132 {
   1133 	char buf[MAXPATHLEN];
   1134 	char *locdir;
   1135 
   1136 	code = -1;
   1137 	if (argc == 1) {
   1138 		argc++;
   1139 		argv[1] = home;
   1140 	}
   1141 	if (argc != 2) {
   1142 		fprintf(ttyout, "usage: %s [local-directory]\n", argv[0]);
   1143 		return;
   1144 	}
   1145 	if ((locdir = globulize(argv[1])) == NULL)
   1146 		return;
   1147 	if (chdir(locdir) < 0)
   1148 		warn("local: %s", locdir);
   1149 	else {
   1150 		if (getcwd(buf, sizeof(buf)) != NULL) {
   1151 			fprintf(ttyout, "Local directory now %s\n", buf);
   1152 			code = 0;
   1153 		} else
   1154 			warn("getcwd: %s", locdir);
   1155 	}
   1156 	(void)free(locdir);
   1157 }
   1158 
   1159 /*
   1160  * Delete a single file.
   1161  */
   1162 void
   1163 delete(int argc, char *argv[])
   1164 {
   1165 
   1166 
   1167 	if (argc == 0 || argc > 2 ||
   1168 	    (argc == 1 && !another(&argc, &argv, "remote-file"))) {
   1169 		fprintf(ttyout, "usage: %s remote-file\n", argv[0]);
   1170 		code = -1;
   1171 		return;
   1172 	}
   1173 	(void)command("DELE %s", argv[1]);
   1174 }
   1175 
   1176 /*
   1177  * Delete multiple files.
   1178  */
   1179 void
   1180 mdelete(int argc, char *argv[])
   1181 {
   1182 	sigfunc oldintr;
   1183 	int ointer;
   1184 	char *cp;
   1185 
   1186 	if (argc == 0 ||
   1187 	    (argc == 1 && !another(&argc, &argv, "remote-files"))) {
   1188 		fprintf(ttyout, "usage: %s [remote-files]\n", argv[0]);
   1189 		code = -1;
   1190 		return;
   1191 	}
   1192 	mname = argv[0];
   1193 	mflag = 1;
   1194 	oldintr = xsignal(SIGINT, mintr);
   1195 	if (sigsetjmp(jabort, 1))
   1196 		mabort();
   1197 	while ((cp = remglob(argv, 0, NULL)) != NULL) {
   1198 		if (*cp == '\0') {
   1199 			mflag = 0;
   1200 			continue;
   1201 		}
   1202 		if (mflag && confirm(argv[0], cp)) {
   1203 			(void)command("DELE %s", cp);
   1204 			if (!mflag && fromatty) {
   1205 				ointer = interactive;
   1206 				interactive = 1;
   1207 				if (confirm("Continue with", "mdelete")) {
   1208 					mflag++;
   1209 				}
   1210 				interactive = ointer;
   1211 			}
   1212 		}
   1213 	}
   1214 	(void)xsignal(SIGINT, oldintr);
   1215 	mflag = 0;
   1216 }
   1217 
   1218 /*
   1219  * Rename a remote file.
   1220  */
   1221 void
   1222 renamefile(int argc, char *argv[])
   1223 {
   1224 
   1225 	if (argc == 0 || (argc == 1 && !another(&argc, &argv, "from-name")))
   1226 		goto usage;
   1227 	if ((argc < 3 && !another(&argc, &argv, "to-name")) || argc > 3) {
   1228  usage:
   1229 		fprintf(ttyout, "usage: %s from-name to-name\n", argv[0]);
   1230 		code = -1;
   1231 		return;
   1232 	}
   1233 	if (command("RNFR %s", argv[1]) == CONTINUE)
   1234 		(void)command("RNTO %s", argv[2]);
   1235 }
   1236 
   1237 /*
   1238  * Get a directory listing of remote files.
   1239  * Supports being invoked as:
   1240  *	cmd		runs
   1241  *	---		----
   1242  *	dir, ls		LIST
   1243  *	mlsd		MLSD
   1244  *	nlist		NLST
   1245  *	pdir, pls	LIST |$PAGER
   1246  *	mmlsd		MLSD |$PAGER
   1247  */
   1248 void
   1249 ls(int argc, char *argv[])
   1250 {
   1251 	const char *cmd;
   1252 	char *remdir, *locfile;
   1253 	int freelocfile, pagecmd, mlsdcmd;
   1254 
   1255 	remdir = NULL;
   1256 	locfile = "-";
   1257 	freelocfile = pagecmd = mlsdcmd = 0;
   1258 			/*
   1259 			 * the only commands that start with `p' are
   1260 			 * the `pager' versions.
   1261 			 */
   1262 	if (argv[0][0] == 'p')
   1263 		pagecmd = 1;
   1264 	if (strcmp(argv[0] + pagecmd , "mlsd") == 0) {
   1265 		if (! features[FEAT_MLST]) {
   1266 			fprintf(ttyout,
   1267 			   "MLSD is not supported by the remote server.\n");
   1268 			return;
   1269 		}
   1270 		mlsdcmd = 1;
   1271 	}
   1272 	if (argc == 0)
   1273 		goto usage;
   1274 
   1275 	if (mlsdcmd)
   1276 		cmd = "MLSD";
   1277 	else if (strcmp(argv[0] + pagecmd, "nlist") == 0)
   1278 		cmd = "NLST";
   1279 	else
   1280 		cmd = "LIST";
   1281 
   1282 	if (argc > 1)
   1283 		remdir = argv[1];
   1284 	if (argc > 2)
   1285 		locfile = argv[2];
   1286 	if (argc > 3 || ((pagecmd | mlsdcmd) && argc > 2)) {
   1287  usage:
   1288 		if (pagecmd || mlsdcmd)
   1289 			fprintf(ttyout,
   1290 			    "usage: %s [remote-path]\n", argv[0]);
   1291 		else
   1292 			fprintf(ttyout,
   1293 			    "usage: %s [remote-path [local-file]]\n",
   1294 			    argv[0]);
   1295 		code = -1;
   1296 		goto freels;
   1297 	}
   1298 
   1299 	if (pagecmd) {
   1300 		char *p;
   1301 		int len;
   1302 
   1303 		p = getoptionvalue("pager");
   1304 		if (EMPTYSTRING(p))
   1305 			p = DEFAULTPAGER;
   1306 		len = strlen(p) + 2;
   1307 		locfile = xmalloc(len);
   1308 		locfile[0] = '|';
   1309 		(void)strlcpy(locfile + 1, p, len - 1);
   1310 		freelocfile = 1;
   1311 	} else if ((strcmp(locfile, "-") != 0) && *locfile != '|') {
   1312 		if ((locfile = globulize(locfile)) == NULL ||
   1313 		    !confirm("output to local-file:", locfile)) {
   1314 			code = -1;
   1315 			goto freels;
   1316 		}
   1317 		freelocfile = 1;
   1318 	}
   1319 	recvrequest(cmd, locfile, remdir, "w", 0, 0);
   1320  freels:
   1321 	if (freelocfile && locfile)
   1322 		(void)free(locfile);
   1323 }
   1324 
   1325 /*
   1326  * Get a directory listing of multiple remote files.
   1327  */
   1328 void
   1329 mls(int argc, char *argv[])
   1330 {
   1331 	sigfunc oldintr;
   1332 	int ointer, i;
   1333 	int dolist;
   1334 	char mode[1], *dest, *odest;
   1335 
   1336 	if (argc == 0)
   1337 		goto usage;
   1338 	if (argc < 2 && !another(&argc, &argv, "remote-files"))
   1339 		goto usage;
   1340 	if (argc < 3 && !another(&argc, &argv, "local-file")) {
   1341  usage:
   1342 		fprintf(ttyout, "usage: %s remote-files local-file\n", argv[0]);
   1343 		code = -1;
   1344 		return;
   1345 	}
   1346 	odest = dest = argv[argc - 1];
   1347 	argv[argc - 1] = NULL;
   1348 	if (strcmp(dest, "-") && *dest != '|')
   1349 		if (((dest = globulize(dest)) == NULL) ||
   1350 		    !confirm("output to local-file:", dest)) {
   1351 			code = -1;
   1352 			return;
   1353 	}
   1354 	dolist = strcmp(argv[0], "mls");
   1355 	mname = argv[0];
   1356 	mflag = 1;
   1357 	oldintr = xsignal(SIGINT, mintr);
   1358 	if (sigsetjmp(jabort, 1))
   1359 		mabort();
   1360 	for (i = 1; mflag && i < argc-1 && connected; i++) {
   1361 		*mode = (i == 1) ? 'w' : 'a';
   1362 		recvrequest(dolist ? "LIST" : "NLST", dest, argv[i], mode,
   1363 		    0, 0);
   1364 		if (!mflag && fromatty) {
   1365 			ointer = interactive;
   1366 			interactive = 1;
   1367 			if (confirm("Continue with", argv[0])) {
   1368 				mflag ++;
   1369 			}
   1370 			interactive = ointer;
   1371 		}
   1372 	}
   1373 	(void)xsignal(SIGINT, oldintr);
   1374 	mflag = 0;
   1375 	if (dest != odest)			/* free up after globulize() */
   1376 		free(dest);
   1377 }
   1378 
   1379 /*
   1380  * Do a shell escape
   1381  */
   1382 /*ARGSUSED*/
   1383 void
   1384 shell(int argc, char *argv[])
   1385 {
   1386 	pid_t pid;
   1387 	sigfunc old1;
   1388 	char shellnam[MAXPATHLEN], *shell, *namep;
   1389 	int wait_status;
   1390 
   1391 	if (argc == 0) {
   1392 		fprintf(ttyout, "usage: %s [command [args]]\n", argv[0]);
   1393 		code = -1;
   1394 		return;
   1395 	}
   1396 	old1 = xsignal(SIGINT, SIG_IGN);
   1397 	if ((pid = fork()) == 0) {
   1398 		for (pid = 3; pid < 20; pid++)
   1399 			(void)close(pid);
   1400 		(void)xsignal(SIGINT, SIG_DFL);
   1401 		shell = getenv("SHELL");
   1402 		if (shell == NULL)
   1403 			shell = _PATH_BSHELL;
   1404 		namep = strrchr(shell, '/');
   1405 		if (namep == NULL)
   1406 			namep = shell;
   1407 		else
   1408 			namep++;
   1409 		(void)strlcpy(shellnam, namep, sizeof(shellnam));
   1410 		if (debug) {
   1411 			fputs(shell, ttyout);
   1412 			putc('\n', ttyout);
   1413 		}
   1414 		if (argc > 1) {
   1415 			execl(shell, shellnam, "-c", altarg, (char *)0);
   1416 		}
   1417 		else {
   1418 			execl(shell, shellnam, (char *)0);
   1419 		}
   1420 		warn("%s", shell);
   1421 		code = -1;
   1422 		exit(1);
   1423 	}
   1424 	if (pid > 0)
   1425 		while (wait(&wait_status) != pid)
   1426 			;
   1427 	(void)xsignal(SIGINT, old1);
   1428 	if (pid == -1) {
   1429 		warn("Try again later");
   1430 		code = -1;
   1431 	} else
   1432 		code = 0;
   1433 }
   1434 
   1435 /*
   1436  * Send new user information (re-login)
   1437  */
   1438 void
   1439 user(int argc, char *argv[])
   1440 {
   1441 	char acct[80];
   1442 	int n, aflag = 0;
   1443 
   1444 	if (argc == 0)
   1445 		goto usage;
   1446 	if (argc < 2)
   1447 		(void)another(&argc, &argv, "username");
   1448 	if (argc < 2 || argc > 4) {
   1449  usage:
   1450 		fprintf(ttyout, "usage: %s username [password [account]]\n",
   1451 		    argv[0]);
   1452 		code = -1;
   1453 		return;
   1454 	}
   1455 	n = command("USER %s", argv[1]);
   1456 	if (n == CONTINUE) {
   1457 		if (argc < 3) {
   1458 			argv[2] = getpass("Password: ");
   1459 			argc++;
   1460 		}
   1461 		n = command("PASS %s", argv[2]);
   1462 	}
   1463 	if (n == CONTINUE) {
   1464 		if (argc < 4) {
   1465 			(void)fputs("Account: ", ttyout);
   1466 			(void)fflush(ttyout);
   1467 			if (fgets(acct, sizeof(acct) - 1, stdin) == NULL) {
   1468 				fprintf(ttyout,
   1469 				    "\nEOF received; login aborted.\n");
   1470 				clearerr(stdin);
   1471 				code = -1;
   1472 				return;
   1473 			}
   1474 			acct[strlen(acct) - 1] = '\0';
   1475 			argv[3] = acct; argc++;
   1476 		}
   1477 		n = command("ACCT %s", argv[3]);
   1478 		aflag++;
   1479 	}
   1480 	if (n != COMPLETE) {
   1481 		fputs("Login failed.\n", ttyout);
   1482 		return;
   1483 	}
   1484 	if (!aflag && argc == 4) {
   1485 		(void)command("ACCT %s", argv[3]);
   1486 	}
   1487 	connected = -1;
   1488 	getremoteinfo();
   1489 }
   1490 
   1491 /*
   1492  * Print working directory on remote machine.
   1493  */
   1494 /*VARARGS*/
   1495 void
   1496 pwd(int argc, char *argv[])
   1497 {
   1498 	int oldverbose = verbose;
   1499 
   1500 	if (argc == 0) {
   1501 		fprintf(ttyout, "usage: %s\n", argv[0]);
   1502 		code = -1;
   1503 		return;
   1504 	}
   1505 	verbose = 1;	/* If we aren't verbose, this doesn't do anything! */
   1506 	if (command("PWD") == ERROR && code == 500) {
   1507 		fputs("PWD command not recognized, trying XPWD.\n", ttyout);
   1508 		(void)command("XPWD");
   1509 	}
   1510 	verbose = oldverbose;
   1511 }
   1512 
   1513 /*
   1514  * Print working directory on local machine.
   1515  */
   1516 void
   1517 lpwd(int argc, char *argv[])
   1518 {
   1519 	char buf[MAXPATHLEN];
   1520 
   1521 	if (argc == 0) {
   1522 		fprintf(ttyout, "usage: %s\n", argv[0]);
   1523 		code = -1;
   1524 		return;
   1525 	}
   1526 	if (getcwd(buf, sizeof(buf)) != NULL) {
   1527 		fprintf(ttyout, "Local directory %s\n", buf);
   1528 		code = 0;
   1529 	} else {
   1530 		warn("getcwd");
   1531 		code = -1;
   1532 	}
   1533 }
   1534 
   1535 /*
   1536  * Make a directory.
   1537  */
   1538 void
   1539 makedir(int argc, char *argv[])
   1540 {
   1541 
   1542 	if (argc == 0 || argc > 2 ||
   1543 	    (argc == 1 && !another(&argc, &argv, "directory-name"))) {
   1544 		fprintf(ttyout, "usage: %s directory-name\n", argv[0]);
   1545 		code = -1;
   1546 		return;
   1547 	}
   1548 	if (command("MKD %s", argv[1]) == ERROR && code == 500) {
   1549 		if (verbose)
   1550 			fputs("MKD command not recognized, trying XMKD.\n",
   1551 			    ttyout);
   1552 		(void)command("XMKD %s", argv[1]);
   1553 	}
   1554 }
   1555 
   1556 /*
   1557  * Remove a directory.
   1558  */
   1559 void
   1560 removedir(int argc, char *argv[])
   1561 {
   1562 
   1563 	if (argc == 0 || argc > 2 ||
   1564 	    (argc == 1 && !another(&argc, &argv, "directory-name"))) {
   1565 		fprintf(ttyout, "usage: %s directory-name\n", argv[0]);
   1566 		code = -1;
   1567 		return;
   1568 	}
   1569 	if (command("RMD %s", argv[1]) == ERROR && code == 500) {
   1570 		if (verbose)
   1571 			fputs("RMD command not recognized, trying XRMD.\n",
   1572 			    ttyout);
   1573 		(void)command("XRMD %s", argv[1]);
   1574 	}
   1575 }
   1576 
   1577 /*
   1578  * Send a line, verbatim, to the remote machine.
   1579  */
   1580 void
   1581 quote(int argc, char *argv[])
   1582 {
   1583 
   1584 	if (argc == 0 ||
   1585 	    (argc == 1 && !another(&argc, &argv, "command line to send"))) {
   1586 		fprintf(ttyout, "usage: %s line-to-send\n", argv[0]);
   1587 		code = -1;
   1588 		return;
   1589 	}
   1590 	quote1("", argc, argv);
   1591 }
   1592 
   1593 /*
   1594  * Send a SITE command to the remote machine.  The line
   1595  * is sent verbatim to the remote machine, except that the
   1596  * word "SITE" is added at the front.
   1597  */
   1598 void
   1599 site(int argc, char *argv[])
   1600 {
   1601 
   1602 	if (argc == 0 ||
   1603 	    (argc == 1 && !another(&argc, &argv, "arguments to SITE command"))){
   1604 		fprintf(ttyout, "usage: %s line-to-send\n", argv[0]);
   1605 		code = -1;
   1606 		return;
   1607 	}
   1608 	quote1("SITE ", argc, argv);
   1609 }
   1610 
   1611 /*
   1612  * Turn argv[1..argc) into a space-separated string, then prepend initial text.
   1613  * Send the result as a one-line command and get response.
   1614  */
   1615 void
   1616 quote1(const char *initial, int argc, char *argv[])
   1617 {
   1618 	int i;
   1619 	char buf[BUFSIZ];		/* must be >= sizeof(line) */
   1620 
   1621 	(void)strlcpy(buf, initial, sizeof(buf));
   1622 	for (i = 1; i < argc; i++) {
   1623 		(void)strlcat(buf, argv[i], sizeof(buf));
   1624 		if (i < (argc - 1))
   1625 			(void)strlcat(buf, " ", sizeof(buf));
   1626 	}
   1627 	if (command("%s", buf) == PRELIM) {
   1628 		while (getreply(0) == PRELIM)
   1629 			continue;
   1630 	}
   1631 }
   1632 
   1633 void
   1634 do_chmod(int argc, char *argv[])
   1635 {
   1636 
   1637 	if (argc == 0 || (argc == 1 && !another(&argc, &argv, "mode")))
   1638 		goto usage;
   1639 	if ((argc < 3 && !another(&argc, &argv, "remote-file")) || argc > 3) {
   1640  usage:
   1641 		fprintf(ttyout, "usage: %s mode remote-file\n", argv[0]);
   1642 		code = -1;
   1643 		return;
   1644 	}
   1645 	(void)command("SITE CHMOD %s %s", argv[1], argv[2]);
   1646 }
   1647 
   1648 #define COMMAND_1ARG(argc, argv, cmd) 			\
   1649 	if (argc == 1)					\
   1650 		command(cmd);				\
   1651 	else						\
   1652 		command(cmd " %s", argv[1])
   1653 
   1654 void
   1655 do_umask(int argc, char *argv[])
   1656 {
   1657 	int oldverbose = verbose;
   1658 
   1659 	if (argc == 0) {
   1660 		fprintf(ttyout, "usage: %s [umask]\n", argv[0]);
   1661 		code = -1;
   1662 		return;
   1663 	}
   1664 	verbose = 1;
   1665 	COMMAND_1ARG(argc, argv, "SITE UMASK");
   1666 	verbose = oldverbose;
   1667 }
   1668 
   1669 void
   1670 idlecmd(int argc, char *argv[])
   1671 {
   1672 	int oldverbose = verbose;
   1673 
   1674 	if (argc < 1 || argc > 2) {
   1675 		fprintf(ttyout, "usage: %s [seconds]\n", argv[0]);
   1676 		code = -1;
   1677 		return;
   1678 	}
   1679 	verbose = 1;
   1680 	COMMAND_1ARG(argc, argv, "SITE IDLE");
   1681 	verbose = oldverbose;
   1682 }
   1683 
   1684 /*
   1685  * Ask the other side for help.
   1686  */
   1687 void
   1688 rmthelp(int argc, char *argv[])
   1689 {
   1690 	int oldverbose = verbose;
   1691 
   1692 	if (argc == 0) {
   1693 		fprintf(ttyout, "usage: %s\n", argv[0]);
   1694 		code = -1;
   1695 		return;
   1696 	}
   1697 	verbose = 1;
   1698 	COMMAND_1ARG(argc, argv, "HELP");
   1699 	verbose = oldverbose;
   1700 }
   1701 
   1702 /*
   1703  * Terminate session and exit.
   1704  * May be called with 0, NULL.
   1705  */
   1706 /*VARARGS*/
   1707 void
   1708 quit(int argc, char *argv[])
   1709 {
   1710 
   1711 			/* this may be called with argc == 0, argv == NULL */
   1712 	if (argc == 0 && argv != NULL) {
   1713 		fprintf(ttyout, "usage: %s\n", argv[0]);
   1714 		code = -1;
   1715 		return;
   1716 	}
   1717 	if (connected)
   1718 		disconnect(0, NULL);
   1719 	pswitch(1);
   1720 	if (connected)
   1721 		disconnect(0, NULL);
   1722 	exit(0);
   1723 }
   1724 
   1725 /*
   1726  * Terminate session, but don't exit.
   1727  * May be called with 0, NULL.
   1728  */
   1729 void
   1730 disconnect(int argc, char *argv[])
   1731 {
   1732 
   1733 			/* this may be called with argc == 0, argv == NULL */
   1734 	if (argc == 0 && argv != NULL) {
   1735 		fprintf(ttyout, "usage: %s\n", argv[0]);
   1736 		code = -1;
   1737 		return;
   1738 	}
   1739 	if (!connected)
   1740 		return;
   1741 	(void)command("QUIT");
   1742 	cleanuppeer();
   1743 }
   1744 
   1745 void
   1746 account(int argc, char *argv[])
   1747 {
   1748 	char *ap;
   1749 
   1750 	if (argc == 0 || argc > 2) {
   1751 		fprintf(ttyout, "usage: %s [password]\n", argv[0]);
   1752 		code = -1;
   1753 		return;
   1754 	}
   1755 	else if (argc == 2)
   1756 		ap = argv[1];
   1757 	else
   1758 		ap = getpass("Account:");
   1759 	(void)command("ACCT %s", ap);
   1760 }
   1761 
   1762 sigjmp_buf abortprox;
   1763 
   1764 void
   1765 proxabort(int notused)
   1766 {
   1767 
   1768 	alarmtimer(0);
   1769 	if (!proxy) {
   1770 		pswitch(1);
   1771 	}
   1772 	if (connected) {
   1773 		proxflag = 1;
   1774 	}
   1775 	else {
   1776 		proxflag = 0;
   1777 	}
   1778 	pswitch(0);
   1779 	siglongjmp(abortprox, 1);
   1780 }
   1781 
   1782 void
   1783 doproxy(int argc, char *argv[])
   1784 {
   1785 	struct cmd *c;
   1786 	int cmdpos;
   1787 	sigfunc oldintr;
   1788 
   1789 	if (argc == 0 || (argc == 1 && !another(&argc, &argv, "command"))) {
   1790 		fprintf(ttyout, "usage: %s command\n", argv[0]);
   1791 		code = -1;
   1792 		return;
   1793 	}
   1794 	c = getcmd(argv[1]);
   1795 	if (c == (struct cmd *) -1) {
   1796 		fputs("?Ambiguous command.\n", ttyout);
   1797 		code = -1;
   1798 		return;
   1799 	}
   1800 	if (c == 0) {
   1801 		fputs("?Invalid command.\n", ttyout);
   1802 		code = -1;
   1803 		return;
   1804 	}
   1805 	if (!c->c_proxy) {
   1806 		fputs("?Invalid proxy command.\n", ttyout);
   1807 		code = -1;
   1808 		return;
   1809 	}
   1810 	if (sigsetjmp(abortprox, 1)) {
   1811 		code = -1;
   1812 		return;
   1813 	}
   1814 	oldintr = xsignal(SIGINT, proxabort);
   1815 	pswitch(1);
   1816 	if (c->c_conn && !connected) {
   1817 		fputs("Not connected.\n", ttyout);
   1818 		pswitch(0);
   1819 		(void)xsignal(SIGINT, oldintr);
   1820 		code = -1;
   1821 		return;
   1822 	}
   1823 	cmdpos = strcspn(line, " \t");
   1824 	if (cmdpos > 0)		/* remove leading "proxy " from input buffer */
   1825 		memmove(line, line + cmdpos + 1, strlen(line) - cmdpos + 1);
   1826 	(*c->c_handler)(argc-1, argv+1);
   1827 	if (connected) {
   1828 		proxflag = 1;
   1829 	}
   1830 	else {
   1831 		proxflag = 0;
   1832 	}
   1833 	pswitch(0);
   1834 	(void)xsignal(SIGINT, oldintr);
   1835 }
   1836 
   1837 void
   1838 setcase(int argc, char *argv[])
   1839 {
   1840 
   1841 	code = togglevar(argc, argv, &mcase, "Case mapping");
   1842 }
   1843 
   1844 /*
   1845  * convert the given name to lower case if it's all upper case, into
   1846  * a static buffer which is returned to the caller
   1847  */
   1848 char *
   1849 docase(char *name)
   1850 {
   1851 	static char new[MAXPATHLEN];
   1852 	int i, dochange;
   1853 
   1854 	dochange = 1;
   1855 	for (i = 0; name[i] != '\0' && i < sizeof(new) - 1; i++) {
   1856 		new[i] = name[i];
   1857 		if (islower((unsigned char)new[i]))
   1858 			dochange = 0;
   1859 	}
   1860 	new[i] = '\0';
   1861 
   1862 	if (dochange) {
   1863 		for (i = 0; new[i] != '\0'; i++)
   1864 			if (isupper((unsigned char)new[i]))
   1865 				new[i] = tolower(new[i]);
   1866 	}
   1867 	return (new);
   1868 }
   1869 
   1870 void
   1871 setcr(int argc, char *argv[])
   1872 {
   1873 
   1874 	code = togglevar(argc, argv, &crflag, "Carriage Return stripping");
   1875 }
   1876 
   1877 void
   1878 setntrans(int argc, char *argv[])
   1879 {
   1880 
   1881 	if (argc == 0 || argc > 3) {
   1882 		fprintf(ttyout, "usage: %s [inchars [outchars]]\n", argv[0]);
   1883 		code = -1;
   1884 		return;
   1885 	}
   1886 	if (argc == 1) {
   1887 		ntflag = 0;
   1888 		fputs("Ntrans off.\n", ttyout);
   1889 		code = ntflag;
   1890 		return;
   1891 	}
   1892 	ntflag++;
   1893 	code = ntflag;
   1894 	(void)strlcpy(ntin, argv[1], sizeof(ntin));
   1895 	if (argc == 2) {
   1896 		ntout[0] = '\0';
   1897 		return;
   1898 	}
   1899 	(void)strlcpy(ntout, argv[2], sizeof(ntout));
   1900 }
   1901 
   1902 char *
   1903 dotrans(char *name)
   1904 {
   1905 	static char new[MAXPATHLEN];
   1906 	char *cp1, *cp2 = new;
   1907 	int i, ostop, found;
   1908 
   1909 	for (ostop = 0; *(ntout + ostop) && ostop < 16; ostop++)
   1910 		continue;
   1911 	for (cp1 = name; *cp1; cp1++) {
   1912 		found = 0;
   1913 		for (i = 0; *(ntin + i) && i < 16; i++) {
   1914 			if (*cp1 == *(ntin + i)) {
   1915 				found++;
   1916 				if (i < ostop) {
   1917 					*cp2++ = *(ntout + i);
   1918 				}
   1919 				break;
   1920 			}
   1921 		}
   1922 		if (!found) {
   1923 			*cp2++ = *cp1;
   1924 		}
   1925 	}
   1926 	*cp2 = '\0';
   1927 	return (new);
   1928 }
   1929 
   1930 void
   1931 setnmap(int argc, char *argv[])
   1932 {
   1933 	char *cp;
   1934 
   1935 	if (argc == 1) {
   1936 		mapflag = 0;
   1937 		fputs("Nmap off.\n", ttyout);
   1938 		code = mapflag;
   1939 		return;
   1940 	}
   1941 	if (argc == 0 ||
   1942 	    (argc < 3 && !another(&argc, &argv, "mapout")) || argc > 3) {
   1943 		fprintf(ttyout, "usage: %s [mapin mapout]\n", argv[0]);
   1944 		code = -1;
   1945 		return;
   1946 	}
   1947 	mapflag = 1;
   1948 	code = 1;
   1949 	cp = strchr(altarg, ' ');
   1950 	if (proxy) {
   1951 		while(*++cp == ' ')
   1952 			continue;
   1953 		altarg = cp;
   1954 		cp = strchr(altarg, ' ');
   1955 	}
   1956 	*cp = '\0';
   1957 	(void)strlcpy(mapin, altarg, MAXPATHLEN);
   1958 	while (*++cp == ' ')
   1959 		continue;
   1960 	(void)strlcpy(mapout, cp, MAXPATHLEN);
   1961 }
   1962 
   1963 char *
   1964 domap(char *name)
   1965 {
   1966 	static char new[MAXPATHLEN];
   1967 	char *cp1 = name, *cp2 = mapin;
   1968 	char *tp[9], *te[9];
   1969 	int i, toks[9], toknum = 0, match = 1;
   1970 
   1971 	for (i=0; i < 9; ++i) {
   1972 		toks[i] = 0;
   1973 	}
   1974 	while (match && *cp1 && *cp2) {
   1975 		switch (*cp2) {
   1976 			case '\\':
   1977 				if (*++cp2 != *cp1) {
   1978 					match = 0;
   1979 				}
   1980 				break;
   1981 			case '$':
   1982 				if (*(cp2+1) >= '1' && (*cp2+1) <= '9') {
   1983 					if (*cp1 != *(++cp2+1)) {
   1984 						toks[toknum = *cp2 - '1']++;
   1985 						tp[toknum] = cp1;
   1986 						while (*++cp1 && *(cp2+1)
   1987 							!= *cp1);
   1988 						te[toknum] = cp1;
   1989 					}
   1990 					cp2++;
   1991 					break;
   1992 				}
   1993 				/* FALLTHROUGH */
   1994 			default:
   1995 				if (*cp2 != *cp1) {
   1996 					match = 0;
   1997 				}
   1998 				break;
   1999 		}
   2000 		if (match && *cp1) {
   2001 			cp1++;
   2002 		}
   2003 		if (match && *cp2) {
   2004 			cp2++;
   2005 		}
   2006 	}
   2007 	if (!match && *cp1) /* last token mismatch */
   2008 	{
   2009 		toks[toknum] = 0;
   2010 	}
   2011 	cp1 = new;
   2012 	*cp1 = '\0';
   2013 	cp2 = mapout;
   2014 	while (*cp2) {
   2015 		match = 0;
   2016 		switch (*cp2) {
   2017 			case '\\':
   2018 				if (*(cp2 + 1)) {
   2019 					*cp1++ = *++cp2;
   2020 				}
   2021 				break;
   2022 			case '[':
   2023 LOOP:
   2024 				if (*++cp2 == '$' &&
   2025 				    isdigit((unsigned char)*(cp2+1))) {
   2026 					if (*++cp2 == '0') {
   2027 						char *cp3 = name;
   2028 
   2029 						while (*cp3) {
   2030 							*cp1++ = *cp3++;
   2031 						}
   2032 						match = 1;
   2033 					}
   2034 					else if (toks[toknum = *cp2 - '1']) {
   2035 						char *cp3 = tp[toknum];
   2036 
   2037 						while (cp3 != te[toknum]) {
   2038 							*cp1++ = *cp3++;
   2039 						}
   2040 						match = 1;
   2041 					}
   2042 				}
   2043 				else {
   2044 					while (*cp2 && *cp2 != ',' &&
   2045 					    *cp2 != ']') {
   2046 						if (*cp2 == '\\') {
   2047 							cp2++;
   2048 						}
   2049 						else if (*cp2 == '$' &&
   2050 						    isdigit((unsigned char)*(cp2+1))) {
   2051 							if (*++cp2 == '0') {
   2052 							   char *cp3 = name;
   2053 
   2054 							   while (*cp3) {
   2055 								*cp1++ = *cp3++;
   2056 							   }
   2057 							}
   2058 							else if (toks[toknum =
   2059 							    *cp2 - '1']) {
   2060 							   char *cp3=tp[toknum];
   2061 
   2062 							   while (cp3 !=
   2063 								  te[toknum]) {
   2064 								*cp1++ = *cp3++;
   2065 							   }
   2066 							}
   2067 						}
   2068 						else if (*cp2) {
   2069 							*cp1++ = *cp2++;
   2070 						}
   2071 					}
   2072 					if (!*cp2) {
   2073 						fputs(
   2074 						"nmap: unbalanced brackets.\n",
   2075 						    ttyout);
   2076 						return (name);
   2077 					}
   2078 					match = 1;
   2079 					cp2--;
   2080 				}
   2081 				if (match) {
   2082 					while (*++cp2 && *cp2 != ']') {
   2083 					      if (*cp2 == '\\' && *(cp2 + 1)) {
   2084 							cp2++;
   2085 					      }
   2086 					}
   2087 					if (!*cp2) {
   2088 						fputs(
   2089 						"nmap: unbalanced brackets.\n",
   2090 						    ttyout);
   2091 						return (name);
   2092 					}
   2093 					break;
   2094 				}
   2095 				switch (*++cp2) {
   2096 					case ',':
   2097 						goto LOOP;
   2098 					case ']':
   2099 						break;
   2100 					default:
   2101 						cp2--;
   2102 						goto LOOP;
   2103 				}
   2104 				break;
   2105 			case '$':
   2106 				if (isdigit((unsigned char)*(cp2 + 1))) {
   2107 					if (*++cp2 == '0') {
   2108 						char *cp3 = name;
   2109 
   2110 						while (*cp3) {
   2111 							*cp1++ = *cp3++;
   2112 						}
   2113 					}
   2114 					else if (toks[toknum = *cp2 - '1']) {
   2115 						char *cp3 = tp[toknum];
   2116 
   2117 						while (cp3 != te[toknum]) {
   2118 							*cp1++ = *cp3++;
   2119 						}
   2120 					}
   2121 					break;
   2122 				}
   2123 				/* intentional drop through */
   2124 			default:
   2125 				*cp1++ = *cp2;
   2126 				break;
   2127 		}
   2128 		cp2++;
   2129 	}
   2130 	*cp1 = '\0';
   2131 	if (!*new) {
   2132 		return (name);
   2133 	}
   2134 	return (new);
   2135 }
   2136 
   2137 void
   2138 setpassive(int argc, char *argv[])
   2139 {
   2140 
   2141 	if (argc == 1) {
   2142 		passivemode = !passivemode;
   2143 		activefallback = passivemode;
   2144 	} else if (argc != 2) {
   2145  passiveusage:
   2146 		fprintf(ttyout, "usage: %s [ on | off | auto ]\n", argv[0]);
   2147 		code = -1;
   2148 		return;
   2149 	} else if (strcasecmp(argv[1], "on") == 0) {
   2150 		passivemode = 1;
   2151 		activefallback = 0;
   2152 	} else if (strcasecmp(argv[1], "off") == 0) {
   2153 		passivemode = 0;
   2154 		activefallback = 0;
   2155 	} else if (strcasecmp(argv[1], "auto") == 0) {
   2156 		passivemode = 1;
   2157 		activefallback = 1;
   2158 	} else
   2159 		goto passiveusage;
   2160 	fprintf(ttyout, "Passive mode: %s; fallback to active mode: %s.\n",
   2161 	    onoff(passivemode), onoff(activefallback));
   2162 	code = passivemode;
   2163 }
   2164 
   2165 void
   2166 setepsv4(int argc, char *argv[])
   2167 {
   2168 
   2169 	code = togglevar(argc, argv, &epsv4,
   2170 	    verbose ? "EPSV/EPRT on IPv4" : NULL);
   2171 	epsv4bad = 0;
   2172 }
   2173 
   2174 void
   2175 setsunique(int argc, char *argv[])
   2176 {
   2177 
   2178 	code = togglevar(argc, argv, &sunique, "Store unique");
   2179 }
   2180 
   2181 void
   2182 setrunique(int argc, char *argv[])
   2183 {
   2184 
   2185 	code = togglevar(argc, argv, &runique, "Receive unique");
   2186 }
   2187 
   2188 int
   2189 parserate(int argc, char *argv[], int cmdlineopt)
   2190 {
   2191 	int dir, max, incr, showonly;
   2192 	sigfunc oldusr1, oldusr2;
   2193 
   2194 	if (argc > 4 || (argc < (cmdlineopt ? 3 : 2))) {
   2195  usage:
   2196 		if (cmdlineopt)
   2197 			fprintf(ttyout,
   2198 	"usage: %s (all|get|put),maximum-bytes[,increment-bytes]]\n",
   2199 			    argv[0]);
   2200 		else
   2201 			fprintf(ttyout,
   2202 	"usage: %s (all|get|put) [maximum-bytes [increment-bytes]]\n",
   2203 			    argv[0]);
   2204 		return -1;
   2205 	}
   2206 	dir = max = incr = showonly = 0;
   2207 #define	RATE_GET	1
   2208 #define	RATE_PUT	2
   2209 #define	RATE_ALL	(RATE_GET | RATE_PUT)
   2210 
   2211 	if (strcasecmp(argv[1], "all") == 0)
   2212 		dir = RATE_ALL;
   2213 	else if (strcasecmp(argv[1], "get") == 0)
   2214 		dir = RATE_GET;
   2215 	else if (strcasecmp(argv[1], "put") == 0)
   2216 		dir = RATE_PUT;
   2217 	else
   2218 		goto usage;
   2219 
   2220 	if (argc >= 3) {
   2221 		if ((max = strsuftoi(argv[2])) < 0)
   2222 			goto usage;
   2223 	} else
   2224 		showonly = 1;
   2225 
   2226 	if (argc == 4) {
   2227 		if ((incr = strsuftoi(argv[3])) <= 0)
   2228 			goto usage;
   2229 	} else
   2230 		incr = DEFAULTINCR;
   2231 
   2232 	oldusr1 = xsignal(SIGUSR1, SIG_IGN);
   2233 	oldusr2 = xsignal(SIGUSR2, SIG_IGN);
   2234 	if (dir & RATE_GET) {
   2235 		if (!showonly) {
   2236 			rate_get = max;
   2237 			rate_get_incr = incr;
   2238 		}
   2239 		if (!cmdlineopt || verbose)
   2240 			fprintf(ttyout,
   2241 		"Get transfer rate throttle: %s; maximum: %d; increment %d.\n",
   2242 			    onoff(rate_get), rate_get, rate_get_incr);
   2243 	}
   2244 	if (dir & RATE_PUT) {
   2245 		if (!showonly) {
   2246 			rate_put = max;
   2247 			rate_put_incr = incr;
   2248 		}
   2249 		if (!cmdlineopt || verbose)
   2250 			fprintf(ttyout,
   2251 		"Put transfer rate throttle: %s; maximum: %d; increment %d.\n",
   2252 			    onoff(rate_put), rate_put, rate_put_incr);
   2253 	}
   2254 	(void)xsignal(SIGUSR1, oldusr1);
   2255 	(void)xsignal(SIGUSR2, oldusr2);
   2256 	return 0;
   2257 }
   2258 
   2259 void
   2260 setrate(int argc, char *argv[])
   2261 {
   2262 
   2263 	code = parserate(argc, argv, 0);
   2264 }
   2265 
   2266 /* change directory to parent directory */
   2267 void
   2268 cdup(int argc, char *argv[])
   2269 {
   2270 	int r;
   2271 
   2272 	if (argc == 0) {
   2273 		fprintf(ttyout, "usage: %s\n", argv[0]);
   2274 		code = -1;
   2275 		return;
   2276 	}
   2277 	r = command("CDUP");
   2278 	if (r == ERROR && code == 500) {
   2279 		if (verbose)
   2280 			fputs("CDUP command not recognized, trying XCUP.\n",
   2281 			    ttyout);
   2282 		r = command("XCUP");
   2283 	}
   2284 	if (r == COMPLETE) {
   2285 		dirchange = 1;
   2286 		updateremotepwd();
   2287 	}
   2288 }
   2289 
   2290 /*
   2291  * Restart transfer at specific point
   2292  */
   2293 void
   2294 restart(int argc, char *argv[])
   2295 {
   2296 
   2297 	if (argc == 0 || argc > 2) {
   2298 		fprintf(ttyout, "usage: %s [restart-point]\n", argv[0]);
   2299 		code = -1;
   2300 		return;
   2301 	}
   2302 	if (! features[FEAT_REST_STREAM]) {
   2303 		fprintf(ttyout,
   2304 		    "Restart is not supported by the remote server.\n");
   2305 		return;
   2306 	}
   2307 	if (argc == 2) {
   2308 		off_t rp;
   2309 		char *ep;
   2310 
   2311 		rp = STRTOLL(argv[1], &ep, 10);
   2312 		if (rp < 0 || *ep != '\0')
   2313 			fprintf(ttyout, "restart: Invalid offset `%s'\n",
   2314 			    argv[1]);
   2315 		else
   2316 			restart_point = rp;
   2317 	}
   2318 	if (restart_point == 0)
   2319 		fputs("No restart point defined.\n", ttyout);
   2320 	else
   2321 		fprintf(ttyout,
   2322 		    "Restarting at " LLF " for next get, put or append\n",
   2323 		    (LLT)restart_point);
   2324 }
   2325 
   2326 /*
   2327  * Show remote system type
   2328  */
   2329 void
   2330 syst(int argc, char *argv[])
   2331 {
   2332 	int oldverbose = verbose;
   2333 
   2334 	if (argc == 0) {
   2335 		fprintf(ttyout, "usage: %s\n", argv[0]);
   2336 		code = -1;
   2337 		return;
   2338 	}
   2339 	verbose = 1;	/* If we aren't verbose, this doesn't do anything! */
   2340 	(void)command("SYST");
   2341 	verbose = oldverbose;
   2342 }
   2343 
   2344 void
   2345 macdef(int argc, char *argv[])
   2346 {
   2347 	char *tmp;
   2348 	int c;
   2349 
   2350 	if (argc == 0)
   2351 		goto usage;
   2352 	if (macnum == 16) {
   2353 		fputs("Limit of 16 macros have already been defined.\n",
   2354 		    ttyout);
   2355 		code = -1;
   2356 		return;
   2357 	}
   2358 	if ((argc < 2 && !another(&argc, &argv, "macro name")) || argc > 2) {
   2359  usage:
   2360 		fprintf(ttyout, "usage: %s macro_name\n", argv[0]);
   2361 		code = -1;
   2362 		return;
   2363 	}
   2364 	if (interactive)
   2365 		fputs(
   2366 		"Enter macro line by line, terminating it with a null line.\n",
   2367 		    ttyout);
   2368 	(void)strlcpy(macros[macnum].mac_name, argv[1],
   2369 	    sizeof(macros[macnum].mac_name));
   2370 	if (macnum == 0)
   2371 		macros[macnum].mac_start = macbuf;
   2372 	else
   2373 		macros[macnum].mac_start = macros[macnum - 1].mac_end + 1;
   2374 	tmp = macros[macnum].mac_start;
   2375 	while (tmp != macbuf+4096) {
   2376 		if ((c = getchar()) == EOF) {
   2377 			fputs("macdef: end of file encountered.\n", ttyout);
   2378 			code = -1;
   2379 			return;
   2380 		}
   2381 		if ((*tmp = c) == '\n') {
   2382 			if (tmp == macros[macnum].mac_start) {
   2383 				macros[macnum++].mac_end = tmp;
   2384 				code = 0;
   2385 				return;
   2386 			}
   2387 			if (*(tmp-1) == '\0') {
   2388 				macros[macnum++].mac_end = tmp - 1;
   2389 				code = 0;
   2390 				return;
   2391 			}
   2392 			*tmp = '\0';
   2393 		}
   2394 		tmp++;
   2395 	}
   2396 	while (1) {
   2397 		while ((c = getchar()) != '\n' && c != EOF)
   2398 			/* LOOP */;
   2399 		if (c == EOF || getchar() == '\n') {
   2400 			fputs("Macro not defined - 4K buffer exceeded.\n",
   2401 			    ttyout);
   2402 			code = -1;
   2403 			return;
   2404 		}
   2405 	}
   2406 }
   2407 
   2408 /*
   2409  * Get size of file on remote machine
   2410  */
   2411 void
   2412 sizecmd(int argc, char *argv[])
   2413 {
   2414 	off_t size;
   2415 
   2416 	if (argc == 0 || argc > 2 ||
   2417 	    (argc == 1 && !another(&argc, &argv, "remote-file"))) {
   2418 		fprintf(ttyout, "usage: %s remote-file\n", argv[0]);
   2419 		code = -1;
   2420 		return;
   2421 	}
   2422 	size = remotesize(argv[1], 1);
   2423 	if (size != -1)
   2424 		fprintf(ttyout,
   2425 		    "%s\t" LLF "\n", argv[1], (LLT)size);
   2426 	code = (size > 0);
   2427 }
   2428 
   2429 /*
   2430  * Get last modification time of file on remote machine
   2431  */
   2432 void
   2433 modtime(int argc, char *argv[])
   2434 {
   2435 	time_t mtime;
   2436 
   2437 	if (argc == 0 || argc > 2 ||
   2438 	    (argc == 1 && !another(&argc, &argv, "remote-file"))) {
   2439 		fprintf(ttyout, "usage: %s remote-file\n", argv[0]);
   2440 		code = -1;
   2441 		return;
   2442 	}
   2443 	mtime = remotemodtime(argv[1], 1);
   2444 	if (mtime != -1)
   2445 		fprintf(ttyout, "%s\t%s", argv[1], asctime(localtime(&mtime)));
   2446 	code = (mtime > 0);
   2447 }
   2448 
   2449 /*
   2450  * Show status on remote machine
   2451  */
   2452 void
   2453 rmtstatus(int argc, char *argv[])
   2454 {
   2455 
   2456 	if (argc == 0) {
   2457 		fprintf(ttyout, "usage: %s [remote-file]\n", argv[0]);
   2458 		code = -1;
   2459 		return;
   2460 	}
   2461 	COMMAND_1ARG(argc, argv, "STAT");
   2462 }
   2463 
   2464 /*
   2465  * Get file if modtime is more recent than current file
   2466  */
   2467 void
   2468 newer(int argc, char *argv[])
   2469 {
   2470 
   2471 	if (getit(argc, argv, -1, "w"))
   2472 		fprintf(ttyout,
   2473 		    "Local file \"%s\" is newer than remote file \"%s\".\n",
   2474 		    argv[2], argv[1]);
   2475 }
   2476 
   2477 /*
   2478  * Display one local file through $PAGER.
   2479  */
   2480 void
   2481 lpage(int argc, char *argv[])
   2482 {
   2483 	int len;
   2484 	char *p, *pager, *locfile;
   2485 
   2486 	if (argc == 0 || argc > 2 ||
   2487 	    (argc == 1 && !another(&argc, &argv, "local-file"))) {
   2488 		fprintf(ttyout, "usage: %s local-file\n", argv[0]);
   2489 		code = -1;
   2490 		return;
   2491 	}
   2492 	if ((locfile = globulize(argv[1])) == NULL) {
   2493 		code = -1;
   2494 		return;
   2495 	}
   2496 	p = getoptionvalue("pager");
   2497 	if (EMPTYSTRING(p))
   2498 		p = DEFAULTPAGER;
   2499 	len = strlen(p) + strlen(locfile) + 2;
   2500 	pager = xmalloc(len);
   2501 	(void)strlcpy(pager, p,		len);
   2502 	(void)strlcat(pager, " ",	len);
   2503 	(void)strlcat(pager, locfile,	len);
   2504 	system(pager);
   2505 	code = 0;
   2506 	(void)free(pager);
   2507 	(void)free(locfile);
   2508 }
   2509 
   2510 /*
   2511  * Display one remote file through $PAGER.
   2512  */
   2513 void
   2514 page(int argc, char *argv[])
   2515 {
   2516 	int ohash, orestart_point, overbose, len;
   2517 	char *p, *pager;
   2518 
   2519 	if (argc == 0 || argc > 2 ||
   2520 	    (argc == 1 && !another(&argc, &argv, "remote-file"))) {
   2521 		fprintf(ttyout, "usage: %s remote-file\n", argv[0]);
   2522 		code = -1;
   2523 		return;
   2524 	}
   2525 	p = getoptionvalue("pager");
   2526 	if (EMPTYSTRING(p))
   2527 		p = DEFAULTPAGER;
   2528 	len = strlen(p) + 2;
   2529 	pager = xmalloc(len);
   2530 	pager[0] = '|';
   2531 	(void)strlcpy(pager + 1, p, len - 1);
   2532 
   2533 	ohash = hash;
   2534 	orestart_point = restart_point;
   2535 	overbose = verbose;
   2536 	hash = restart_point = verbose = 0;
   2537 	recvrequest("RETR", pager, argv[1], "r+w", 1, 0);
   2538 	hash = ohash;
   2539 	restart_point = orestart_point;
   2540 	verbose = overbose;
   2541 	(void)free(pager);
   2542 }
   2543 
   2544 /*
   2545  * Set the socket send or receive buffer size.
   2546  */
   2547 void
   2548 setxferbuf(int argc, char *argv[])
   2549 {
   2550 	int size, dir;
   2551 
   2552 	if (argc != 2) {
   2553  usage:
   2554 		fprintf(ttyout, "usage: %s size\n", argv[0]);
   2555 		code = -1;
   2556 		return;
   2557 	}
   2558 	if (strcasecmp(argv[0], "sndbuf") == 0)
   2559 		dir = RATE_PUT;
   2560 	else if (strcasecmp(argv[0], "rcvbuf") == 0)
   2561 		dir = RATE_GET;
   2562 	else if (strcasecmp(argv[0], "xferbuf") == 0)
   2563 		dir = RATE_ALL;
   2564 	else
   2565 		goto usage;
   2566 
   2567 	if ((size = strsuftoi(argv[1])) == -1)
   2568 		goto usage;
   2569 
   2570 	if (size == 0) {
   2571 		fprintf(ttyout, "%s: size must be positive.\n", argv[0]);
   2572 		goto usage;
   2573 	}
   2574 
   2575 	if (dir & RATE_PUT)
   2576 		sndbuf_size = size;
   2577 	if (dir & RATE_GET)
   2578 		rcvbuf_size = size;
   2579 	fprintf(ttyout, "Socket buffer sizes: send %d, receive %d.\n",
   2580 	    sndbuf_size, rcvbuf_size);
   2581 	code = 0;
   2582 }
   2583 
   2584 /*
   2585  * Set or display options (defaults are provided by various env vars)
   2586  */
   2587 void
   2588 setoption(int argc, char *argv[])
   2589 {
   2590 	struct option *o;
   2591 
   2592 	code = -1;
   2593 	if (argc == 0 || (argc != 1 && argc != 3)) {
   2594 		fprintf(ttyout, "usage: %s [option value]\n", argv[0]);
   2595 		return;
   2596 	}
   2597 
   2598 #define	OPTIONINDENT ((int) sizeof("http_proxy"))
   2599 	if (argc == 1) {
   2600 		for (o = optiontab; o->name != NULL; o++) {
   2601 			fprintf(ttyout, "%-*s\t%s\n", OPTIONINDENT,
   2602 			    o->name, o->value ? o->value : "");
   2603 		}
   2604 	} else {
   2605 		o = getoption(argv[1]);
   2606 		if (o == NULL) {
   2607 			fprintf(ttyout, "No such option `%s'.\n", argv[1]);
   2608 			return;
   2609 		}
   2610 		FREEPTR(o->value);
   2611 		o->value = xstrdup(argv[2]);
   2612 		if (verbose)
   2613 			fprintf(ttyout, "Setting `%s' to `%s'.\n",
   2614 			    o->name, o->value);
   2615 	}
   2616 	code = 0;
   2617 }
   2618 
   2619 /*
   2620  * Unset an option
   2621  */
   2622 void
   2623 unsetoption(int argc, char *argv[])
   2624 {
   2625 	struct option *o;
   2626 
   2627 	code = -1;
   2628 	if (argc == 0 || argc != 2) {
   2629 		fprintf(ttyout, "usage: %s option\n", argv[0]);
   2630 		return;
   2631 	}
   2632 
   2633 	o = getoption(argv[1]);
   2634 	if (o == NULL) {
   2635 		fprintf(ttyout, "No such option `%s'.\n", argv[1]);
   2636 		return;
   2637 	}
   2638 	FREEPTR(o->value);
   2639 	fprintf(ttyout, "Unsetting `%s'.\n", o->name);
   2640 	code = 0;
   2641 }
   2642 
   2643 /*
   2644  * Display features supported by the remote host.
   2645  */
   2646 void
   2647 feat(int argc, char *argv[])
   2648 {
   2649 	int oldverbose = verbose;
   2650 
   2651 	if (argc == 0) {
   2652 		fprintf(ttyout, "usage: %s\n", argv[0]);
   2653 		code = -1;
   2654 		return;
   2655 	}
   2656 	if (! features[FEAT_FEAT]) {
   2657 		fprintf(ttyout,
   2658 		    "FEAT is not supported by the remote server.\n");
   2659 		return;
   2660 	}
   2661 	verbose = 1;	/* If we aren't verbose, this doesn't do anything! */
   2662 	(void)command("FEAT");
   2663 	verbose = oldverbose;
   2664 }
   2665 
   2666 void
   2667 mlst(int argc, char *argv[])
   2668 {
   2669 	int oldverbose = verbose;
   2670 
   2671 	if (argc < 1 || argc > 2) {
   2672 		fprintf(ttyout, "usage: %s [remote-path]\n", argv[0]);
   2673 		code = -1;
   2674 		return;
   2675 	}
   2676 	if (! features[FEAT_MLST]) {
   2677 		fprintf(ttyout,
   2678 		    "MLST is not supported by the remote server.\n");
   2679 		return;
   2680 	}
   2681 	verbose = 1;	/* If we aren't verbose, this doesn't do anything! */
   2682 	COMMAND_1ARG(argc, argv, "MLST");
   2683 	verbose = oldverbose;
   2684 }
   2685 
   2686 void
   2687 opts(int argc, char *argv[])
   2688 {
   2689 	int oldverbose = verbose;
   2690 
   2691 	if (argc < 2 || argc > 3) {
   2692 		fprintf(ttyout, "usage: %s command [options]\n", argv[0]);
   2693 		code = -1;
   2694 		return;
   2695 	}
   2696 	if (! features[FEAT_FEAT]) {
   2697 		fprintf(ttyout,
   2698 		    "OPTS is not supported by the remote server.\n");
   2699 		return;
   2700 	}
   2701 	verbose = 1;	/* If we aren't verbose, this doesn't do anything! */
   2702 	if (argc == 2)
   2703 		command("OPTS %s", argv[1]);
   2704 	else
   2705 		command("OPTS %s %s", argv[1], argv[2]);
   2706 	verbose = oldverbose;
   2707 }
   2708