1 /* $NetBSD: cmds.c,v 1.118 2006/01/31 20:05:35 christos Exp $ */
4 * Copyright (c) 1996-2005 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.
22 * 3. All advertising materials mentioning features or use of this software
23 * must display the following acknowledgement:
24 * This product includes software developed by the NetBSD
25 * Foundation, Inc. and its contributors.
26 * 4. Neither the name of The NetBSD Foundation nor the names of its
27 * contributors may be used to endorse or promote products derived
28 * from this software without specific prior written permission.
30 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
31 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
32 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
33 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
34 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
35 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
36 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
37 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
38 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
39 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
40 * POSSIBILITY OF SUCH DAMAGE.
44 * Copyright (c) 1985, 1989, 1993, 1994
45 * The Regents of the University of California. All rights reserved.
47 * Redistribution and use in source and binary forms, with or without
48 * modification, are permitted provided that the following conditions
50 * 1. Redistributions of source code must retain the above copyright
51 * notice, this list of conditions and the following disclaimer.
52 * 2. Redistributions in binary form must reproduce the above copyright
53 * notice, this list of conditions and the following disclaimer in the
54 * documentation and/or other materials provided with the distribution.
55 * 3. Neither the name of the University nor the names of its contributors
56 * may be used to endorse or promote products derived from this software
57 * without specific prior written permission.
59 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
60 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
61 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
62 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
63 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
64 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
65 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
66 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
67 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
68 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
73 * Copyright (C) 1997 and 1998 WIDE Project.
74 * All rights reserved.
76 * Redistribution and use in source and binary forms, with or without
77 * modification, are permitted provided that the following conditions
79 * 1. Redistributions of source code must retain the above copyright
80 * notice, this list of conditions and the following disclaimer.
81 * 2. Redistributions in binary form must reproduce the above copyright
82 * notice, this list of conditions and the following disclaimer in the
83 * documentation and/or other materials provided with the distribution.
84 * 3. Neither the name of the project nor the names of its contributors
85 * may be used to endorse or promote products derived from this software
86 * without specific prior written permission.
88 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
89 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
90 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
91 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
92 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
93 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
94 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
95 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
96 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
97 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
101 #include <sys/cdefs.h>
104 static char sccsid
[] = "@(#)cmds.c 8.6 (Berkeley) 10/9/94";
106 __RCSID("$NetBSD: cmds.c,v 1.118 2006/01/31 20:05:35 christos Exp $");
108 #endif /* not lint */
111 * FTP User Program -- Command Routines.
113 #include <sys/types.h>
114 #include <sys/socket.h>
115 #include <sys/stat.h>
116 #include <sys/wait.h>
117 #include <arpa/ftp.h>
140 { "ascii", "A", TYPE_A
, 0 },
141 { "binary", "I", TYPE_I
, 0 },
142 { "image", "I", TYPE_I
, 0 },
143 { "ebcdic", "E", TYPE_E
, 0 },
144 { "tenex", "L", TYPE_L
, bytename
},
151 static int confirm(const char *, const char *);
153 static const char *doprocess(char *, size_t, const char *, int, int, int);
154 static const char *domap(char *, size_t, const char *);
155 static const char *docase(char *, size_t, const char *);
156 static const char *dotrans(char *, size_t, const char *);
159 confirm(const char *cmd
, const char *file
)
161 const char *errormsg
;
164 if (!interactive
|| confirmrest
)
167 fprintf(ttyout
, "%s %s [anpqy?]? ", cmd
, file
);
168 (void)fflush(ttyout
);
169 if (getline(stdin
, line
, sizeof(line
), &errormsg
) < 0) {
171 fprintf(ttyout
, "%s; %s aborted\n", errormsg
, mname
);
174 switch (tolower((unsigned char)*line
)) {
178 "Prompting off for duration of %s.\n", cmd
);
182 fputs("Interactive mode: off.\n", ttyout
);
186 fprintf(ttyout
, "%s aborted.\n", mname
);
192 " confirmation options:\n"
193 "\ta answer `yes' for the duration of %s\n"
194 "\tn answer `no' for this file\n"
195 "\tp turn off `prompt' mode\n"
196 "\tq stop the current %s\n"
197 "\ty answer `yes' for this file\n"
198 "\t? this help list\n",
200 continue; /* back to while(1) */
211 settype(int argc
, char *argv
[])
216 if (argc
== 0 || argc
> 2) {
219 UPRINTF("usage: %s [", argv
[0]);
221 for (p
= types
; p
->t_name
; p
++) {
222 fprintf(ttyout
, "%s%s", sep
, p
->t_name
);
225 fputs(" ]\n", ttyout
);
230 fprintf(ttyout
, "Using %s mode to transfer files.\n", typename
);
234 for (p
= types
; p
->t_name
; p
++)
235 if (strcmp(argv
[1], p
->t_name
) == 0)
237 if (p
->t_name
== 0) {
238 fprintf(ttyout
, "%s: unknown mode.\n", argv
[1]);
242 if ((p
->t_arg
!= NULL
) && (*(p
->t_arg
) != '\0'))
243 comret
= command("TYPE %s %s", p
->t_mode
, p
->t_arg
);
245 comret
= command("TYPE %s", p
->t_mode
);
246 if (comret
== COMPLETE
) {
247 (void)strlcpy(typename
, p
->t_name
, sizeof(typename
));
248 curtype
= type
= p
->t_type
;
253 * Internal form of settype; changes current type in use with server
254 * without changing our notion of the type for data transfers.
255 * Used to change to and from ascii for listings.
258 changetype(int newtype
, int show
)
261 int comret
, oldverbose
= verbose
;
265 if (newtype
== curtype
)
267 if (ftp_debug
== 0 && show
== 0)
269 for (p
= types
; p
->t_name
; p
++)
270 if (newtype
== p
->t_type
)
272 if (p
->t_name
== 0) {
273 warnx("internal error: unknown type %d.", newtype
);
276 if (newtype
== TYPE_L
&& bytename
[0] != '\0')
277 comret
= command("TYPE %s %s", p
->t_mode
, bytename
);
279 comret
= command("TYPE %s", p
->t_mode
);
280 if (comret
== COMPLETE
)
282 verbose
= oldverbose
;
292 * Set binary transfer type.
296 setbinary(int argc
, char *argv
[])
300 UPRINTF("usage: %s\n", argv
[0]);
309 * Set ascii transfer type.
313 setascii(int argc
, char *argv
[])
317 UPRINTF("usage: %s\n", argv
[0]);
326 * Set tenex transfer type.
330 settenex(int argc
, char *argv
[])
334 UPRINTF("usage: %s\n", argv
[0]);
343 * Set file transfer mode.
347 setftmode(int argc
, char *argv
[])
351 UPRINTF("usage: %s mode-name\n", argv
[0]);
355 fprintf(ttyout
, "We only support %s mode, sorry.\n", modename
);
360 * Set file transfer format.
364 setform(int argc
, char *argv
[])
368 UPRINTF("usage: %s format\n", argv
[0]);
372 fprintf(ttyout
, "We only support %s format, sorry.\n", formname
);
377 * Set file transfer structure.
381 setstruct(int argc
, char *argv
[])
385 UPRINTF("usage: %s struct-mode\n", argv
[0]);
389 fprintf(ttyout
, "We only support %s structure, sorry.\n", structname
);
394 * Send a single file.
397 put(int argc
, char *argv
[])
399 char buf
[MAXPATHLEN
];
410 if (argc
== 0 || (argc
== 1 && !another(&argc
, &argv
, "local-file")))
412 if ((argc
< 3 && !another(&argc
, &argv
, "remote-file")) || argc
> 3) {
414 UPRINTF("usage: %s local-file [remote-file]\n", argv
[0]);
418 if ((locfile
= globulize(argv
[1])) == NULL
) {
423 if (loc
) /* If argv[2] is a copy of the old argv[1], update it */
425 cmd
= (argv
[0][0] == 'a') ? "APPE" : ((sunique
) ? "STOU" : "STOR");
426 remfile
= doprocess(buf
, sizeof(buf
), remfile
,
427 0, loc
&& ntflag
, loc
&& mapflag
);
428 sendrequest(cmd
, locfile
, remfile
,
429 locfile
!= argv
[1] || remfile
!= argv
[2]);
434 doprocess(char *dst
, size_t dlen
, const char *src
,
435 int casef
, int transf
, int mapf
)
438 src
= docase(dst
, dlen
, src
);
440 src
= dotrans(dst
, dlen
, src
);
442 src
= domap(dst
, dlen
, src
);
447 * Send multiple files.
450 mput(int argc
, char *argv
[])
457 if (argc
== 0 || (argc
== 1 && !another(&argc
, &argv
, "local-files"))) {
458 UPRINTF("usage: %s local-files\n", argv
[0]);
464 oldintr
= xsignal(SIGINT
, mintr
);
465 if (sigsetjmp(jabort
, 1))
470 while ((cp
= remglob(argv
, 0, NULL
)) != NULL
) {
471 if (*cp
== '\0' || !connected
) {
475 if (mflag
&& confirm(argv
[0], cp
)) {
476 char buf
[MAXPATHLEN
];
477 tp
= doprocess(buf
, sizeof(buf
), cp
,
478 mcase
, ntflag
, mapflag
);
479 sendrequest((sunique
) ? "STOU" : "STOR",
480 cp
, tp
, cp
!= tp
|| !interactive
);
481 if (!mflag
&& fromatty
) {
482 ointer
= interactive
;
484 if (confirm("Continue with", "mput")) {
487 interactive
= ointer
;
493 for (i
= 1; i
< argc
&& connected
; i
++) {
499 if (mflag
&& confirm(argv
[0], argv
[i
])) {
500 char buf
[MAXPATHLEN
];
501 tp
= doprocess(buf
, sizeof(buf
), argv
[i
],
503 sendrequest((sunique
) ? "STOU" : "STOR",
504 argv
[i
], tp
, tp
!= argv
[i
] || !interactive
);
505 if (!mflag
&& fromatty
) {
506 ointer
= interactive
;
508 if (confirm("Continue with", "mput")) {
511 interactive
= ointer
;
517 memset(&gl
, 0, sizeof(gl
));
518 flags
= GLOB_BRACE
|GLOB_NOCHECK
|GLOB_TILDE
;
519 if (glob(argv
[i
], flags
, NULL
, &gl
) || gl
.gl_pathc
== 0) {
520 warnx("%s: not found", argv
[i
]);
524 for (cpp
= gl
.gl_pathv
; cpp
&& *cpp
!= NULL
&& connected
;
526 if (mflag
&& confirm(argv
[0], *cpp
)) {
527 char buf
[MAXPATHLEN
];
529 tp
= doprocess(buf
, sizeof(buf
), *cpp
,
531 sendrequest((sunique
) ? "STOU" : "STOR",
532 *cpp
, tp
, *cpp
!= tp
|| !interactive
);
533 if (!mflag
&& fromatty
) {
534 ointer
= interactive
;
536 if (confirm("Continue with", "mput")) {
539 interactive
= ointer
;
546 (void)xsignal(SIGINT
, oldintr
);
551 reget(int argc
, char *argv
[])
554 (void)getit(argc
, argv
, 1, "r+");
558 get(int argc
, char *argv
[])
561 (void)getit(argc
, argv
, 0, restart_point
? "r+" : "w" );
566 * If restartit is 1, restart the xfer always.
567 * If restartit is -1, restart the xfer only if the remote file is newer.
570 getit(int argc
, char *argv
[], int restartit
, const char *mode
)
573 char *remfile
, *olocfile
;
575 char buf
[MAXPATHLEN
];
583 if (argc
== 0 || (argc
== 1 && !another(&argc
, &argv
, "remote-file")))
585 if ((argc
< 3 && !another(&argc
, &argv
, "local-file")) || argc
> 3) {
587 UPRINTF("usage: %s remote-file [local-file]\n", argv
[0]);
592 if ((olocfile
= globulize(argv
[2])) == NULL
) {
596 locfile
= doprocess(buf
, sizeof(buf
), olocfile
,
597 loc
&& mcase
, loc
&& ntflag
, loc
&& mapflag
);
602 if (! features
[FEAT_REST_STREAM
]) {
604 "Restart is not supported by the remote server.\n");
607 ret
= stat(locfile
, &stbuf
);
608 if (restartit
== 1) {
610 warn("local: %s", locfile
);
613 restart_point
= stbuf
.st_size
;
618 mtime
= remotemodtime(argv
[1], 0);
621 if (stbuf
.st_mtime
>= mtime
) {
629 recvrequest("RETR", locfile
, remfile
, mode
,
630 remfile
!= argv
[1] || locfile
!= argv
[2], loc
);
633 (void)free(olocfile
);
644 write(fileno(ttyout
), "\n", 1);
645 siglongjmp(jabort
, 1);
653 if (mflag
&& fromatty
) {
654 ointer
= interactive
;
658 if (confirm("Continue with", mname
)) {
659 interactive
= ointer
;
663 interactive
= ointer
;
670 * Get multiple files.
673 mget(int argc
, char *argv
[])
682 (argc
== 1 && !another(&argc
, &argv
, "remote-files"))) {
683 UPRINTF("usage: %s remote-files\n", argv
[0]);
691 if (strcmp(argv
[0], "mreget") == 0) {
692 if (! features
[FEAT_REST_STREAM
]) {
694 "Restart is not supported by the remote server.\n");
699 oldintr
= xsignal(SIGINT
, mintr
);
700 if (sigsetjmp(jabort
, 1))
702 while ((cp
= remglob(argv
, proxy
, NULL
)) != NULL
) {
703 char buf
[MAXPATHLEN
];
704 if (*cp
== '\0' || !connected
) {
710 if (! fileindir(cp
, localcwd
)) {
711 fprintf(ttyout
, "Skipping non-relative filename `%s'\n",
715 if (!confirm(argv
[0], cp
))
717 tp
= doprocess(buf
, sizeof(buf
), cp
, mcase
, ntflag
, mapflag
);
721 if (stat(tp
, &stbuf
) == 0)
722 restart_point
= stbuf
.st_size
;
726 recvrequest("RETR", tp
, cp
, restart_point
? "r+" : "w",
727 tp
!= cp
|| !interactive
, 1);
729 if (!mflag
&& fromatty
) {
730 ointer
= interactive
;
732 if (confirm("Continue with", "mget"))
734 interactive
= ointer
;
737 (void)xsignal(SIGINT
, oldintr
);
742 * Read list of filenames from a local file and get those
745 fget(int argc
, char *argv
[])
751 UPRINTF("usage: %s localfile\n", argv
[0]);
756 fp
= fopen(argv
[1], "r");
758 fprintf(ttyout
, "Cannot open source file %s\n", argv
[1]);
764 mode
= restart_point
? "r+" : "w";
767 (buf
= fparseln(fp
, NULL
, NULL
, "\0\0\0", 0)) != NULL
;
772 (void)getit(argc
, argv
, 0, mode
);
781 return (bool ? "on" : "off");
789 status(int argc
, char *argv
[])
793 UPRINTF("usage: %s\n", argv
[0]);
799 fprintf(ttyout
, "Connected %sto %s.\n",
800 connected
== -1 ? "and logged in" : "", hostname
);
802 fputs("Not connected.\n", ttyout
);
806 fprintf(ttyout
, "Connected for proxy commands to %s.\n",
810 fputs("No proxy connection.\n", ttyout
);
814 fprintf(ttyout
, "Gate ftp: %s, server %s, port %s.\n", onoff(gatemode
),
815 *gateserver
? gateserver
: "(none)", gateport
);
816 fprintf(ttyout
, "Passive mode: %s; fallback to active mode: %s.\n",
817 onoff(passivemode
), onoff(activefallback
));
818 fprintf(ttyout
, "Mode: %s; Type: %s; Form: %s; Structure: %s.\n",
819 modename
, typename
, formname
, structname
);
820 fprintf(ttyout
, "Verbose: %s; Bell: %s; Prompting: %s; Globbing: %s.\n",
821 onoff(verbose
), onoff(bell
), onoff(interactive
), onoff(doglob
));
822 fprintf(ttyout
, "Store unique: %s; Receive unique: %s.\n",
823 onoff(sunique
), onoff(runique
));
824 fprintf(ttyout
, "Preserve modification times: %s.\n", onoff(preserve
));
825 fprintf(ttyout
, "Case: %s; CR stripping: %s.\n", onoff(mcase
),
828 fprintf(ttyout
, "Ntrans: (in) %s (out) %s\n", ntin
, ntout
);
831 fputs("Ntrans: off.\n", ttyout
);
834 fprintf(ttyout
, "Nmap: (in) %s (out) %s\n", mapin
, mapout
);
837 fputs("Nmap: off.\n", ttyout
);
840 "Hash mark printing: %s; Mark count: %d; Progress bar: %s.\n",
841 onoff(hash
), mark
, onoff(progress
));
843 "Get transfer rate throttle: %s; maximum: %d; increment %d.\n",
844 onoff(rate_get
), rate_get
, rate_get_incr
);
846 "Put transfer rate throttle: %s; maximum: %d; increment %d.\n",
847 onoff(rate_put
), rate_put
, rate_put_incr
);
849 "Socket buffer sizes: send %d, receive %d.\n",
850 sndbuf_size
, rcvbuf_size
);
851 fprintf(ttyout
, "Use of PORT cmds: %s.\n", onoff(sendport
));
852 fprintf(ttyout
, "Use of EPSV/EPRT cmds for IPv4: %s%s.\n", onoff(epsv4
),
853 epsv4bad
? " (disabled for this connection)" : "");
854 fprintf(ttyout
, "Command line editing: %s.\n",
855 #ifdef NO_EDITCOMPLETE
856 "support not compiled in"
857 #else /* !def NO_EDITCOMPLETE */
859 #endif /* !def NO_EDITCOMPLETE */
864 fputs("Macros:\n", ttyout
);
865 for (i
=0; i
<macnum
; i
++) {
866 fprintf(ttyout
, "\t%s\n", macros
[i
].mac_name
);
869 #endif /* !def NO_STATUS */
870 fprintf(ttyout
, "Version: %s %s\n", FTP_PRODUCT
, FTP_VERSION
);
878 togglevar(int argc
, char *argv
[], int *var
, const char *mesg
)
882 } else if (argc
== 2 && strcasecmp(argv
[1], "on") == 0) {
884 } else if (argc
== 2 && strcasecmp(argv
[1], "off") == 0) {
887 UPRINTF("usage: %s [ on | off ]\n", argv
[0]);
891 fprintf(ttyout
, "%s %s.\n", mesg
, onoff(*var
));
896 * Set beep on cmd completed mode.
900 setbell(int argc
, char *argv
[])
903 code
= togglevar(argc
, argv
, &bell
, "Bell mode");
907 * Set command line editing
911 setedit(int argc
, char *argv
[])
914 #ifdef NO_EDITCOMPLETE
916 UPRINTF("usage: %s\n", argv
[0]);
921 fputs("Editing support not compiled in; ignoring command.\n",
923 #else /* !def NO_EDITCOMPLETE */
924 code
= togglevar(argc
, argv
, &editing
, "Editing mode");
926 #endif /* !def NO_EDITCOMPLETE */
930 * Turn on packet tracing.
934 settrace(int argc
, char *argv
[])
937 code
= togglevar(argc
, argv
, &trace
, "Packet tracing");
941 * Toggle hash mark printing during transfers, or set hash mark bytecount.
945 sethash(int argc
, char *argv
[])
949 else if (argc
!= 2) {
950 UPRINTF("usage: %s [ on | off | bytecount ]\n",
954 } else if (strcasecmp(argv
[1], "on") == 0)
956 else if (strcasecmp(argv
[1], "off") == 0)
961 nmark
= strsuftoi(argv
[1]);
963 fprintf(ttyout
, "mark: bad bytecount value `%s'.\n",
971 fprintf(ttyout
, "Hash mark printing %s", onoff(hash
));
973 fprintf(ttyout
, " (%d bytes/hash mark)", mark
);
974 fputs(".\n", ttyout
);
981 * Turn on printing of server echo's.
985 setverbose(int argc
, char *argv
[])
988 code
= togglevar(argc
, argv
, &verbose
, "Verbose mode");
992 * Toggle PORT/LPRT cmd use before each data connection.
996 setport(int argc
, char *argv
[])
999 code
= togglevar(argc
, argv
, &sendport
, "Use of PORT/LPRT cmds");
1003 * Toggle transfer progress bar.
1007 setprogress(int argc
, char *argv
[])
1010 code
= togglevar(argc
, argv
, &progress
, "Progress bar");
1016 * Turn on interactive prompting during mget, mput, and mdelete.
1020 setprompt(int argc
, char *argv
[])
1023 code
= togglevar(argc
, argv
, &interactive
, "Interactive mode");
1027 * Toggle gate-ftp mode, or set gate-ftp server
1031 setgate(int argc
, char *argv
[])
1033 static char gsbuf
[MAXHOSTNAMELEN
];
1035 if (argc
== 0 || argc
> 3) {
1037 "usage: %s [ on | off | gateserver [port] ]\n", argv
[0]);
1040 } else if (argc
< 2) {
1041 gatemode
= !gatemode
;
1043 if (argc
== 2 && strcasecmp(argv
[1], "on") == 0)
1045 else if (argc
== 2 && strcasecmp(argv
[1], "off") == 0)
1049 gateport
= ftp_strdup(argv
[2]);
1050 (void)strlcpy(gsbuf
, argv
[1], sizeof(gsbuf
));
1055 if (gatemode
&& (gateserver
== NULL
|| *gateserver
== '\0')) {
1057 "Disabling gate-ftp mode - no gate-ftp server defined.\n");
1060 fprintf(ttyout
, "Gate ftp: %s, server %s, port %s.\n",
1061 onoff(gatemode
), *gateserver
? gateserver
: "(none)",
1068 * Toggle metacharacter interpretation on local file names.
1072 setglob(int argc
, char *argv
[])
1075 code
= togglevar(argc
, argv
, &doglob
, "Globbing");
1079 * Toggle preserving modification times on retrieved files.
1083 setpreserve(int argc
, char *argv
[])
1086 code
= togglevar(argc
, argv
, &preserve
, "Preserve modification times");
1090 * Set debugging mode on/off and/or set level of debugging.
1094 setdebug(int argc
, char *argv
[])
1096 if (argc
== 0 || argc
> 2) {
1097 UPRINTF("usage: %s [ on | off | debuglevel ]\n", argv
[0]);
1100 } else if (argc
== 2) {
1101 if (strcasecmp(argv
[1], "on") == 0)
1103 else if (strcasecmp(argv
[1], "off") == 0)
1108 val
= strsuftoi(argv
[1]);
1110 fprintf(ttyout
, "%s: bad debugging value.\n",
1118 ftp_debug
= !ftp_debug
;
1120 options
|= SO_DEBUG
;
1122 options
&= ~SO_DEBUG
;
1123 fprintf(ttyout
, "Debugging %s (ftp_debug=%d).\n", onoff(ftp_debug
), ftp_debug
);
1124 code
= ftp_debug
> 0;
1128 * Set current working directory on remote machine.
1131 cd(int argc
, char *argv
[])
1135 if (argc
== 0 || argc
> 2 ||
1136 (argc
== 1 && !another(&argc
, &argv
, "remote-directory"))) {
1137 UPRINTF("usage: %s remote-directory\n", argv
[0]);
1141 r
= command("CWD %s", argv
[1]);
1142 if (r
== ERROR
&& code
== 500) {
1144 fputs("CWD command not recognized, trying XCWD.\n",
1146 r
= command("XCWD %s", argv
[1]);
1148 if (r
== COMPLETE
) {
1155 * Set current working directory on local machine.
1158 lcd(int argc
, char *argv
[])
1165 argv
[1] = localhome
;
1168 UPRINTF("usage: %s [local-directory]\n", argv
[0]);
1171 if ((locdir
= globulize(argv
[1])) == NULL
)
1173 if (chdir(locdir
) == -1)
1174 warn("lcd %s", locdir
);
1178 fprintf(ttyout
, "Local directory now: %s\n", localcwd
);
1181 fprintf(ttyout
, "Unable to determine local directory\n");
1188 * Delete a single file.
1191 delete(int argc
, char *argv
[])
1194 if (argc
== 0 || argc
> 2 ||
1195 (argc
== 1 && !another(&argc
, &argv
, "remote-file"))) {
1196 UPRINTF("usage: %s remote-file\n", argv
[0]);
1200 if (command("DELE %s", argv
[1]) == COMPLETE
)
1205 * Delete multiple files.
1208 mdelete(int argc
, char *argv
[])
1215 (argc
== 1 && !another(&argc
, &argv
, "remote-files"))) {
1216 UPRINTF("usage: %s [remote-files]\n", argv
[0]);
1222 oldintr
= xsignal(SIGINT
, mintr
);
1223 if (sigsetjmp(jabort
, 1))
1225 while ((cp
= remglob(argv
, 0, NULL
)) != NULL
) {
1230 if (mflag
&& confirm(argv
[0], cp
)) {
1231 if (command("DELE %s", cp
) == COMPLETE
)
1233 if (!mflag
&& fromatty
) {
1234 ointer
= interactive
;
1236 if (confirm("Continue with", "mdelete")) {
1239 interactive
= ointer
;
1243 (void)xsignal(SIGINT
, oldintr
);
1248 * Rename a remote file.
1251 renamefile(int argc
, char *argv
[])
1254 if (argc
== 0 || (argc
== 1 && !another(&argc
, &argv
, "from-name")))
1256 if ((argc
< 3 && !another(&argc
, &argv
, "to-name")) || argc
> 3) {
1258 UPRINTF("usage: %s from-name to-name\n", argv
[0]);
1262 if (command("RNFR %s", argv
[1]) == CONTINUE
&&
1263 command("RNTO %s", argv
[2]) == COMPLETE
)
1268 * Get a directory listing of remote files.
1269 * Supports being invoked as:
1275 * pdir, pls LIST |$PAGER
1276 * mmlsd MLSD |$PAGER
1279 ls(int argc
, char *argv
[])
1282 char *remdir
, *locfile
;
1283 int freelocfile
, pagecmd
, mlsdcmd
;
1287 freelocfile
= pagecmd
= mlsdcmd
= 0;
1289 * the only commands that start with `p' are
1290 * the `pager' versions.
1292 if (argv
[0][0] == 'p')
1294 if (strcmp(argv
[0] + pagecmd
, "mlsd") == 0) {
1295 if (! features
[FEAT_MLST
]) {
1297 "MLSD is not supported by the remote server.\n");
1307 else if (strcmp(argv
[0] + pagecmd
, "nlist") == 0)
1316 if (argc
> 3 || ((pagecmd
| mlsdcmd
) && argc
> 2)) {
1318 if (pagecmd
|| mlsdcmd
)
1319 UPRINTF("usage: %s [remote-path]\n", argv
[0]);
1321 UPRINTF("usage: %s [remote-path [local-file]]\n",
1331 p
= getoptionvalue("pager");
1334 len
= strlen(p
) + 2;
1335 locfile
= ftp_malloc(len
);
1337 (void)strlcpy(locfile
+ 1, p
, len
- 1);
1339 } else if ((strcmp(locfile
, "-") != 0) && *locfile
!= '|') {
1341 if ((locfile
= globulize(locfile
)) == NULL
||
1342 !confirm("output to local-file:", locfile
)) {
1348 recvrequest(cmd
, locfile
, remdir
, "w", 0, 0);
1350 if (freelocfile
&& locfile
)
1351 (void)free(locfile
);
1355 * Get a directory listing of multiple remote files.
1358 mls(int argc
, char *argv
[])
1363 char *mode
, *dest
, *odest
;
1367 if (argc
< 2 && !another(&argc
, &argv
, "remote-files"))
1369 if (argc
< 3 && !another(&argc
, &argv
, "local-file")) {
1371 UPRINTF("usage: %s remote-files local-file\n", argv
[0]);
1375 odest
= dest
= argv
[argc
- 1];
1376 argv
[argc
- 1] = NULL
;
1378 if (strcmp(dest
, "-") && *dest
!= '|')
1379 if (((dest
= globulize(dest
)) == NULL
) ||
1380 !confirm("output to local-file:", dest
)) {
1384 dolist
= strcmp(argv
[0], "mls");
1386 oldintr
= xsignal(SIGINT
, mintr
);
1387 if (sigsetjmp(jabort
, 1))
1389 for (i
= 1; mflag
&& i
< argc
-1 && connected
; i
++) {
1390 mode
= (i
== 1) ? "w" : "a";
1391 recvrequest(dolist
? "LIST" : "NLST", dest
, argv
[i
], mode
,
1393 if (!mflag
&& fromatty
) {
1394 ointer
= interactive
;
1396 if (confirm("Continue with", argv
[0])) {
1399 interactive
= ointer
;
1402 (void)xsignal(SIGINT
, oldintr
);
1404 if (dest
!= odest
) /* free up after globulize() */
1413 shell(int argc
, char *argv
[])
1417 char shellnam
[MAXPATHLEN
], *shell
, *namep
;
1421 UPRINTF("usage: %s [command [args]]\n", argv
[0]);
1425 oldintr
= xsignal(SIGINT
, SIG_IGN
);
1426 if ((pid
= fork()) == 0) {
1427 for (pid
= 3; pid
< 20; pid
++)
1429 (void)xsignal(SIGINT
, SIG_DFL
);
1430 shell
= getenv("SHELL");
1432 shell
= _PATH_BSHELL
;
1433 namep
= strrchr(shell
, '/');
1438 (void)strlcpy(shellnam
, namep
, sizeof(shellnam
));
1440 fputs(shell
, ttyout
);
1444 execl(shell
, shellnam
, "-c", altarg
, (char *)0);
1447 execl(shell
, shellnam
, (char *)0);
1454 while (wait(&wait_status
) != pid
)
1456 (void)xsignal(SIGINT
, oldintr
);
1458 warn("Try again later");
1465 * Send new user information (re-login)
1468 user(int argc
, char *argv
[])
1476 (void)another(&argc
, &argv
, "username");
1477 if (argc
< 2 || argc
> 4) {
1479 UPRINTF("usage: %s username [password [account]]\n",
1484 n
= command("USER %s", argv
[1]);
1485 if (n
== CONTINUE
) {
1487 password
= getpass("Password: ");
1491 n
= command("PASS %s", password
);
1492 memset(password
, 0, strlen(password
));
1494 if (n
== CONTINUE
) {
1497 password
= getpass("Account: ");
1501 n
= command("ACCT %s", password
);
1502 memset(password
, 0, strlen(password
));
1504 if (n
!= COMPLETE
) {
1505 fputs("Login failed.\n", ttyout
);
1508 if (!aflag
&& argc
== 4) {
1510 (void)command("ACCT %s", password
);
1511 memset(password
, 0, strlen(password
));
1518 * Print working directory on remote machine.
1522 pwd(int argc
, char *argv
[])
1527 UPRINTF("usage: %s\n", argv
[0]);
1533 fprintf(ttyout
, "Unable to determine remote directory\n");
1535 fprintf(ttyout
, "Remote directory: %s\n", remotecwd
);
1541 * Print working directory on local machine.
1544 lpwd(int argc
, char *argv
[])
1549 UPRINTF("usage: %s\n", argv
[0]);
1555 fprintf(ttyout
, "Unable to determine local directory\n");
1557 fprintf(ttyout
, "Local directory: %s\n", localcwd
);
1566 makedir(int argc
, char *argv
[])
1570 if (argc
== 0 || argc
> 2 ||
1571 (argc
== 1 && !another(&argc
, &argv
, "directory-name"))) {
1572 UPRINTF("usage: %s directory-name\n", argv
[0]);
1576 r
= command("MKD %s", argv
[1]);
1577 if (r
== ERROR
&& code
== 500) {
1579 fputs("MKD command not recognized, trying XMKD.\n",
1581 r
= command("XMKD %s", argv
[1]);
1588 * Remove a directory.
1591 removedir(int argc
, char *argv
[])
1595 if (argc
== 0 || argc
> 2 ||
1596 (argc
== 1 && !another(&argc
, &argv
, "directory-name"))) {
1597 UPRINTF("usage: %s directory-name\n", argv
[0]);
1601 r
= command("RMD %s", argv
[1]);
1602 if (r
== ERROR
&& code
== 500) {
1604 fputs("RMD command not recognized, trying XRMD.\n",
1606 r
= command("XRMD %s", argv
[1]);
1613 * Send a line, verbatim, to the remote machine.
1616 quote(int argc
, char *argv
[])
1620 (argc
== 1 && !another(&argc
, &argv
, "command line to send"))) {
1621 UPRINTF("usage: %s line-to-send\n", argv
[0]);
1625 quote1("", argc
, argv
);
1629 * Send a SITE command to the remote machine. The line
1630 * is sent verbatim to the remote machine, except that the
1631 * word "SITE" is added at the front.
1634 site(int argc
, char *argv
[])
1638 (argc
== 1 && !another(&argc
, &argv
, "arguments to SITE command"))){
1639 UPRINTF("usage: %s line-to-send\n", argv
[0]);
1643 quote1("SITE ", argc
, argv
);
1647 * Turn argv[1..argc) into a space-separated string, then prepend initial text.
1648 * Send the result as a one-line command and get response.
1651 quote1(const char *initial
, int argc
, char *argv
[])
1654 char buf
[BUFSIZ
]; /* must be >= sizeof(line) */
1656 (void)strlcpy(buf
, initial
, sizeof(buf
));
1657 for (i
= 1; i
< argc
; i
++) {
1658 (void)strlcat(buf
, argv
[i
], sizeof(buf
));
1660 (void)strlcat(buf
, " ", sizeof(buf
));
1662 if (command("%s", buf
) == PRELIM
) {
1663 while (getreply(0) == PRELIM
)
1670 do_chmod(int argc
, char *argv
[])
1673 if (argc
== 0 || (argc
== 1 && !another(&argc
, &argv
, "mode")))
1675 if ((argc
< 3 && !another(&argc
, &argv
, "remote-file")) || argc
> 3) {
1677 UPRINTF("usage: %s mode remote-file\n", argv
[0]);
1681 (void)command("SITE CHMOD %s %s", argv
[1], argv
[2]);
1684 #define COMMAND_1ARG(argc, argv, cmd) \
1688 command(cmd " %s", argv[1])
1691 do_umask(int argc
, char *argv
[])
1693 int oldverbose
= verbose
;
1696 UPRINTF("usage: %s [umask]\n", argv
[0]);
1701 COMMAND_1ARG(argc
, argv
, "SITE UMASK");
1702 verbose
= oldverbose
;
1706 idlecmd(int argc
, char *argv
[])
1708 int oldverbose
= verbose
;
1710 if (argc
< 1 || argc
> 2) {
1711 UPRINTF("usage: %s [seconds]\n", argv
[0]);
1716 COMMAND_1ARG(argc
, argv
, "SITE IDLE");
1717 verbose
= oldverbose
;
1721 * Ask the other side for help.
1724 rmthelp(int argc
, char *argv
[])
1726 int oldverbose
= verbose
;
1729 UPRINTF("usage: %s\n", argv
[0]);
1734 COMMAND_1ARG(argc
, argv
, "HELP");
1735 verbose
= oldverbose
;
1739 * Terminate session and exit.
1740 * May be called with 0, NULL.
1744 quit(int argc
, char *argv
[])
1747 /* this may be called with argc == 0, argv == NULL */
1748 if (argc
== 0 && argv
!= NULL
) {
1749 UPRINTF("usage: %s\n", argv
[0]);
1754 disconnect(0, NULL
);
1757 disconnect(0, NULL
);
1762 * Terminate session, but don't exit.
1763 * May be called with 0, NULL.
1766 disconnect(int argc
, char *argv
[])
1769 /* this may be called with argc == 0, argv == NULL */
1770 if (argc
== 0 && argv
!= NULL
) {
1771 UPRINTF("usage: %s\n", argv
[0]);
1777 (void)command("QUIT");
1782 account(int argc
, char *argv
[])
1786 if (argc
== 0 || argc
> 2) {
1787 UPRINTF("usage: %s [password]\n", argv
[0]);
1794 ap
= getpass("Account:");
1795 (void)command("ACCT %s", ap
);
1798 sigjmp_buf abortprox
;
1801 proxabort(int notused
)
1816 siglongjmp(abortprox
, 1);
1820 doproxy(int argc
, char *argv
[])
1826 if (argc
== 0 || (argc
== 1 && !another(&argc
, &argv
, "command"))) {
1827 UPRINTF("usage: %s command\n", argv
[0]);
1831 c
= getcmd(argv
[1]);
1832 if (c
== (struct cmd
*) -1) {
1833 fputs("?Ambiguous command.\n", ttyout
);
1838 fputs("?Invalid command.\n", ttyout
);
1843 fputs("?Invalid proxy command.\n", ttyout
);
1847 if (sigsetjmp(abortprox
, 1)) {
1851 oldintr
= xsignal(SIGINT
, proxabort
);
1853 if (c
->c_conn
&& !connected
) {
1854 fputs("Not connected.\n", ttyout
);
1856 (void)xsignal(SIGINT
, oldintr
);
1860 cmdpos
= strcspn(line
, " \t");
1861 if (cmdpos
> 0) /* remove leading "proxy " from input buffer */
1862 memmove(line
, line
+ cmdpos
+ 1, strlen(line
) - cmdpos
+ 1);
1863 argv
[1] = c
->c_name
;
1864 (*c
->c_handler
)(argc
-1, argv
+1);
1872 (void)xsignal(SIGINT
, oldintr
);
1876 setcase(int argc
, char *argv
[])
1879 code
= togglevar(argc
, argv
, &mcase
, "Case mapping");
1883 * convert the given name to lower case if it's all upper case, into
1884 * a static buffer which is returned to the caller
1887 docase(char *dst
, size_t dlen
, const char *src
)
1892 for (i
= 0; src
[i
] != '\0' && i
< dlen
- 1; i
++) {
1894 if (islower((unsigned char)dst
[i
]))
1900 for (i
= 0; dst
[i
] != '\0'; i
++)
1901 if (isupper((unsigned char)dst
[i
]))
1902 dst
[i
] = tolower((unsigned char)dst
[i
]);
1908 setcr(int argc
, char *argv
[])
1911 code
= togglevar(argc
, argv
, &crflag
, "Carriage Return stripping");
1915 setntrans(int argc
, char *argv
[])
1918 if (argc
== 0 || argc
> 3) {
1919 UPRINTF("usage: %s [inchars [outchars]]\n", argv
[0]);
1925 fputs("Ntrans off.\n", ttyout
);
1931 (void)strlcpy(ntin
, argv
[1], sizeof(ntin
));
1936 (void)strlcpy(ntout
, argv
[2], sizeof(ntout
));
1940 dotrans(char *dst
, size_t dlen
, const char *src
)
1946 for (ostop
= 0; *(ntout
+ ostop
) && ostop
< 16; ostop
++)
1948 for (cp1
= src
; *cp1
; cp1
++) {
1950 for (i
= 0; *(ntin
+ i
) && i
< 16; i
++) {
1951 if (*cp1
== *(ntin
+ i
)) {
1954 *cp2
++ = *(ntout
+ i
);
1955 if (cp2
- dst
>= dlen
- 1)
1971 setnmap(int argc
, char *argv
[])
1977 fputs("Nmap off.\n", ttyout
);
1982 (argc
< 3 && !another(&argc
, &argv
, "mapout")) || argc
> 3) {
1983 UPRINTF("usage: %s [mapin mapout]\n", argv
[0]);
1989 cp
= strchr(altarg
, ' ');
1994 cp
= strchr(altarg
, ' ');
1997 (void)strlcpy(mapin
, altarg
, MAXPATHLEN
);
1998 while (*++cp
== ' ')
2000 (void)strlcpy(mapout
, cp
, MAXPATHLEN
);
2004 domap(char *dst
, size_t dlen
, const char *src
)
2006 const char *cp1
= src
;
2008 const char *tp
[9], *te
[9];
2009 int i
, toks
[9], toknum
= 0, match
= 1;
2011 for (i
=0; i
< 9; ++i
) {
2014 while (match
&& *cp1
&& *cp2
) {
2017 if (*++cp2
!= *cp1
) {
2022 if (*(cp2
+1) >= '1' && (*cp2
+1) <= '9') {
2023 if (*cp1
!= *(++cp2
+1)) {
2024 toks
[toknum
= *cp2
- '1']++;
2026 while (*++cp1
&& *(cp2
+1)
2040 if (match
&& *cp1
) {
2043 if (match
&& *cp2
) {
2047 if (!match
&& *cp1
) /* last token mismatch */
2064 if (*++cp1
== '$' &&
2065 isdigit((unsigned char)*(cp1
+1))) {
2066 if (*++cp1
== '0') {
2067 const char *cp3
= src
;
2074 else if (toks
[toknum
= *cp1
- '1']) {
2075 const char *cp3
= tp
[toknum
];
2077 while (cp3
!= te
[toknum
]) {
2084 while (*cp1
&& *cp1
!= ',' &&
2089 else if (*cp1
== '$' &&
2090 isdigit((unsigned char)*(cp1
+1))) {
2091 if (*++cp1
== '0') {
2092 const char *cp3
= src
;
2098 else if (toks
[toknum
=
2100 const char *cp3
=tp
[toknum
];
2114 "nmap: unbalanced brackets.\n",
2122 while (*++cp1
&& *cp1
!= ']') {
2123 if (*cp1
== '\\' && *(cp1
+ 1)) {
2129 "nmap: unbalanced brackets.\n",
2146 if (isdigit((unsigned char)*(cp1
+ 1))) {
2147 if (*++cp1
== '0') {
2148 const char *cp3
= src
;
2154 else if (toks
[toknum
= *cp1
- '1']) {
2155 const char *cp3
= tp
[toknum
];
2157 while (cp3
!= te
[toknum
]) {
2163 /* intentional drop through */
2171 return *dst
? dst
: src
;
2175 setpassive(int argc
, char *argv
[])
2179 passivemode
= !passivemode
;
2180 activefallback
= passivemode
;
2181 } else if (argc
!= 2) {
2183 UPRINTF("usage: %s [ on | off | auto ]\n", argv
[0]);
2186 } else if (strcasecmp(argv
[1], "on") == 0) {
2189 } else if (strcasecmp(argv
[1], "off") == 0) {
2192 } else if (strcasecmp(argv
[1], "auto") == 0) {
2197 fprintf(ttyout
, "Passive mode: %s; fallback to active mode: %s.\n",
2198 onoff(passivemode
), onoff(activefallback
));
2203 setepsv4(int argc
, char *argv
[])
2206 code
= togglevar(argc
, argv
, &epsv4
,
2207 verbose
? "EPSV/EPRT on IPv4" : NULL
);
2212 setsunique(int argc
, char *argv
[])
2215 code
= togglevar(argc
, argv
, &sunique
, "Store unique");
2219 setrunique(int argc
, char *argv
[])
2222 code
= togglevar(argc
, argv
, &runique
, "Receive unique");
2226 parserate(int argc
, char *argv
[], int cmdlineopt
)
2228 int dir
, max
, incr
, showonly
;
2229 sigfunc oldusr1
, oldusr2
;
2231 if (argc
> 4 || (argc
< (cmdlineopt
? 3 : 2))) {
2235 "usage: %s (all|get|put),maximum-bytes[,increment-bytes]]\n",
2239 "usage: %s (all|get|put) [maximum-bytes [increment-bytes]]\n",
2243 dir
= max
= incr
= showonly
= 0;
2246 #define RATE_ALL (RATE_GET | RATE_PUT)
2248 if (strcasecmp(argv
[1], "all") == 0)
2250 else if (strcasecmp(argv
[1], "get") == 0)
2252 else if (strcasecmp(argv
[1], "put") == 0)
2258 if ((max
= strsuftoi(argv
[2])) < 0)
2264 if ((incr
= strsuftoi(argv
[3])) <= 0)
2269 oldusr1
= xsignal(SIGUSR1
, SIG_IGN
);
2270 oldusr2
= xsignal(SIGUSR2
, SIG_IGN
);
2271 if (dir
& RATE_GET
) {
2274 rate_get_incr
= incr
;
2276 if (!cmdlineopt
|| verbose
)
2278 "Get transfer rate throttle: %s; maximum: %d; increment %d.\n",
2279 onoff(rate_get
), rate_get
, rate_get_incr
);
2281 if (dir
& RATE_PUT
) {
2284 rate_put_incr
= incr
;
2286 if (!cmdlineopt
|| verbose
)
2288 "Put transfer rate throttle: %s; maximum: %d; increment %d.\n",
2289 onoff(rate_put
), rate_put
, rate_put_incr
);
2291 (void)xsignal(SIGUSR1
, oldusr1
);
2292 (void)xsignal(SIGUSR2
, oldusr2
);
2297 setrate(int argc
, char *argv
[])
2300 code
= parserate(argc
, argv
, 0);
2303 /* change directory to parent directory */
2305 cdup(int argc
, char *argv
[])
2310 UPRINTF("usage: %s\n", argv
[0]);
2314 r
= command("CDUP");
2315 if (r
== ERROR
&& code
== 500) {
2317 fputs("CDUP command not recognized, trying XCUP.\n",
2319 r
= command("XCUP");
2321 if (r
== COMPLETE
) {
2328 * Restart transfer at specific point
2331 restart(int argc
, char *argv
[])
2334 if (argc
== 0 || argc
> 2) {
2335 UPRINTF("usage: %s [restart-point]\n", argv
[0]);
2339 if (! features
[FEAT_REST_STREAM
]) {
2341 "Restart is not supported by the remote server.\n");
2348 rp
= STRTOLL(argv
[1], &ep
, 10);
2349 if (rp
< 0 || *ep
!= '\0')
2350 fprintf(ttyout
, "restart: Invalid offset `%s'\n",
2355 if (restart_point
== 0)
2356 fputs("No restart point defined.\n", ttyout
);
2359 "Restarting at " LLF
" for next get, put or append\n",
2360 (LLT
)restart_point
);
2364 * Show remote system type
2367 syst(int argc
, char *argv
[])
2369 int oldverbose
= verbose
;
2372 UPRINTF("usage: %s\n", argv
[0]);
2376 verbose
= 1; /* If we aren't verbose, this doesn't do anything! */
2377 (void)command("SYST");
2378 verbose
= oldverbose
;
2382 macdef(int argc
, char *argv
[])
2390 fputs("Limit of 16 macros have already been defined.\n",
2395 if ((argc
< 2 && !another(&argc
, &argv
, "macro name")) || argc
> 2) {
2397 UPRINTF("usage: %s macro_name\n", argv
[0]);
2403 "Enter macro line by line, terminating it with a null line.\n",
2405 (void)strlcpy(macros
[macnum
].mac_name
, argv
[1],
2406 sizeof(macros
[macnum
].mac_name
));
2408 macros
[macnum
].mac_start
= macbuf
;
2410 macros
[macnum
].mac_start
= macros
[macnum
- 1].mac_end
+ 1;
2411 tmp
= macros
[macnum
].mac_start
;
2412 while (tmp
!= macbuf
+4096) {
2413 if ((c
= getchar()) == EOF
) {
2414 fputs("macdef: end of file encountered.\n", ttyout
);
2418 if ((*tmp
= c
) == '\n') {
2419 if (tmp
== macros
[macnum
].mac_start
) {
2420 macros
[macnum
++].mac_end
= tmp
;
2424 if (*(tmp
-1) == '\0') {
2425 macros
[macnum
++].mac_end
= tmp
- 1;
2434 while ((c
= getchar()) != '\n' && c
!= EOF
)
2436 if (c
== EOF
|| getchar() == '\n') {
2437 fputs("Macro not defined - 4K buffer exceeded.\n",
2446 * Get size of file on remote machine
2449 sizecmd(int argc
, char *argv
[])
2453 if (argc
== 0 || argc
> 2 ||
2454 (argc
== 1 && !another(&argc
, &argv
, "remote-file"))) {
2455 UPRINTF("usage: %s remote-file\n", argv
[0]);
2459 size
= remotesize(argv
[1], 1);
2462 "%s\t" LLF
"\n", argv
[1], (LLT
)size
);
2467 * Get last modification time of file on remote machine
2470 modtime(int argc
, char *argv
[])
2474 if (argc
== 0 || argc
> 2 ||
2475 (argc
== 1 && !another(&argc
, &argv
, "remote-file"))) {
2476 UPRINTF("usage: %s remote-file\n", argv
[0]);
2480 mtime
= remotemodtime(argv
[1], 1);
2482 fprintf(ttyout
, "%s\t%s", argv
[1], asctime(localtime(&mtime
)));
2487 * Show status on remote machine
2490 rmtstatus(int argc
, char *argv
[])
2494 UPRINTF("usage: %s [remote-file]\n", argv
[0]);
2498 COMMAND_1ARG(argc
, argv
, "STAT");
2502 * Get file if modtime is more recent than current file
2505 newer(int argc
, char *argv
[])
2508 if (getit(argc
, argv
, -1, "w"))
2510 "Local file \"%s\" is newer than remote file \"%s\".\n",
2515 * Display one local file through $PAGER.
2518 lpage(int argc
, char *argv
[])
2521 char *p
, *pager
, *locfile
;
2523 if (argc
== 0 || argc
> 2 ||
2524 (argc
== 1 && !another(&argc
, &argv
, "local-file"))) {
2525 UPRINTF("usage: %s local-file\n", argv
[0]);
2529 if ((locfile
= globulize(argv
[1])) == NULL
) {
2533 p
= getoptionvalue("pager");
2536 len
= strlen(p
) + strlen(locfile
) + 2;
2537 pager
= ftp_malloc(len
);
2538 (void)strlcpy(pager
, p
, len
);
2539 (void)strlcat(pager
, " ", len
);
2540 (void)strlcat(pager
, locfile
, len
);
2544 (void)free(locfile
);
2548 * Display one remote file through $PAGER.
2551 page(int argc
, char *argv
[])
2553 int ohash
, orestart_point
, overbose
;
2557 if (argc
== 0 || argc
> 2 ||
2558 (argc
== 1 && !another(&argc
, &argv
, "remote-file"))) {
2559 UPRINTF("usage: %s remote-file\n", argv
[0]);
2563 p
= getoptionvalue("pager");
2566 len
= strlen(p
) + 2;
2567 pager
= ftp_malloc(len
);
2569 (void)strlcpy(pager
+ 1, p
, len
- 1);
2572 orestart_point
= restart_point
;
2574 hash
= restart_point
= verbose
= 0;
2575 recvrequest("RETR", pager
, argv
[1], "r+", 1, 0);
2577 restart_point
= orestart_point
;
2583 * Set the socket send or receive buffer size.
2586 setxferbuf(int argc
, char *argv
[])
2592 UPRINTF("usage: %s size\n", argv
[0]);
2596 if (strcasecmp(argv
[0], "sndbuf") == 0)
2598 else if (strcasecmp(argv
[0], "rcvbuf") == 0)
2600 else if (strcasecmp(argv
[0], "xferbuf") == 0)
2605 if ((size
= strsuftoi(argv
[1])) == -1)
2609 fprintf(ttyout
, "%s: size must be positive.\n", argv
[0]);
2617 fprintf(ttyout
, "Socket buffer sizes: send %d, receive %d.\n",
2618 sndbuf_size
, rcvbuf_size
);
2623 * Set or display options (defaults are provided by various env vars)
2626 setoption(int argc
, char *argv
[])
2631 if (argc
== 0 || (argc
!= 1 && argc
!= 3)) {
2632 UPRINTF("usage: %s [option value]\n", argv
[0]);
2636 #define OPTIONINDENT ((int) sizeof("http_proxy"))
2638 for (o
= optiontab
; o
->name
!= NULL
; o
++) {
2639 fprintf(ttyout
, "%-*s\t%s\n", OPTIONINDENT
,
2640 o
->name
, o
->value
? o
->value
: "");
2643 o
= getoption(argv
[1]);
2645 fprintf(ttyout
, "No such option `%s'.\n", argv
[1]);
2649 o
->value
= ftp_strdup(argv
[2]);
2651 fprintf(ttyout
, "Setting `%s' to `%s'.\n",
2661 unsetoption(int argc
, char *argv
[])
2666 if (argc
== 0 || argc
!= 2) {
2667 UPRINTF("usage: %s option\n", argv
[0]);
2671 o
= getoption(argv
[1]);
2673 fprintf(ttyout
, "No such option `%s'.\n", argv
[1]);
2677 fprintf(ttyout
, "Unsetting `%s'.\n", o
->name
);
2682 * Display features supported by the remote host.
2685 feat(int argc
, char *argv
[])
2687 int oldverbose
= verbose
;
2690 UPRINTF("usage: %s\n", argv
[0]);
2694 if (! features
[FEAT_FEAT
]) {
2696 "FEAT is not supported by the remote server.\n");
2699 verbose
= 1; /* If we aren't verbose, this doesn't do anything! */
2700 (void)command("FEAT");
2701 verbose
= oldverbose
;
2705 mlst(int argc
, char *argv
[])
2707 int oldverbose
= verbose
;
2709 if (argc
< 1 || argc
> 2) {
2710 UPRINTF("usage: %s [remote-path]\n", argv
[0]);
2714 if (! features
[FEAT_MLST
]) {
2716 "MLST is not supported by the remote server.\n");
2719 verbose
= 1; /* If we aren't verbose, this doesn't do anything! */
2720 COMMAND_1ARG(argc
, argv
, "MLST");
2721 verbose
= oldverbose
;
2725 opts(int argc
, char *argv
[])
2727 int oldverbose
= verbose
;
2729 if (argc
< 2 || argc
> 3) {
2730 UPRINTF("usage: %s command [options]\n", argv
[0]);
2734 if (! features
[FEAT_FEAT
]) {
2736 "OPTS is not supported by the remote server.\n");
2739 verbose
= 1; /* If we aren't verbose, this doesn't do anything! */
2741 command("OPTS %s", argv
[1]);
2743 command("OPTS %s %s", argv
[1], argv
[2]);
2744 verbose
= oldverbose
;