1 /* $NetBSD: cmds.c,v 1.129 2009/04/12 10:18:52 lukem 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.129 2009/04/12 10:18:52 lukem 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>
128 static struct types
{
134 { "ascii", "A", TYPE_A
, 0 },
135 { "binary", "I", TYPE_I
, 0 },
136 { "image", "I", TYPE_I
, 0 },
137 { "ebcdic", "E", TYPE_E
, 0 },
138 { "tenex", "L", TYPE_L
, bytename
},
139 { NULL
, NULL
, 0, NULL
}
142 static sigjmp_buf jabort
;
144 static int confirm(const char *, const char *);
145 static void mintr(int);
146 static void mabort(const char *);
147 static void set_type(const char *);
149 static const char *doprocess(char *, size_t, const char *, int, int, int);
150 static const char *domap(char *, size_t, const char *);
151 static const char *docase(char *, size_t, const char *);
152 static const char *dotrans(char *, size_t, const char *);
155 * Confirm if "cmd" is to be performed upon "file".
156 * If "file" is NULL, generate a "Continue with" prompt instead.
159 confirm(const char *cmd
, const char *file
)
161 const char *errormsg
;
163 const char *promptleft
, *promptright
;
165 if (!interactive
|| confirmrest
)
168 promptleft
= "Continue with";
175 fprintf(ttyout
, "%s %s [anpqy?]? ", promptleft
, promptright
);
176 (void)fflush(ttyout
);
177 if (get_line(stdin
, cline
, sizeof(cline
), &errormsg
) < 0) {
179 fprintf(ttyout
, "%s; %s aborted\n", errormsg
, cmd
);
182 switch (tolower((unsigned char)*cline
)) {
186 "Prompting off for duration of %s.\n", cmd
);
190 fputs("Interactive mode: off.\n", ttyout
);
194 fprintf(ttyout
, "%s aborted.\n", cmd
);
200 " confirmation options:\n"
201 "\ta answer `yes' for the duration of %s\n"
202 "\tn answer `no' for this file\n"
203 "\tp turn off `prompt' mode\n"
204 "\tq stop the current %s\n"
205 "\ty answer `yes' for this file\n"
206 "\t? this help list\n",
208 continue; /* back to while(1) */
219 settype(int argc
, char *argv
[])
223 if (argc
== 0 || argc
> 2) {
226 UPRINTF("usage: %s [", argv
[0]);
228 for (p
= types
; p
->t_name
; p
++) {
229 fprintf(ttyout
, "%s%s", sep
, p
->t_name
);
232 fputs(" ]\n", ttyout
);
237 fprintf(ttyout
, "Using %s mode to transfer files.\n", typename
);
245 set_type(const char *ttype
)
250 for (p
= types
; p
->t_name
; p
++)
251 if (strcmp(ttype
, p
->t_name
) == 0)
253 if (p
->t_name
== 0) {
254 fprintf(ttyout
, "%s: unknown mode.\n", ttype
);
258 if ((p
->t_arg
!= NULL
) && (*(p
->t_arg
) != '\0'))
259 comret
= command("TYPE %s %s", p
->t_mode
, p
->t_arg
);
261 comret
= command("TYPE %s", p
->t_mode
);
262 if (comret
== COMPLETE
) {
263 (void)strlcpy(typename
, p
->t_name
, sizeof(typename
));
264 curtype
= type
= p
->t_type
;
269 * Internal form of settype; changes current type in use with server
270 * without changing our notion of the type for data transfers.
271 * Used to change to and from ascii for listings.
274 changetype(int newtype
, int show
)
277 int comret
, oldverbose
= verbose
;
281 if (newtype
== curtype
)
283 if (ftp_debug
== 0 && show
== 0)
285 for (p
= types
; p
->t_name
; p
++)
286 if (newtype
== p
->t_type
)
288 if (p
->t_name
== 0) {
289 errx(1, "changetype: unknown type %d", newtype
);
291 if (newtype
== TYPE_L
&& bytename
[0] != '\0')
292 comret
= command("TYPE %s %s", p
->t_mode
, bytename
);
294 comret
= command("TYPE %s", p
->t_mode
);
295 if (comret
== COMPLETE
)
297 verbose
= oldverbose
;
301 * Set binary transfer type.
305 setbinary(int argc
, char *argv
[])
309 UPRINTF("usage: %s\n", argv
[0]);
317 * Set ascii transfer type.
321 setascii(int argc
, char *argv
[])
325 UPRINTF("usage: %s\n", argv
[0]);
333 * Set tenex transfer type.
337 settenex(int argc
, char *argv
[])
341 UPRINTF("usage: %s\n", argv
[0]);
349 * Set file transfer mode.
353 setftmode(int argc
, char *argv
[])
357 UPRINTF("usage: %s mode-name\n", argv
[0]);
361 fprintf(ttyout
, "We only support %s mode, sorry.\n", modename
);
366 * Set file transfer format.
370 setform(int argc
, char *argv
[])
374 UPRINTF("usage: %s format\n", argv
[0]);
378 fprintf(ttyout
, "We only support %s format, sorry.\n", formname
);
383 * Set file transfer structure.
387 setstruct(int argc
, char *argv
[])
391 UPRINTF("usage: %s struct-mode\n", argv
[0]);
395 fprintf(ttyout
, "We only support %s structure, sorry.\n", structname
);
400 * Send a single file.
403 put(int argc
, char *argv
[])
405 char buf
[MAXPATHLEN
];
416 if (argc
== 0 || (argc
== 1 && !another(&argc
, &argv
, "local-file")))
418 if ((argc
< 3 && !another(&argc
, &argv
, "remote-file")) || argc
> 3) {
420 UPRINTF("usage: %s local-file [remote-file]\n", argv
[0]);
424 if ((locfile
= globulize(argv
[1])) == NULL
) {
429 if (loc
) /* If argv[2] is a copy of the old argv[1], update it */
431 cmd
= (argv
[0][0] == 'a') ? "APPE" : ((sunique
) ? "STOU" : "STOR");
432 remfile
= doprocess(buf
, sizeof(buf
), remfile
,
433 0, loc
&& ntflag
, loc
&& mapflag
);
434 sendrequest(cmd
, locfile
, remfile
,
435 locfile
!= argv
[1] || remfile
!= argv
[2]);
440 doprocess(char *dst
, size_t dlen
, const char *src
,
441 int casef
, int transf
, int mapf
)
444 src
= docase(dst
, dlen
, src
);
446 src
= dotrans(dst
, dlen
, src
);
448 src
= domap(dst
, dlen
, src
);
453 * Send multiple files.
456 mput(int argc
, char *argv
[])
463 if (argc
== 0 || (argc
== 1 && !another(&argc
, &argv
, "local-files"))) {
464 UPRINTF("usage: %s local-files\n", argv
[0]);
469 oldintr
= xsignal(SIGINT
, mintr
);
470 if (sigsetjmp(jabort
, 1))
475 while ((cp
= remglob(argv
, 0, NULL
)) != NULL
) {
476 if (*cp
== '\0' || !connected
) {
480 if (mflag
&& confirm(argv
[0], cp
)) {
481 char buf
[MAXPATHLEN
];
482 tp
= doprocess(buf
, sizeof(buf
), cp
,
483 mcase
, ntflag
, mapflag
);
484 sendrequest((sunique
) ? "STOU" : "STOR",
485 cp
, tp
, cp
!= tp
|| !interactive
);
486 if (!mflag
&& fromatty
) {
487 ointer
= interactive
;
489 if (confirm(argv
[0], NULL
)) {
492 interactive
= ointer
;
498 for (i
= 1; i
< argc
&& connected
; i
++) {
504 if (mflag
&& confirm(argv
[0], argv
[i
])) {
505 char buf
[MAXPATHLEN
];
506 tp
= doprocess(buf
, sizeof(buf
), argv
[i
],
508 sendrequest((sunique
) ? "STOU" : "STOR",
509 argv
[i
], tp
, tp
!= argv
[i
] || !interactive
);
510 if (!mflag
&& fromatty
) {
511 ointer
= interactive
;
513 if (confirm(argv
[0], NULL
)) {
516 interactive
= ointer
;
522 memset(&gl
, 0, sizeof(gl
));
523 flags
= GLOB_BRACE
|GLOB_NOCHECK
|GLOB_TILDE
;
524 if (glob(argv
[i
], flags
, NULL
, &gl
) || gl
.gl_pathc
== 0) {
525 warnx("Glob pattern `%s' not found", argv
[i
]);
529 for (cpp
= gl
.gl_pathv
; cpp
&& *cpp
!= NULL
&& connected
;
531 if (mflag
&& confirm(argv
[0], *cpp
)) {
532 char buf
[MAXPATHLEN
];
534 tp
= doprocess(buf
, sizeof(buf
), *cpp
,
536 sendrequest((sunique
) ? "STOU" : "STOR",
537 *cpp
, tp
, *cpp
!= tp
|| !interactive
);
538 if (!mflag
&& fromatty
) {
539 ointer
= interactive
;
541 if (confirm(argv
[0], NULL
)) {
544 interactive
= ointer
;
551 (void)xsignal(SIGINT
, oldintr
);
556 reget(int argc
, char *argv
[])
559 (void)getit(argc
, argv
, 1, "r+");
563 get(int argc
, char *argv
[])
566 (void)getit(argc
, argv
, 0, restart_point
? "r+" : "w" );
571 * If restartit is 1, restart the xfer always.
572 * If restartit is -1, restart the xfer only if the remote file is newer.
575 getit(int argc
, char *argv
[], int restartit
, const char *gmode
)
578 char *remfile
, *olocfile
;
580 char buf
[MAXPATHLEN
];
588 if (argc
== 0 || (argc
== 1 && !another(&argc
, &argv
, "remote-file")))
590 if ((argc
< 3 && !another(&argc
, &argv
, "local-file")) || argc
> 3) {
592 UPRINTF("usage: %s remote-file [local-file]\n", argv
[0]);
597 if ((olocfile
= globulize(argv
[2])) == NULL
) {
601 locfile
= doprocess(buf
, sizeof(buf
), olocfile
,
602 loc
&& mcase
, loc
&& ntflag
, loc
&& mapflag
);
607 if (! features
[FEAT_REST_STREAM
]) {
609 "Restart is not supported by the remote server.\n");
612 ret
= stat(locfile
, &stbuf
);
613 if (restartit
== 1) {
615 warn("Can't stat `%s'", locfile
);
618 restart_point
= stbuf
.st_size
;
623 mtime
= remotemodtime(argv
[1], 0);
626 if (stbuf
.st_mtime
>= mtime
) {
634 recvrequest("RETR", locfile
, remfile
, gmode
,
635 remfile
!= argv
[1] || locfile
!= argv
[2], loc
);
638 (void)free(olocfile
);
649 write(fileno(ttyout
), "\n", 1);
650 siglongjmp(jabort
, 1);
654 mabort(const char *cmd
)
658 if (mflag
&& fromatty
) {
659 ointer
= interactive
;
663 if (confirm(cmd
, NULL
)) {
664 interactive
= ointer
;
668 interactive
= ointer
;
675 * Get multiple files.
678 mget(int argc
, char *argv
[])
684 int volatile restartit
;
687 (argc
== 1 && !another(&argc
, &argv
, "remote-files"))) {
688 UPRINTF("usage: %s remote-files\n", argv
[0]);
695 if (strcmp(argv
[0], "mreget") == 0) {
696 if (! features
[FEAT_REST_STREAM
]) {
698 "Restart is not supported by the remote server.\n");
703 oldintr
= xsignal(SIGINT
, mintr
);
704 if (sigsetjmp(jabort
, 1))
706 while ((cp
= remglob(argv
, proxy
, NULL
)) != NULL
) {
707 char buf
[MAXPATHLEN
];
708 if (*cp
== '\0' || !connected
) {
714 if (! fileindir(cp
, localcwd
)) {
715 fprintf(ttyout
, "Skipping non-relative filename `%s'\n",
719 if (!confirm(argv
[0], cp
))
721 tp
= doprocess(buf
, sizeof(buf
), cp
, mcase
, ntflag
, mapflag
);
725 if (stat(tp
, &stbuf
) == 0)
726 restart_point
= stbuf
.st_size
;
728 warn("Can't stat `%s'", tp
);
730 recvrequest("RETR", tp
, cp
, restart_point
? "r+" : "w",
731 tp
!= cp
|| !interactive
, 1);
733 if (!mflag
&& fromatty
) {
734 ointer
= interactive
;
736 if (confirm(argv
[0], NULL
))
738 interactive
= ointer
;
741 (void)xsignal(SIGINT
, oldintr
);
746 * Read list of filenames from a local file and get those
749 fget(int argc
, char *argv
[])
753 char buf
[MAXPATHLEN
], cmdbuf
[MAX_C_NAME
];
756 UPRINTF("usage: %s localfile\n", argv
[0]);
761 fp
= fopen(argv
[1], "r");
763 fprintf(ttyout
, "Can't open source file %s\n", argv
[1]);
768 (void)strlcpy(cmdbuf
, "get", sizeof(cmdbuf
));
770 gmode
= restart_point
? "r+" : "w";
772 while (get_line(fp
, buf
, sizeof(buf
), NULL
) >= 0) {
776 (void)getit(argc
, argv
, 0, gmode
);
785 return (bool ? "on" : "off");
793 status(int argc
, char *argv
[])
797 UPRINTF("usage: %s\n", argv
[0]);
803 fprintf(ttyout
, "Connected %sto %s.\n",
804 connected
== -1 ? "and logged in" : "", hostname
);
806 fputs("Not connected.\n", ttyout
);
810 fprintf(ttyout
, "Connected for proxy commands to %s.\n",
814 fputs("No proxy connection.\n", ttyout
);
818 fprintf(ttyout
, "Gate ftp: %s, server %s, port %s.\n", onoff(gatemode
),
819 *gateserver
? gateserver
: "(none)", gateport
);
820 fprintf(ttyout
, "Passive mode: %s; fallback to active mode: %s.\n",
821 onoff(passivemode
), onoff(activefallback
));
822 fprintf(ttyout
, "Mode: %s; Type: %s; Form: %s; Structure: %s.\n",
823 modename
, typename
, formname
, structname
);
824 fprintf(ttyout
, "Verbose: %s; Bell: %s; Prompting: %s; Globbing: %s.\n",
825 onoff(verbose
), onoff(bell
), onoff(interactive
), onoff(doglob
));
826 fprintf(ttyout
, "Store unique: %s; Receive unique: %s.\n",
827 onoff(sunique
), onoff(runique
));
828 fprintf(ttyout
, "Preserve modification times: %s.\n", onoff(preserve
));
829 fprintf(ttyout
, "Case: %s; CR stripping: %s.\n", onoff(mcase
),
832 fprintf(ttyout
, "Ntrans: (in) %s (out) %s\n", ntin
, ntout
);
835 fputs("Ntrans: off.\n", ttyout
);
838 fprintf(ttyout
, "Nmap: (in) %s (out) %s\n", mapin
, mapout
);
841 fputs("Nmap: off.\n", ttyout
);
844 "Hash mark printing: %s; Mark count: %d; Progress bar: %s.\n",
845 onoff(hash
), mark
, onoff(progress
));
847 "Get transfer rate throttle: %s; maximum: %d; increment %d.\n",
848 onoff(rate_get
), rate_get
, rate_get_incr
);
850 "Put transfer rate throttle: %s; maximum: %d; increment %d.\n",
851 onoff(rate_put
), rate_put
, rate_put_incr
);
853 "Socket buffer sizes: send %d, receive %d.\n",
854 sndbuf_size
, rcvbuf_size
);
855 fprintf(ttyout
, "Use of PORT cmds: %s.\n", onoff(sendport
));
856 fprintf(ttyout
, "Use of EPSV/EPRT cmds for IPv4: %s%s.\n", onoff(epsv4
),
857 epsv4bad
? " (disabled for this connection)" : "");
858 fprintf(ttyout
, "Use of EPSV/EPRT cmds for IPv6: %s%s.\n", onoff(epsv6
),
859 epsv6bad
? " (disabled for this connection)" : "");
860 fprintf(ttyout
, "Command line editing: %s.\n",
861 #ifdef NO_EDITCOMPLETE
862 "support not compiled in"
863 #else /* !def NO_EDITCOMPLETE */
865 #endif /* !def NO_EDITCOMPLETE */
870 fputs("Macros:\n", ttyout
);
871 for (i
=0; i
<macnum
; i
++) {
872 fprintf(ttyout
, "\t%s\n", macros
[i
].mac_name
);
875 #endif /* !def NO_STATUS */
876 fprintf(ttyout
, "Version: %s %s\n", FTP_PRODUCT
, FTP_VERSION
);
884 togglevar(int argc
, char *argv
[], int *var
, const char *mesg
)
888 } else if (argc
== 2 && strcasecmp(argv
[1], "on") == 0) {
890 } else if (argc
== 2 && strcasecmp(argv
[1], "off") == 0) {
893 UPRINTF("usage: %s [ on | off ]\n", argv
[0]);
897 fprintf(ttyout
, "%s %s.\n", mesg
, onoff(*var
));
902 * Set beep on cmd completed mode.
906 setbell(int argc
, char *argv
[])
909 code
= togglevar(argc
, argv
, &bell
, "Bell mode");
913 * Set command line editing
917 setedit(int argc
, char *argv
[])
920 #ifdef NO_EDITCOMPLETE
922 UPRINTF("usage: %s\n", argv
[0]);
927 fputs("Editing support not compiled in; ignoring command.\n",
929 #else /* !def NO_EDITCOMPLETE */
930 code
= togglevar(argc
, argv
, &editing
, "Editing mode");
932 #endif /* !def NO_EDITCOMPLETE */
936 * Turn on packet tracing.
940 settrace(int argc
, char *argv
[])
943 code
= togglevar(argc
, argv
, &trace
, "Packet tracing");
947 * Toggle hash mark printing during transfers, or set hash mark bytecount.
951 sethash(int argc
, char *argv
[])
955 else if (argc
!= 2) {
956 UPRINTF("usage: %s [ on | off | bytecount ]\n",
960 } else if (strcasecmp(argv
[1], "on") == 0)
962 else if (strcasecmp(argv
[1], "off") == 0)
967 nmark
= strsuftoi(argv
[1]);
969 fprintf(ttyout
, "mark: bad bytecount value `%s'.\n",
977 fprintf(ttyout
, "Hash mark printing %s", onoff(hash
));
979 fprintf(ttyout
, " (%d bytes/hash mark)", mark
);
980 fputs(".\n", ttyout
);
987 * Turn on printing of server echo's.
991 setverbose(int argc
, char *argv
[])
994 code
= togglevar(argc
, argv
, &verbose
, "Verbose mode");
998 * Toggle PORT/LPRT cmd use before each data connection.
1002 setport(int argc
, char *argv
[])
1005 code
= togglevar(argc
, argv
, &sendport
, "Use of PORT/LPRT cmds");
1009 * Toggle transfer progress bar.
1013 setprogress(int argc
, char *argv
[])
1016 code
= togglevar(argc
, argv
, &progress
, "Progress bar");
1022 * Turn on interactive prompting during mget, mput, and mdelete.
1026 setprompt(int argc
, char *argv
[])
1029 code
= togglevar(argc
, argv
, &interactive
, "Interactive mode");
1033 * Toggle gate-ftp mode, or set gate-ftp server
1037 setgate(int argc
, char *argv
[])
1039 static char gsbuf
[MAXHOSTNAMELEN
];
1041 if (argc
== 0 || argc
> 3) {
1043 "usage: %s [ on | off | gateserver [port] ]\n", argv
[0]);
1046 } else if (argc
< 2) {
1047 gatemode
= !gatemode
;
1049 if (argc
== 2 && strcasecmp(argv
[1], "on") == 0)
1051 else if (argc
== 2 && strcasecmp(argv
[1], "off") == 0)
1055 gateport
= ftp_strdup(argv
[2]);
1056 (void)strlcpy(gsbuf
, argv
[1], sizeof(gsbuf
));
1061 if (gatemode
&& (gateserver
== NULL
|| *gateserver
== '\0')) {
1063 "Disabling gate-ftp mode - no gate-ftp server defined.\n");
1066 fprintf(ttyout
, "Gate ftp: %s, server %s, port %s.\n",
1067 onoff(gatemode
), *gateserver
? gateserver
: "(none)",
1074 * Toggle metacharacter interpretation on local file names.
1078 setglob(int argc
, char *argv
[])
1081 code
= togglevar(argc
, argv
, &doglob
, "Globbing");
1085 * Toggle preserving modification times on retrieved files.
1089 setpreserve(int argc
, char *argv
[])
1092 code
= togglevar(argc
, argv
, &preserve
, "Preserve modification times");
1096 * Set debugging mode on/off and/or set level of debugging.
1100 setdebug(int argc
, char *argv
[])
1102 if (argc
== 0 || argc
> 2) {
1103 UPRINTF("usage: %s [ on | off | debuglevel ]\n", argv
[0]);
1106 } else if (argc
== 2) {
1107 if (strcasecmp(argv
[1], "on") == 0)
1109 else if (strcasecmp(argv
[1], "off") == 0)
1114 val
= strsuftoi(argv
[1]);
1116 fprintf(ttyout
, "%s: bad debugging value.\n",
1124 ftp_debug
= !ftp_debug
;
1126 options
|= SO_DEBUG
;
1128 options
&= ~SO_DEBUG
;
1129 fprintf(ttyout
, "Debugging %s (ftp_debug=%d).\n", onoff(ftp_debug
), ftp_debug
);
1130 code
= ftp_debug
> 0;
1134 * Set current working directory on remote machine.
1137 cd(int argc
, char *argv
[])
1141 if (argc
== 0 || argc
> 2 ||
1142 (argc
== 1 && !another(&argc
, &argv
, "remote-directory"))) {
1143 UPRINTF("usage: %s remote-directory\n", argv
[0]);
1147 r
= command("CWD %s", argv
[1]);
1148 if (r
== ERROR
&& code
== 500) {
1150 fputs("CWD command not recognized, trying XCWD.\n",
1152 r
= command("XCWD %s", argv
[1]);
1154 if (r
== COMPLETE
) {
1161 * Set current working directory on local machine.
1164 lcd(int argc
, char *argv
[])
1171 argv
[1] = localhome
;
1174 UPRINTF("usage: %s [local-directory]\n", argv
[0]);
1177 if ((locdir
= globulize(argv
[1])) == NULL
)
1179 if (chdir(locdir
) == -1)
1180 warn("Can't chdir `%s'", locdir
);
1184 fprintf(ttyout
, "Local directory now: %s\n", localcwd
);
1187 fprintf(ttyout
, "Unable to determine local directory\n");
1194 * Delete a single file.
1197 delete(int argc
, char *argv
[])
1200 if (argc
== 0 || argc
> 2 ||
1201 (argc
== 1 && !another(&argc
, &argv
, "remote-file"))) {
1202 UPRINTF("usage: %s remote-file\n", argv
[0]);
1206 if (command("DELE %s", argv
[1]) == COMPLETE
)
1211 * Delete multiple files.
1214 mdelete(int argc
, char *argv
[])
1221 (argc
== 1 && !another(&argc
, &argv
, "remote-files"))) {
1222 UPRINTF("usage: %s [remote-files]\n", argv
[0]);
1227 oldintr
= xsignal(SIGINT
, mintr
);
1228 if (sigsetjmp(jabort
, 1))
1230 while ((cp
= remglob(argv
, 0, NULL
)) != NULL
) {
1235 if (mflag
&& confirm(argv
[0], cp
)) {
1236 if (command("DELE %s", cp
) == COMPLETE
)
1238 if (!mflag
&& fromatty
) {
1239 ointer
= interactive
;
1241 if (confirm(argv
[0], NULL
)) {
1244 interactive
= ointer
;
1248 (void)xsignal(SIGINT
, oldintr
);
1253 * Rename a remote file.
1256 renamefile(int argc
, char *argv
[])
1259 if (argc
== 0 || (argc
== 1 && !another(&argc
, &argv
, "from-name")))
1261 if ((argc
< 3 && !another(&argc
, &argv
, "to-name")) || argc
> 3) {
1263 UPRINTF("usage: %s from-name to-name\n", argv
[0]);
1267 if (command("RNFR %s", argv
[1]) == CONTINUE
&&
1268 command("RNTO %s", argv
[2]) == COMPLETE
)
1273 * Get a directory listing of remote files.
1274 * Supports being invoked as:
1280 * pdir, pls LIST |$PAGER
1281 * pmlsd MLSD |$PAGER
1284 ls(int argc
, char *argv
[])
1287 char *remdir
, *locbuf
;
1288 const char *locfile
;
1289 int pagecmd
, mlsdcmd
;
1294 pagecmd
= mlsdcmd
= 0;
1296 * the only commands that start with `p' are
1297 * the `pager' versions.
1299 if (argv
[0][0] == 'p')
1301 if (strcmp(argv
[0] + pagecmd
, "mlsd") == 0) {
1302 if (! features
[FEAT_MLST
]) {
1304 "MLSD is not supported by the remote server.\n");
1314 else if (strcmp(argv
[0] + pagecmd
, "nlist") == 0)
1323 if (argc
> 3 || ((pagecmd
| mlsdcmd
) && argc
> 2)) {
1325 if (pagecmd
|| mlsdcmd
)
1326 UPRINTF("usage: %s [remote-path]\n", argv
[0]);
1328 UPRINTF("usage: %s [remote-path [local-file]]\n",
1338 p
= getoptionvalue("pager");
1341 len
= strlen(p
) + 2;
1342 locbuf
= ftp_malloc(len
);
1344 (void)strlcpy(locbuf
+ 1, p
, len
- 1);
1346 } else if ((strcmp(locfile
, "-") != 0) && *locfile
!= '|') {
1347 if ((locbuf
= globulize(locfile
)) == NULL
||
1348 !confirm("output to local-file:", locbuf
)) {
1354 recvrequest(cmd
, locfile
, remdir
, "w", 0, 0);
1361 * Get a directory listing of multiple remote files.
1364 mls(int argc
, char *argv
[])
1368 int volatile dolist
;
1369 char * volatile dest
, *odest
;
1374 if (argc
< 2 && !another(&argc
, &argv
, "remote-files"))
1376 if (argc
< 3 && !another(&argc
, &argv
, "local-file")) {
1378 UPRINTF("usage: %s remote-files local-file\n", argv
[0]);
1382 odest
= dest
= argv
[argc
- 1];
1383 argv
[argc
- 1] = NULL
;
1384 if (strcmp(dest
, "-") && *dest
!= '|')
1385 if (((dest
= globulize(dest
)) == NULL
) ||
1386 !confirm("output to local-file:", dest
)) {
1390 dolist
= strcmp(argv
[0], "mls");
1392 oldintr
= xsignal(SIGINT
, mintr
);
1393 if (sigsetjmp(jabort
, 1))
1395 for (i
= 1; mflag
&& i
< argc
-1 && connected
; i
++) {
1396 lmode
= (i
== 1) ? "w" : "a";
1397 recvrequest(dolist
? "LIST" : "NLST", dest
, argv
[i
], lmode
,
1399 if (!mflag
&& fromatty
) {
1400 ointer
= interactive
;
1402 if (confirm(argv
[0], NULL
)) {
1405 interactive
= ointer
;
1408 (void)xsignal(SIGINT
, oldintr
);
1410 if (dest
!= odest
) /* free up after globulize() */
1419 shell(int argc
, char *argv
[])
1423 char shellnam
[MAXPATHLEN
];
1424 const char *shellp
, *namep
;
1428 UPRINTF("usage: %s [command [args]]\n", argv
[0]);
1432 oldintr
= xsignal(SIGINT
, SIG_IGN
);
1433 if ((pid
= fork()) == 0) {
1434 for (pid
= 3; pid
< 20; pid
++)
1436 (void)xsignal(SIGINT
, SIG_DFL
);
1437 shellp
= getenv("SHELL");
1439 shellp
= _PATH_BSHELL
;
1440 namep
= strrchr(shellp
, '/');
1445 (void)strlcpy(shellnam
, namep
, sizeof(shellnam
));
1447 fputs(shellp
, ttyout
);
1451 execl(shellp
, shellnam
, "-c", altarg
, (char *)0);
1454 execl(shellp
, shellnam
, (char *)0);
1456 warn("Can't execute `%s'", shellp
);
1461 while (wait(&wait_status
) != pid
)
1463 (void)xsignal(SIGINT
, oldintr
);
1465 warn("Can't fork a subshell; try again later");
1472 * Send new user information (re-login)
1475 user(int argc
, char *argv
[])
1478 char emptypass
[] = "";
1484 (void)another(&argc
, &argv
, "username");
1485 if (argc
< 2 || argc
> 4) {
1487 UPRINTF("usage: %s username [password [account]]\n",
1492 n
= command("USER %s", argv
[1]);
1493 if (n
== CONTINUE
) {
1495 password
= getpass("Password: ");
1496 if (password
== NULL
)
1497 password
= emptypass
;
1501 n
= command("PASS %s", password
);
1502 memset(password
, 0, strlen(password
));
1504 if (n
== CONTINUE
) {
1507 password
= getpass("Account: ");
1508 if (password
== NULL
)
1509 password
= emptypass
;
1513 n
= command("ACCT %s", password
);
1514 memset(password
, 0, strlen(password
));
1516 if (n
!= COMPLETE
) {
1517 fputs("Login failed.\n", ttyout
);
1520 if (!aflag
&& argc
== 4) {
1522 (void)command("ACCT %s", password
);
1523 memset(password
, 0, strlen(password
));
1530 * Print working directory on remote machine.
1534 pwd(int argc
, char *argv
[])
1539 UPRINTF("usage: %s\n", argv
[0]);
1545 fprintf(ttyout
, "Unable to determine remote directory\n");
1547 fprintf(ttyout
, "Remote directory: %s\n", remotecwd
);
1553 * Print working directory on local machine.
1556 lpwd(int argc
, char *argv
[])
1561 UPRINTF("usage: %s\n", argv
[0]);
1567 fprintf(ttyout
, "Unable to determine local directory\n");
1569 fprintf(ttyout
, "Local directory: %s\n", localcwd
);
1578 makedir(int argc
, char *argv
[])
1582 if (argc
== 0 || argc
> 2 ||
1583 (argc
== 1 && !another(&argc
, &argv
, "directory-name"))) {
1584 UPRINTF("usage: %s directory-name\n", argv
[0]);
1588 r
= command("MKD %s", argv
[1]);
1589 if (r
== ERROR
&& code
== 500) {
1591 fputs("MKD command not recognized, trying XMKD.\n",
1593 r
= command("XMKD %s", argv
[1]);
1600 * Remove a directory.
1603 removedir(int argc
, char *argv
[])
1607 if (argc
== 0 || argc
> 2 ||
1608 (argc
== 1 && !another(&argc
, &argv
, "directory-name"))) {
1609 UPRINTF("usage: %s directory-name\n", argv
[0]);
1613 r
= command("RMD %s", argv
[1]);
1614 if (r
== ERROR
&& code
== 500) {
1616 fputs("RMD command not recognized, trying XRMD.\n",
1618 r
= command("XRMD %s", argv
[1]);
1625 * Send a line, verbatim, to the remote machine.
1628 quote(int argc
, char *argv
[])
1632 (argc
== 1 && !another(&argc
, &argv
, "command line to send"))) {
1633 UPRINTF("usage: %s line-to-send\n", argv
[0]);
1637 quote1("", argc
, argv
);
1641 * Send a SITE command to the remote machine. The line
1642 * is sent verbatim to the remote machine, except that the
1643 * word "SITE" is added at the front.
1646 site(int argc
, char *argv
[])
1650 (argc
== 1 && !another(&argc
, &argv
, "arguments to SITE command"))){
1651 UPRINTF("usage: %s line-to-send\n", argv
[0]);
1655 quote1("SITE ", argc
, argv
);
1659 * Turn argv[1..argc) into a space-separated string, then prepend initial text.
1660 * Send the result as a one-line command and get response.
1663 quote1(const char *initial
, int argc
, char *argv
[])
1666 char buf
[BUFSIZ
]; /* must be >= sizeof(line) */
1668 (void)strlcpy(buf
, initial
, sizeof(buf
));
1669 for (i
= 1; i
< argc
; i
++) {
1670 (void)strlcat(buf
, argv
[i
], sizeof(buf
));
1672 (void)strlcat(buf
, " ", sizeof(buf
));
1674 if (command("%s", buf
) == PRELIM
) {
1675 while (getreply(0) == PRELIM
)
1682 do_chmod(int argc
, char *argv
[])
1685 if (argc
== 0 || (argc
== 1 && !another(&argc
, &argv
, "mode")))
1687 if ((argc
< 3 && !another(&argc
, &argv
, "remote-file")) || argc
> 3) {
1689 UPRINTF("usage: %s mode remote-file\n", argv
[0]);
1693 (void)command("SITE CHMOD %s %s", argv
[1], argv
[2]);
1696 #define COMMAND_1ARG(argc, argv, cmd) \
1700 command(cmd " %s", argv[1])
1703 do_umask(int argc
, char *argv
[])
1705 int oldverbose
= verbose
;
1708 UPRINTF("usage: %s [umask]\n", argv
[0]);
1713 COMMAND_1ARG(argc
, argv
, "SITE UMASK");
1714 verbose
= oldverbose
;
1718 idlecmd(int argc
, char *argv
[])
1720 int oldverbose
= verbose
;
1722 if (argc
< 1 || argc
> 2) {
1723 UPRINTF("usage: %s [seconds]\n", argv
[0]);
1728 COMMAND_1ARG(argc
, argv
, "SITE IDLE");
1729 verbose
= oldverbose
;
1733 * Ask the other side for help.
1736 rmthelp(int argc
, char *argv
[])
1738 int oldverbose
= verbose
;
1741 UPRINTF("usage: %s\n", argv
[0]);
1746 COMMAND_1ARG(argc
, argv
, "HELP");
1747 verbose
= oldverbose
;
1751 * Terminate session and exit.
1752 * May be called with 0, NULL.
1756 quit(int argc
, char *argv
[])
1759 /* this may be called with argc == 0, argv == NULL */
1760 if (argc
== 0 && argv
!= NULL
) {
1761 UPRINTF("usage: %s\n", argv
[0]);
1766 disconnect(0, NULL
);
1769 disconnect(0, NULL
);
1774 * Terminate session, but don't exit.
1775 * May be called with 0, NULL.
1778 disconnect(int argc
, char *argv
[])
1781 /* this may be called with argc == 0, argv == NULL */
1782 if (argc
== 0 && argv
!= NULL
) {
1783 UPRINTF("usage: %s\n", argv
[0]);
1789 (void)command("QUIT");
1794 account(int argc
, char *argv
[])
1797 char emptypass
[] = "";
1799 if (argc
== 0 || argc
> 2) {
1800 UPRINTF("usage: %s [password]\n", argv
[0]);
1807 ap
= getpass("Account:");
1811 (void)command("ACCT %s", ap
);
1812 memset(ap
, 0, strlen(ap
));
1815 sigjmp_buf abortprox
;
1818 proxabort(int notused
)
1833 siglongjmp(abortprox
, 1);
1837 doproxy(int argc
, char *argv
[])
1842 char cmdbuf
[MAX_C_NAME
];
1844 if (argc
== 0 || (argc
== 1 && !another(&argc
, &argv
, "command"))) {
1845 UPRINTF("usage: %s command\n", argv
[0]);
1849 c
= getcmd(argv
[1]);
1850 if (c
== (struct cmd
*) -1) {
1851 fputs("?Ambiguous command.\n", ttyout
);
1856 fputs("?Invalid command.\n", ttyout
);
1861 fputs("?Invalid proxy command.\n", ttyout
);
1865 if (sigsetjmp(abortprox
, 1)) {
1869 oldintr
= xsignal(SIGINT
, proxabort
);
1871 if (c
->c_conn
&& !connected
) {
1872 fputs("Not connected.\n", ttyout
);
1874 (void)xsignal(SIGINT
, oldintr
);
1878 cmdpos
= strcspn(line
, " \t");
1879 if (cmdpos
> 0) /* remove leading "proxy " from input buffer */
1880 memmove(line
, line
+ cmdpos
+ 1, strlen(line
) - cmdpos
+ 1);
1881 (void)strlcpy(cmdbuf
, c
->c_name
, sizeof(cmdbuf
));
1883 (*c
->c_handler
)(argc
-1, argv
+1);
1891 (void)xsignal(SIGINT
, oldintr
);
1895 setcase(int argc
, char *argv
[])
1898 code
= togglevar(argc
, argv
, &mcase
, "Case mapping");
1902 * convert the given name to lower case if it's all upper case, into
1903 * a static buffer which is returned to the caller
1906 docase(char *dst
, size_t dlen
, const char *src
)
1911 for (i
= 0; src
[i
] != '\0' && i
< dlen
- 1; i
++) {
1913 if (islower((unsigned char)dst
[i
]))
1919 for (i
= 0; dst
[i
] != '\0'; i
++)
1920 if (isupper((unsigned char)dst
[i
]))
1921 dst
[i
] = tolower((unsigned char)dst
[i
]);
1927 setcr(int argc
, char *argv
[])
1930 code
= togglevar(argc
, argv
, &crflag
, "Carriage Return stripping");
1934 setntrans(int argc
, char *argv
[])
1937 if (argc
== 0 || argc
> 3) {
1938 UPRINTF("usage: %s [inchars [outchars]]\n", argv
[0]);
1944 fputs("Ntrans off.\n", ttyout
);
1950 (void)strlcpy(ntin
, argv
[1], sizeof(ntin
));
1955 (void)strlcpy(ntout
, argv
[2], sizeof(ntout
));
1959 dotrans(char *dst
, size_t dlen
, const char *src
)
1965 for (ostop
= 0; *(ntout
+ ostop
) && ostop
< 16; ostop
++)
1967 for (cp1
= src
; *cp1
; cp1
++) {
1969 for (i
= 0; *(ntin
+ i
) && i
< 16; i
++) {
1970 if (*cp1
== *(ntin
+ i
)) {
1973 *cp2
++ = *(ntout
+ i
);
1974 if (cp2
- dst
>= (ptrdiff_t)(dlen
- 1))
1990 setnmap(int argc
, char *argv
[])
1996 fputs("Nmap off.\n", ttyout
);
2001 (argc
< 3 && !another(&argc
, &argv
, "mapout")) || argc
> 3) {
2002 UPRINTF("usage: %s [mapin mapout]\n", argv
[0]);
2008 cp
= strchr(altarg
, ' ');
2013 cp
= strchr(altarg
, ' ');
2016 (void)strlcpy(mapin
, altarg
, MAXPATHLEN
);
2017 while (*++cp
== ' ')
2019 (void)strlcpy(mapout
, cp
, MAXPATHLEN
);
2023 domap(char *dst
, size_t dlen
, const char *src
)
2025 const char *cp1
= src
;
2027 const char *tp
[9], *te
[9];
2028 int i
, toks
[9], toknum
= 0, match
= 1;
2030 for (i
=0; i
< 9; ++i
) {
2033 while (match
&& *cp1
&& *cp2
) {
2036 if (*++cp2
!= *cp1
) {
2041 if (*(cp2
+1) >= '1' && (*cp2
+1) <= '9') {
2042 if (*cp1
!= *(++cp2
+1)) {
2043 toks
[toknum
= *cp2
- '1']++;
2045 while (*++cp1
&& *(cp2
+1)
2059 if (match
&& *cp1
) {
2062 if (match
&& *cp2
) {
2066 if (!match
&& *cp1
) /* last token mismatch */
2083 if (*++cp1
== '$' &&
2084 isdigit((unsigned char)*(cp1
+1))) {
2085 if (*++cp1
== '0') {
2086 const char *cp3
= src
;
2093 else if (toks
[toknum
= *cp1
- '1']) {
2094 const char *cp3
= tp
[toknum
];
2096 while (cp3
!= te
[toknum
]) {
2103 while (*cp1
&& *cp1
!= ',' &&
2108 else if (*cp1
== '$' &&
2109 isdigit((unsigned char)*(cp1
+1))) {
2110 if (*++cp1
== '0') {
2111 const char *cp3
= src
;
2117 else if (toks
[toknum
=
2119 const char *cp3
=tp
[toknum
];
2133 "nmap: unbalanced brackets.\n",
2141 while (*++cp1
&& *cp1
!= ']') {
2142 if (*cp1
== '\\' && *(cp1
+ 1)) {
2148 "nmap: unbalanced brackets.\n",
2165 if (isdigit((unsigned char)*(cp1
+ 1))) {
2166 if (*++cp1
== '0') {
2167 const char *cp3
= src
;
2173 else if (toks
[toknum
= *cp1
- '1']) {
2174 const char *cp3
= tp
[toknum
];
2176 while (cp3
!= te
[toknum
]) {
2182 /* intentional drop through */
2190 return *dst
? dst
: src
;
2194 setpassive(int argc
, char *argv
[])
2198 passivemode
= !passivemode
;
2199 activefallback
= passivemode
;
2200 } else if (argc
!= 2) {
2202 UPRINTF("usage: %s [ on | off | auto ]\n", argv
[0]);
2205 } else if (strcasecmp(argv
[1], "on") == 0) {
2208 } else if (strcasecmp(argv
[1], "off") == 0) {
2211 } else if (strcasecmp(argv
[1], "auto") == 0) {
2216 fprintf(ttyout
, "Passive mode: %s; fallback to active mode: %s.\n",
2217 onoff(passivemode
), onoff(activefallback
));
2223 setepsv4(int argc
, char *argv
[])
2225 code
= togglevar(argc
, argv
, &epsv4
,
2226 verbose
? "EPSV/EPRT on IPv4" : NULL
);
2231 setepsv6(int argc
, char *argv
[])
2233 code
= togglevar(argc
, argv
, &epsv6
,
2234 verbose
? "EPSV/EPRT on IPv6" : NULL
);
2239 setepsv(int argc
, char*argv
[])
2241 setepsv4(argc
,argv
);
2242 setepsv6(argc
,argv
);
2246 setsunique(int argc
, char *argv
[])
2249 code
= togglevar(argc
, argv
, &sunique
, "Store unique");
2253 setrunique(int argc
, char *argv
[])
2256 code
= togglevar(argc
, argv
, &runique
, "Receive unique");
2260 parserate(int argc
, char *argv
[], int cmdlineopt
)
2262 int dir
, max
, incr
, showonly
;
2263 sigfunc oldusr1
, oldusr2
;
2265 if (argc
> 4 || (argc
< (cmdlineopt
? 3 : 2))) {
2269 "usage: %s (all|get|put),maximum-bytes[,increment-bytes]]\n",
2273 "usage: %s (all|get|put) [maximum-bytes [increment-bytes]]\n",
2277 dir
= max
= incr
= showonly
= 0;
2280 #define RATE_ALL (RATE_GET | RATE_PUT)
2282 if (strcasecmp(argv
[1], "all") == 0)
2284 else if (strcasecmp(argv
[1], "get") == 0)
2286 else if (strcasecmp(argv
[1], "put") == 0)
2292 if ((max
= strsuftoi(argv
[2])) < 0)
2298 if ((incr
= strsuftoi(argv
[3])) <= 0)
2303 oldusr1
= xsignal(SIGUSR1
, SIG_IGN
);
2304 oldusr2
= xsignal(SIGUSR2
, SIG_IGN
);
2305 if (dir
& RATE_GET
) {
2308 rate_get_incr
= incr
;
2310 if (!cmdlineopt
|| verbose
)
2312 "Get transfer rate throttle: %s; maximum: %d; increment %d.\n",
2313 onoff(rate_get
), rate_get
, rate_get_incr
);
2315 if (dir
& RATE_PUT
) {
2318 rate_put_incr
= incr
;
2320 if (!cmdlineopt
|| verbose
)
2322 "Put transfer rate throttle: %s; maximum: %d; increment %d.\n",
2323 onoff(rate_put
), rate_put
, rate_put_incr
);
2325 (void)xsignal(SIGUSR1
, oldusr1
);
2326 (void)xsignal(SIGUSR2
, oldusr2
);
2331 setrate(int argc
, char *argv
[])
2334 code
= parserate(argc
, argv
, 0);
2337 /* change directory to parent directory */
2339 cdup(int argc
, char *argv
[])
2344 UPRINTF("usage: %s\n", argv
[0]);
2348 r
= command("CDUP");
2349 if (r
== ERROR
&& code
== 500) {
2351 fputs("CDUP command not recognized, trying XCUP.\n",
2353 r
= command("XCUP");
2355 if (r
== COMPLETE
) {
2362 * Restart transfer at specific point
2365 restart(int argc
, char *argv
[])
2368 if (argc
== 0 || argc
> 2) {
2369 UPRINTF("usage: %s [restart-point]\n", argv
[0]);
2373 if (! features
[FEAT_REST_STREAM
]) {
2375 "Restart is not supported by the remote server.\n");
2382 rp
= STRTOLL(argv
[1], &ep
, 10);
2383 if (rp
< 0 || *ep
!= '\0')
2384 fprintf(ttyout
, "restart: Invalid offset `%s'\n",
2389 if (restart_point
== 0)
2390 fputs("No restart point defined.\n", ttyout
);
2393 "Restarting at " LLF
" for next get, put or append\n",
2394 (LLT
)restart_point
);
2398 * Show remote system type
2401 syst(int argc
, char *argv
[])
2403 int oldverbose
= verbose
;
2406 UPRINTF("usage: %s\n", argv
[0]);
2410 verbose
= 1; /* If we aren't verbose, this doesn't do anything! */
2411 (void)command("SYST");
2412 verbose
= oldverbose
;
2416 macdef(int argc
, char *argv
[])
2424 fputs("Limit of 16 macros have already been defined.\n",
2429 if ((argc
< 2 && !another(&argc
, &argv
, "macro name")) || argc
> 2) {
2431 UPRINTF("usage: %s macro_name\n", argv
[0]);
2437 "Enter macro line by line, terminating it with a null line.\n",
2439 (void)strlcpy(macros
[macnum
].mac_name
, argv
[1],
2440 sizeof(macros
[macnum
].mac_name
));
2442 macros
[macnum
].mac_start
= macbuf
;
2444 macros
[macnum
].mac_start
= macros
[macnum
- 1].mac_end
+ 1;
2445 tmp
= macros
[macnum
].mac_start
;
2446 while (tmp
!= macbuf
+4096) {
2447 if ((c
= getchar()) == EOF
) {
2448 fputs("macdef: end of file encountered.\n", ttyout
);
2452 if ((*tmp
= c
) == '\n') {
2453 if (tmp
== macros
[macnum
].mac_start
) {
2454 macros
[macnum
++].mac_end
= tmp
;
2458 if (*(tmp
-1) == '\0') {
2459 macros
[macnum
++].mac_end
= tmp
- 1;
2468 while ((c
= getchar()) != '\n' && c
!= EOF
)
2470 if (c
== EOF
|| getchar() == '\n') {
2471 fputs("Macro not defined - 4K buffer exceeded.\n",
2480 * Get size of file on remote machine
2483 sizecmd(int argc
, char *argv
[])
2487 if (argc
== 0 || argc
> 2 ||
2488 (argc
== 1 && !another(&argc
, &argv
, "remote-file"))) {
2489 UPRINTF("usage: %s remote-file\n", argv
[0]);
2493 size
= remotesize(argv
[1], 1);
2496 "%s\t" LLF
"\n", argv
[1], (LLT
)size
);
2501 * Get last modification time of file on remote machine
2504 modtime(int argc
, char *argv
[])
2508 if (argc
== 0 || argc
> 2 ||
2509 (argc
== 1 && !another(&argc
, &argv
, "remote-file"))) {
2510 UPRINTF("usage: %s remote-file\n", argv
[0]);
2514 mtime
= remotemodtime(argv
[1], 1);
2516 fprintf(ttyout
, "%s\t%s", argv
[1],
2517 rfc2822time(localtime(&mtime
)));
2522 * Show status on remote machine
2525 rmtstatus(int argc
, char *argv
[])
2529 UPRINTF("usage: %s [remote-file]\n", argv
[0]);
2533 COMMAND_1ARG(argc
, argv
, "STAT");
2537 * Get file if modtime is more recent than current file
2540 newer(int argc
, char *argv
[])
2543 if (getit(argc
, argv
, -1, "w"))
2545 "Local file \"%s\" is newer than remote file \"%s\".\n",
2550 * Display one local file through $PAGER.
2553 lpage(int argc
, char *argv
[])
2557 char *pager
, *locfile
;
2559 if (argc
== 0 || argc
> 2 ||
2560 (argc
== 1 && !another(&argc
, &argv
, "local-file"))) {
2561 UPRINTF("usage: %s local-file\n", argv
[0]);
2565 if ((locfile
= globulize(argv
[1])) == NULL
) {
2569 p
= getoptionvalue("pager");
2572 len
= strlen(p
) + strlen(locfile
) + 2;
2573 pager
= ftp_malloc(len
);
2574 (void)strlcpy(pager
, p
, len
);
2575 (void)strlcat(pager
, " ", len
);
2576 (void)strlcat(pager
, locfile
, len
);
2580 (void)free(locfile
);
2584 * Display one remote file through $PAGER.
2587 page(int argc
, char *argv
[])
2589 int ohash
, orestart_point
, overbose
;
2594 if (argc
== 0 || argc
> 2 ||
2595 (argc
== 1 && !another(&argc
, &argv
, "remote-file"))) {
2596 UPRINTF("usage: %s remote-file\n", argv
[0]);
2600 p
= getoptionvalue("pager");
2603 len
= strlen(p
) + 2;
2604 pager
= ftp_malloc(len
);
2606 (void)strlcpy(pager
+ 1, p
, len
- 1);
2609 orestart_point
= restart_point
;
2611 hash
= restart_point
= verbose
= 0;
2612 recvrequest("RETR", pager
, argv
[1], "r+", 1, 0);
2614 restart_point
= orestart_point
;
2620 * Set the socket send or receive buffer size.
2623 setxferbuf(int argc
, char *argv
[])
2629 UPRINTF("usage: %s size\n", argv
[0]);
2633 if (strcasecmp(argv
[0], "sndbuf") == 0)
2635 else if (strcasecmp(argv
[0], "rcvbuf") == 0)
2637 else if (strcasecmp(argv
[0], "xferbuf") == 0)
2642 if ((size
= strsuftoi(argv
[1])) == -1)
2646 fprintf(ttyout
, "%s: size must be positive.\n", argv
[0]);
2654 fprintf(ttyout
, "Socket buffer sizes: send %d, receive %d.\n",
2655 sndbuf_size
, rcvbuf_size
);
2660 * Set or display options (defaults are provided by various env vars)
2663 setoption(int argc
, char *argv
[])
2668 if (argc
== 0 || (argc
!= 1 && argc
!= 3)) {
2669 UPRINTF("usage: %s [option value]\n", argv
[0]);
2673 #define OPTIONINDENT ((int) sizeof("http_proxy"))
2675 for (o
= optiontab
; o
->name
!= NULL
; o
++) {
2676 fprintf(ttyout
, "%-*s\t%s\n", OPTIONINDENT
,
2677 o
->name
, o
->value
? o
->value
: "");
2680 set_option(argv
[1], argv
[2], 1);
2686 set_option(const char * option
, const char * value
, int doverbose
)
2690 o
= getoption(option
);
2692 fprintf(ttyout
, "No such option `%s'.\n", option
);
2696 o
->value
= ftp_strdup(value
);
2697 if (verbose
&& doverbose
)
2698 fprintf(ttyout
, "Setting `%s' to `%s'.\n",
2706 unsetoption(int argc
, char *argv
[])
2711 if (argc
== 0 || argc
!= 2) {
2712 UPRINTF("usage: %s option\n", argv
[0]);
2716 o
= getoption(argv
[1]);
2718 fprintf(ttyout
, "No such option `%s'.\n", argv
[1]);
2722 fprintf(ttyout
, "Unsetting `%s'.\n", o
->name
);
2727 * Display features supported by the remote host.
2730 feat(int argc
, char *argv
[])
2732 int oldverbose
= verbose
;
2735 UPRINTF("usage: %s\n", argv
[0]);
2739 if (! features
[FEAT_FEAT
]) {
2741 "FEAT is not supported by the remote server.\n");
2744 verbose
= 1; /* If we aren't verbose, this doesn't do anything! */
2745 (void)command("FEAT");
2746 verbose
= oldverbose
;
2750 mlst(int argc
, char *argv
[])
2752 int oldverbose
= verbose
;
2754 if (argc
< 1 || argc
> 2) {
2755 UPRINTF("usage: %s [remote-path]\n", argv
[0]);
2759 if (! features
[FEAT_MLST
]) {
2761 "MLST is not supported by the remote server.\n");
2764 verbose
= 1; /* If we aren't verbose, this doesn't do anything! */
2765 COMMAND_1ARG(argc
, argv
, "MLST");
2766 verbose
= oldverbose
;
2770 opts(int argc
, char *argv
[])
2772 int oldverbose
= verbose
;
2774 if (argc
< 2 || argc
> 3) {
2775 UPRINTF("usage: %s command [options]\n", argv
[0]);
2779 if (! features
[FEAT_FEAT
]) {
2781 "OPTS is not supported by the remote server.\n");
2784 verbose
= 1; /* If we aren't verbose, this doesn't do anything! */
2786 command("OPTS %s", argv
[1]);
2788 command("OPTS %s %s", argv
[1], argv
[2]);
2789 verbose
= oldverbose
;