1 /* $NetBSD: cmds.c,v 1.134 2012/01/15 20:43:24 christos Exp $ */
4 * Copyright (c) 1996-2009 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * This code is derived from software contributed to The NetBSD Foundation
11 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
12 * NASA Ames Research Center.
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the distribution.
23 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
24 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
25 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
27 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 * POSSIBILITY OF SUCH DAMAGE.
37 * Copyright (c) 1985, 1989, 1993, 1994
38 * The Regents of the University of California. All rights reserved.
40 * Redistribution and use in source and binary forms, with or without
41 * modification, are permitted provided that the following conditions
43 * 1. Redistributions of source code must retain the above copyright
44 * notice, this list of conditions and the following disclaimer.
45 * 2. Redistributions in binary form must reproduce the above copyright
46 * notice, this list of conditions and the following disclaimer in the
47 * documentation and/or other materials provided with the distribution.
48 * 3. Neither the name of the University nor the names of its contributors
49 * may be used to endorse or promote products derived from this software
50 * without specific prior written permission.
52 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
53 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
55 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
56 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
57 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
58 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
59 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
60 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
61 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
66 * Copyright (C) 1997 and 1998 WIDE Project.
67 * All rights reserved.
69 * Redistribution and use in source and binary forms, with or without
70 * modification, are permitted provided that the following conditions
72 * 1. Redistributions of source code must retain the above copyright
73 * notice, this list of conditions and the following disclaimer.
74 * 2. Redistributions in binary form must reproduce the above copyright
75 * notice, this list of conditions and the following disclaimer in the
76 * documentation and/or other materials provided with the distribution.
77 * 3. Neither the name of the project nor the names of its contributors
78 * may be used to endorse or promote products derived from this software
79 * without specific prior written permission.
81 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
82 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
83 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
84 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
85 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
86 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
87 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
88 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
89 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
90 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
94 #include <sys/cdefs.h>
97 static char sccsid
[] = "@(#)cmds.c 8.6 (Berkeley) 10/9/94";
99 __RCSID("$NetBSD: cmds.c,v 1.134 2012/01/15 20:43:24 christos Exp $");
101 #endif /* not lint */
104 * FTP User Program -- Command Routines.
106 #include <sys/types.h>
107 #include <sys/socket.h>
108 #include <sys/stat.h>
109 #include <sys/wait.h>
110 #include <arpa/ftp.h>
130 static struct types
{
136 { "ascii", "A", TYPE_A
, 0 },
137 { "binary", "I", TYPE_I
, 0 },
138 { "image", "I", TYPE_I
, 0 },
139 { "ebcdic", "E", TYPE_E
, 0 },
140 { "tenex", "L", TYPE_L
, bytename
},
141 { NULL
, NULL
, 0, NULL
}
144 static sigjmp_buf jabort
;
146 static int confirm(const char *, const char *);
147 __dead
static void mintr(int);
148 static void mabort(const char *);
149 static void set_type(const char *);
151 static const char *doprocess(char *, size_t, const char *, int, int, int);
152 static const char *domap(char *, size_t, const char *);
153 static const char *docase(char *, size_t, const char *);
154 static const char *dotrans(char *, size_t, const char *);
157 * Confirm if "cmd" is to be performed upon "file".
158 * If "file" is NULL, generate a "Continue with" prompt instead.
161 confirm(const char *cmd
, const char *file
)
163 const char *errormsg
;
165 const char *promptleft
, *promptright
;
167 if (!interactive
|| confirmrest
)
170 promptleft
= "Continue with";
177 fprintf(ttyout
, "%s %s [anpqy?]? ", promptleft
, promptright
);
178 (void)fflush(ttyout
);
179 if (get_line(stdin
, cline
, sizeof(cline
), &errormsg
) < 0) {
181 fprintf(ttyout
, "%s; %s aborted\n", errormsg
, cmd
);
184 switch (tolower((unsigned char)*cline
)) {
188 "Prompting off for duration of %s.\n", cmd
);
192 fputs("Interactive mode: off.\n", ttyout
);
196 fprintf(ttyout
, "%s aborted.\n", cmd
);
202 " confirmation options:\n"
203 "\ta answer `yes' for the duration of %s\n"
204 "\tn answer `no' for this file\n"
205 "\tp turn off `prompt' mode\n"
206 "\tq stop the current %s\n"
207 "\ty answer `yes' for this file\n"
208 "\t? this help list\n",
210 continue; /* back to while(1) */
221 settype(int argc
, char *argv
[])
225 if (argc
== 0 || argc
> 2) {
228 UPRINTF("usage: %s [", argv
[0]);
230 for (p
= types
; p
->t_name
; p
++) {
231 fprintf(ttyout
, "%s%s", sep
, p
->t_name
);
234 fputs(" ]\n", ttyout
);
239 fprintf(ttyout
, "Using %s mode to transfer files.\n", typename
);
247 set_type(const char *ttype
)
252 for (p
= types
; p
->t_name
; p
++)
253 if (strcmp(ttype
, p
->t_name
) == 0)
255 if (p
->t_name
== 0) {
256 fprintf(ttyout
, "%s: unknown mode.\n", ttype
);
260 if ((p
->t_arg
!= NULL
) && (*(p
->t_arg
) != '\0'))
261 comret
= command("TYPE %s %s", p
->t_mode
, p
->t_arg
);
263 comret
= command("TYPE %s", p
->t_mode
);
264 if (comret
== COMPLETE
) {
265 (void)strlcpy(typename
, p
->t_name
, sizeof(typename
));
266 curtype
= type
= p
->t_type
;
271 * Internal form of settype; changes current type in use with server
272 * without changing our notion of the type for data transfers.
273 * Used to change to and from ascii for listings.
276 changetype(int newtype
, int show
)
279 int comret
, oldverbose
= verbose
;
283 if (newtype
== curtype
)
285 if (ftp_debug
== 0 && show
== 0)
287 for (p
= types
; p
->t_name
; p
++)
288 if (newtype
== p
->t_type
)
290 if (p
->t_name
== 0) {
291 errx(1, "changetype: unknown type %d", newtype
);
293 if (newtype
== TYPE_L
&& bytename
[0] != '\0')
294 comret
= command("TYPE %s %s", p
->t_mode
, bytename
);
296 comret
= command("TYPE %s", p
->t_mode
);
297 if (comret
== COMPLETE
)
299 verbose
= oldverbose
;
303 * Set binary transfer type.
307 setbinary(int argc
, char *argv
[])
311 UPRINTF("usage: %s\n", argv
[0]);
319 * Set ascii transfer type.
323 setascii(int argc
, char *argv
[])
327 UPRINTF("usage: %s\n", argv
[0]);
335 * Set tenex transfer type.
339 settenex(int argc
, char *argv
[])
343 UPRINTF("usage: %s\n", argv
[0]);
351 * Set file transfer mode.
355 setftmode(int argc
, char *argv
[])
359 UPRINTF("usage: %s mode-name\n", argv
[0]);
363 fprintf(ttyout
, "We only support %s mode, sorry.\n", modename
);
368 * Set file transfer format.
372 setform(int argc
, char *argv
[])
376 UPRINTF("usage: %s format\n", argv
[0]);
380 fprintf(ttyout
, "We only support %s format, sorry.\n", formname
);
385 * Set file transfer structure.
389 setstruct(int argc
, char *argv
[])
393 UPRINTF("usage: %s struct-mode\n", argv
[0]);
397 fprintf(ttyout
, "We only support %s structure, sorry.\n", structname
);
402 * Send a single file.
405 put(int argc
, char *argv
[])
407 char buf
[MAXPATHLEN
];
418 if (argc
== 0 || (argc
== 1 && !another(&argc
, &argv
, "local-file")))
420 if ((argc
< 3 && !another(&argc
, &argv
, "remote-file")) || argc
> 3) {
422 UPRINTF("usage: %s local-file [remote-file]\n", argv
[0]);
426 if ((locfile
= globulize(argv
[1])) == NULL
) {
431 if (loc
) /* If argv[2] is a copy of the old argv[1], update it */
433 cmd
= (argv
[0][0] == 'a') ? "APPE" : ((sunique
) ? "STOU" : "STOR");
434 remfile
= doprocess(buf
, sizeof(buf
), remfile
,
435 0, loc
&& ntflag
, loc
&& mapflag
);
436 sendrequest(cmd
, locfile
, remfile
,
437 locfile
!= argv
[1] || remfile
!= argv
[2]);
442 doprocess(char *dst
, size_t dlen
, const char *src
,
443 int casef
, int transf
, int mapf
)
446 src
= docase(dst
, dlen
, src
);
448 src
= dotrans(dst
, dlen
, src
);
450 src
= domap(dst
, dlen
, src
);
455 * Send multiple files.
458 mput(int argc
, char *argv
[])
465 if (argc
== 0 || (argc
== 1 && !another(&argc
, &argv
, "local-files"))) {
466 UPRINTF("usage: %s local-files\n", argv
[0]);
471 oldintr
= xsignal(SIGINT
, mintr
);
472 if (sigsetjmp(jabort
, 1))
477 while ((cp
= remglob(argv
, 0, NULL
)) != NULL
) {
478 if (*cp
== '\0' || !connected
) {
482 if (mflag
&& confirm(argv
[0], cp
)) {
483 char buf
[MAXPATHLEN
];
484 tp
= doprocess(buf
, sizeof(buf
), cp
,
485 mcase
, ntflag
, mapflag
);
486 sendrequest((sunique
) ? "STOU" : "STOR",
487 cp
, tp
, cp
!= tp
|| !interactive
);
488 if (!mflag
&& fromatty
) {
489 ointer
= interactive
;
491 if (confirm(argv
[0], NULL
)) {
494 interactive
= ointer
;
500 for (i
= 1; i
< argc
&& connected
; i
++) {
506 if (mflag
&& confirm(argv
[0], argv
[i
])) {
507 char buf
[MAXPATHLEN
];
508 tp
= doprocess(buf
, sizeof(buf
), argv
[i
],
510 sendrequest((sunique
) ? "STOU" : "STOR",
511 argv
[i
], tp
, tp
!= argv
[i
] || !interactive
);
512 if (!mflag
&& fromatty
) {
513 ointer
= interactive
;
515 if (confirm(argv
[0], NULL
)) {
518 interactive
= ointer
;
524 memset(&gl
, 0, sizeof(gl
));
525 flags
= GLOB_BRACE
|GLOB_NOCHECK
|GLOB_TILDE
;
526 if (glob(argv
[i
], flags
, NULL
, &gl
) || gl
.gl_pathc
== 0) {
527 warnx("Glob pattern `%s' not found", argv
[i
]);
531 for (cpp
= gl
.gl_pathv
; cpp
&& *cpp
!= NULL
&& connected
;
533 if (mflag
&& confirm(argv
[0], *cpp
)) {
534 char buf
[MAXPATHLEN
];
536 tp
= doprocess(buf
, sizeof(buf
), *cpp
,
538 sendrequest((sunique
) ? "STOU" : "STOR",
539 *cpp
, tp
, *cpp
!= tp
|| !interactive
);
540 if (!mflag
&& fromatty
) {
541 ointer
= interactive
;
543 if (confirm(argv
[0], NULL
)) {
546 interactive
= ointer
;
553 (void)xsignal(SIGINT
, oldintr
);
558 reget(int argc
, char *argv
[])
561 (void)getit(argc
, argv
, 1, restart_point
? "r+" : "a");
565 get(int argc
, char *argv
[])
568 (void)getit(argc
, argv
, 0, restart_point
? "r+" : "w");
573 * If restartit is 1, restart the xfer always.
574 * If restartit is -1, restart the xfer only if the remote file is newer.
577 getit(int argc
, char *argv
[], int restartit
, const char *gmode
)
580 char *remfile
, *olocfile
;
582 char buf
[MAXPATHLEN
];
590 if (argc
== 0 || (argc
== 1 && !another(&argc
, &argv
, "remote-file")))
592 if ((argc
< 3 && !another(&argc
, &argv
, "local-file")) || argc
> 3) {
594 UPRINTF("usage: %s remote-file [local-file]\n", argv
[0]);
599 if ((olocfile
= globulize(argv
[2])) == NULL
) {
603 locfile
= doprocess(buf
, sizeof(buf
), olocfile
,
604 loc
&& mcase
, loc
&& ntflag
, loc
&& mapflag
);
609 if (! features
[FEAT_REST_STREAM
]) {
611 "Restart is not supported by the remote server.\n");
614 ret
= stat(locfile
, &stbuf
);
615 if (restartit
== 1) {
617 if (errno
!= ENOENT
) {
618 warn("Can't stat `%s'", locfile
);
624 restart_point
= stbuf
.st_size
;
629 mtime
= remotemodtime(argv
[1], 0);
632 if (stbuf
.st_mtime
>= mtime
) {
640 recvrequest("RETR", locfile
, remfile
, gmode
,
641 remfile
!= argv
[1] || locfile
!= argv
[2], loc
);
644 (void)free(olocfile
);
655 write(fileno(ttyout
), "\n", 1);
656 siglongjmp(jabort
, 1);
660 mabort(const char *cmd
)
664 if (mflag
&& fromatty
) {
665 ointer
= interactive
;
669 if (confirm(cmd
, NULL
)) {
670 interactive
= ointer
;
674 interactive
= ointer
;
681 * Get multiple files.
684 mget(int argc
, char *argv
[])
690 int volatile restartit
;
693 (argc
== 1 && !another(&argc
, &argv
, "remote-files"))) {
694 UPRINTF("usage: %s remote-files\n", argv
[0]);
701 if (strcmp(argv
[0], "mreget") == 0) {
702 if (! features
[FEAT_REST_STREAM
]) {
704 "Restart is not supported by the remote server.\n");
709 oldintr
= xsignal(SIGINT
, mintr
);
710 if (sigsetjmp(jabort
, 1))
712 while ((cp
= remglob(argv
, proxy
, NULL
)) != NULL
) {
713 char buf
[MAXPATHLEN
];
714 if (*cp
== '\0' || !connected
) {
720 if (! fileindir(cp
, localcwd
)) {
721 fprintf(ttyout
, "Skipping non-relative filename `%s'\n",
725 if (!confirm(argv
[0], cp
))
727 tp
= doprocess(buf
, sizeof(buf
), cp
, mcase
, ntflag
, mapflag
);
731 if (stat(tp
, &stbuf
) == 0)
732 restart_point
= stbuf
.st_size
;
734 warn("Can't stat `%s'", tp
);
736 recvrequest("RETR", tp
, cp
, restart_point
? "r+" : "w",
737 tp
!= cp
|| !interactive
, 1);
739 if (!mflag
&& fromatty
) {
740 ointer
= interactive
;
742 if (confirm(argv
[0], NULL
))
744 interactive
= ointer
;
747 (void)xsignal(SIGINT
, oldintr
);
752 * Read list of filenames from a local file and get those
755 fget(int argc
, char *argv
[])
759 char buf
[MAXPATHLEN
], cmdbuf
[MAX_C_NAME
];
762 UPRINTF("usage: %s localfile\n", argv
[0]);
767 fp
= fopen(argv
[1], "r");
769 fprintf(ttyout
, "Can't open source file %s\n", argv
[1]);
774 (void)strlcpy(cmdbuf
, "get", sizeof(cmdbuf
));
776 gmode
= restart_point
? "r+" : "w";
778 while (get_line(fp
, buf
, sizeof(buf
), NULL
) >= 0) {
782 (void)getit(argc
, argv
, 0, gmode
);
791 return (val
? "on" : "off");
799 status(int argc
, char *argv
[])
803 UPRINTF("usage: %s\n", argv
[0]);
809 fprintf(ttyout
, "Connected %sto %s.\n",
810 connected
== -1 ? "and logged in" : "", hostname
);
812 fputs("Not connected.\n", ttyout
);
816 fprintf(ttyout
, "Connected for proxy commands to %s.\n",
820 fputs("No proxy connection.\n", ttyout
);
824 fprintf(ttyout
, "Gate ftp: %s, server %s, port %s.\n", onoff(gatemode
),
825 *gateserver
? gateserver
: "(none)", gateport
);
826 fprintf(ttyout
, "Passive mode: %s; fallback to active mode: %s.\n",
827 onoff(passivemode
), onoff(activefallback
));
828 fprintf(ttyout
, "Mode: %s; Type: %s; Form: %s; Structure: %s.\n",
829 modename
, typename
, formname
, structname
);
830 fprintf(ttyout
, "Verbose: %s; Bell: %s; Prompting: %s; Globbing: %s.\n",
831 onoff(verbose
), onoff(bell
), onoff(interactive
), onoff(doglob
));
832 fprintf(ttyout
, "Store unique: %s; Receive unique: %s.\n",
833 onoff(sunique
), onoff(runique
));
834 fprintf(ttyout
, "Preserve modification times: %s.\n", onoff(preserve
));
835 fprintf(ttyout
, "Case: %s; CR stripping: %s.\n", onoff(mcase
),
838 fprintf(ttyout
, "Ntrans: (in) %s (out) %s\n", ntin
, ntout
);
841 fputs("Ntrans: off.\n", ttyout
);
844 fprintf(ttyout
, "Nmap: (in) %s (out) %s\n", mapin
, mapout
);
847 fputs("Nmap: off.\n", ttyout
);
850 "Hash mark printing: %s; Mark count: %d; Progress bar: %s.\n",
851 onoff(hash
), mark
, onoff(progress
));
853 "Get transfer rate throttle: %s; maximum: %d; increment %d.\n",
854 onoff(rate_get
), rate_get
, rate_get_incr
);
856 "Put transfer rate throttle: %s; maximum: %d; increment %d.\n",
857 onoff(rate_put
), rate_put
, rate_put_incr
);
859 "Socket buffer sizes: send %d, receive %d.\n",
860 sndbuf_size
, rcvbuf_size
);
861 fprintf(ttyout
, "Use of PORT cmds: %s.\n", onoff(sendport
));
862 fprintf(ttyout
, "Use of EPSV/EPRT cmds for IPv4: %s%s.\n", onoff(epsv4
),
863 epsv4bad
? " (disabled for this connection)" : "");
864 fprintf(ttyout
, "Use of EPSV/EPRT cmds for IPv6: %s%s.\n", onoff(epsv6
),
865 epsv6bad
? " (disabled for this connection)" : "");
866 fprintf(ttyout
, "Command line editing: %s.\n",
867 #ifdef NO_EDITCOMPLETE
868 "support not compiled in"
869 #else /* !def NO_EDITCOMPLETE */
871 #endif /* !def NO_EDITCOMPLETE */
876 fputs("Macros:\n", ttyout
);
877 for (i
=0; i
<macnum
; i
++) {
878 fprintf(ttyout
, "\t%s\n", macros
[i
].mac_name
);
881 #endif /* !def NO_STATUS */
882 fprintf(ttyout
, "Version: %s %s\n", FTP_PRODUCT
, FTP_VERSION
);
890 togglevar(int argc
, char *argv
[], int *var
, const char *mesg
)
894 } else if (argc
== 2 && strcasecmp(argv
[1], "on") == 0) {
896 } else if (argc
== 2 && strcasecmp(argv
[1], "off") == 0) {
899 UPRINTF("usage: %s [ on | off ]\n", argv
[0]);
903 fprintf(ttyout
, "%s %s.\n", mesg
, onoff(*var
));
908 * Set beep on cmd completed mode.
912 setbell(int argc
, char *argv
[])
915 code
= togglevar(argc
, argv
, &bell
, "Bell mode");
919 * Set command line editing
923 setedit(int argc
, char *argv
[])
926 #ifdef NO_EDITCOMPLETE
928 UPRINTF("usage: %s\n", argv
[0]);
933 fputs("Editing support not compiled in; ignoring command.\n",
935 #else /* !def NO_EDITCOMPLETE */
936 code
= togglevar(argc
, argv
, &editing
, "Editing mode");
938 #endif /* !def NO_EDITCOMPLETE */
942 * Turn on packet tracing.
946 settrace(int argc
, char *argv
[])
949 code
= togglevar(argc
, argv
, &trace
, "Packet tracing");
953 * Toggle hash mark printing during transfers, or set hash mark bytecount.
957 sethash(int argc
, char *argv
[])
961 else if (argc
!= 2) {
962 UPRINTF("usage: %s [ on | off | bytecount ]\n",
966 } else if (strcasecmp(argv
[1], "on") == 0)
968 else if (strcasecmp(argv
[1], "off") == 0)
973 nmark
= strsuftoi(argv
[1]);
975 fprintf(ttyout
, "mark: bad bytecount value `%s'.\n",
983 fprintf(ttyout
, "Hash mark printing %s", onoff(hash
));
985 fprintf(ttyout
, " (%d bytes/hash mark)", mark
);
986 fputs(".\n", ttyout
);
993 * Turn on printing of server echo's.
997 setverbose(int argc
, char *argv
[])
1000 code
= togglevar(argc
, argv
, &verbose
, "Verbose mode");
1004 * Toggle PORT/LPRT cmd use before each data connection.
1008 setport(int argc
, char *argv
[])
1011 code
= togglevar(argc
, argv
, &sendport
, "Use of PORT/LPRT cmds");
1015 * Toggle transfer progress bar.
1019 setprogress(int argc
, char *argv
[])
1022 code
= togglevar(argc
, argv
, &progress
, "Progress bar");
1028 * Turn on interactive prompting during mget, mput, and mdelete.
1032 setprompt(int argc
, char *argv
[])
1035 code
= togglevar(argc
, argv
, &interactive
, "Interactive mode");
1039 * Toggle gate-ftp mode, or set gate-ftp server
1043 setgate(int argc
, char *argv
[])
1045 static char gsbuf
[MAXHOSTNAMELEN
];
1047 if (argc
== 0 || argc
> 3) {
1049 "usage: %s [ on | off | gateserver [port] ]\n", argv
[0]);
1052 } else if (argc
< 2) {
1053 gatemode
= !gatemode
;
1055 if (argc
== 2 && strcasecmp(argv
[1], "on") == 0)
1057 else if (argc
== 2 && strcasecmp(argv
[1], "off") == 0)
1061 gateport
= ftp_strdup(argv
[2]);
1062 (void)strlcpy(gsbuf
, argv
[1], sizeof(gsbuf
));
1067 if (gatemode
&& (gateserver
== NULL
|| *gateserver
== '\0')) {
1069 "Disabling gate-ftp mode - no gate-ftp server defined.\n");
1072 fprintf(ttyout
, "Gate ftp: %s, server %s, port %s.\n",
1073 onoff(gatemode
), *gateserver
? gateserver
: "(none)",
1080 * Toggle metacharacter interpretation on local file names.
1084 setglob(int argc
, char *argv
[])
1087 code
= togglevar(argc
, argv
, &doglob
, "Globbing");
1091 * Toggle preserving modification times on retrieved files.
1095 setpreserve(int argc
, char *argv
[])
1098 code
= togglevar(argc
, argv
, &preserve
, "Preserve modification times");
1102 * Set debugging mode on/off and/or set level of debugging.
1106 setdebug(int argc
, char *argv
[])
1108 if (argc
== 0 || argc
> 2) {
1109 UPRINTF("usage: %s [ on | off | debuglevel ]\n", argv
[0]);
1112 } else if (argc
== 2) {
1113 if (strcasecmp(argv
[1], "on") == 0)
1115 else if (strcasecmp(argv
[1], "off") == 0)
1120 val
= strsuftoi(argv
[1]);
1122 fprintf(ttyout
, "%s: bad debugging value.\n",
1130 ftp_debug
= !ftp_debug
;
1132 options
|= SO_DEBUG
;
1134 options
&= ~SO_DEBUG
;
1135 fprintf(ttyout
, "Debugging %s (ftp_debug=%d).\n", onoff(ftp_debug
), ftp_debug
);
1136 code
= ftp_debug
> 0;
1140 * Set current working directory on remote machine.
1143 cd(int argc
, char *argv
[])
1147 if (argc
== 0 || argc
> 2 ||
1148 (argc
== 1 && !another(&argc
, &argv
, "remote-directory"))) {
1149 UPRINTF("usage: %s remote-directory\n", argv
[0]);
1153 r
= command("CWD %s", argv
[1]);
1154 if (r
== ERROR
&& code
== 500) {
1156 fputs("CWD command not recognized, trying XCWD.\n",
1158 r
= command("XCWD %s", argv
[1]);
1160 if (r
== COMPLETE
) {
1167 * Set current working directory on local machine.
1170 lcd(int argc
, char *argv
[])
1177 argv
[1] = localhome
;
1180 UPRINTF("usage: %s [local-directory]\n", argv
[0]);
1183 if ((locdir
= globulize(argv
[1])) == NULL
)
1185 if (chdir(locdir
) == -1)
1186 warn("Can't chdir `%s'", locdir
);
1190 fprintf(ttyout
, "Local directory now: %s\n", localcwd
);
1193 fprintf(ttyout
, "Unable to determine local directory\n");
1200 * Delete a single file.
1203 delete(int argc
, char *argv
[])
1206 if (argc
== 0 || argc
> 2 ||
1207 (argc
== 1 && !another(&argc
, &argv
, "remote-file"))) {
1208 UPRINTF("usage: %s remote-file\n", argv
[0]);
1212 if (command("DELE %s", argv
[1]) == COMPLETE
)
1217 * Delete multiple files.
1220 mdelete(int argc
, char *argv
[])
1227 (argc
== 1 && !another(&argc
, &argv
, "remote-files"))) {
1228 UPRINTF("usage: %s [remote-files]\n", argv
[0]);
1233 oldintr
= xsignal(SIGINT
, mintr
);
1234 if (sigsetjmp(jabort
, 1))
1236 while ((cp
= remglob(argv
, 0, NULL
)) != NULL
) {
1241 if (mflag
&& confirm(argv
[0], cp
)) {
1242 if (command("DELE %s", cp
) == COMPLETE
)
1244 if (!mflag
&& fromatty
) {
1245 ointer
= interactive
;
1247 if (confirm(argv
[0], NULL
)) {
1250 interactive
= ointer
;
1254 (void)xsignal(SIGINT
, oldintr
);
1259 * Rename a remote file.
1262 renamefile(int argc
, char *argv
[])
1265 if (argc
== 0 || (argc
== 1 && !another(&argc
, &argv
, "from-name")))
1267 if ((argc
< 3 && !another(&argc
, &argv
, "to-name")) || argc
> 3) {
1269 UPRINTF("usage: %s from-name to-name\n", argv
[0]);
1273 if (command("RNFR %s", argv
[1]) == CONTINUE
&&
1274 command("RNTO %s", argv
[2]) == COMPLETE
)
1279 * Get a directory listing of remote files.
1280 * Supports being invoked as:
1286 * pdir, pls LIST |$PAGER
1287 * pmlsd MLSD |$PAGER
1290 ls(int argc
, char *argv
[])
1293 char *remdir
, *locbuf
;
1294 const char *locfile
;
1295 int pagecmd
, mlsdcmd
;
1300 pagecmd
= mlsdcmd
= 0;
1302 * the only commands that start with `p' are
1303 * the `pager' versions.
1305 if (argv
[0][0] == 'p')
1307 if (strcmp(argv
[0] + pagecmd
, "mlsd") == 0) {
1308 if (! features
[FEAT_MLST
]) {
1310 "MLSD is not supported by the remote server.\n");
1320 else if (strcmp(argv
[0] + pagecmd
, "nlist") == 0)
1329 if (argc
> 3 || ((pagecmd
| mlsdcmd
) && argc
> 2)) {
1331 if (pagecmd
|| mlsdcmd
)
1332 UPRINTF("usage: %s [remote-path]\n", argv
[0]);
1334 UPRINTF("usage: %s [remote-path [local-file]]\n",
1344 p
= getoptionvalue("pager");
1347 len
= strlen(p
) + 2;
1348 locbuf
= ftp_malloc(len
);
1350 (void)strlcpy(locbuf
+ 1, p
, len
- 1);
1352 } else if ((strcmp(locfile
, "-") != 0) && *locfile
!= '|') {
1353 if ((locbuf
= globulize(locfile
)) == NULL
||
1354 !confirm("output to local-file:", locbuf
)) {
1360 recvrequest(cmd
, locfile
, remdir
, "w", 0, 0);
1367 * Get a directory listing of multiple remote files.
1370 mls(int argc
, char *argv
[])
1374 int volatile dolist
;
1375 char * volatile dest
, *odest
;
1380 if (argc
< 2 && !another(&argc
, &argv
, "remote-files"))
1382 if (argc
< 3 && !another(&argc
, &argv
, "local-file")) {
1384 UPRINTF("usage: %s remote-files local-file\n", argv
[0]);
1388 odest
= dest
= argv
[argc
- 1];
1389 argv
[argc
- 1] = NULL
;
1390 if (strcmp(dest
, "-") && *dest
!= '|')
1391 if (((dest
= globulize(dest
)) == NULL
) ||
1392 !confirm("output to local-file:", dest
)) {
1396 dolist
= strcmp(argv
[0], "mls");
1398 oldintr
= xsignal(SIGINT
, mintr
);
1399 if (sigsetjmp(jabort
, 1))
1401 for (i
= 1; mflag
&& i
< argc
-1 && connected
; i
++) {
1402 lmode
= (i
== 1) ? "w" : "a";
1403 recvrequest(dolist
? "LIST" : "NLST", dest
, argv
[i
], lmode
,
1405 if (!mflag
&& fromatty
) {
1406 ointer
= interactive
;
1408 if (confirm(argv
[0], NULL
)) {
1411 interactive
= ointer
;
1414 (void)xsignal(SIGINT
, oldintr
);
1416 if (dest
!= odest
) /* free up after globulize() */
1425 shell(int argc
, char *argv
[])
1429 char shellnam
[MAXPATHLEN
];
1430 const char *shellp
, *namep
;
1434 UPRINTF("usage: %s [command [args]]\n", argv
[0]);
1438 oldintr
= xsignal(SIGINT
, SIG_IGN
);
1439 if ((pid
= fork()) == 0) {
1440 for (pid
= 3; pid
< 20; pid
++)
1442 (void)xsignal(SIGINT
, SIG_DFL
);
1443 shellp
= getenv("SHELL");
1445 shellp
= _PATH_BSHELL
;
1446 namep
= strrchr(shellp
, '/');
1451 (void)strlcpy(shellnam
, namep
, sizeof(shellnam
));
1453 fputs(shellp
, ttyout
);
1457 execl(shellp
, shellnam
, "-c", altarg
, (char *)0);
1460 execl(shellp
, shellnam
, (char *)0);
1462 warn("Can't execute `%s'", shellp
);
1467 while (wait(&wait_status
) != pid
)
1469 (void)xsignal(SIGINT
, oldintr
);
1471 warn("Can't fork a subshell; try again later");
1478 * Send new user information (re-login)
1481 user(int argc
, char *argv
[])
1484 char emptypass
[] = "";
1490 (void)another(&argc
, &argv
, "username");
1491 if (argc
< 2 || argc
> 4) {
1493 UPRINTF("usage: %s username [password [account]]\n",
1498 n
= command("USER %s", argv
[1]);
1499 if (n
== CONTINUE
) {
1501 password
= getpass("Password: ");
1502 if (password
== NULL
)
1503 password
= emptypass
;
1507 n
= command("PASS %s", password
);
1508 memset(password
, 0, strlen(password
));
1510 if (n
== CONTINUE
) {
1513 password
= getpass("Account: ");
1514 if (password
== NULL
)
1515 password
= emptypass
;
1519 n
= command("ACCT %s", password
);
1520 memset(password
, 0, strlen(password
));
1522 if (n
!= COMPLETE
) {
1523 fputs("Login failed.\n", ttyout
);
1526 if (!aflag
&& argc
== 4) {
1528 (void)command("ACCT %s", password
);
1529 memset(password
, 0, strlen(password
));
1536 * Print working directory on remote machine.
1540 pwd(int argc
, char *argv
[])
1545 UPRINTF("usage: %s\n", argv
[0]);
1551 fprintf(ttyout
, "Unable to determine remote directory\n");
1553 fprintf(ttyout
, "Remote directory: %s\n", remotecwd
);
1559 * Print working directory on local machine.
1562 lpwd(int argc
, char *argv
[])
1567 UPRINTF("usage: %s\n", argv
[0]);
1573 fprintf(ttyout
, "Unable to determine local directory\n");
1575 fprintf(ttyout
, "Local directory: %s\n", localcwd
);
1584 makedir(int argc
, char *argv
[])
1588 if (argc
== 0 || argc
> 2 ||
1589 (argc
== 1 && !another(&argc
, &argv
, "directory-name"))) {
1590 UPRINTF("usage: %s directory-name\n", argv
[0]);
1594 r
= command("MKD %s", argv
[1]);
1595 if (r
== ERROR
&& code
== 500) {
1597 fputs("MKD command not recognized, trying XMKD.\n",
1599 r
= command("XMKD %s", argv
[1]);
1606 * Remove a directory.
1609 removedir(int argc
, char *argv
[])
1613 if (argc
== 0 || argc
> 2 ||
1614 (argc
== 1 && !another(&argc
, &argv
, "directory-name"))) {
1615 UPRINTF("usage: %s directory-name\n", argv
[0]);
1619 r
= command("RMD %s", argv
[1]);
1620 if (r
== ERROR
&& code
== 500) {
1622 fputs("RMD command not recognized, trying XRMD.\n",
1624 r
= command("XRMD %s", argv
[1]);
1631 * Send a line, verbatim, to the remote machine.
1634 quote(int argc
, char *argv
[])
1638 (argc
== 1 && !another(&argc
, &argv
, "command line to send"))) {
1639 UPRINTF("usage: %s line-to-send\n", argv
[0]);
1643 quote1("", argc
, argv
);
1647 * Send a SITE command to the remote machine. The line
1648 * is sent verbatim to the remote machine, except that the
1649 * word "SITE" is added at the front.
1652 site(int argc
, char *argv
[])
1656 (argc
== 1 && !another(&argc
, &argv
, "arguments to SITE command"))){
1657 UPRINTF("usage: %s line-to-send\n", argv
[0]);
1661 quote1("SITE ", argc
, argv
);
1665 * Turn argv[1..argc) into a space-separated string, then prepend initial text.
1666 * Send the result as a one-line command and get response.
1669 quote1(const char *initial
, int argc
, char *argv
[])
1672 char buf
[BUFSIZ
]; /* must be >= sizeof(line) */
1674 (void)strlcpy(buf
, initial
, sizeof(buf
));
1675 for (i
= 1; i
< argc
; i
++) {
1676 (void)strlcat(buf
, argv
[i
], sizeof(buf
));
1678 (void)strlcat(buf
, " ", sizeof(buf
));
1680 if (command("%s", buf
) == PRELIM
) {
1681 while (getreply(0) == PRELIM
)
1688 do_chmod(int argc
, char *argv
[])
1691 if (argc
== 0 || (argc
== 1 && !another(&argc
, &argv
, "mode")))
1693 if ((argc
< 3 && !another(&argc
, &argv
, "remote-file")) || argc
> 3) {
1695 UPRINTF("usage: %s mode remote-file\n", argv
[0]);
1699 (void)command("SITE CHMOD %s %s", argv
[1], argv
[2]);
1702 #define COMMAND_1ARG(argc, argv, cmd) \
1706 command(cmd " %s", argv[1])
1709 do_umask(int argc
, char *argv
[])
1711 int oldverbose
= verbose
;
1714 UPRINTF("usage: %s [umask]\n", argv
[0]);
1719 COMMAND_1ARG(argc
, argv
, "SITE UMASK");
1720 verbose
= oldverbose
;
1724 idlecmd(int argc
, char *argv
[])
1726 int oldverbose
= verbose
;
1728 if (argc
< 1 || argc
> 2) {
1729 UPRINTF("usage: %s [seconds]\n", argv
[0]);
1734 COMMAND_1ARG(argc
, argv
, "SITE IDLE");
1735 verbose
= oldverbose
;
1739 * Ask the other side for help.
1742 rmthelp(int argc
, char *argv
[])
1744 int oldverbose
= verbose
;
1747 UPRINTF("usage: %s\n", argv
[0]);
1752 COMMAND_1ARG(argc
, argv
, "HELP");
1753 verbose
= oldverbose
;
1757 * Terminate session and exit.
1758 * May be called with 0, NULL.
1762 quit(int argc
, char *argv
[])
1765 /* this may be called with argc == 0, argv == NULL */
1766 if (argc
== 0 && argv
!= NULL
) {
1767 UPRINTF("usage: %s\n", argv
[0]);
1772 disconnect(0, NULL
);
1775 disconnect(0, NULL
);
1780 * Terminate session, but don't exit.
1781 * May be called with 0, NULL.
1784 disconnect(int argc
, char *argv
[])
1787 /* this may be called with argc == 0, argv == NULL */
1788 if (argc
== 0 && argv
!= NULL
) {
1789 UPRINTF("usage: %s\n", argv
[0]);
1795 (void)command("QUIT");
1800 account(int argc
, char *argv
[])
1803 char emptypass
[] = "";
1805 if (argc
== 0 || argc
> 2) {
1806 UPRINTF("usage: %s [password]\n", argv
[0]);
1813 ap
= getpass("Account:");
1817 (void)command("ACCT %s", ap
);
1818 memset(ap
, 0, strlen(ap
));
1821 sigjmp_buf abortprox
;
1824 proxabort(int notused
)
1839 siglongjmp(abortprox
, 1);
1843 doproxy(int argc
, char *argv
[])
1848 char cmdbuf
[MAX_C_NAME
];
1850 if (argc
== 0 || (argc
== 1 && !another(&argc
, &argv
, "command"))) {
1851 UPRINTF("usage: %s command\n", argv
[0]);
1855 c
= getcmd(argv
[1]);
1856 if (c
== (struct cmd
*) -1) {
1857 fputs("?Ambiguous command.\n", ttyout
);
1862 fputs("?Invalid command.\n", ttyout
);
1867 fputs("?Invalid proxy command.\n", ttyout
);
1871 if (sigsetjmp(abortprox
, 1)) {
1875 oldintr
= xsignal(SIGINT
, proxabort
);
1877 if (c
->c_conn
&& !connected
) {
1878 fputs("Not connected.\n", ttyout
);
1880 (void)xsignal(SIGINT
, oldintr
);
1884 cmdpos
= strcspn(line
, " \t");
1885 if (cmdpos
> 0) /* remove leading "proxy " from input buffer */
1886 memmove(line
, line
+ cmdpos
+ 1, strlen(line
) - cmdpos
+ 1);
1887 (void)strlcpy(cmdbuf
, c
->c_name
, sizeof(cmdbuf
));
1889 (*c
->c_handler
)(argc
-1, argv
+1);
1897 (void)xsignal(SIGINT
, oldintr
);
1901 setcase(int argc
, char *argv
[])
1904 code
= togglevar(argc
, argv
, &mcase
, "Case mapping");
1908 * convert the given name to lower case if it's all upper case, into
1909 * a static buffer which is returned to the caller
1912 docase(char *dst
, size_t dlen
, const char *src
)
1917 for (i
= 0; src
[i
] != '\0' && i
< dlen
- 1; i
++) {
1919 if (islower((unsigned char)dst
[i
]))
1925 for (i
= 0; dst
[i
] != '\0'; i
++)
1926 if (isupper((unsigned char)dst
[i
]))
1927 dst
[i
] = tolower((unsigned char)dst
[i
]);
1933 setcr(int argc
, char *argv
[])
1936 code
= togglevar(argc
, argv
, &crflag
, "Carriage Return stripping");
1940 setntrans(int argc
, char *argv
[])
1943 if (argc
== 0 || argc
> 3) {
1944 UPRINTF("usage: %s [inchars [outchars]]\n", argv
[0]);
1950 fputs("Ntrans off.\n", ttyout
);
1956 (void)strlcpy(ntin
, argv
[1], sizeof(ntin
));
1961 (void)strlcpy(ntout
, argv
[2], sizeof(ntout
));
1965 dotrans(char *dst
, size_t dlen
, const char *src
)
1971 for (ostop
= 0; *(ntout
+ ostop
) && ostop
< 16; ostop
++)
1973 for (cp1
= src
; *cp1
; cp1
++) {
1975 for (i
= 0; *(ntin
+ i
) && i
< 16; i
++) {
1976 if (*cp1
== *(ntin
+ i
)) {
1979 *cp2
++ = *(ntout
+ i
);
1980 if (cp2
- dst
>= (ptrdiff_t)(dlen
- 1))
1996 setnmap(int argc
, char *argv
[])
2002 fputs("Nmap off.\n", ttyout
);
2007 (argc
< 3 && !another(&argc
, &argv
, "mapout")) || argc
> 3) {
2008 UPRINTF("usage: %s [mapin mapout]\n", argv
[0]);
2014 cp
= strchr(altarg
, ' ');
2019 cp
= strchr(altarg
, ' ');
2022 (void)strlcpy(mapin
, altarg
, MAXPATHLEN
);
2023 while (*++cp
== ' ')
2025 (void)strlcpy(mapout
, cp
, MAXPATHLEN
);
2029 domap(char *dst
, size_t dlen
, const char *src
)
2031 const char *cp1
= src
;
2033 const char *tp
[9], *te
[9];
2034 int i
, toks
[9], toknum
= 0, match
= 1;
2036 for (i
=0; i
< 9; ++i
) {
2039 while (match
&& *cp1
&& *cp2
) {
2042 if (*++cp2
!= *cp1
) {
2047 if (*(cp2
+1) >= '1' && (*cp2
+1) <= '9') {
2048 if (*cp1
!= *(++cp2
+1)) {
2049 toks
[toknum
= *cp2
- '1']++;
2051 while (*++cp1
&& *(cp2
+1)
2065 if (match
&& *cp1
) {
2068 if (match
&& *cp2
) {
2072 if (!match
&& *cp1
) /* last token mismatch */
2089 if (*++cp1
== '$' &&
2090 isdigit((unsigned char)*(cp1
+1))) {
2091 if (*++cp1
== '0') {
2092 const char *cp3
= src
;
2099 else if (toks
[toknum
= *cp1
- '1']) {
2100 const char *cp3
= tp
[toknum
];
2102 while (cp3
!= te
[toknum
]) {
2109 while (*cp1
&& *cp1
!= ',' &&
2114 else if (*cp1
== '$' &&
2115 isdigit((unsigned char)*(cp1
+1))) {
2116 if (*++cp1
== '0') {
2117 const char *cp3
= src
;
2123 else if (toks
[toknum
=
2125 const char *cp3
=tp
[toknum
];
2139 "nmap: unbalanced brackets.\n",
2147 while (*++cp1
&& *cp1
!= ']') {
2148 if (*cp1
== '\\' && *(cp1
+ 1)) {
2154 "nmap: unbalanced brackets.\n",
2171 if (isdigit((unsigned char)*(cp1
+ 1))) {
2172 if (*++cp1
== '0') {
2173 const char *cp3
= src
;
2179 else if (toks
[toknum
= *cp1
- '1']) {
2180 const char *cp3
= tp
[toknum
];
2182 while (cp3
!= te
[toknum
]) {
2188 /* intentional drop through */
2196 return *dst
? dst
: src
;
2200 setpassive(int argc
, char *argv
[])
2204 passivemode
= !passivemode
;
2205 activefallback
= passivemode
;
2206 } else if (argc
!= 2) {
2208 UPRINTF("usage: %s [ on | off | auto ]\n", argv
[0]);
2211 } else if (strcasecmp(argv
[1], "on") == 0) {
2214 } else if (strcasecmp(argv
[1], "off") == 0) {
2217 } else if (strcasecmp(argv
[1], "auto") == 0) {
2222 fprintf(ttyout
, "Passive mode: %s; fallback to active mode: %s.\n",
2223 onoff(passivemode
), onoff(activefallback
));
2229 setepsv4(int argc
, char *argv
[])
2231 code
= togglevar(argc
, argv
, &epsv4
,
2232 verbose
? "EPSV/EPRT on IPv4" : NULL
);
2237 setepsv6(int argc
, char *argv
[])
2239 code
= togglevar(argc
, argv
, &epsv6
,
2240 verbose
? "EPSV/EPRT on IPv6" : NULL
);
2245 setepsv(int argc
, char*argv
[])
2247 setepsv4(argc
,argv
);
2248 setepsv6(argc
,argv
);
2252 setsunique(int argc
, char *argv
[])
2255 code
= togglevar(argc
, argv
, &sunique
, "Store unique");
2259 setrunique(int argc
, char *argv
[])
2262 code
= togglevar(argc
, argv
, &runique
, "Receive unique");
2266 parserate(int argc
, char *argv
[], int cmdlineopt
)
2268 int dir
, max
, incr
, showonly
;
2269 sigfunc oldusr1
, oldusr2
;
2271 if (argc
> 4 || (argc
< (cmdlineopt
? 3 : 2))) {
2275 "usage: %s (all|get|put),maximum-bytes[,increment-bytes]]\n",
2279 "usage: %s (all|get|put) [maximum-bytes [increment-bytes]]\n",
2283 dir
= max
= incr
= showonly
= 0;
2286 #define RATE_ALL (RATE_GET | RATE_PUT)
2288 if (strcasecmp(argv
[1], "all") == 0)
2290 else if (strcasecmp(argv
[1], "get") == 0)
2292 else if (strcasecmp(argv
[1], "put") == 0)
2298 if ((max
= strsuftoi(argv
[2])) < 0)
2304 if ((incr
= strsuftoi(argv
[3])) <= 0)
2309 oldusr1
= xsignal(SIGUSR1
, SIG_IGN
);
2310 oldusr2
= xsignal(SIGUSR2
, SIG_IGN
);
2311 if (dir
& RATE_GET
) {
2314 rate_get_incr
= incr
;
2316 if (!cmdlineopt
|| verbose
)
2318 "Get transfer rate throttle: %s; maximum: %d; increment %d.\n",
2319 onoff(rate_get
), rate_get
, rate_get_incr
);
2321 if (dir
& RATE_PUT
) {
2324 rate_put_incr
= incr
;
2326 if (!cmdlineopt
|| verbose
)
2328 "Put transfer rate throttle: %s; maximum: %d; increment %d.\n",
2329 onoff(rate_put
), rate_put
, rate_put_incr
);
2331 (void)xsignal(SIGUSR1
, oldusr1
);
2332 (void)xsignal(SIGUSR2
, oldusr2
);
2337 setrate(int argc
, char *argv
[])
2340 code
= parserate(argc
, argv
, 0);
2343 /* change directory to parent directory */
2345 cdup(int argc
, char *argv
[])
2350 UPRINTF("usage: %s\n", argv
[0]);
2354 r
= command("CDUP");
2355 if (r
== ERROR
&& code
== 500) {
2357 fputs("CDUP command not recognized, trying XCUP.\n",
2359 r
= command("XCUP");
2361 if (r
== COMPLETE
) {
2368 * Restart transfer at specific point
2371 restart(int argc
, char *argv
[])
2374 if (argc
== 0 || argc
> 2) {
2375 UPRINTF("usage: %s [restart-point]\n", argv
[0]);
2379 if (! features
[FEAT_REST_STREAM
]) {
2381 "Restart is not supported by the remote server.\n");
2388 rp
= STRTOLL(argv
[1], &ep
, 10);
2389 if (rp
< 0 || *ep
!= '\0')
2390 fprintf(ttyout
, "restart: Invalid offset `%s'\n",
2395 if (restart_point
== 0)
2396 fputs("No restart point defined.\n", ttyout
);
2399 "Restarting at " LLF
" for next get, put or append\n",
2400 (LLT
)restart_point
);
2404 * Show remote system type
2407 syst(int argc
, char *argv
[])
2409 int oldverbose
= verbose
;
2412 UPRINTF("usage: %s\n", argv
[0]);
2416 verbose
= 1; /* If we aren't verbose, this doesn't do anything! */
2417 (void)command("SYST");
2418 verbose
= oldverbose
;
2422 macdef(int argc
, char *argv
[])
2430 fputs("Limit of 16 macros have already been defined.\n",
2435 if ((argc
< 2 && !another(&argc
, &argv
, "macro name")) || argc
> 2) {
2437 UPRINTF("usage: %s macro_name\n", argv
[0]);
2443 "Enter macro line by line, terminating it with a null line.\n",
2445 (void)strlcpy(macros
[macnum
].mac_name
, argv
[1],
2446 sizeof(macros
[macnum
].mac_name
));
2448 macros
[macnum
].mac_start
= macbuf
;
2450 macros
[macnum
].mac_start
= macros
[macnum
- 1].mac_end
+ 1;
2451 tmp
= macros
[macnum
].mac_start
;
2452 while (tmp
!= macbuf
+4096) {
2453 if ((c
= getchar()) == EOF
) {
2454 fputs("macdef: end of file encountered.\n", ttyout
);
2458 if ((*tmp
= c
) == '\n') {
2459 if (tmp
== macros
[macnum
].mac_start
) {
2460 macros
[macnum
++].mac_end
= tmp
;
2464 if (*(tmp
-1) == '\0') {
2465 macros
[macnum
++].mac_end
= tmp
- 1;
2474 while ((c
= getchar()) != '\n' && c
!= EOF
)
2476 if (c
== EOF
|| getchar() == '\n') {
2477 fputs("Macro not defined - 4K buffer exceeded.\n",
2486 * Get size of file on remote machine
2489 sizecmd(int argc
, char *argv
[])
2493 if (argc
== 0 || argc
> 2 ||
2494 (argc
== 1 && !another(&argc
, &argv
, "remote-file"))) {
2495 UPRINTF("usage: %s remote-file\n", argv
[0]);
2499 size
= remotesize(argv
[1], 1);
2502 "%s\t" LLF
"\n", argv
[1], (LLT
)size
);
2507 * Get last modification time of file on remote machine
2510 modtime(int argc
, char *argv
[])
2514 if (argc
== 0 || argc
> 2 ||
2515 (argc
== 1 && !another(&argc
, &argv
, "remote-file"))) {
2516 UPRINTF("usage: %s remote-file\n", argv
[0]);
2520 mtime
= remotemodtime(argv
[1], 1);
2522 fprintf(ttyout
, "%s\t%s", argv
[1],
2523 rfc2822time(localtime(&mtime
)));
2528 * Show status on remote machine
2531 rmtstatus(int argc
, char *argv
[])
2535 UPRINTF("usage: %s [remote-file]\n", argv
[0]);
2539 COMMAND_1ARG(argc
, argv
, "STAT");
2543 * Get file if modtime is more recent than current file
2546 newer(int argc
, char *argv
[])
2549 if (getit(argc
, argv
, -1, "w"))
2551 "Local file \"%s\" is newer than remote file \"%s\".\n",
2556 * Display one local file through $PAGER.
2559 lpage(int argc
, char *argv
[])
2563 char *pager
, *locfile
;
2565 if (argc
== 0 || argc
> 2 ||
2566 (argc
== 1 && !another(&argc
, &argv
, "local-file"))) {
2567 UPRINTF("usage: %s local-file\n", argv
[0]);
2571 if ((locfile
= globulize(argv
[1])) == NULL
) {
2575 p
= getoptionvalue("pager");
2578 len
= strlen(p
) + strlen(locfile
) + 2;
2579 pager
= ftp_malloc(len
);
2580 (void)strlcpy(pager
, p
, len
);
2581 (void)strlcat(pager
, " ", len
);
2582 (void)strlcat(pager
, locfile
, len
);
2586 (void)free(locfile
);
2590 * Display one remote file through $PAGER.
2593 page(int argc
, char *argv
[])
2595 int ohash
, orestart_point
, overbose
;
2600 if (argc
== 0 || argc
> 2 ||
2601 (argc
== 1 && !another(&argc
, &argv
, "remote-file"))) {
2602 UPRINTF("usage: %s remote-file\n", argv
[0]);
2606 p
= getoptionvalue("pager");
2609 len
= strlen(p
) + 2;
2610 pager
= ftp_malloc(len
);
2612 (void)strlcpy(pager
+ 1, p
, len
- 1);
2615 orestart_point
= restart_point
;
2617 hash
= restart_point
= verbose
= 0;
2618 recvrequest("RETR", pager
, argv
[1], "r+", 1, 0);
2620 restart_point
= orestart_point
;
2626 * Set the socket send or receive buffer size.
2629 setxferbuf(int argc
, char *argv
[])
2635 UPRINTF("usage: %s size\n", argv
[0]);
2639 if (strcasecmp(argv
[0], "sndbuf") == 0)
2641 else if (strcasecmp(argv
[0], "rcvbuf") == 0)
2643 else if (strcasecmp(argv
[0], "xferbuf") == 0)
2648 if ((size
= strsuftoi(argv
[1])) == -1)
2652 fprintf(ttyout
, "%s: size must be positive.\n", argv
[0]);
2660 fprintf(ttyout
, "Socket buffer sizes: send %d, receive %d.\n",
2661 sndbuf_size
, rcvbuf_size
);
2666 * Set or display options (defaults are provided by various env vars)
2669 setoption(int argc
, char *argv
[])
2674 if (argc
== 0 || (argc
!= 1 && argc
!= 3)) {
2675 UPRINTF("usage: %s [option value]\n", argv
[0]);
2679 #define OPTIONINDENT ((int) sizeof("http_proxy"))
2681 for (o
= optiontab
; o
->name
!= NULL
; o
++) {
2682 fprintf(ttyout
, "%-*s\t%s\n", OPTIONINDENT
,
2683 o
->name
, o
->value
? o
->value
: "");
2686 set_option(argv
[1], argv
[2], 1);
2692 set_option(const char * option
, const char * value
, int doverbose
)
2696 o
= getoption(option
);
2698 fprintf(ttyout
, "No such option `%s'.\n", option
);
2702 o
->value
= ftp_strdup(value
);
2703 if (verbose
&& doverbose
)
2704 fprintf(ttyout
, "Setting `%s' to `%s'.\n",
2712 unsetoption(int argc
, char *argv
[])
2717 if (argc
== 0 || argc
!= 2) {
2718 UPRINTF("usage: %s option\n", argv
[0]);
2722 o
= getoption(argv
[1]);
2724 fprintf(ttyout
, "No such option `%s'.\n", argv
[1]);
2728 fprintf(ttyout
, "Unsetting `%s'.\n", o
->name
);
2733 * Display features supported by the remote host.
2736 feat(int argc
, char *argv
[])
2738 int oldverbose
= verbose
;
2741 UPRINTF("usage: %s\n", argv
[0]);
2745 if (! features
[FEAT_FEAT
]) {
2747 "FEAT is not supported by the remote server.\n");
2750 verbose
= 1; /* If we aren't verbose, this doesn't do anything! */
2751 (void)command("FEAT");
2752 verbose
= oldverbose
;
2756 mlst(int argc
, char *argv
[])
2758 int oldverbose
= verbose
;
2760 if (argc
< 1 || argc
> 2) {
2761 UPRINTF("usage: %s [remote-path]\n", argv
[0]);
2765 if (! features
[FEAT_MLST
]) {
2767 "MLST is not supported by the remote server.\n");
2770 verbose
= 1; /* If we aren't verbose, this doesn't do anything! */
2771 COMMAND_1ARG(argc
, argv
, "MLST");
2772 verbose
= oldverbose
;
2776 opts(int argc
, char *argv
[])
2778 int oldverbose
= verbose
;
2780 if (argc
< 2 || argc
> 3) {
2781 UPRINTF("usage: %s command [options]\n", argv
[0]);
2785 if (! features
[FEAT_FEAT
]) {
2787 "OPTS is not supported by the remote server.\n");
2790 verbose
= 1; /* If we aren't verbose, this doesn't do anything! */
2792 command("OPTS %s", argv
[1]);
2794 command("OPTS %s %s", argv
[1], argv
[2]);
2795 verbose
= oldverbose
;