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