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