1 /* $NetBSD: cmds.c,v 1.135 2012/12/22 16:57:09 christos Exp $ */
4 * Copyright (c) 1996-2009 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * This code is derived from software contributed to The NetBSD Foundation
11 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
12 * NASA Ames Research Center.
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the distribution.
23 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
24 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
25 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
27 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 * POSSIBILITY OF SUCH DAMAGE.
37 * Copyright (c) 1985, 1989, 1993, 1994
38 * The Regents of the University of California. All rights reserved.
40 * Redistribution and use in source and binary forms, with or without
41 * modification, are permitted provided that the following conditions
43 * 1. Redistributions of source code must retain the above copyright
44 * notice, this list of conditions and the following disclaimer.
45 * 2. Redistributions in binary form must reproduce the above copyright
46 * notice, this list of conditions and the following disclaimer in the
47 * documentation and/or other materials provided with the distribution.
48 * 3. Neither the name of the University nor the names of its contributors
49 * may be used to endorse or promote products derived from this software
50 * without specific prior written permission.
52 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
53 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
55 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
56 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
57 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
58 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
59 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
60 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
61 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
66 * Copyright (C) 1997 and 1998 WIDE Project.
67 * All rights reserved.
69 * Redistribution and use in source and binary forms, with or without
70 * modification, are permitted provided that the following conditions
72 * 1. Redistributions of source code must retain the above copyright
73 * notice, this list of conditions and the following disclaimer.
74 * 2. Redistributions in binary form must reproduce the above copyright
75 * notice, this list of conditions and the following disclaimer in the
76 * documentation and/or other materials provided with the distribution.
77 * 3. Neither the name of the project nor the names of its contributors
78 * may be used to endorse or promote products derived from this software
79 * without specific prior written permission.
81 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
82 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
83 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
84 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
85 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
86 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
87 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
88 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
89 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
90 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
94 #include <sys/cdefs.h>
97 static char sccsid[] = "@(#)cmds.c 8.6 (Berkeley) 10/9/94";
99 __RCSID("$NetBSD: cmds.c,v 1.135 2012/12/22 16:57:09 christos Exp $");
101 #endif /* not lint */
104 * FTP User Program -- Command Routines.
106 #include <sys/types.h>
107 #include <sys/socket.h>
108 #include <sys/stat.h>
109 #include <sys/wait.h>
110 #include <arpa/ftp.h>
129 static struct types {
135 { "ascii", "A", TYPE_A, 0 },
136 { "binary", "I", TYPE_I, 0 },
137 { "image", "I", TYPE_I, 0 },
138 { "ebcdic", "E", TYPE_E, 0 },
139 { "tenex", "L", TYPE_L, bytename },
140 { NULL, NULL, 0, NULL }
143 static sigjmp_buf jabort;
145 static int confirm(const char *, const char *);
146 __dead static void mintr(int);
147 static void mabort(const char *);
148 static void set_type(const char *);
150 static const char *doprocess(char *, size_t, const char *, int, int, int);
151 static const char *domap(char *, size_t, const char *);
152 static const char *docase(char *, size_t, const char *);
153 static const char *dotrans(char *, size_t, const char *);
156 * Confirm if "cmd" is to be performed upon "file".
157 * If "file" is NULL, generate a "Continue with" prompt instead.
160 confirm(const char *cmd, const char *file)
162 const char *errormsg;
164 const char *promptleft, *promptright;
166 if (!interactive || confirmrest)
169 promptleft = "Continue with";
176 fprintf(ttyout, "%s %s [anpqy?]? ", promptleft, promptright);
177 (void)fflush(ttyout);
178 if (get_line(stdin, cline, sizeof(cline), &errormsg) < 0) {
180 fprintf(ttyout, "%s; %s aborted\n", errormsg, cmd);
183 switch (tolower((unsigned char)*cline)) {
187 "Prompting off for duration of %s.\n", cmd);
191 fputs("Interactive mode: off.\n", ttyout);
195 fprintf(ttyout, "%s aborted.\n", cmd);
201 " confirmation options:\n"
202 "\ta answer `yes' for the duration of %s\n"
203 "\tn answer `no' for this file\n"
204 "\tp turn off `prompt' mode\n"
205 "\tq stop the current %s\n"
206 "\ty answer `yes' for this file\n"
207 "\t? this help list\n",
209 continue; /* back to while(1) */
220 settype(int argc, char *argv[])
224 if (argc == 0 || argc > 2) {
227 UPRINTF("usage: %s [", argv[0]);
229 for (p = types; p->t_name; p++) {
230 fprintf(ttyout, "%s%s", sep, p->t_name);
233 fputs(" ]\n", ttyout);
238 fprintf(ttyout, "Using %s mode to transfer files.\n", typename);
246 set_type(const char *ttype)
251 for (p = types; p->t_name; p++)
252 if (strcmp(ttype, p->t_name) == 0)
254 if (p->t_name == 0) {
255 fprintf(ttyout, "%s: unknown mode.\n", ttype);
259 if ((p->t_arg != NULL) && (*(p->t_arg) != '\0'))
260 comret = command("TYPE %s %s", p->t_mode, p->t_arg);
262 comret = command("TYPE %s", p->t_mode);
263 if (comret == COMPLETE) {
264 (void)strlcpy(typename, p->t_name, sizeof(typename));
265 curtype = type = p->t_type;
270 * Internal form of settype; changes current type in use with server
271 * without changing our notion of the type for data transfers.
272 * Used to change to and from ascii for listings.
275 changetype(int newtype, int show)
278 int comret, oldverbose = verbose;
282 if (newtype == curtype)
284 if (ftp_debug == 0 && show == 0)
286 for (p = types; p->t_name; p++)
287 if (newtype == p->t_type)
289 if (p->t_name == 0) {
290 errx(1, "changetype: unknown type %d", newtype);
292 if (newtype == TYPE_L && bytename[0] != '\0')
293 comret = command("TYPE %s %s", p->t_mode, bytename);
295 comret = command("TYPE %s", p->t_mode);
296 if (comret == COMPLETE)
298 verbose = oldverbose;
302 * Set binary transfer type.
306 setbinary(int argc, char *argv[])
310 UPRINTF("usage: %s\n", argv[0]);
318 * Set ascii transfer type.
322 setascii(int argc, char *argv[])
326 UPRINTF("usage: %s\n", argv[0]);
334 * Set tenex transfer type.
338 settenex(int argc, char *argv[])
342 UPRINTF("usage: %s\n", argv[0]);
350 * Set file transfer mode.
354 setftmode(int argc, char *argv[])
358 UPRINTF("usage: %s mode-name\n", argv[0]);
362 fprintf(ttyout, "We only support %s mode, sorry.\n", modename);
367 * Set file transfer format.
371 setform(int argc, char *argv[])
375 UPRINTF("usage: %s format\n", argv[0]);
379 fprintf(ttyout, "We only support %s format, sorry.\n", formname);
384 * Set file transfer structure.
388 setstruct(int argc, char *argv[])
392 UPRINTF("usage: %s struct-mode\n", argv[0]);
396 fprintf(ttyout, "We only support %s structure, sorry.\n", structname);
401 * Send a single file.
404 put(int argc, char *argv[])
406 char buf[MAXPATHLEN];
417 if (argc == 0 || (argc == 1 && !another(&argc, &argv, "local-file")))
419 if ((argc < 3 && !another(&argc, &argv, "remote-file")) || argc > 3) {
421 UPRINTF("usage: %s local-file [remote-file]\n", argv[0]);
425 if ((locfile = globulize(argv[1])) == NULL) {
430 if (loc) /* If argv[2] is a copy of the old argv[1], update it */
432 cmd = (argv[0][0] == 'a') ? "APPE" : ((sunique) ? "STOU" : "STOR");
433 remfile = doprocess(buf, sizeof(buf), remfile,
434 0, loc && ntflag, loc && mapflag);
435 sendrequest(cmd, locfile, remfile,
436 locfile != argv[1] || remfile != argv[2]);
441 doprocess(char *dst, size_t dlen, const char *src,
442 int casef, int transf, int mapf)
445 src = docase(dst, dlen, src);
447 src = dotrans(dst, dlen, src);
449 src = domap(dst, dlen, src);
454 * Send multiple files.
457 mput(int argc, char *argv[])
464 if (argc == 0 || (argc == 1 && !another(&argc, &argv, "local-files"))) {
465 UPRINTF("usage: %s local-files\n", argv[0]);
470 oldintr = xsignal(SIGINT, mintr);
471 if (sigsetjmp(jabort, 1))
476 while ((cp = remglob(argv, 0, NULL)) != NULL) {
477 if (*cp == '\0' || !connected) {
481 if (mflag && confirm(argv[0], cp)) {
482 char buf[MAXPATHLEN];
483 tp = doprocess(buf, sizeof(buf), cp,
484 mcase, ntflag, mapflag);
485 sendrequest((sunique) ? "STOU" : "STOR",
486 cp, tp, cp != tp || !interactive);
487 if (!mflag && fromatty) {
488 ointer = interactive;
490 if (confirm(argv[0], NULL)) {
493 interactive = ointer;
499 for (i = 1; i < argc && connected; i++) {
505 if (mflag && confirm(argv[0], argv[i])) {
506 char buf[MAXPATHLEN];
507 tp = doprocess(buf, sizeof(buf), argv[i],
509 sendrequest((sunique) ? "STOU" : "STOR",
510 argv[i], tp, tp != argv[i] || !interactive);
511 if (!mflag && fromatty) {
512 ointer = interactive;
514 if (confirm(argv[0], NULL)) {
517 interactive = ointer;
523 memset(&gl, 0, sizeof(gl));
524 flags = GLOB_BRACE|GLOB_NOCHECK|GLOB_TILDE;
525 if (glob(argv[i], flags, NULL, &gl) || gl.gl_pathc == 0) {
526 warnx("Glob pattern `%s' not found", argv[i]);
530 for (cpp = gl.gl_pathv; cpp && *cpp != NULL && connected;
532 if (mflag && confirm(argv[0], *cpp)) {
533 char buf[MAXPATHLEN];
535 tp = doprocess(buf, sizeof(buf), *cpp,
537 sendrequest((sunique) ? "STOU" : "STOR",
538 *cpp, tp, *cpp != tp || !interactive);
539 if (!mflag && fromatty) {
540 ointer = interactive;
542 if (confirm(argv[0], NULL)) {
545 interactive = ointer;
552 (void)xsignal(SIGINT, oldintr);
557 reget(int argc, char *argv[])
560 (void)getit(argc, argv, 1, restart_point ? "r+" : "a");
564 get(int argc, char *argv[])
567 (void)getit(argc, argv, 0, restart_point ? "r+" : "w");
572 * If restartit is 1, restart the xfer always.
573 * If restartit is -1, restart the xfer only if the remote file is newer.
576 getit(int argc, char *argv[], int restartit, const char *gmode)
579 char *remfile, *olocfile;
581 char buf[MAXPATHLEN];
589 if (argc == 0 || (argc == 1 && !another(&argc, &argv, "remote-file")))
591 if ((argc < 3 && !another(&argc, &argv, "local-file")) || argc > 3) {
593 UPRINTF("usage: %s remote-file [local-file]\n", argv[0]);
598 if ((olocfile = globulize(argv[2])) == NULL) {
602 locfile = doprocess(buf, sizeof(buf), olocfile,
603 loc && mcase, loc && ntflag, loc && mapflag);
608 if (! features[FEAT_REST_STREAM]) {
610 "Restart is not supported by the remote server.\n");
613 ret = stat(locfile, &stbuf);
614 if (restartit == 1) {
616 if (errno != ENOENT) {
617 warn("Can't stat `%s'", locfile);
623 restart_point = stbuf.st_size;
628 mtime = remotemodtime(argv[1], 0);
631 if (stbuf.st_mtime >= mtime) {
639 recvrequest("RETR", locfile, remfile, gmode,
640 remfile != argv[1] || locfile != argv[2], loc);
643 (void)free(olocfile);
654 write(fileno(ttyout), "\n", 1);
655 siglongjmp(jabort, 1);
659 mabort(const char *cmd)
663 if (mflag && fromatty) {
664 ointer = interactive;
668 if (confirm(cmd, NULL)) {
669 interactive = ointer;
673 interactive = ointer;
680 * Get multiple files.
683 mget(int argc, char *argv[])
689 int volatile restartit;
692 (argc == 1 && !another(&argc, &argv, "remote-files"))) {
693 UPRINTF("usage: %s remote-files\n", argv[0]);
700 if (strcmp(argv[0], "mreget") == 0) {
701 if (! features[FEAT_REST_STREAM]) {
703 "Restart is not supported by the remote server.\n");
708 oldintr = xsignal(SIGINT, mintr);
709 if (sigsetjmp(jabort, 1))
711 while ((cp = remglob(argv, proxy, NULL)) != NULL) {
712 char buf[MAXPATHLEN];
713 if (*cp == '\0' || !connected) {
719 if (! fileindir(cp, localcwd)) {
720 fprintf(ttyout, "Skipping non-relative filename `%s'\n",
724 if (!confirm(argv[0], cp))
726 tp = doprocess(buf, sizeof(buf), cp, mcase, ntflag, mapflag);
730 if (stat(tp, &stbuf) == 0)
731 restart_point = stbuf.st_size;
733 warn("Can't stat `%s'", tp);
735 recvrequest("RETR", tp, cp, restart_point ? "r+" : "w",
736 tp != cp || !interactive, 1);
738 if (!mflag && fromatty) {
739 ointer = interactive;
741 if (confirm(argv[0], NULL))
743 interactive = ointer;
746 (void)xsignal(SIGINT, oldintr);
751 * Read list of filenames from a local file and get those
754 fget(int argc, char *argv[])
758 char buf[MAXPATHLEN], cmdbuf[MAX_C_NAME];
761 UPRINTF("usage: %s localfile\n", argv[0]);
766 fp = fopen(argv[1], "r");
768 fprintf(ttyout, "Can't open source file %s\n", argv[1]);
773 (void)strlcpy(cmdbuf, "get", sizeof(cmdbuf));
775 gmode = restart_point ? "r+" : "w";
777 while (get_line(fp, buf, sizeof(buf), NULL) >= 0) {
781 (void)getit(argc, argv, 0, gmode);
790 return (val ? "on" : "off");
798 status(int argc, char *argv[])
802 UPRINTF("usage: %s\n", argv[0]);
808 fprintf(ttyout, "Connected %sto %s.\n",
809 connected == -1 ? "and logged in" : "", hostname);
811 fputs("Not connected.\n", ttyout);
815 fprintf(ttyout, "Connected for proxy commands to %s.\n",
819 fputs("No proxy connection.\n", ttyout);
823 fprintf(ttyout, "Gate ftp: %s, server %s, port %s.\n", onoff(gatemode),
824 *gateserver ? gateserver : "(none)", gateport);
825 fprintf(ttyout, "Passive mode: %s; fallback to active mode: %s.\n",
826 onoff(passivemode), onoff(activefallback));
827 fprintf(ttyout, "Mode: %s; Type: %s; Form: %s; Structure: %s.\n",
828 modename, typename, formname, structname);
829 fprintf(ttyout, "Verbose: %s; Bell: %s; Prompting: %s; Globbing: %s.\n",
830 onoff(verbose), onoff(bell), onoff(interactive), onoff(doglob));
831 fprintf(ttyout, "Store unique: %s; Receive unique: %s.\n",
832 onoff(sunique), onoff(runique));
833 fprintf(ttyout, "Preserve modification times: %s.\n", onoff(preserve));
834 fprintf(ttyout, "Case: %s; CR stripping: %s.\n", onoff(mcase),
837 fprintf(ttyout, "Ntrans: (in) %s (out) %s\n", ntin, ntout);
840 fputs("Ntrans: off.\n", ttyout);
843 fprintf(ttyout, "Nmap: (in) %s (out) %s\n", mapin, mapout);
846 fputs("Nmap: off.\n", ttyout);
849 "Hash mark printing: %s; Mark count: %d; Progress bar: %s.\n",
850 onoff(hash), mark, onoff(progress));
852 "Get transfer rate throttle: %s; maximum: %d; increment %d.\n",
853 onoff(rate_get), rate_get, rate_get_incr);
855 "Put transfer rate throttle: %s; maximum: %d; increment %d.\n",
856 onoff(rate_put), rate_put, rate_put_incr);
858 "Socket buffer sizes: send %d, receive %d.\n",
859 sndbuf_size, rcvbuf_size);
860 fprintf(ttyout, "Use of PORT cmds: %s.\n", onoff(sendport));
861 fprintf(ttyout, "Use of EPSV/EPRT cmds for IPv4: %s%s.\n", onoff(epsv4),
862 epsv4bad ? " (disabled for this connection)" : "");
863 fprintf(ttyout, "Use of EPSV/EPRT cmds for IPv6: %s%s.\n", onoff(epsv6),
864 epsv6bad ? " (disabled for this connection)" : "");
865 fprintf(ttyout, "Command line editing: %s.\n",
866 #ifdef NO_EDITCOMPLETE
867 "support not compiled in"
868 #else /* !def NO_EDITCOMPLETE */
870 #endif /* !def NO_EDITCOMPLETE */
875 fputs("Macros:\n", ttyout);
876 for (i=0; i<macnum; i++) {
877 fprintf(ttyout, "\t%s\n", macros[i].mac_name);
880 #endif /* !def NO_STATUS */
881 fprintf(ttyout, "Version: %s %s\n", FTP_PRODUCT, FTP_VERSION);
889 togglevar(int argc, char *argv[], int *var, const char *mesg)
893 } else if (argc == 2 && strcasecmp(argv[1], "on") == 0) {
895 } else if (argc == 2 && strcasecmp(argv[1], "off") == 0) {
898 UPRINTF("usage: %s [ on | off ]\n", argv[0]);
902 fprintf(ttyout, "%s %s.\n", mesg, onoff(*var));
907 * Set beep on cmd completed mode.
911 setbell(int argc, char *argv[])
914 code = togglevar(argc, argv, &bell, "Bell mode");
918 * Set command line editing
922 setedit(int argc, char *argv[])
925 #ifdef NO_EDITCOMPLETE
927 UPRINTF("usage: %s\n", argv[0]);
932 fputs("Editing support not compiled in; ignoring command.\n",
934 #else /* !def NO_EDITCOMPLETE */
935 code = togglevar(argc, argv, &editing, "Editing mode");
937 #endif /* !def NO_EDITCOMPLETE */
941 * Turn on packet tracing.
945 settrace(int argc, char *argv[])
948 code = togglevar(argc, argv, &trace, "Packet tracing");
952 * Toggle hash mark printing during transfers, or set hash mark bytecount.
956 sethash(int argc, char *argv[])
960 else if (argc != 2) {
961 UPRINTF("usage: %s [ on | off | bytecount ]\n",
965 } else if (strcasecmp(argv[1], "on") == 0)
967 else if (strcasecmp(argv[1], "off") == 0)
972 nmark = strsuftoi(argv[1]);
974 fprintf(ttyout, "mark: bad bytecount value `%s'.\n",
982 fprintf(ttyout, "Hash mark printing %s", onoff(hash));
984 fprintf(ttyout, " (%d bytes/hash mark)", mark);
985 fputs(".\n", ttyout);
992 * Turn on printing of server echo's.
996 setverbose(int argc, char *argv[])
999 code = togglevar(argc, argv, &verbose, "Verbose mode");
1003 * Toggle PORT/LPRT cmd use before each data connection.
1007 setport(int argc, char *argv[])
1010 code = togglevar(argc, argv, &sendport, "Use of PORT/LPRT cmds");
1014 * Toggle transfer progress bar.
1018 setprogress(int argc, char *argv[])
1021 code = togglevar(argc, argv, &progress, "Progress bar");
1027 * Turn on interactive prompting during mget, mput, and mdelete.
1031 setprompt(int argc, char *argv[])
1034 code = togglevar(argc, argv, &interactive, "Interactive mode");
1038 * Toggle gate-ftp mode, or set gate-ftp server
1042 setgate(int argc, char *argv[])
1044 static char gsbuf[MAXHOSTNAMELEN];
1046 if (argc == 0 || argc > 3) {
1048 "usage: %s [ on | off | gateserver [port] ]\n", argv[0]);
1051 } else if (argc < 2) {
1052 gatemode = !gatemode;
1054 if (argc == 2 && strcasecmp(argv[1], "on") == 0)
1056 else if (argc == 2 && strcasecmp(argv[1], "off") == 0)
1060 gateport = ftp_strdup(argv[2]);
1061 (void)strlcpy(gsbuf, argv[1], sizeof(gsbuf));
1066 if (gatemode && (gateserver == NULL || *gateserver == '\0')) {
1068 "Disabling gate-ftp mode - no gate-ftp server defined.\n");
1071 fprintf(ttyout, "Gate ftp: %s, server %s, port %s.\n",
1072 onoff(gatemode), *gateserver ? gateserver : "(none)",
1079 * Toggle metacharacter interpretation on local file names.
1083 setglob(int argc, char *argv[])
1086 code = togglevar(argc, argv, &doglob, "Globbing");
1090 * Toggle preserving modification times on retrieved files.
1094 setpreserve(int argc, char *argv[])
1097 code = togglevar(argc, argv, &preserve, "Preserve modification times");
1101 * Set debugging mode on/off and/or set level of debugging.
1105 setdebug(int argc, char *argv[])
1107 if (argc == 0 || argc > 2) {
1108 UPRINTF("usage: %s [ on | off | debuglevel ]\n", argv[0]);
1111 } else if (argc == 2) {
1112 if (strcasecmp(argv[1], "on") == 0)
1114 else if (strcasecmp(argv[1], "off") == 0)
1119 val = strsuftoi(argv[1]);
1121 fprintf(ttyout, "%s: bad debugging value.\n",
1129 ftp_debug = !ftp_debug;
1131 options |= SO_DEBUG;
1133 options &= ~SO_DEBUG;
1134 fprintf(ttyout, "Debugging %s (ftp_debug=%d).\n", onoff(ftp_debug), ftp_debug);
1135 code = ftp_debug > 0;
1139 * Set current working directory on remote machine.
1142 cd(int argc, char *argv[])
1146 if (argc == 0 || argc > 2 ||
1147 (argc == 1 && !another(&argc, &argv, "remote-directory"))) {
1148 UPRINTF("usage: %s remote-directory\n", argv[0]);
1152 r = command("CWD %s", argv[1]);
1153 if (r == ERROR && code == 500) {
1155 fputs("CWD command not recognized, trying XCWD.\n",
1157 r = command("XCWD %s", argv[1]);
1159 if (r == COMPLETE) {
1166 * Set current working directory on local machine.
1169 lcd(int argc, char *argv[])
1176 argv[1] = localhome;
1179 UPRINTF("usage: %s [local-directory]\n", argv[0]);
1182 if ((locdir = globulize(argv[1])) == NULL)
1184 if (chdir(locdir) == -1)
1185 warn("Can't chdir `%s'", locdir);
1189 fprintf(ttyout, "Local directory now: %s\n", localcwd);
1192 fprintf(ttyout, "Unable to determine local directory\n");
1199 * Delete a single file.
1202 delete(int argc, char *argv[])
1205 if (argc == 0 || argc > 2 ||
1206 (argc == 1 && !another(&argc, &argv, "remote-file"))) {
1207 UPRINTF("usage: %s remote-file\n", argv[0]);
1211 if (command("DELE %s", argv[1]) == COMPLETE)
1216 * Delete multiple files.
1219 mdelete(int argc, char *argv[])
1226 (argc == 1 && !another(&argc, &argv, "remote-files"))) {
1227 UPRINTF("usage: %s [remote-files]\n", argv[0]);
1232 oldintr = xsignal(SIGINT, mintr);
1233 if (sigsetjmp(jabort, 1))
1235 while ((cp = remglob(argv, 0, NULL)) != NULL) {
1240 if (mflag && confirm(argv[0], cp)) {
1241 if (command("DELE %s", cp) == COMPLETE)
1243 if (!mflag && fromatty) {
1244 ointer = interactive;
1246 if (confirm(argv[0], NULL)) {
1249 interactive = ointer;
1253 (void)xsignal(SIGINT, oldintr);
1258 * Rename a remote file.
1261 renamefile(int argc, char *argv[])
1264 if (argc == 0 || (argc == 1 && !another(&argc, &argv, "from-name")))
1266 if ((argc < 3 && !another(&argc, &argv, "to-name")) || argc > 3) {
1268 UPRINTF("usage: %s from-name to-name\n", argv[0]);
1272 if (command("RNFR %s", argv[1]) == CONTINUE &&
1273 command("RNTO %s", argv[2]) == COMPLETE)
1278 * Get a directory listing of remote files.
1279 * Supports being invoked as:
1285 * pdir, pls LIST |$PAGER
1286 * pmlsd MLSD |$PAGER
1289 ls(int argc, char *argv[])
1292 char *remdir, *locbuf;
1293 const char *locfile;
1294 int pagecmd, mlsdcmd;
1299 pagecmd = mlsdcmd = 0;
1301 * the only commands that start with `p' are
1302 * the `pager' versions.
1304 if (argv[0][0] == 'p')
1306 if (strcmp(argv[0] + pagecmd , "mlsd") == 0) {
1307 if (! features[FEAT_MLST]) {
1309 "MLSD is not supported by the remote server.\n");
1319 else if (strcmp(argv[0] + pagecmd, "nlist") == 0)
1328 if (argc > 3 || ((pagecmd | mlsdcmd) && argc > 2)) {
1330 if (pagecmd || mlsdcmd)
1331 UPRINTF("usage: %s [remote-path]\n", argv[0]);
1333 UPRINTF("usage: %s [remote-path [local-file]]\n",
1343 p = getoptionvalue("pager");
1346 len = strlen(p) + 2;
1347 locbuf = ftp_malloc(len);
1349 (void)strlcpy(locbuf + 1, p, len - 1);
1351 } else if ((strcmp(locfile, "-") != 0) && *locfile != '|') {
1352 if ((locbuf = globulize(locfile)) == NULL ||
1353 !confirm("output to local-file:", locbuf)) {
1359 recvrequest(cmd, locfile, remdir, "w", 0, 0);
1366 * Get a directory listing of multiple remote files.
1369 mls(int argc, char *argv[])
1373 int volatile dolist;
1374 char * volatile dest, *odest;
1379 if (argc < 2 && !another(&argc, &argv, "remote-files"))
1381 if (argc < 3 && !another(&argc, &argv, "local-file")) {
1383 UPRINTF("usage: %s remote-files local-file\n", argv[0]);
1387 odest = dest = argv[argc - 1];
1388 argv[argc - 1] = NULL;
1389 if (strcmp(dest, "-") && *dest != '|')
1390 if (((dest = globulize(dest)) == NULL) ||
1391 !confirm("output to local-file:", dest)) {
1395 dolist = strcmp(argv[0], "mls");
1397 oldintr = xsignal(SIGINT, mintr);
1398 if (sigsetjmp(jabort, 1))
1400 for (i = 1; mflag && i < argc-1 && connected; i++) {
1401 lmode = (i == 1) ? "w" : "a";
1402 recvrequest(dolist ? "LIST" : "NLST", dest, argv[i], lmode,
1404 if (!mflag && fromatty) {
1405 ointer = interactive;
1407 if (confirm(argv[0], NULL)) {
1410 interactive = ointer;
1413 (void)xsignal(SIGINT, oldintr);
1415 if (dest != odest) /* free up after globulize() */
1424 shell(int argc, char *argv[])
1428 char shellnam[MAXPATHLEN];
1429 const char *shellp, *namep;
1433 UPRINTF("usage: %s [command [args]]\n", argv[0]);
1437 oldintr = xsignal(SIGINT, SIG_IGN);
1438 if ((pid = fork()) == 0) {
1439 for (pid = 3; pid < 20; pid++)
1441 (void)xsignal(SIGINT, SIG_DFL);
1442 shellp = getenv("SHELL");
1444 shellp = _PATH_BSHELL;
1445 namep = strrchr(shellp, '/');
1450 (void)strlcpy(shellnam, namep, sizeof(shellnam));
1452 fputs(shellp, ttyout);
1456 execl(shellp, shellnam, "-c", altarg, (char *)0);
1459 execl(shellp, shellnam, (char *)0);
1461 warn("Can't execute `%s'", shellp);
1466 while (wait(&wait_status) != pid)
1468 (void)xsignal(SIGINT, oldintr);
1470 warn("Can't fork a subshell; try again later");
1477 * Send new user information (re-login)
1480 user(int argc, char *argv[])
1483 char emptypass[] = "";
1489 (void)another(&argc, &argv, "username");
1490 if (argc < 2 || argc > 4) {
1492 UPRINTF("usage: %s username [password [account]]\n",
1497 n = command("USER %s", argv[1]);
1498 if (n == CONTINUE) {
1500 password = getpass("Password: ");
1501 if (password == NULL)
1502 password = emptypass;
1506 n = command("PASS %s", password);
1507 memset(password, 0, strlen(password));
1509 if (n == CONTINUE) {
1512 password = getpass("Account: ");
1513 if (password == NULL)
1514 password = emptypass;
1518 n = command("ACCT %s", password);
1519 memset(password, 0, strlen(password));
1521 if (n != COMPLETE) {
1522 fputs("Login failed.\n", ttyout);
1525 if (!aflag && argc == 4) {
1527 (void)command("ACCT %s", password);
1528 memset(password, 0, strlen(password));
1535 * Print working directory on remote machine.
1539 pwd(int argc, char *argv[])
1544 UPRINTF("usage: %s\n", argv[0]);
1550 fprintf(ttyout, "Unable to determine remote directory\n");
1552 fprintf(ttyout, "Remote directory: %s\n", remotecwd);
1558 * Print working directory on local machine.
1561 lpwd(int argc, char *argv[])
1566 UPRINTF("usage: %s\n", argv[0]);
1572 fprintf(ttyout, "Unable to determine local directory\n");
1574 fprintf(ttyout, "Local directory: %s\n", localcwd);
1583 makedir(int argc, char *argv[])
1587 if (argc == 0 || argc > 2 ||
1588 (argc == 1 && !another(&argc, &argv, "directory-name"))) {
1589 UPRINTF("usage: %s directory-name\n", argv[0]);
1593 r = command("MKD %s", argv[1]);
1594 if (r == ERROR && code == 500) {
1596 fputs("MKD command not recognized, trying XMKD.\n",
1598 r = command("XMKD %s", argv[1]);
1605 * Remove a directory.
1608 removedir(int argc, char *argv[])
1612 if (argc == 0 || argc > 2 ||
1613 (argc == 1 && !another(&argc, &argv, "directory-name"))) {
1614 UPRINTF("usage: %s directory-name\n", argv[0]);
1618 r = command("RMD %s", argv[1]);
1619 if (r == ERROR && code == 500) {
1621 fputs("RMD command not recognized, trying XRMD.\n",
1623 r = command("XRMD %s", argv[1]);
1630 * Send a line, verbatim, to the remote machine.
1633 quote(int argc, char *argv[])
1637 (argc == 1 && !another(&argc, &argv, "command line to send"))) {
1638 UPRINTF("usage: %s line-to-send\n", argv[0]);
1642 quote1("", argc, argv);
1646 * Send a SITE command to the remote machine. The line
1647 * is sent verbatim to the remote machine, except that the
1648 * word "SITE" is added at the front.
1651 site(int argc, char *argv[])
1655 (argc == 1 && !another(&argc, &argv, "arguments to SITE command"))){
1656 UPRINTF("usage: %s line-to-send\n", argv[0]);
1660 quote1("SITE ", argc, argv);
1664 * Turn argv[1..argc) into a space-separated string, then prepend initial text.
1665 * Send the result as a one-line command and get response.
1668 quote1(const char *initial, int argc, char *argv[])
1671 char buf[BUFSIZ]; /* must be >= sizeof(line) */
1673 (void)strlcpy(buf, initial, sizeof(buf));
1674 for (i = 1; i < argc; i++) {
1675 (void)strlcat(buf, argv[i], sizeof(buf));
1677 (void)strlcat(buf, " ", sizeof(buf));
1679 if (command("%s", buf) == PRELIM) {
1680 while (getreply(0) == PRELIM)
1687 do_chmod(int argc, char *argv[])
1690 if (argc == 0 || (argc == 1 && !another(&argc, &argv, "mode")))
1692 if ((argc < 3 && !another(&argc, &argv, "remote-file")) || argc > 3) {
1694 UPRINTF("usage: %s mode remote-file\n", argv[0]);
1698 (void)command("SITE CHMOD %s %s", argv[1], argv[2]);
1701 #define COMMAND_1ARG(argc, argv, cmd) \
1705 command(cmd " %s", argv[1])
1708 do_umask(int argc, char *argv[])
1710 int oldverbose = verbose;
1713 UPRINTF("usage: %s [umask]\n", argv[0]);
1718 COMMAND_1ARG(argc, argv, "SITE UMASK");
1719 verbose = oldverbose;
1723 idlecmd(int argc, char *argv[])
1725 int oldverbose = verbose;
1727 if (argc < 1 || argc > 2) {
1728 UPRINTF("usage: %s [seconds]\n", argv[0]);
1733 COMMAND_1ARG(argc, argv, "SITE IDLE");
1734 verbose = oldverbose;
1738 * Ask the other side for help.
1741 rmthelp(int argc, char *argv[])
1743 int oldverbose = verbose;
1746 UPRINTF("usage: %s\n", argv[0]);
1751 COMMAND_1ARG(argc, argv, "HELP");
1752 verbose = oldverbose;
1756 * Terminate session and exit.
1757 * May be called with 0, NULL.
1761 quit(int argc, char *argv[])
1764 /* this may be called with argc == 0, argv == NULL */
1765 if (argc == 0 && argv != NULL) {
1766 UPRINTF("usage: %s\n", argv[0]);
1771 disconnect(0, NULL);
1774 disconnect(0, NULL);
1779 * Terminate session, but don't exit.
1780 * May be called with 0, NULL.
1783 disconnect(int argc, char *argv[])
1786 /* this may be called with argc == 0, argv == NULL */
1787 if (argc == 0 && argv != NULL) {
1788 UPRINTF("usage: %s\n", argv[0]);
1794 (void)command("QUIT");
1799 account(int argc, char *argv[])
1802 char emptypass[] = "";
1804 if (argc == 0 || argc > 2) {
1805 UPRINTF("usage: %s [password]\n", argv[0]);
1812 ap = getpass("Account:");
1816 (void)command("ACCT %s", ap);
1817 memset(ap, 0, strlen(ap));
1820 sigjmp_buf abortprox;
1823 proxabort(int notused)
1838 siglongjmp(abortprox, 1);
1842 doproxy(int argc, char *argv[])
1847 char cmdbuf[MAX_C_NAME];
1849 if (argc == 0 || (argc == 1 && !another(&argc, &argv, "command"))) {
1850 UPRINTF("usage: %s command\n", argv[0]);
1854 c = getcmd(argv[1]);
1855 if (c == (struct cmd *) -1) {
1856 fputs("?Ambiguous command.\n", ttyout);
1861 fputs("?Invalid command.\n", ttyout);
1866 fputs("?Invalid proxy command.\n", ttyout);
1870 if (sigsetjmp(abortprox, 1)) {
1874 oldintr = xsignal(SIGINT, proxabort);
1876 if (c->c_conn && !connected) {
1877 fputs("Not connected.\n", ttyout);
1879 (void)xsignal(SIGINT, oldintr);
1883 cmdpos = strcspn(line, " \t");
1884 if (cmdpos > 0) /* remove leading "proxy " from input buffer */
1885 memmove(line, line + cmdpos + 1, strlen(line) - cmdpos + 1);
1886 (void)strlcpy(cmdbuf, c->c_name, sizeof(cmdbuf));
1888 (*c->c_handler)(argc-1, argv+1);
1896 (void)xsignal(SIGINT, oldintr);
1900 setcase(int argc, char *argv[])
1903 code = togglevar(argc, argv, &mcase, "Case mapping");
1907 * convert the given name to lower case if it's all upper case, into
1908 * a static buffer which is returned to the caller
1911 docase(char *dst, size_t dlen, const char *src)
1916 for (i = 0; src[i] != '\0' && i < dlen - 1; i++) {
1918 if (islower((unsigned char)dst[i]))
1924 for (i = 0; dst[i] != '\0'; i++)
1925 if (isupper((unsigned char)dst[i]))
1926 dst[i] = tolower((unsigned char)dst[i]);
1932 setcr(int argc, char *argv[])
1935 code = togglevar(argc, argv, &crflag, "Carriage Return stripping");
1939 setntrans(int argc, char *argv[])
1942 if (argc == 0 || argc > 3) {
1943 UPRINTF("usage: %s [inchars [outchars]]\n", argv[0]);
1949 fputs("Ntrans off.\n", ttyout);
1955 (void)strlcpy(ntin, argv[1], sizeof(ntin));
1960 (void)strlcpy(ntout, argv[2], sizeof(ntout));
1964 dotrans(char *dst, size_t dlen, const char *src)
1970 for (ostop = 0; *(ntout + ostop) && ostop < 16; ostop++)
1972 for (cp1 = src; *cp1; cp1++) {
1974 for (i = 0; *(ntin + i) && i < 16; i++) {
1975 if (*cp1 == *(ntin + i)) {
1978 *cp2++ = *(ntout + i);
1979 if (cp2 - dst >= (ptrdiff_t)(dlen - 1))
1995 setnmap(int argc, char *argv[])
2001 fputs("Nmap off.\n", ttyout);
2006 (argc < 3 && !another(&argc, &argv, "mapout")) || argc > 3) {
2007 UPRINTF("usage: %s [mapin mapout]\n", argv[0]);
2013 cp = strchr(altarg, ' ');
2018 cp = strchr(altarg, ' ');
2021 (void)strlcpy(mapin, altarg, MAXPATHLEN);
2022 while (*++cp == ' ')
2024 (void)strlcpy(mapout, cp, MAXPATHLEN);
2028 domap(char *dst, size_t dlen, const char *src)
2030 const char *cp1 = src;
2032 const char *tp[9], *te[9];
2033 int i, toks[9], toknum = 0, match = 1;
2035 for (i=0; i < 9; ++i) {
2038 while (match && *cp1 && *cp2) {
2041 if (*++cp2 != *cp1) {
2046 if (*(cp2+1) >= '1' && (*cp2+1) <= '9') {
2047 if (*cp1 != *(++cp2+1)) {
2048 toks[toknum = *cp2 - '1']++;
2050 while (*++cp1 && *(cp2+1)
2064 if (match && *cp1) {
2067 if (match && *cp2) {
2071 if (!match && *cp1) /* last token mismatch */
2088 if (*++cp1 == '$' &&
2089 isdigit((unsigned char)*(cp1+1))) {
2090 if (*++cp1 == '0') {
2091 const char *cp3 = src;
2098 else if (toks[toknum = *cp1 - '1']) {
2099 const char *cp3 = tp[toknum];
2101 while (cp3 != te[toknum]) {
2108 while (*cp1 && *cp1 != ',' &&
2113 else if (*cp1 == '$' &&
2114 isdigit((unsigned char)*(cp1+1))) {
2115 if (*++cp1 == '0') {
2116 const char *cp3 = src;
2122 else if (toks[toknum =
2124 const char *cp3=tp[toknum];
2138 "nmap: unbalanced brackets.\n",
2146 while (*++cp1 && *cp1 != ']') {
2147 if (*cp1 == '\\' && *(cp1 + 1)) {
2153 "nmap: unbalanced brackets.\n",
2170 if (isdigit((unsigned char)*(cp1 + 1))) {
2171 if (*++cp1 == '0') {
2172 const char *cp3 = src;
2178 else if (toks[toknum = *cp1 - '1']) {
2179 const char *cp3 = tp[toknum];
2181 while (cp3 != te[toknum]) {
2187 /* intentional drop through */
2195 return *dst ? dst : src;
2199 setpassive(int argc, char *argv[])
2203 passivemode = !passivemode;
2204 activefallback = passivemode;
2205 } else if (argc != 2) {
2207 UPRINTF("usage: %s [ on | off | auto ]\n", argv[0]);
2210 } else if (strcasecmp(argv[1], "on") == 0) {
2213 } else if (strcasecmp(argv[1], "off") == 0) {
2216 } else if (strcasecmp(argv[1], "auto") == 0) {
2221 fprintf(ttyout, "Passive mode: %s; fallback to active mode: %s.\n",
2222 onoff(passivemode), onoff(activefallback));
2228 setepsv4(int argc, char *argv[])
2230 code = togglevar(argc, argv, &epsv4,
2231 verbose ? "EPSV/EPRT on IPv4" : NULL);
2236 setepsv6(int argc, char *argv[])
2238 code = togglevar(argc, argv, &epsv6,
2239 verbose ? "EPSV/EPRT on IPv6" : NULL);
2244 setepsv(int argc, char*argv[])
2246 setepsv4(argc,argv);
2247 setepsv6(argc,argv);
2251 setsunique(int argc, char *argv[])
2254 code = togglevar(argc, argv, &sunique, "Store unique");
2258 setrunique(int argc, char *argv[])
2261 code = togglevar(argc, argv, &runique, "Receive unique");
2265 parserate(int argc, char *argv[], int cmdlineopt)
2267 int dir, max, incr, showonly;
2268 sigfunc oldusr1, oldusr2;
2270 if (argc > 4 || (argc < (cmdlineopt ? 3 : 2))) {
2274 "usage: %s (all|get|put),maximum-bytes[,increment-bytes]]\n",
2278 "usage: %s (all|get|put) [maximum-bytes [increment-bytes]]\n",
2282 dir = max = incr = showonly = 0;
2285 #define RATE_ALL (RATE_GET | RATE_PUT)
2287 if (strcasecmp(argv[1], "all") == 0)
2289 else if (strcasecmp(argv[1], "get") == 0)
2291 else if (strcasecmp(argv[1], "put") == 0)
2297 if ((max = strsuftoi(argv[2])) < 0)
2303 if ((incr = strsuftoi(argv[3])) <= 0)
2308 oldusr1 = xsignal(SIGUSR1, SIG_IGN);
2309 oldusr2 = xsignal(SIGUSR2, SIG_IGN);
2310 if (dir & RATE_GET) {
2313 rate_get_incr = incr;
2315 if (!cmdlineopt || verbose)
2317 "Get transfer rate throttle: %s; maximum: %d; increment %d.\n",
2318 onoff(rate_get), rate_get, rate_get_incr);
2320 if (dir & RATE_PUT) {
2323 rate_put_incr = incr;
2325 if (!cmdlineopt || verbose)
2327 "Put transfer rate throttle: %s; maximum: %d; increment %d.\n",
2328 onoff(rate_put), rate_put, rate_put_incr);
2330 (void)xsignal(SIGUSR1, oldusr1);
2331 (void)xsignal(SIGUSR2, oldusr2);
2336 setrate(int argc, char *argv[])
2339 code = parserate(argc, argv, 0);
2342 /* change directory to parent directory */
2344 cdup(int argc, char *argv[])
2349 UPRINTF("usage: %s\n", argv[0]);
2353 r = command("CDUP");
2354 if (r == ERROR && code == 500) {
2356 fputs("CDUP command not recognized, trying XCUP.\n",
2358 r = command("XCUP");
2360 if (r == COMPLETE) {
2367 * Restart transfer at specific point
2370 restart(int argc, char *argv[])
2373 if (argc == 0 || argc > 2) {
2374 UPRINTF("usage: %s [restart-point]\n", argv[0]);
2378 if (! features[FEAT_REST_STREAM]) {
2380 "Restart is not supported by the remote server.\n");
2387 rp = STRTOLL(argv[1], &ep, 10);
2388 if (rp < 0 || *ep != '\0')
2389 fprintf(ttyout, "restart: Invalid offset `%s'\n",
2394 if (restart_point == 0)
2395 fputs("No restart point defined.\n", ttyout);
2398 "Restarting at " LLF " for next get, put or append\n",
2399 (LLT)restart_point);
2403 * Show remote system type
2406 syst(int argc, char *argv[])
2408 int oldverbose = verbose;
2411 UPRINTF("usage: %s\n", argv[0]);
2415 verbose = 1; /* If we aren't verbose, this doesn't do anything! */
2416 (void)command("SYST");
2417 verbose = oldverbose;
2421 macdef(int argc, char *argv[])
2429 fputs("Limit of 16 macros have already been defined.\n",
2434 if ((argc < 2 && !another(&argc, &argv, "macro name")) || argc > 2) {
2436 UPRINTF("usage: %s macro_name\n", argv[0]);
2442 "Enter macro line by line, terminating it with a null line.\n",
2444 (void)strlcpy(macros[macnum].mac_name, argv[1],
2445 sizeof(macros[macnum].mac_name));
2447 macros[macnum].mac_start = macbuf;
2449 macros[macnum].mac_start = macros[macnum - 1].mac_end + 1;
2450 tmp = macros[macnum].mac_start;
2451 while (tmp != macbuf+4096) {
2452 if ((c = getchar()) == EOF) {
2453 fputs("macdef: end of file encountered.\n", ttyout);
2457 if ((*tmp = c) == '\n') {
2458 if (tmp == macros[macnum].mac_start) {
2459 macros[macnum++].mac_end = tmp;
2463 if (*(tmp-1) == '\0') {
2464 macros[macnum++].mac_end = tmp - 1;
2473 while ((c = getchar()) != '\n' && c != EOF)
2475 if (c == EOF || getchar() == '\n') {
2476 fputs("Macro not defined - 4K buffer exceeded.\n",
2485 * Get size of file on remote machine
2488 sizecmd(int argc, char *argv[])
2492 if (argc == 0 || argc > 2 ||
2493 (argc == 1 && !another(&argc, &argv, "remote-file"))) {
2494 UPRINTF("usage: %s remote-file\n", argv[0]);
2498 size = remotesize(argv[1], 1);
2501 "%s\t" LLF "\n", argv[1], (LLT)size);
2506 * Get last modification time of file on remote machine
2509 modtime(int argc, char *argv[])
2513 if (argc == 0 || argc > 2 ||
2514 (argc == 1 && !another(&argc, &argv, "remote-file"))) {
2515 UPRINTF("usage: %s remote-file\n", argv[0]);
2519 mtime = remotemodtime(argv[1], 1);
2521 fprintf(ttyout, "%s\t%s", argv[1],
2522 rfc2822time(localtime(&mtime)));
2527 * Show status on remote machine
2530 rmtstatus(int argc, char *argv[])
2534 UPRINTF("usage: %s [remote-file]\n", argv[0]);
2538 COMMAND_1ARG(argc, argv, "STAT");
2542 * Get file if modtime is more recent than current file
2545 newer(int argc, char *argv[])
2548 if (getit(argc, argv, -1, "w"))
2550 "Local file \"%s\" is newer than remote file \"%s\".\n",
2555 * Display one local file through $PAGER.
2558 lpage(int argc, char *argv[])
2562 char *pager, *locfile;
2564 if (argc == 0 || argc > 2 ||
2565 (argc == 1 && !another(&argc, &argv, "local-file"))) {
2566 UPRINTF("usage: %s local-file\n", argv[0]);
2570 if ((locfile = globulize(argv[1])) == NULL) {
2574 p = getoptionvalue("pager");
2577 len = strlen(p) + strlen(locfile) + 2;
2578 pager = ftp_malloc(len);
2579 (void)strlcpy(pager, p, len);
2580 (void)strlcat(pager, " ", len);
2581 (void)strlcat(pager, locfile, len);
2585 (void)free(locfile);
2589 * Display one remote file through $PAGER.
2592 page(int argc, char *argv[])
2594 int ohash, orestart_point, overbose;
2599 if (argc == 0 || argc > 2 ||
2600 (argc == 1 && !another(&argc, &argv, "remote-file"))) {
2601 UPRINTF("usage: %s remote-file\n", argv[0]);
2605 p = getoptionvalue("pager");
2608 len = strlen(p) + 2;
2609 pager = ftp_malloc(len);
2611 (void)strlcpy(pager + 1, p, len - 1);
2614 orestart_point = restart_point;
2616 hash = restart_point = verbose = 0;
2617 recvrequest("RETR", pager, argv[1], "r+", 1, 0);
2619 restart_point = orestart_point;
2625 * Set the socket send or receive buffer size.
2628 setxferbuf(int argc, char *argv[])
2634 UPRINTF("usage: %s size\n", argv[0]);
2638 if (strcasecmp(argv[0], "sndbuf") == 0)
2640 else if (strcasecmp(argv[0], "rcvbuf") == 0)
2642 else if (strcasecmp(argv[0], "xferbuf") == 0)
2647 if ((size = strsuftoi(argv[1])) == -1)
2651 fprintf(ttyout, "%s: size must be positive.\n", argv[0]);
2659 fprintf(ttyout, "Socket buffer sizes: send %d, receive %d.\n",
2660 sndbuf_size, rcvbuf_size);
2665 * Set or display options (defaults are provided by various env vars)
2668 setoption(int argc, char *argv[])
2673 if (argc == 0 || (argc != 1 && argc != 3)) {
2674 UPRINTF("usage: %s [option value]\n", argv[0]);
2678 #define OPTIONINDENT ((int) sizeof("https_proxy"))
2680 for (o = optiontab; o->name != NULL; o++) {
2681 fprintf(ttyout, "%-*s\t%s\n", OPTIONINDENT,
2682 o->name, o->value ? o->value : "");
2685 set_option(argv[1], argv[2], 1);
2691 set_option(const char * option, const char * value, int doverbose)
2695 o = getoption(option);
2697 fprintf(ttyout, "No such option `%s'.\n", option);
2701 o->value = ftp_strdup(value);
2702 if (verbose && doverbose)
2703 fprintf(ttyout, "Setting `%s' to `%s'.\n",
2711 unsetoption(int argc, char *argv[])
2716 if (argc == 0 || argc != 2) {
2717 UPRINTF("usage: %s option\n", argv[0]);
2721 o = getoption(argv[1]);
2723 fprintf(ttyout, "No such option `%s'.\n", argv[1]);
2727 fprintf(ttyout, "Unsetting `%s'.\n", o->name);
2732 * Display features supported by the remote host.
2735 feat(int argc, char *argv[])
2737 int oldverbose = verbose;
2740 UPRINTF("usage: %s\n", argv[0]);
2744 if (! features[FEAT_FEAT]) {
2746 "FEAT is not supported by the remote server.\n");
2749 verbose = 1; /* If we aren't verbose, this doesn't do anything! */
2750 (void)command("FEAT");
2751 verbose = oldverbose;
2755 mlst(int argc, char *argv[])
2757 int oldverbose = verbose;
2759 if (argc < 1 || argc > 2) {
2760 UPRINTF("usage: %s [remote-path]\n", argv[0]);
2764 if (! features[FEAT_MLST]) {
2766 "MLST is not supported by the remote server.\n");
2769 verbose = 1; /* If we aren't verbose, this doesn't do anything! */
2770 COMMAND_1ARG(argc, argv, "MLST");
2771 verbose = oldverbose;
2775 opts(int argc, char *argv[])
2777 int oldverbose = verbose;
2779 if (argc < 2 || argc > 3) {
2780 UPRINTF("usage: %s command [options]\n", argv[0]);
2784 if (! features[FEAT_FEAT]) {
2786 "OPTS is not supported by the remote server.\n");
2789 verbose = 1; /* If we aren't verbose, this doesn't do anything! */
2791 command("OPTS %s", argv[1]);
2793 command("OPTS %s %s", argv[1], argv[2]);
2794 verbose = oldverbose;