1 /* $NetBSD: cmds.c,v 1.18 2013/05/05 11:17:30 lukem Exp $ */
2 /* from NetBSD: cmds.c,v 1.135 2012/12/22 16:57:09 christos Exp */
5 * Copyright (c) 1996-2009 The NetBSD Foundation, Inc.
8 * This code is derived from software contributed to The NetBSD Foundation
11 * This code is derived from software contributed to The NetBSD Foundation
12 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
13 * NASA Ames Research Center.
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in the
22 * documentation and/or other materials provided with the distribution.
24 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
25 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
26 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
27 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
28 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 * POSSIBILITY OF SUCH DAMAGE.
38 * Copyright (c) 1985, 1989, 1993, 1994
39 * The Regents of the University of California. All rights reserved.
41 * Redistribution and use in source and binary forms, with or without
42 * modification, are permitted provided that the following conditions
44 * 1. Redistributions of source code must retain the above copyright
45 * notice, this list of conditions and the following disclaimer.
46 * 2. Redistributions in binary form must reproduce the above copyright
47 * notice, this list of conditions and the following disclaimer in the
48 * documentation and/or other materials provided with the distribution.
49 * 3. Neither the name of the University nor the names of its contributors
50 * may be used to endorse or promote products derived from this software
51 * without specific prior written permission.
53 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
54 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
55 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
56 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
57 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
58 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
59 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
60 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
61 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
62 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
67 * Copyright (C) 1997 and 1998 WIDE Project.
68 * All rights reserved.
70 * Redistribution and use in source and binary forms, with or without
71 * modification, are permitted provided that the following conditions
73 * 1. Redistributions of source code must retain the above copyright
74 * notice, this list of conditions and the following disclaimer.
75 * 2. Redistributions in binary form must reproduce the above copyright
76 * notice, this list of conditions and the following disclaimer in the
77 * documentation and/or other materials provided with the distribution.
78 * 3. Neither the name of the project nor the names of its contributors
79 * may be used to endorse or promote products derived from this software
80 * without specific prior written permission.
82 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
83 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
84 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
85 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
86 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
87 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
88 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
89 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
90 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
91 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
99 #include <sys/cdefs.h>
102 static char sccsid
[] = "@(#)cmds.c 8.6 (Berkeley) 10/9/94";
104 __RCSID(" NetBSD: cmds.c,v 1.135 2012/12/22 16:57:09 christos Exp ");
106 #endif /* not lint */
109 * FTP User Program -- Command Routines.
111 #include <sys/types.h>
112 #include <sys/socket.h>
113 #include <sys/stat.h>
114 #include <sys/wait.h>
115 #include <arpa/ftp.h>
136 static struct types
{
142 { "ascii", "A", TYPE_A
, 0 },
143 { "binary", "I", TYPE_I
, 0 },
144 { "image", "I", TYPE_I
, 0 },
145 { "ebcdic", "E", TYPE_E
, 0 },
146 { "tenex", "L", TYPE_L
, bytename
},
147 { NULL
, NULL
, 0, NULL
}
150 static sigjmp_buf jabort
;
152 static int confirm(const char *, const char *);
153 __dead
static void mintr(int);
154 static void mabort(const char *);
155 static void set_type(const char *);
157 static const char *doprocess(char *, size_t, const char *, int, int, int);
158 static const char *domap(char *, size_t, const char *);
159 static const char *docase(char *, size_t, const char *);
160 static const char *dotrans(char *, size_t, const char *);
163 * Confirm if "cmd" is to be performed upon "file".
164 * If "file" is NULL, generate a "Continue with" prompt instead.
167 confirm(const char *cmd
, const char *file
)
169 const char *errormsg
;
171 const char *promptleft
, *promptright
;
173 if (!interactive
|| confirmrest
)
176 promptleft
= "Continue with";
183 fprintf(ttyout
, "%s %s [anpqy?]? ", promptleft
, promptright
);
184 (void)fflush(ttyout
);
185 if (get_line(stdin
, cline
, sizeof(cline
), &errormsg
) < 0) {
187 fprintf(ttyout
, "%s; %s aborted\n", errormsg
, cmd
);
190 switch (tolower((unsigned char)*cline
)) {
194 "Prompting off for duration of %s.\n", cmd
);
198 fputs("Interactive mode: off.\n", ttyout
);
202 fprintf(ttyout
, "%s aborted.\n", cmd
);
208 " confirmation options:\n"
209 "\ta answer `yes' for the duration of %s\n"
210 "\tn answer `no' for this file\n"
211 "\tp turn off `prompt' mode\n"
212 "\tq stop the current %s\n"
213 "\ty answer `yes' for this file\n"
214 "\t? this help list\n",
216 continue; /* back to while(1) */
227 settype(int argc
, char *argv
[])
231 if (argc
== 0 || argc
> 2) {
234 UPRINTF("usage: %s [", argv
[0]);
236 for (p
= types
; p
->t_name
; p
++) {
237 fprintf(ttyout
, "%s%s", sep
, p
->t_name
);
240 fputs(" ]\n", ttyout
);
245 fprintf(ttyout
, "Using %s mode to transfer files.\n", typename
);
253 set_type(const char *ttype
)
258 for (p
= types
; p
->t_name
; p
++)
259 if (strcmp(ttype
, p
->t_name
) == 0)
261 if (p
->t_name
== 0) {
262 fprintf(ttyout
, "%s: unknown mode.\n", ttype
);
266 if ((p
->t_arg
!= NULL
) && (*(p
->t_arg
) != '\0'))
267 comret
= command("TYPE %s %s", p
->t_mode
, p
->t_arg
);
269 comret
= command("TYPE %s", p
->t_mode
);
270 if (comret
== COMPLETE
) {
271 (void)strlcpy(typename
, p
->t_name
, sizeof(typename
));
272 curtype
= type
= p
->t_type
;
277 * Internal form of settype; changes current type in use with server
278 * without changing our notion of the type for data transfers.
279 * Used to change to and from ascii for listings.
282 changetype(int newtype
, int show
)
285 int comret
, oldverbose
= verbose
;
289 if (newtype
== curtype
)
291 if (ftp_debug
== 0 && show
== 0)
293 for (p
= types
; p
->t_name
; p
++)
294 if (newtype
== p
->t_type
)
296 if (p
->t_name
== 0) {
297 errx(1, "changetype: unknown type %d", newtype
);
299 if (newtype
== TYPE_L
&& bytename
[0] != '\0')
300 comret
= command("TYPE %s %s", p
->t_mode
, bytename
);
302 comret
= command("TYPE %s", p
->t_mode
);
303 if (comret
== COMPLETE
)
305 verbose
= oldverbose
;
309 * Set binary transfer type.
313 setbinary(int argc
, char *argv
[])
317 UPRINTF("usage: %s\n", argv
[0]);
325 * Set ascii transfer type.
329 setascii(int argc
, char *argv
[])
333 UPRINTF("usage: %s\n", argv
[0]);
341 * Set tenex transfer type.
345 settenex(int argc
, char *argv
[])
349 UPRINTF("usage: %s\n", argv
[0]);
357 * Set file transfer mode.
361 setftmode(int argc
, char *argv
[])
365 UPRINTF("usage: %s mode-name\n", argv
[0]);
369 fprintf(ttyout
, "We only support %s mode, sorry.\n", modename
);
374 * Set file transfer format.
378 setform(int argc
, char *argv
[])
382 UPRINTF("usage: %s format\n", argv
[0]);
386 fprintf(ttyout
, "We only support %s format, sorry.\n", formname
);
391 * Set file transfer structure.
395 setstruct(int argc
, char *argv
[])
399 UPRINTF("usage: %s struct-mode\n", argv
[0]);
403 fprintf(ttyout
, "We only support %s structure, sorry.\n", structname
);
408 * Send a single file.
411 put(int argc
, char *argv
[])
413 char buf
[MAXPATHLEN
];
424 if (argc
== 0 || (argc
== 1 && !another(&argc
, &argv
, "local-file")))
426 if ((argc
< 3 && !another(&argc
, &argv
, "remote-file")) || argc
> 3) {
428 UPRINTF("usage: %s local-file [remote-file]\n", argv
[0]);
432 if ((locfile
= globulize(argv
[1])) == NULL
) {
437 if (loc
) /* If argv[2] is a copy of the old argv[1], update it */
439 cmd
= (argv
[0][0] == 'a') ? "APPE" : ((sunique
) ? "STOU" : "STOR");
440 remfile
= doprocess(buf
, sizeof(buf
), remfile
,
441 0, loc
&& ntflag
, loc
&& mapflag
);
442 sendrequest(cmd
, locfile
, remfile
,
443 locfile
!= argv
[1] || remfile
!= argv
[2]);
448 doprocess(char *dst
, size_t dlen
, const char *src
,
449 int casef
, int transf
, int mapf
)
452 src
= docase(dst
, dlen
, src
);
454 src
= dotrans(dst
, dlen
, src
);
456 src
= domap(dst
, dlen
, src
);
461 * Send multiple files.
464 mput(int argc
, char *argv
[])
471 if (argc
== 0 || (argc
== 1 && !another(&argc
, &argv
, "local-files"))) {
472 UPRINTF("usage: %s local-files\n", argv
[0]);
477 oldintr
= xsignal(SIGINT
, mintr
);
478 if (sigsetjmp(jabort
, 1))
483 while ((cp
= remglob(argv
, 0, NULL
)) != NULL
) {
484 if (*cp
== '\0' || !connected
) {
488 if (mflag
&& confirm(argv
[0], cp
)) {
489 char buf
[MAXPATHLEN
];
490 tp
= doprocess(buf
, sizeof(buf
), cp
,
491 mcase
, ntflag
, mapflag
);
492 sendrequest((sunique
) ? "STOU" : "STOR",
493 cp
, tp
, cp
!= tp
|| !interactive
);
494 if (!mflag
&& fromatty
) {
495 ointer
= interactive
;
497 if (confirm(argv
[0], NULL
)) {
500 interactive
= ointer
;
506 for (i
= 1; i
< argc
&& connected
; i
++) {
512 if (mflag
&& confirm(argv
[0], argv
[i
])) {
513 char buf
[MAXPATHLEN
];
514 tp
= doprocess(buf
, sizeof(buf
), argv
[i
],
516 sendrequest((sunique
) ? "STOU" : "STOR",
517 argv
[i
], tp
, tp
!= argv
[i
] || !interactive
);
518 if (!mflag
&& fromatty
) {
519 ointer
= interactive
;
521 if (confirm(argv
[0], NULL
)) {
524 interactive
= ointer
;
530 memset(&gl
, 0, sizeof(gl
));
531 flags
= GLOB_BRACE
|GLOB_NOCHECK
|GLOB_TILDE
;
532 if (glob(argv
[i
], flags
, NULL
, &gl
) || gl
.gl_pathc
== 0) {
533 warnx("Glob pattern `%s' not found", argv
[i
]);
537 for (cpp
= gl
.gl_pathv
; cpp
&& *cpp
!= NULL
&& connected
;
539 if (mflag
&& confirm(argv
[0], *cpp
)) {
540 char buf
[MAXPATHLEN
];
542 tp
= doprocess(buf
, sizeof(buf
), *cpp
,
544 sendrequest((sunique
) ? "STOU" : "STOR",
545 *cpp
, tp
, *cpp
!= tp
|| !interactive
);
546 if (!mflag
&& fromatty
) {
547 ointer
= interactive
;
549 if (confirm(argv
[0], NULL
)) {
552 interactive
= ointer
;
559 (void)xsignal(SIGINT
, oldintr
);
564 reget(int argc
, char *argv
[])
567 (void)getit(argc
, argv
, 1, restart_point
? "r+" : "a");
571 get(int argc
, char *argv
[])
574 (void)getit(argc
, argv
, 0, restart_point
? "r+" : "w");
579 * If restartit is 1, restart the xfer always.
580 * If restartit is -1, restart the xfer only if the remote file is newer.
583 getit(int argc
, char *argv
[], int restartit
, const char *gmode
)
586 char *remfile
, *olocfile
;
588 char buf
[MAXPATHLEN
];
596 if (argc
== 0 || (argc
== 1 && !another(&argc
, &argv
, "remote-file")))
598 if ((argc
< 3 && !another(&argc
, &argv
, "local-file")) || argc
> 3) {
600 UPRINTF("usage: %s remote-file [local-file]\n", argv
[0]);
605 if ((olocfile
= globulize(argv
[2])) == NULL
) {
609 locfile
= doprocess(buf
, sizeof(buf
), olocfile
,
610 loc
&& mcase
, loc
&& ntflag
, loc
&& mapflag
);
615 if (! features
[FEAT_REST_STREAM
]) {
617 "Restart is not supported by the remote server.\n");
620 ret
= stat(locfile
, &stbuf
);
621 if (restartit
== 1) {
623 if (errno
!= ENOENT
) {
624 warn("Can't stat `%s'", locfile
);
630 restart_point
= stbuf
.st_size
;
635 mtime
= remotemodtime(argv
[1], 0);
638 if (stbuf
.st_mtime
>= mtime
) {
646 recvrequest("RETR", locfile
, remfile
, gmode
,
647 remfile
!= argv
[1] || locfile
!= argv
[2], loc
);
650 (void)free(olocfile
);
661 write(fileno(ttyout
), "\n", 1);
662 siglongjmp(jabort
, 1);
666 mabort(const char *cmd
)
670 if (mflag
&& fromatty
) {
671 ointer
= interactive
;
675 if (confirm(cmd
, NULL
)) {
676 interactive
= ointer
;
680 interactive
= ointer
;
687 * Get multiple files.
690 mget(int argc
, char *argv
[])
696 int volatile restartit
;
699 (argc
== 1 && !another(&argc
, &argv
, "remote-files"))) {
700 UPRINTF("usage: %s remote-files\n", argv
[0]);
707 if (strcmp(argv
[0], "mreget") == 0) {
708 if (! features
[FEAT_REST_STREAM
]) {
710 "Restart is not supported by the remote server.\n");
715 oldintr
= xsignal(SIGINT
, mintr
);
716 if (sigsetjmp(jabort
, 1))
718 while ((cp
= remglob(argv
, proxy
, NULL
)) != NULL
) {
719 char buf
[MAXPATHLEN
];
720 if (*cp
== '\0' || !connected
) {
726 if (! fileindir(cp
, localcwd
)) {
727 fprintf(ttyout
, "Skipping non-relative filename `%s'\n",
731 if (!confirm(argv
[0], cp
))
733 tp
= doprocess(buf
, sizeof(buf
), cp
, mcase
, ntflag
, mapflag
);
737 if (stat(tp
, &stbuf
) == 0)
738 restart_point
= stbuf
.st_size
;
740 warn("Can't stat `%s'", tp
);
742 recvrequest("RETR", tp
, cp
, restart_point
? "r+" : "w",
743 tp
!= cp
|| !interactive
, 1);
745 if (!mflag
&& fromatty
) {
746 ointer
= interactive
;
748 if (confirm(argv
[0], NULL
))
750 interactive
= ointer
;
753 (void)xsignal(SIGINT
, oldintr
);
758 * Read list of filenames from a local file and get those
761 fget(int argc
, char *argv
[])
765 char buf
[MAXPATHLEN
], cmdbuf
[MAX_C_NAME
];
768 UPRINTF("usage: %s localfile\n", argv
[0]);
773 fp
= fopen(argv
[1], "r");
775 fprintf(ttyout
, "Can't open source file %s\n", argv
[1]);
780 (void)strlcpy(cmdbuf
, "get", sizeof(cmdbuf
));
782 gmode
= restart_point
? "r+" : "w";
784 while (get_line(fp
, buf
, sizeof(buf
), NULL
) >= 0) {
788 (void)getit(argc
, argv
, 0, gmode
);
797 return (val
? "on" : "off");
805 status(int argc
, char *argv
[])
809 UPRINTF("usage: %s\n", argv
[0]);
815 fprintf(ttyout
, "Connected %sto %s.\n",
816 connected
== -1 ? "and logged in" : "", hostname
);
818 fputs("Not connected.\n", ttyout
);
822 fprintf(ttyout
, "Connected for proxy commands to %s.\n",
826 fputs("No proxy connection.\n", ttyout
);
830 fprintf(ttyout
, "Gate ftp: %s, server %s, port %s.\n", onoff(gatemode
),
831 *gateserver
? gateserver
: "(none)", gateport
);
832 fprintf(ttyout
, "Passive mode: %s; fallback to active mode: %s.\n",
833 onoff(passivemode
), onoff(activefallback
));
834 fprintf(ttyout
, "Mode: %s; Type: %s; Form: %s; Structure: %s.\n",
835 modename
, typename
, formname
, structname
);
836 fprintf(ttyout
, "Verbose: %s; Bell: %s; Prompting: %s; Globbing: %s.\n",
837 onoff(verbose
), onoff(bell
), onoff(interactive
), onoff(doglob
));
838 fprintf(ttyout
, "Store unique: %s; Receive unique: %s.\n",
839 onoff(sunique
), onoff(runique
));
840 fprintf(ttyout
, "Preserve modification times: %s.\n", onoff(preserve
));
841 fprintf(ttyout
, "Case: %s; CR stripping: %s.\n", onoff(mcase
),
844 fprintf(ttyout
, "Ntrans: (in) %s (out) %s\n", ntin
, ntout
);
847 fputs("Ntrans: off.\n", ttyout
);
850 fprintf(ttyout
, "Nmap: (in) %s (out) %s\n", mapin
, mapout
);
853 fputs("Nmap: off.\n", ttyout
);
856 "Hash mark printing: %s; Mark count: %d; Progress bar: %s.\n",
857 onoff(hash
), mark
, onoff(progress
));
859 "Get transfer rate throttle: %s; maximum: %d; increment %d.\n",
860 onoff(rate_get
), rate_get
, rate_get_incr
);
862 "Put transfer rate throttle: %s; maximum: %d; increment %d.\n",
863 onoff(rate_put
), rate_put
, rate_put_incr
);
865 "Socket buffer sizes: send %d, receive %d.\n",
866 sndbuf_size
, rcvbuf_size
);
867 fprintf(ttyout
, "Use of PORT cmds: %s.\n", onoff(sendport
));
868 fprintf(ttyout
, "Use of EPSV/EPRT cmds for IPv4: %s%s.\n", onoff(epsv4
),
869 epsv4bad
? " (disabled for this connection)" : "");
870 fprintf(ttyout
, "Use of EPSV/EPRT cmds for IPv6: %s%s.\n", onoff(epsv6
),
871 epsv6bad
? " (disabled for this connection)" : "");
872 fprintf(ttyout
, "Command line editing: %s.\n",
873 #ifdef NO_EDITCOMPLETE
874 "support not compiled in"
875 #else /* !def NO_EDITCOMPLETE */
877 #endif /* !def NO_EDITCOMPLETE */
882 fputs("Macros:\n", ttyout
);
883 for (i
=0; i
<macnum
; i
++) {
884 fprintf(ttyout
, "\t%s\n", macros
[i
].mac_name
);
887 #endif /* !def NO_STATUS */
888 fprintf(ttyout
, "Version: %s %s\n", FTP_PRODUCT
, FTP_VERSION
);
896 togglevar(int argc
, char *argv
[], int *var
, const char *mesg
)
900 } else if (argc
== 2 && strcasecmp(argv
[1], "on") == 0) {
902 } else if (argc
== 2 && strcasecmp(argv
[1], "off") == 0) {
905 UPRINTF("usage: %s [ on | off ]\n", argv
[0]);
909 fprintf(ttyout
, "%s %s.\n", mesg
, onoff(*var
));
914 * Set beep on cmd completed mode.
918 setbell(int argc
, char *argv
[])
921 code
= togglevar(argc
, argv
, &bell
, "Bell mode");
925 * Set command line editing
929 setedit(int argc
, char *argv
[])
932 #ifdef NO_EDITCOMPLETE
934 UPRINTF("usage: %s\n", argv
[0]);
939 fputs("Editing support not compiled in; ignoring command.\n",
941 #else /* !def NO_EDITCOMPLETE */
942 code
= togglevar(argc
, argv
, &editing
, "Editing mode");
944 #endif /* !def NO_EDITCOMPLETE */
948 * Turn on packet tracing.
952 settrace(int argc
, char *argv
[])
955 code
= togglevar(argc
, argv
, &trace
, "Packet tracing");
959 * Toggle hash mark printing during transfers, or set hash mark bytecount.
963 sethash(int argc
, char *argv
[])
967 else if (argc
!= 2) {
968 UPRINTF("usage: %s [ on | off | bytecount ]\n",
972 } else if (strcasecmp(argv
[1], "on") == 0)
974 else if (strcasecmp(argv
[1], "off") == 0)
979 nmark
= strsuftoi(argv
[1]);
981 fprintf(ttyout
, "mark: bad bytecount value `%s'.\n",
989 fprintf(ttyout
, "Hash mark printing %s", onoff(hash
));
991 fprintf(ttyout
, " (%d bytes/hash mark)", mark
);
992 fputs(".\n", ttyout
);
999 * Turn on printing of server echo's.
1003 setverbose(int argc
, char *argv
[])
1006 code
= togglevar(argc
, argv
, &verbose
, "Verbose mode");
1010 * Toggle PORT/LPRT cmd use before each data connection.
1014 setport(int argc
, char *argv
[])
1017 code
= togglevar(argc
, argv
, &sendport
, "Use of PORT/LPRT cmds");
1021 * Toggle transfer progress bar.
1025 setprogress(int argc
, char *argv
[])
1028 code
= togglevar(argc
, argv
, &progress
, "Progress bar");
1034 * Turn on interactive prompting during mget, mput, and mdelete.
1038 setprompt(int argc
, char *argv
[])
1041 code
= togglevar(argc
, argv
, &interactive
, "Interactive mode");
1045 * Toggle gate-ftp mode, or set gate-ftp server
1049 setgate(int argc
, char *argv
[])
1051 static char gsbuf
[MAXHOSTNAMELEN
];
1053 if (argc
== 0 || argc
> 3) {
1055 "usage: %s [ on | off | gateserver [port] ]\n", argv
[0]);
1058 } else if (argc
< 2) {
1059 gatemode
= !gatemode
;
1061 if (argc
== 2 && strcasecmp(argv
[1], "on") == 0)
1063 else if (argc
== 2 && strcasecmp(argv
[1], "off") == 0)
1067 gateport
= ftp_strdup(argv
[2]);
1068 (void)strlcpy(gsbuf
, argv
[1], sizeof(gsbuf
));
1073 if (gatemode
&& (gateserver
== NULL
|| *gateserver
== '\0')) {
1075 "Disabling gate-ftp mode - no gate-ftp server defined.\n");
1078 fprintf(ttyout
, "Gate ftp: %s, server %s, port %s.\n",
1079 onoff(gatemode
), *gateserver
? gateserver
: "(none)",
1086 * Toggle metacharacter interpretation on local file names.
1090 setglob(int argc
, char *argv
[])
1093 code
= togglevar(argc
, argv
, &doglob
, "Globbing");
1097 * Toggle preserving modification times on retrieved files.
1101 setpreserve(int argc
, char *argv
[])
1104 code
= togglevar(argc
, argv
, &preserve
, "Preserve modification times");
1108 * Set debugging mode on/off and/or set level of debugging.
1112 setdebug(int argc
, char *argv
[])
1114 if (argc
== 0 || argc
> 2) {
1115 UPRINTF("usage: %s [ on | off | debuglevel ]\n", argv
[0]);
1118 } else if (argc
== 2) {
1119 if (strcasecmp(argv
[1], "on") == 0)
1121 else if (strcasecmp(argv
[1], "off") == 0)
1126 val
= strsuftoi(argv
[1]);
1128 fprintf(ttyout
, "%s: bad debugging value.\n",
1136 ftp_debug
= !ftp_debug
;
1138 options
|= SO_DEBUG
;
1140 options
&= ~SO_DEBUG
;
1141 fprintf(ttyout
, "Debugging %s (ftp_debug=%d).\n", onoff(ftp_debug
), ftp_debug
);
1142 code
= ftp_debug
> 0;
1146 * Set current working directory on remote machine.
1149 cd(int argc
, char *argv
[])
1153 if (argc
== 0 || argc
> 2 ||
1154 (argc
== 1 && !another(&argc
, &argv
, "remote-directory"))) {
1155 UPRINTF("usage: %s remote-directory\n", argv
[0]);
1159 r
= command("CWD %s", argv
[1]);
1160 if (r
== ERROR
&& code
== 500) {
1162 fputs("CWD command not recognized, trying XCWD.\n",
1164 r
= command("XCWD %s", argv
[1]);
1166 if (r
== COMPLETE
) {
1173 * Set current working directory on local machine.
1176 lcd(int argc
, char *argv
[])
1183 argv
[1] = localhome
;
1186 UPRINTF("usage: %s [local-directory]\n", argv
[0]);
1189 if ((locdir
= globulize(argv
[1])) == NULL
)
1191 if (chdir(locdir
) == -1)
1192 warn("Can't chdir `%s'", locdir
);
1196 fprintf(ttyout
, "Local directory now: %s\n", localcwd
);
1199 fprintf(ttyout
, "Unable to determine local directory\n");
1206 * Delete a single file.
1209 delete(int argc
, char *argv
[])
1212 if (argc
== 0 || argc
> 2 ||
1213 (argc
== 1 && !another(&argc
, &argv
, "remote-file"))) {
1214 UPRINTF("usage: %s remote-file\n", argv
[0]);
1218 if (command("DELE %s", argv
[1]) == COMPLETE
)
1223 * Delete multiple files.
1226 mdelete(int argc
, char *argv
[])
1233 (argc
== 1 && !another(&argc
, &argv
, "remote-files"))) {
1234 UPRINTF("usage: %s [remote-files]\n", argv
[0]);
1239 oldintr
= xsignal(SIGINT
, mintr
);
1240 if (sigsetjmp(jabort
, 1))
1242 while ((cp
= remglob(argv
, 0, NULL
)) != NULL
) {
1247 if (mflag
&& confirm(argv
[0], cp
)) {
1248 if (command("DELE %s", cp
) == COMPLETE
)
1250 if (!mflag
&& fromatty
) {
1251 ointer
= interactive
;
1253 if (confirm(argv
[0], NULL
)) {
1256 interactive
= ointer
;
1260 (void)xsignal(SIGINT
, oldintr
);
1265 * Rename a remote file.
1268 renamefile(int argc
, char *argv
[])
1271 if (argc
== 0 || (argc
== 1 && !another(&argc
, &argv
, "from-name")))
1273 if ((argc
< 3 && !another(&argc
, &argv
, "to-name")) || argc
> 3) {
1275 UPRINTF("usage: %s from-name to-name\n", argv
[0]);
1279 if (command("RNFR %s", argv
[1]) == CONTINUE
&&
1280 command("RNTO %s", argv
[2]) == COMPLETE
)
1285 * Get a directory listing of remote files.
1286 * Supports being invoked as:
1292 * pdir, pls LIST |$PAGER
1293 * pmlsd MLSD |$PAGER
1296 ls(int argc
, char *argv
[])
1299 char *remdir
, *locbuf
;
1300 const char *locfile
;
1301 int pagecmd
, mlsdcmd
;
1306 pagecmd
= mlsdcmd
= 0;
1308 * the only commands that start with `p' are
1309 * the `pager' versions.
1311 if (argv
[0][0] == 'p')
1313 if (strcmp(argv
[0] + pagecmd
, "mlsd") == 0) {
1314 if (! features
[FEAT_MLST
]) {
1316 "MLSD is not supported by the remote server.\n");
1326 else if (strcmp(argv
[0] + pagecmd
, "nlist") == 0)
1335 if (argc
> 3 || ((pagecmd
| mlsdcmd
) && argc
> 2)) {
1337 if (pagecmd
|| mlsdcmd
)
1338 UPRINTF("usage: %s [remote-path]\n", argv
[0]);
1340 UPRINTF("usage: %s [remote-path [local-file]]\n",
1350 p
= getoptionvalue("pager");
1353 len
= strlen(p
) + 2;
1354 locbuf
= ftp_malloc(len
);
1356 (void)strlcpy(locbuf
+ 1, p
, len
- 1);
1358 } else if ((strcmp(locfile
, "-") != 0) && *locfile
!= '|') {
1359 if ((locbuf
= globulize(locfile
)) == NULL
||
1360 !confirm("output to local-file:", locbuf
)) {
1366 recvrequest(cmd
, locfile
, remdir
, "w", 0, 0);
1373 * Get a directory listing of multiple remote files.
1376 mls(int argc
, char *argv
[])
1380 int volatile dolist
;
1381 char * volatile dest
, *odest
;
1386 if (argc
< 2 && !another(&argc
, &argv
, "remote-files"))
1388 if (argc
< 3 && !another(&argc
, &argv
, "local-file")) {
1390 UPRINTF("usage: %s remote-files local-file\n", argv
[0]);
1394 odest
= dest
= argv
[argc
- 1];
1395 argv
[argc
- 1] = NULL
;
1396 if (strcmp(dest
, "-") && *dest
!= '|')
1397 if (((dest
= globulize(dest
)) == NULL
) ||
1398 !confirm("output to local-file:", dest
)) {
1402 dolist
= strcmp(argv
[0], "mls");
1404 oldintr
= xsignal(SIGINT
, mintr
);
1405 if (sigsetjmp(jabort
, 1))
1407 for (i
= 1; mflag
&& i
< argc
-1 && connected
; i
++) {
1408 lmode
= (i
== 1) ? "w" : "a";
1409 recvrequest(dolist
? "LIST" : "NLST", dest
, argv
[i
], lmode
,
1411 if (!mflag
&& fromatty
) {
1412 ointer
= interactive
;
1414 if (confirm(argv
[0], NULL
)) {
1417 interactive
= ointer
;
1420 (void)xsignal(SIGINT
, oldintr
);
1422 if (dest
!= odest
) /* free up after globulize() */
1431 shell(int argc
, char *argv
[])
1435 char shellnam
[MAXPATHLEN
];
1436 const char *shellp
, *namep
;
1440 UPRINTF("usage: %s [command [args]]\n", argv
[0]);
1444 oldintr
= xsignal(SIGINT
, SIG_IGN
);
1445 if ((pid
= fork()) == 0) {
1446 for (pid
= 3; pid
< 20; pid
++)
1448 (void)xsignal(SIGINT
, SIG_DFL
);
1449 shellp
= getenv("SHELL");
1451 shellp
= _PATH_BSHELL
;
1452 namep
= strrchr(shellp
, '/');
1457 (void)strlcpy(shellnam
, namep
, sizeof(shellnam
));
1459 fputs(shellp
, ttyout
);
1463 execl(shellp
, shellnam
, "-c", altarg
, (char *)0);
1466 execl(shellp
, shellnam
, (char *)0);
1468 warn("Can't execute `%s'", shellp
);
1473 while (wait(&wait_status
) != pid
)
1475 (void)xsignal(SIGINT
, oldintr
);
1477 warn("Can't fork a subshell; try again later");
1484 * Send new user information (re-login)
1487 user(int argc
, char *argv
[])
1490 char emptypass
[] = "";
1496 (void)another(&argc
, &argv
, "username");
1497 if (argc
< 2 || argc
> 4) {
1499 UPRINTF("usage: %s username [password [account]]\n",
1504 n
= command("USER %s", argv
[1]);
1505 if (n
== CONTINUE
) {
1507 password
= getpass("Password: ");
1508 if (password
== NULL
)
1509 password
= emptypass
;
1513 n
= command("PASS %s", password
);
1514 memset(password
, 0, strlen(password
));
1516 if (n
== CONTINUE
) {
1519 password
= getpass("Account: ");
1520 if (password
== NULL
)
1521 password
= emptypass
;
1525 n
= command("ACCT %s", password
);
1526 memset(password
, 0, strlen(password
));
1528 if (n
!= COMPLETE
) {
1529 fputs("Login failed.\n", ttyout
);
1532 if (!aflag
&& argc
== 4) {
1534 (void)command("ACCT %s", password
);
1535 memset(password
, 0, strlen(password
));
1542 * Print working directory on remote machine.
1546 pwd(int argc
, char *argv
[])
1551 UPRINTF("usage: %s\n", argv
[0]);
1557 fprintf(ttyout
, "Unable to determine remote directory\n");
1559 fprintf(ttyout
, "Remote directory: %s\n", remotecwd
);
1565 * Print working directory on local machine.
1568 lpwd(int argc
, char *argv
[])
1573 UPRINTF("usage: %s\n", argv
[0]);
1579 fprintf(ttyout
, "Unable to determine local directory\n");
1581 fprintf(ttyout
, "Local directory: %s\n", localcwd
);
1590 makedir(int argc
, char *argv
[])
1594 if (argc
== 0 || argc
> 2 ||
1595 (argc
== 1 && !another(&argc
, &argv
, "directory-name"))) {
1596 UPRINTF("usage: %s directory-name\n", argv
[0]);
1600 r
= command("MKD %s", argv
[1]);
1601 if (r
== ERROR
&& code
== 500) {
1603 fputs("MKD command not recognized, trying XMKD.\n",
1605 r
= command("XMKD %s", argv
[1]);
1612 * Remove a directory.
1615 removedir(int argc
, char *argv
[])
1619 if (argc
== 0 || argc
> 2 ||
1620 (argc
== 1 && !another(&argc
, &argv
, "directory-name"))) {
1621 UPRINTF("usage: %s directory-name\n", argv
[0]);
1625 r
= command("RMD %s", argv
[1]);
1626 if (r
== ERROR
&& code
== 500) {
1628 fputs("RMD command not recognized, trying XRMD.\n",
1630 r
= command("XRMD %s", argv
[1]);
1637 * Send a line, verbatim, to the remote machine.
1640 quote(int argc
, char *argv
[])
1644 (argc
== 1 && !another(&argc
, &argv
, "command line to send"))) {
1645 UPRINTF("usage: %s line-to-send\n", argv
[0]);
1649 quote1("", argc
, argv
);
1653 * Send a SITE command to the remote machine. The line
1654 * is sent verbatim to the remote machine, except that the
1655 * word "SITE" is added at the front.
1658 site(int argc
, char *argv
[])
1662 (argc
== 1 && !another(&argc
, &argv
, "arguments to SITE command"))){
1663 UPRINTF("usage: %s line-to-send\n", argv
[0]);
1667 quote1("SITE ", argc
, argv
);
1671 * Turn argv[1..argc) into a space-separated string, then prepend initial text.
1672 * Send the result as a one-line command and get response.
1675 quote1(const char *initial
, int argc
, char *argv
[])
1678 char buf
[BUFSIZ
]; /* must be >= sizeof(line) */
1680 (void)strlcpy(buf
, initial
, sizeof(buf
));
1681 for (i
= 1; i
< argc
; i
++) {
1682 (void)strlcat(buf
, argv
[i
], sizeof(buf
));
1684 (void)strlcat(buf
, " ", sizeof(buf
));
1686 if (command("%s", buf
) == PRELIM
) {
1687 while (getreply(0) == PRELIM
)
1694 do_chmod(int argc
, char *argv
[])
1697 if (argc
== 0 || (argc
== 1 && !another(&argc
, &argv
, "mode")))
1699 if ((argc
< 3 && !another(&argc
, &argv
, "remote-file")) || argc
> 3) {
1701 UPRINTF("usage: %s mode remote-file\n", argv
[0]);
1705 (void)command("SITE CHMOD %s %s", argv
[1], argv
[2]);
1708 #define COMMAND_1ARG(argc, argv, cmd) \
1712 command(cmd " %s", argv[1])
1715 do_umask(int argc
, char *argv
[])
1717 int oldverbose
= verbose
;
1720 UPRINTF("usage: %s [umask]\n", argv
[0]);
1725 COMMAND_1ARG(argc
, argv
, "SITE UMASK");
1726 verbose
= oldverbose
;
1730 idlecmd(int argc
, char *argv
[])
1732 int oldverbose
= verbose
;
1734 if (argc
< 1 || argc
> 2) {
1735 UPRINTF("usage: %s [seconds]\n", argv
[0]);
1740 COMMAND_1ARG(argc
, argv
, "SITE IDLE");
1741 verbose
= oldverbose
;
1745 * Ask the other side for help.
1748 rmthelp(int argc
, char *argv
[])
1750 int oldverbose
= verbose
;
1753 UPRINTF("usage: %s\n", argv
[0]);
1758 COMMAND_1ARG(argc
, argv
, "HELP");
1759 verbose
= oldverbose
;
1763 * Terminate session and exit.
1764 * May be called with 0, NULL.
1768 quit(int argc
, char *argv
[])
1771 /* this may be called with argc == 0, argv == NULL */
1772 if (argc
== 0 && argv
!= NULL
) {
1773 UPRINTF("usage: %s\n", argv
[0]);
1778 disconnect(0, NULL
);
1781 disconnect(0, NULL
);
1786 * Terminate session, but don't exit.
1787 * May be called with 0, NULL.
1790 disconnect(int argc
, char *argv
[])
1793 /* this may be called with argc == 0, argv == NULL */
1794 if (argc
== 0 && argv
!= NULL
) {
1795 UPRINTF("usage: %s\n", argv
[0]);
1801 (void)command("QUIT");
1806 account(int argc
, char *argv
[])
1809 char emptypass
[] = "";
1811 if (argc
== 0 || argc
> 2) {
1812 UPRINTF("usage: %s [password]\n", argv
[0]);
1819 ap
= getpass("Account:");
1823 (void)command("ACCT %s", ap
);
1824 memset(ap
, 0, strlen(ap
));
1827 sigjmp_buf abortprox
;
1830 proxabort(int notused
)
1845 siglongjmp(abortprox
, 1);
1849 doproxy(int argc
, char *argv
[])
1854 char cmdbuf
[MAX_C_NAME
];
1856 if (argc
== 0 || (argc
== 1 && !another(&argc
, &argv
, "command"))) {
1857 UPRINTF("usage: %s command\n", argv
[0]);
1861 c
= getcmd(argv
[1]);
1862 if (c
== (struct cmd
*) -1) {
1863 fputs("?Ambiguous command.\n", ttyout
);
1868 fputs("?Invalid command.\n", ttyout
);
1873 fputs("?Invalid proxy command.\n", ttyout
);
1877 if (sigsetjmp(abortprox
, 1)) {
1881 oldintr
= xsignal(SIGINT
, proxabort
);
1883 if (c
->c_conn
&& !connected
) {
1884 fputs("Not connected.\n", ttyout
);
1886 (void)xsignal(SIGINT
, oldintr
);
1890 cmdpos
= strcspn(line
, " \t");
1891 if (cmdpos
> 0) /* remove leading "proxy " from input buffer */
1892 memmove(line
, line
+ cmdpos
+ 1, strlen(line
) - cmdpos
+ 1);
1893 (void)strlcpy(cmdbuf
, c
->c_name
, sizeof(cmdbuf
));
1895 (*c
->c_handler
)(argc
-1, argv
+1);
1903 (void)xsignal(SIGINT
, oldintr
);
1907 setcase(int argc
, char *argv
[])
1910 code
= togglevar(argc
, argv
, &mcase
, "Case mapping");
1914 * convert the given name to lower case if it's all upper case, into
1915 * a static buffer which is returned to the caller
1918 docase(char *dst
, size_t dlen
, const char *src
)
1923 for (i
= 0; src
[i
] != '\0' && i
< dlen
- 1; i
++) {
1925 if (islower((unsigned char)dst
[i
]))
1931 for (i
= 0; dst
[i
] != '\0'; i
++)
1932 if (isupper((unsigned char)dst
[i
]))
1933 dst
[i
] = tolower((unsigned char)dst
[i
]);
1939 setcr(int argc
, char *argv
[])
1942 code
= togglevar(argc
, argv
, &crflag
, "Carriage Return stripping");
1946 setntrans(int argc
, char *argv
[])
1949 if (argc
== 0 || argc
> 3) {
1950 UPRINTF("usage: %s [inchars [outchars]]\n", argv
[0]);
1956 fputs("Ntrans off.\n", ttyout
);
1962 (void)strlcpy(ntin
, argv
[1], sizeof(ntin
));
1967 (void)strlcpy(ntout
, argv
[2], sizeof(ntout
));
1971 dotrans(char *dst
, size_t dlen
, const char *src
)
1977 for (ostop
= 0; *(ntout
+ ostop
) && ostop
< 16; ostop
++)
1979 for (cp1
= src
; *cp1
; cp1
++) {
1981 for (i
= 0; *(ntin
+ i
) && i
< 16; i
++) {
1982 if (*cp1
== *(ntin
+ i
)) {
1985 *cp2
++ = *(ntout
+ i
);
1986 if (cp2
- dst
>= (ptrdiff_t)(dlen
- 1))
2002 setnmap(int argc
, char *argv
[])
2008 fputs("Nmap off.\n", ttyout
);
2013 (argc
< 3 && !another(&argc
, &argv
, "mapout")) || argc
> 3) {
2014 UPRINTF("usage: %s [mapin mapout]\n", argv
[0]);
2020 cp
= strchr(altarg
, ' ');
2025 cp
= strchr(altarg
, ' ');
2028 (void)strlcpy(mapin
, altarg
, MAXPATHLEN
);
2029 while (*++cp
== ' ')
2031 (void)strlcpy(mapout
, cp
, MAXPATHLEN
);
2035 domap(char *dst
, size_t dlen
, const char *src
)
2037 const char *cp1
= src
;
2039 const char *tp
[9], *te
[9];
2040 int i
, toks
[9], toknum
= 0, match
= 1;
2042 for (i
=0; i
< 9; ++i
) {
2045 while (match
&& *cp1
&& *cp2
) {
2048 if (*++cp2
!= *cp1
) {
2053 if (*(cp2
+1) >= '1' && (*cp2
+1) <= '9') {
2054 if (*cp1
!= *(++cp2
+1)) {
2055 toks
[toknum
= *cp2
- '1']++;
2057 while (*++cp1
&& *(cp2
+1)
2071 if (match
&& *cp1
) {
2074 if (match
&& *cp2
) {
2078 if (!match
&& *cp1
) /* last token mismatch */
2095 if (*++cp1
== '$' &&
2096 isdigit((unsigned char)*(cp1
+1))) {
2097 if (*++cp1
== '0') {
2098 const char *cp3
= src
;
2105 else if (toks
[toknum
= *cp1
- '1']) {
2106 const char *cp3
= tp
[toknum
];
2108 while (cp3
!= te
[toknum
]) {
2115 while (*cp1
&& *cp1
!= ',' &&
2120 else if (*cp1
== '$' &&
2121 isdigit((unsigned char)*(cp1
+1))) {
2122 if (*++cp1
== '0') {
2123 const char *cp3
= src
;
2129 else if (toks
[toknum
=
2131 const char *cp3
=tp
[toknum
];
2145 "nmap: unbalanced brackets.\n",
2153 while (*++cp1
&& *cp1
!= ']') {
2154 if (*cp1
== '\\' && *(cp1
+ 1)) {
2160 "nmap: unbalanced brackets.\n",
2177 if (isdigit((unsigned char)*(cp1
+ 1))) {
2178 if (*++cp1
== '0') {
2179 const char *cp3
= src
;
2185 else if (toks
[toknum
= *cp1
- '1']) {
2186 const char *cp3
= tp
[toknum
];
2188 while (cp3
!= te
[toknum
]) {
2194 /* intentional drop through */
2202 return *dst
? dst
: src
;
2206 setpassive(int argc
, char *argv
[])
2210 passivemode
= !passivemode
;
2211 activefallback
= passivemode
;
2212 } else if (argc
!= 2) {
2214 UPRINTF("usage: %s [ on | off | auto ]\n", argv
[0]);
2217 } else if (strcasecmp(argv
[1], "on") == 0) {
2220 } else if (strcasecmp(argv
[1], "off") == 0) {
2223 } else if (strcasecmp(argv
[1], "auto") == 0) {
2228 fprintf(ttyout
, "Passive mode: %s; fallback to active mode: %s.\n",
2229 onoff(passivemode
), onoff(activefallback
));
2235 setepsv4(int argc
, char *argv
[])
2237 code
= togglevar(argc
, argv
, &epsv4
,
2238 verbose
? "EPSV/EPRT on IPv4" : NULL
);
2243 setepsv6(int argc
, char *argv
[])
2245 code
= togglevar(argc
, argv
, &epsv6
,
2246 verbose
? "EPSV/EPRT on IPv6" : NULL
);
2251 setepsv(int argc
, char*argv
[])
2253 setepsv4(argc
,argv
);
2254 setepsv6(argc
,argv
);
2258 setsunique(int argc
, char *argv
[])
2261 code
= togglevar(argc
, argv
, &sunique
, "Store unique");
2265 setrunique(int argc
, char *argv
[])
2268 code
= togglevar(argc
, argv
, &runique
, "Receive unique");
2272 parserate(int argc
, char *argv
[], int cmdlineopt
)
2274 int dir
, max
, incr
, showonly
;
2275 sigfunc oldusr1
, oldusr2
;
2277 if (argc
> 4 || (argc
< (cmdlineopt
? 3 : 2))) {
2281 "usage: %s (all|get|put),maximum-bytes[,increment-bytes]]\n",
2285 "usage: %s (all|get|put) [maximum-bytes [increment-bytes]]\n",
2289 dir
= max
= incr
= showonly
= 0;
2292 #define RATE_ALL (RATE_GET | RATE_PUT)
2294 if (strcasecmp(argv
[1], "all") == 0)
2296 else if (strcasecmp(argv
[1], "get") == 0)
2298 else if (strcasecmp(argv
[1], "put") == 0)
2304 if ((max
= strsuftoi(argv
[2])) < 0)
2310 if ((incr
= strsuftoi(argv
[3])) <= 0)
2315 oldusr1
= xsignal(SIGUSR1
, SIG_IGN
);
2316 oldusr2
= xsignal(SIGUSR2
, SIG_IGN
);
2317 if (dir
& RATE_GET
) {
2320 rate_get_incr
= incr
;
2322 if (!cmdlineopt
|| verbose
)
2324 "Get transfer rate throttle: %s; maximum: %d; increment %d.\n",
2325 onoff(rate_get
), rate_get
, rate_get_incr
);
2327 if (dir
& RATE_PUT
) {
2330 rate_put_incr
= incr
;
2332 if (!cmdlineopt
|| verbose
)
2334 "Put transfer rate throttle: %s; maximum: %d; increment %d.\n",
2335 onoff(rate_put
), rate_put
, rate_put_incr
);
2337 (void)xsignal(SIGUSR1
, oldusr1
);
2338 (void)xsignal(SIGUSR2
, oldusr2
);
2343 setrate(int argc
, char *argv
[])
2346 code
= parserate(argc
, argv
, 0);
2349 /* change directory to parent directory */
2351 cdup(int argc
, char *argv
[])
2356 UPRINTF("usage: %s\n", argv
[0]);
2360 r
= command("CDUP");
2361 if (r
== ERROR
&& code
== 500) {
2363 fputs("CDUP command not recognized, trying XCUP.\n",
2365 r
= command("XCUP");
2367 if (r
== COMPLETE
) {
2374 * Restart transfer at specific point
2377 restart(int argc
, char *argv
[])
2380 if (argc
== 0 || argc
> 2) {
2381 UPRINTF("usage: %s [restart-point]\n", argv
[0]);
2385 if (! features
[FEAT_REST_STREAM
]) {
2387 "Restart is not supported by the remote server.\n");
2394 rp
= STRTOLL(argv
[1], &ep
, 10);
2395 if (rp
< 0 || *ep
!= '\0')
2396 fprintf(ttyout
, "restart: Invalid offset `%s'\n",
2401 if (restart_point
== 0)
2402 fputs("No restart point defined.\n", ttyout
);
2405 "Restarting at " LLF
" for next get, put or append\n",
2406 (LLT
)restart_point
);
2410 * Show remote system type
2413 syst(int argc
, char *argv
[])
2415 int oldverbose
= verbose
;
2418 UPRINTF("usage: %s\n", argv
[0]);
2422 verbose
= 1; /* If we aren't verbose, this doesn't do anything! */
2423 (void)command("SYST");
2424 verbose
= oldverbose
;
2428 macdef(int argc
, char *argv
[])
2436 fputs("Limit of 16 macros have already been defined.\n",
2441 if ((argc
< 2 && !another(&argc
, &argv
, "macro name")) || argc
> 2) {
2443 UPRINTF("usage: %s macro_name\n", argv
[0]);
2449 "Enter macro line by line, terminating it with a null line.\n",
2451 (void)strlcpy(macros
[macnum
].mac_name
, argv
[1],
2452 sizeof(macros
[macnum
].mac_name
));
2454 macros
[macnum
].mac_start
= macbuf
;
2456 macros
[macnum
].mac_start
= macros
[macnum
- 1].mac_end
+ 1;
2457 tmp
= macros
[macnum
].mac_start
;
2458 while (tmp
!= macbuf
+4096) {
2459 if ((c
= getchar()) == EOF
) {
2460 fputs("macdef: end of file encountered.\n", ttyout
);
2464 if ((*tmp
= c
) == '\n') {
2465 if (tmp
== macros
[macnum
].mac_start
) {
2466 macros
[macnum
++].mac_end
= tmp
;
2470 if (*(tmp
-1) == '\0') {
2471 macros
[macnum
++].mac_end
= tmp
- 1;
2480 while ((c
= getchar()) != '\n' && c
!= EOF
)
2482 if (c
== EOF
|| getchar() == '\n') {
2483 fputs("Macro not defined - 4K buffer exceeded.\n",
2492 * Get size of file on remote machine
2495 sizecmd(int argc
, char *argv
[])
2499 if (argc
== 0 || argc
> 2 ||
2500 (argc
== 1 && !another(&argc
, &argv
, "remote-file"))) {
2501 UPRINTF("usage: %s remote-file\n", argv
[0]);
2505 size
= remotesize(argv
[1], 1);
2508 "%s\t" LLF
"\n", argv
[1], (LLT
)size
);
2513 * Get last modification time of file on remote machine
2516 modtime(int argc
, char *argv
[])
2520 if (argc
== 0 || argc
> 2 ||
2521 (argc
== 1 && !another(&argc
, &argv
, "remote-file"))) {
2522 UPRINTF("usage: %s remote-file\n", argv
[0]);
2526 mtime
= remotemodtime(argv
[1], 1);
2528 fprintf(ttyout
, "%s\t%s", argv
[1],
2529 rfc2822time(localtime(&mtime
)));
2534 * Show status on remote machine
2537 rmtstatus(int argc
, char *argv
[])
2541 UPRINTF("usage: %s [remote-file]\n", argv
[0]);
2545 COMMAND_1ARG(argc
, argv
, "STAT");
2549 * Get file if modtime is more recent than current file
2552 newer(int argc
, char *argv
[])
2555 if (getit(argc
, argv
, -1, "w"))
2557 "Local file \"%s\" is newer than remote file \"%s\".\n",
2562 * Display one local file through $PAGER.
2565 lpage(int argc
, char *argv
[])
2569 char *pager
, *locfile
;
2571 if (argc
== 0 || argc
> 2 ||
2572 (argc
== 1 && !another(&argc
, &argv
, "local-file"))) {
2573 UPRINTF("usage: %s local-file\n", argv
[0]);
2577 if ((locfile
= globulize(argv
[1])) == NULL
) {
2581 p
= getoptionvalue("pager");
2584 len
= strlen(p
) + strlen(locfile
) + 2;
2585 pager
= ftp_malloc(len
);
2586 (void)strlcpy(pager
, p
, len
);
2587 (void)strlcat(pager
, " ", len
);
2588 (void)strlcat(pager
, locfile
, len
);
2592 (void)free(locfile
);
2596 * Display one remote file through $PAGER.
2599 page(int argc
, char *argv
[])
2601 int ohash
, orestart_point
, overbose
;
2606 if (argc
== 0 || argc
> 2 ||
2607 (argc
== 1 && !another(&argc
, &argv
, "remote-file"))) {
2608 UPRINTF("usage: %s remote-file\n", argv
[0]);
2612 p
= getoptionvalue("pager");
2615 len
= strlen(p
) + 2;
2616 pager
= ftp_malloc(len
);
2618 (void)strlcpy(pager
+ 1, p
, len
- 1);
2621 orestart_point
= restart_point
;
2623 hash
= restart_point
= verbose
= 0;
2624 recvrequest("RETR", pager
, argv
[1], "r+", 1, 0);
2626 restart_point
= orestart_point
;
2632 * Set the socket send or receive buffer size.
2635 setxferbuf(int argc
, char *argv
[])
2641 UPRINTF("usage: %s size\n", argv
[0]);
2645 if (strcasecmp(argv
[0], "sndbuf") == 0)
2647 else if (strcasecmp(argv
[0], "rcvbuf") == 0)
2649 else if (strcasecmp(argv
[0], "xferbuf") == 0)
2654 if ((size
= strsuftoi(argv
[1])) == -1)
2658 fprintf(ttyout
, "%s: size must be positive.\n", argv
[0]);
2666 fprintf(ttyout
, "Socket buffer sizes: send %d, receive %d.\n",
2667 sndbuf_size
, rcvbuf_size
);
2672 * Set or display options (defaults are provided by various env vars)
2675 setoption(int argc
, char *argv
[])
2680 if (argc
== 0 || (argc
!= 1 && argc
!= 3)) {
2681 UPRINTF("usage: %s [option value]\n", argv
[0]);
2685 #define OPTIONINDENT ((int) sizeof("https_proxy"))
2687 for (o
= optiontab
; o
->name
!= NULL
; o
++) {
2688 fprintf(ttyout
, "%-*s\t%s\n", OPTIONINDENT
,
2689 o
->name
, o
->value
? o
->value
: "");
2692 set_option(argv
[1], argv
[2], 1);
2698 set_option(const char * option
, const char * value
, int doverbose
)
2702 o
= getoption(option
);
2704 fprintf(ttyout
, "No such option `%s'.\n", option
);
2708 o
->value
= ftp_strdup(value
);
2709 if (verbose
&& doverbose
)
2710 fprintf(ttyout
, "Setting `%s' to `%s'.\n",
2718 unsetoption(int argc
, char *argv
[])
2723 if (argc
== 0 || argc
!= 2) {
2724 UPRINTF("usage: %s option\n", argv
[0]);
2728 o
= getoption(argv
[1]);
2730 fprintf(ttyout
, "No such option `%s'.\n", argv
[1]);
2734 fprintf(ttyout
, "Unsetting `%s'.\n", o
->name
);
2739 * Display features supported by the remote host.
2742 feat(int argc
, char *argv
[])
2744 int oldverbose
= verbose
;
2747 UPRINTF("usage: %s\n", argv
[0]);
2751 if (! features
[FEAT_FEAT
]) {
2753 "FEAT is not supported by the remote server.\n");
2756 verbose
= 1; /* If we aren't verbose, this doesn't do anything! */
2757 (void)command("FEAT");
2758 verbose
= oldverbose
;
2762 mlst(int argc
, char *argv
[])
2764 int oldverbose
= verbose
;
2766 if (argc
< 1 || argc
> 2) {
2767 UPRINTF("usage: %s [remote-path]\n", argv
[0]);
2771 if (! features
[FEAT_MLST
]) {
2773 "MLST is not supported by the remote server.\n");
2776 verbose
= 1; /* If we aren't verbose, this doesn't do anything! */
2777 COMMAND_1ARG(argc
, argv
, "MLST");
2778 verbose
= oldverbose
;
2782 opts(int argc
, char *argv
[])
2784 int oldverbose
= verbose
;
2786 if (argc
< 2 || argc
> 3) {
2787 UPRINTF("usage: %s command [options]\n", argv
[0]);
2791 if (! features
[FEAT_FEAT
]) {
2793 "OPTS is not supported by the remote server.\n");
2796 verbose
= 1; /* If we aren't verbose, this doesn't do anything! */
2798 command("OPTS %s", argv
[1]);
2800 command("OPTS %s %s", argv
[1], argv
[2]);
2801 verbose
= oldverbose
;