2 * Copyright (c) 1985, 1989, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * FTP User Program -- Command Routines.
39 RCSID("$Id: cmds.c,v 1.44 2001/08/05 06:39:14 assar Exp $");
41 typedef void (*sighand
)(int);
48 * `Another' gets another argument, and stores the new argc and argv.
49 * It reverts to the top level (via main.c's intr()) on EOF/error.
51 * Returns false if no new arguments have been added.
54 another(int *pargc
, char ***pargv
, char *prompt
)
56 int len
= strlen(line
), ret
;
58 if (len
>= sizeof(line
) - 3) {
59 printf("sorry, arguments too long\n");
62 printf("(%s) ", prompt
);
64 if (fgets(&line
[len
], sizeof(line
) - len
, stdin
) == NULL
)
66 len
+= strlen(&line
[len
]);
67 if (len
> 0 && line
[len
- 1] == '\n')
77 * Connect to peer server and
78 * auto-login, if possible.
81 setpeer(int argc
, char **argv
)
88 printf("Already connected to %s, use close first.\n",
94 another(&argc
, &argv
, "to");
95 if (argc
< 2 || argc
> 3) {
96 printf("usage: %s host-name [port]\n", argv
[0]);
100 sp
= getservbyname("ftp", "tcp");
102 errx(1, "You bastard. You removed ftp/tcp from services");
105 sp
= getservbyname(argv
[2], "tcp");
111 port
= strtol(argv
[2], &ep
, 0);
113 printf("%s: bad port number-- %s\n",
115 printf ("usage: %s host-name [port]\n",
123 host
= hookup(argv
[1], port
);
129 * Set up defaults for FTP.
131 strlcpy(typename
, "ascii", sizeof(typename
));
134 strlcpy(formname
, "non-print", sizeof(formname
));
136 strlcpy(modename
, "stream", sizeof(modename
));
138 strlcpy(structname
, "file", sizeof(structname
));
140 strlcpy(bytename
, "8", sizeof(bytename
));
145 #if (defined(unix) || defined(__unix__) || defined(__unix) || defined(_AIX) || defined(_CRAY) || defined(__NetBSD__)) && NBBY == 8
147 * this ifdef is to keep someone form "porting" this to an incompatible
148 * system and not checking this out. This way they have to think about it.
153 if (command("SYST") == COMPLETE
&& overbose
) {
155 cp
= strchr(reply_string
+4, ' ');
157 cp
= strchr(reply_string
+4, '\r');
165 printf("Remote system type is %s.\n",
170 if (!strncmp(reply_string
, "215 UNIX Type: L8", 17)) {
176 * Set type to 0 (not specified by user),
177 * meaning binary by default, but don't bother
178 * telling server. We can use binary
179 * for text files unless changed by the user.
182 strlcpy(typename
, "binary", sizeof(typename
));
184 printf("Using %s mode to transfer files.\n",
192 !strncmp(reply_string
, "215 TOPS20", 10))
194 "Remember to set tenex mode when transfering binary files from this machine.\n");
207 { "ascii", "A", TYPE_A
, 0 },
208 { "binary", "I", TYPE_I
, 0 },
209 { "image", "I", TYPE_I
, 0 },
210 { "ebcdic", "E", TYPE_E
, 0 },
211 { "tenex", "L", TYPE_L
, bytename
},
219 settype(int argc
, char **argv
)
227 printf("usage: %s [", argv
[0]);
229 for (p
= types
; p
->t_name
; p
++) {
230 printf("%s%s", sep
, p
->t_name
);
238 printf("Using %s mode to transfer files.\n", typename
);
242 for (p
= types
; p
->t_name
; p
++)
243 if (strcmp(argv
[1], p
->t_name
) == 0)
245 if (p
->t_name
== 0) {
246 printf("%s: unknown mode\n", argv
[1]);
250 if ((p
->t_arg
!= NULL
) && (*(p
->t_arg
) != '\0'))
251 comret
= command ("TYPE %s %s", p
->t_mode
, p
->t_arg
);
253 comret
= command("TYPE %s", p
->t_mode
);
254 if (comret
== COMPLETE
) {
255 strlcpy(typename
, p
->t_name
, sizeof(typename
));
256 curtype
= type
= p
->t_type
;
261 * Internal form of settype; changes current type in use with server
262 * without changing our notion of the type for data transfers.
263 * Used to change to and from ascii for listings.
266 changetype(int newtype
, int show
)
269 int comret
, oldverbose
= verbose
;
273 if (newtype
== curtype
)
275 if (debug
== 0 && show
== 0)
277 for (p
= types
; p
->t_name
; p
++)
278 if (newtype
== p
->t_type
)
280 if (p
->t_name
== 0) {
281 printf("ftp: internal error: unknown type %d\n", newtype
);
284 if (newtype
== TYPE_L
&& bytename
[0] != '\0')
285 comret
= command("TYPE %s %s", p
->t_mode
, bytename
);
287 comret
= command("TYPE %s", p
->t_mode
);
288 if (comret
== COMPLETE
)
290 verbose
= oldverbose
;
300 * Set binary transfer type.
304 setbinary(int argc
, char **argv
)
312 * Set ascii transfer type.
316 setascii(int argc
, char **argv
)
324 * Set tenex transfer type.
328 settenex(int argc
, char **argv
)
336 * Set file transfer mode.
340 setftmode(int argc
, char **argv
)
343 printf("We only support %s mode, sorry.\n", modename
);
348 * Set file transfer format.
352 setform(int argc
, char **argv
)
355 printf("We only support %s format, sorry.\n", formname
);
360 * Set file transfer structure.
364 setstruct(int argc
, char **argv
)
367 printf("We only support %s structure, sorry.\n", structname
);
372 * Send a single file.
375 put(int argc
, char **argv
)
379 char *oldargv1
, *oldargv2
;
386 if (argc
< 2 && !another(&argc
, &argv
, "local-file"))
388 if (argc
< 3 && !another(&argc
, &argv
, "remote-file")) {
390 printf("usage: %s local-file remote-file\n", argv
[0]);
396 if (!globulize(&argv
[1])) {
401 * If "globulize" modifies argv[1], and argv[2] is a copy of
402 * the old argv[1], make it a copy of the new argv[1].
404 if (argv
[1] != oldargv1
&& argv
[2] == oldargv1
) {
407 cmd
= (argv
[0][0] == 'a') ? "APPE" : ((sunique
) ? "STOU" : "STOR");
409 argv
[2] = dotrans(argv
[2]);
411 if (loc
&& mapflag
) {
412 argv
[2] = domap(argv
[2]);
414 sendrequest(cmd
, argv
[1], argv
[2],
415 curtype
== TYPE_I
? "rb" : "r",
416 argv
[1] != oldargv1
|| argv
[2] != oldargv2
);
427 if (mflag
&& fromatty
) {
428 ointer
= interactive
;
430 if (confirm("Continue with", mname
)) {
431 interactive
= ointer
;
434 interactive
= ointer
;
441 * Send multiple files.
444 mput(int argc
, char **argv
)
447 RETSIGTYPE (*oldintr
)(int);
451 if (argc
< 2 && !another(&argc
, &argv
, "local-files")) {
452 printf("usage: %s local-files\n", argv
[0]);
458 oldintr
= signal(SIGINT
, mabort
);
461 char *cp
, *tp2
, tmpbuf
[MaxPathLen
];
463 while ((cp
= remglob(argv
,0)) != NULL
) {
468 if (mflag
&& confirm(argv
[0], cp
)) {
471 while (*tp
&& !islower((unsigned char)*tp
)) {
477 while ((*tp2
= *tp
) != '\0') {
478 if (isupper((unsigned char)*tp2
)) {
479 *tp2
= 'a' + *tp2
- 'A';
493 sendrequest((sunique
) ? "STOU" : "STOR",
495 curtype
== TYPE_I
? "rb" : "r",
496 cp
!= tp
|| !interactive
);
497 if (!mflag
&& fromatty
) {
498 ointer
= interactive
;
500 if (confirm("Continue with","mput")) {
503 interactive
= ointer
;
507 signal(SIGINT
, oldintr
);
511 for (i
= 1; i
< argc
; i
++) {
517 if (mflag
&& confirm(argv
[0], argv
[i
])) {
518 tp
= (ntflag
) ? dotrans(argv
[i
]) : argv
[i
];
519 tp
= (mapflag
) ? domap(tp
) : tp
;
520 sendrequest((sunique
) ? "STOU" : "STOR",
522 curtype
== TYPE_I
? "rb" : "r",
523 tp
, tp
!= argv
[i
] || !interactive
);
524 if (!mflag
&& fromatty
) {
525 ointer
= interactive
;
527 if (confirm("Continue with","mput")) {
530 interactive
= ointer
;
536 memset(&gl
, 0, sizeof(gl
));
537 flags
= GLOB_BRACE
|GLOB_NOCHECK
|GLOB_QUOTE
|GLOB_TILDE
;
538 if (glob(argv
[i
], flags
, NULL
, &gl
) || gl
.gl_pathc
== 0) {
539 warnx("%s: not found", argv
[i
]);
543 for (cpp
= gl
.gl_pathv
; cpp
&& *cpp
!= NULL
; cpp
++) {
544 if (mflag
&& confirm(argv
[0], *cpp
)) {
545 tp
= (ntflag
) ? dotrans(*cpp
) : *cpp
;
546 tp
= (mapflag
) ? domap(tp
) : tp
;
547 sendrequest((sunique
) ? "STOU" : "STOR",
549 curtype
== TYPE_I
? "rb" : "r",
550 *cpp
!= tp
|| !interactive
);
551 if (!mflag
&& fromatty
) {
552 ointer
= interactive
;
554 if (confirm("Continue with","mput")) {
557 interactive
= ointer
;
563 signal(SIGINT
, oldintr
);
568 reget(int argc
, char **argv
)
570 getit(argc
, argv
, 1, curtype
== TYPE_I
? "r+wb" : "r+w");
574 get(int argc
, char **argv
)
579 if (curtype
== TYPE_I
)
584 if (curtype
== TYPE_I
)
590 getit(argc
, argv
, 0, mode
);
597 getit(int argc
, char **argv
, int restartit
, char *mode
)
601 char *oldargv1
, *oldargv2
;
609 if ((argc
< 2 && !another(&argc
, &argv
, "remote-file")) ||
610 (argc
< 3 && !another(&argc
, &argv
, "local-file"))) {
611 printf("usage: %s remote-file [ local-file ]\n", argv
[0]);
617 if (!globulize(&argv
[2])) {
622 char *tp
= argv
[1], *tp2
, tmpbuf
[MaxPathLen
];
624 while (*tp
&& !islower((unsigned char)*tp
)) {
630 while ((*tp2
= *tp
) != '\0') {
631 if (isupper((unsigned char)*tp2
)) {
632 *tp2
= 'a' + *tp2
- 'A';
641 argv
[2] = dotrans(argv
[2]);
643 argv
[2] = domap(argv
[2]);
648 ret
= stat(argv
[2], &stbuf
);
649 if (restartit
== 1) {
651 warn("local: %s", argv
[2]);
654 restart_point
= stbuf
.st_size
;
655 } else if (ret
== 0) {
658 int yy
, mo
, day
, hour
, min
, sec
;
660 time_t mtime
= stbuf
.st_mtime
;
665 cmdret
= command("MDTM %s", argv
[1]);
667 if (cmdret
!= COMPLETE
) {
668 printf("%s\n", reply_string
);
671 if (sscanf(reply_string
,
672 "%*s %04d%02d%02d%02d%02d%02d",
673 &yy
, &mo
, &day
, &hour
, &min
, &sec
)
675 printf ("bad MDTM result\n");
683 if ((tm
->tm_year
> yy
) ||
684 (tm
->tm_year
== yy
&&
687 tm
->tm_mday
> day
) ||
688 (tm
->tm_mday
== day
&&
689 tm
->tm_hour
> hour
) ||
690 (tm
->tm_hour
== hour
&&
692 (tm
->tm_min
== min
&&
698 recvrequest("RETR", argv
[2], argv
[1], mode
,
699 argv
[1] != oldargv1
|| argv
[2] != oldargv2
, local_given
);
705 suspicious_filename(const char *fn
)
707 return strstr(fn
, "../") != NULL
|| *fn
== '/';
711 * Get multiple files.
714 mget(int argc
, char **argv
)
718 char *cp
, *tp
, *tp2
, tmpbuf
[MaxPathLen
];
720 if (argc
< 2 && !another(&argc
, &argv
, "remote-files")) {
721 printf("usage: %s remote-files\n", argv
[0]);
727 oldintr
= signal(SIGINT
, mabort
);
729 while ((cp
= remglob(argv
,proxy
)) != NULL
) {
734 if (mflag
&& suspicious_filename(cp
))
735 printf("*** Suspicious filename: %s\n", cp
);
736 if (mflag
&& confirm(argv
[0], cp
)) {
739 for (tp2
= tmpbuf
; (ch
= *tp
++);)
740 *tp2
++ = tolower(ch
);
750 recvrequest("RETR", tp
, cp
,
751 curtype
== TYPE_I
? "wb" : "w",
752 tp
!= cp
|| !interactive
, 0);
753 if (!mflag
&& fromatty
) {
754 ointer
= interactive
;
756 if (confirm("Continue with","mget")) {
759 interactive
= ointer
;
763 signal(SIGINT
,oldintr
);
768 remglob(char **argv
, int doswitch
)
771 static char buf
[MaxPathLen
];
772 static FILE *ftemp
= NULL
;
774 int oldverbose
, oldhash
;
792 if ((cp
= *++args
) == NULL
)
798 strlcpy(temp
, _PATH_TMP_XXX
, sizeof(temp
));
801 warn("unable to create temporary file %s", temp
);
805 oldverbose
= verbose
, verbose
= 0;
806 oldhash
= hash
, hash
= 0;
810 for (mode
= "w"; *++argv
!= NULL
; mode
= "a")
811 recvrequest ("NLST", temp
, *argv
, mode
, 0, 0);
815 verbose
= oldverbose
; hash
= oldhash
;
816 ftemp
= fopen(temp
, "r");
819 printf("can't find list of remote files, oops\n");
823 while(fgets(buf
, sizeof (buf
), ftemp
)) {
824 if ((cp
= strchr(buf
, '\n')) != NULL
)
826 if(!interactive
&& suspicious_filename(buf
)){
827 printf("Ignoring remote globbed file `%s'\n", buf
);
841 return (bool ? "on" : "off");
849 status(int argc
, char **argv
)
854 printf("Connected to %s.\n", hostname
);
856 printf("Not connected.\n");
860 printf("Connected for proxy commands to %s.\n", hostname
);
863 printf("No proxy connection.\n");
868 printf("Mode: %s; Type: %s; Form: %s; Structure: %s\n",
869 modename
, typename
, formname
, structname
);
870 printf("Verbose: %s; Bell: %s; Prompting: %s; Globbing: %s\n",
871 onoff(verbose
), onoff(bell
), onoff(interactive
),
873 printf("Store unique: %s; Receive unique: %s\n", onoff(sunique
),
875 printf("Case: %s; CR stripping: %s\n",onoff(mcase
),onoff(crflag
));
877 printf("Ntrans: (in) %s (out) %s\n", ntin
,ntout
);
880 printf("Ntrans: off\n");
883 printf("Nmap: (in) %s (out) %s\n", mapin
, mapout
);
886 printf("Nmap: off\n");
888 printf("Hash mark printing: %s; Use of PORT cmds: %s\n",
889 onoff(hash
), onoff(sendport
));
892 for (i
=0; i
<macnum
; i
++) {
893 printf("\t%s\n",macros
[i
].mac_name
);
900 * Set beep on cmd completed mode.
904 setbell(int argc
, char **argv
)
908 printf("Bell mode %s.\n", onoff(bell
));
913 * Turn on packet tracing.
917 settrace(int argc
, char **argv
)
921 printf("Packet tracing %s.\n", onoff(trace
));
926 * Toggle hash mark printing during transfers.
930 sethash(int argc
, char **argv
)
934 printf("Hash mark printing %s", onoff(hash
));
937 printf(" (%d bytes/hash mark)", 1024);
942 * Turn on printing of server echo's.
946 setverbose(int argc
, char **argv
)
950 printf("Verbose mode %s.\n", onoff(verbose
));
955 * Toggle PORT cmd use before each data connection.
959 setport(int argc
, char **argv
)
962 sendport
= !sendport
;
963 printf("Use of PORT cmds %s.\n", onoff(sendport
));
968 * Turn on interactive prompting
969 * during mget, mput, and mdelete.
973 setprompt(int argc
, char **argv
)
976 interactive
= !interactive
;
977 printf("Interactive mode %s.\n", onoff(interactive
));
982 * Toggle metacharacter interpretation
983 * on local file names.
987 setglob(int argc
, char **argv
)
991 printf("Globbing %s.\n", onoff(doglob
));
996 * Set debugging mode on/off and/or
997 * set level of debugging.
1001 setdebug(int argc
, char **argv
)
1006 val
= atoi(argv
[1]);
1008 printf("%s: bad debugging value.\n", argv
[1]);
1016 options
|= SO_DEBUG
;
1018 options
&= ~SO_DEBUG
;
1019 printf("Debugging %s (debug=%d).\n", onoff(debug
), debug
);
1024 * Set current working directory
1025 * on remote machine.
1028 cd(int argc
, char **argv
)
1031 if (argc
< 2 && !another(&argc
, &argv
, "remote-directory")) {
1032 printf("usage: %s remote-directory\n", argv
[0]);
1036 if (command("CWD %s", argv
[1]) == ERROR
&& code
== 500) {
1038 printf("CWD command not recognized, trying XCWD\n");
1039 command("XCWD %s", argv
[1]);
1044 * Set current working directory
1048 lcd(int argc
, char **argv
)
1050 char buf
[MaxPathLen
];
1053 argc
++, argv
[1] = home
;
1055 printf("usage: %s local-directory\n", argv
[0]);
1059 if (!globulize(&argv
[1])) {
1063 if (chdir(argv
[1]) < 0) {
1064 warn("local: %s", argv
[1]);
1068 if (getcwd(buf
, sizeof(buf
)) != NULL
)
1069 printf("Local directory now %s\n", buf
);
1071 warnx("getwd: %s", buf
);
1076 * Delete a single file.
1079 delete(int argc
, char **argv
)
1082 if (argc
< 2 && !another(&argc
, &argv
, "remote-file")) {
1083 printf("usage: %s remote-file\n", argv
[0]);
1087 command("DELE %s", argv
[1]);
1091 * Delete multiple files.
1094 mdelete(int argc
, char **argv
)
1100 if (argc
< 2 && !another(&argc
, &argv
, "remote-files")) {
1101 printf("usage: %s remote-files\n", argv
[0]);
1107 oldintr
= signal(SIGINT
, mabort
);
1109 while ((cp
= remglob(argv
,0)) != NULL
) {
1114 if (mflag
&& confirm(argv
[0], cp
)) {
1115 command("DELE %s", cp
);
1116 if (!mflag
&& fromatty
) {
1117 ointer
= interactive
;
1119 if (confirm("Continue with", "mdelete")) {
1122 interactive
= ointer
;
1126 signal(SIGINT
, oldintr
);
1131 * Rename a remote file.
1134 renamefile(int argc
, char **argv
)
1137 if (argc
< 2 && !another(&argc
, &argv
, "from-name"))
1139 if (argc
< 3 && !another(&argc
, &argv
, "to-name")) {
1141 printf("%s from-name to-name\n", argv
[0]);
1145 if (command("RNFR %s", argv
[1]) == CONTINUE
)
1146 command("RNTO %s", argv
[2]);
1150 * Get a directory listing
1154 ls(int argc
, char **argv
)
1159 argc
++, argv
[1] = NULL
;
1161 argc
++, argv
[2] = "-";
1163 printf("usage: %s remote-directory local-file\n", argv
[0]);
1167 cmd
= argv
[0][0] == 'n' ? "NLST" : "LIST";
1168 if (strcmp(argv
[2], "-") && !globulize(&argv
[2])) {
1172 if (strcmp(argv
[2], "-") && *argv
[2] != '|')
1173 if (!globulize(&argv
[2]) || !confirm("output to local-file:",
1178 recvrequest(cmd
, argv
[2], argv
[1], "w", 0, 1);
1182 * Get a directory listing
1183 * of multiple remote files.
1186 mls(int argc
, char **argv
)
1190 char *cmd
, mode
[1], *dest
;
1192 if (argc
< 2 && !another(&argc
, &argv
, "remote-files"))
1194 if (argc
< 3 && !another(&argc
, &argv
, "local-file")) {
1196 printf("usage: %s remote-files local-file\n", argv
[0]);
1200 dest
= argv
[argc
- 1];
1201 argv
[argc
- 1] = NULL
;
1202 if (strcmp(dest
, "-") && *dest
!= '|')
1203 if (!globulize(&dest
) ||
1204 !confirm("output to local-file:", dest
)) {
1208 cmd
= argv
[0][1] == 'l' ? "NLST" : "LIST";
1211 oldintr
= signal(SIGINT
, mabort
);
1213 for (i
= 1; mflag
&& i
< argc
-1; ++i
) {
1214 *mode
= (i
== 1) ? 'w' : 'a';
1215 recvrequest(cmd
, dest
, argv
[i
], mode
, 0, 1);
1216 if (!mflag
&& fromatty
) {
1217 ointer
= interactive
;
1219 if (confirm("Continue with", argv
[0])) {
1222 interactive
= ointer
;
1225 signal(SIGINT
, oldintr
);
1234 shell(int argc
, char **argv
)
1237 RETSIGTYPE (*old1
)(int), (*old2
)(int);
1238 char shellnam
[40], *shell
, *namep
;
1241 old1
= signal (SIGINT
, SIG_IGN
);
1242 old2
= signal (SIGQUIT
, SIG_IGN
);
1243 if ((pid
= fork()) == 0) {
1244 for (pid
= 3; pid
< 20; pid
++)
1246 signal(SIGINT
, SIG_DFL
);
1247 signal(SIGQUIT
, SIG_DFL
);
1248 shell
= getenv("SHELL");
1250 shell
= _PATH_BSHELL
;
1251 namep
= strrchr(shell
,'/');
1254 snprintf (shellnam
, sizeof(shellnam
),
1256 if (strcmp(namep
, "sh") != 0)
1259 printf ("%s\n", shell
);
1263 execl(shell
,shellnam
,"-c",altarg
,(char *)0);
1266 execl(shell
,shellnam
,(char *)0);
1273 while (waitpid(-1, &status
, 0) != pid
)
1275 signal(SIGINT
, old1
);
1276 signal(SIGQUIT
, old2
);
1278 warn("%s", "Try again later");
1287 * Send new user information (re-login)
1290 user(int argc
, char **argv
)
1297 another(&argc
, &argv
, "username");
1298 if (argc
< 2 || argc
> 4) {
1299 printf("usage: %s username [password] [account]\n", argv
[0]);
1303 n
= command("USER %s", argv
[1]);
1304 if (n
== CONTINUE
) {
1306 des_read_pw_string (tmp
,
1312 n
= command("PASS %s", argv
[2]);
1314 if (n
== CONTINUE
) {
1316 printf("Account: "); fflush(stdout
);
1317 fgets(acct
, sizeof(acct
) - 1, stdin
);
1318 acct
[strlen(acct
) - 1] = '\0';
1319 argv
[3] = acct
; argc
++;
1321 n
= command("ACCT %s", argv
[3]);
1324 if (n
!= COMPLETE
) {
1325 fprintf(stdout
, "Login failed.\n");
1328 if (!aflag
&& argc
== 4) {
1329 command("ACCT %s", argv
[3]);
1334 * Print working directory.
1338 pwd(int argc
, char **argv
)
1340 int oldverbose
= verbose
;
1343 * If we aren't verbose, this doesn't do anything!
1346 if (command("PWD") == ERROR
&& code
== 500) {
1347 printf("PWD command not recognized, trying XPWD\n");
1350 verbose
= oldverbose
;
1357 makedir(int argc
, char **argv
)
1360 if (argc
< 2 && !another(&argc
, &argv
, "directory-name")) {
1361 printf("usage: %s directory-name\n", argv
[0]);
1365 if (command("MKD %s", argv
[1]) == ERROR
&& code
== 500) {
1367 printf("MKD command not recognized, trying XMKD\n");
1368 command("XMKD %s", argv
[1]);
1373 * Remove a directory.
1376 removedir(int argc
, char **argv
)
1379 if (argc
< 2 && !another(&argc
, &argv
, "directory-name")) {
1380 printf("usage: %s directory-name\n", argv
[0]);
1384 if (command("RMD %s", argv
[1]) == ERROR
&& code
== 500) {
1386 printf("RMD command not recognized, trying XRMD\n");
1387 command("XRMD %s", argv
[1]);
1392 * Send a line, verbatim, to the remote machine.
1395 quote(int argc
, char **argv
)
1398 if (argc
< 2 && !another(&argc
, &argv
, "command line to send")) {
1399 printf("usage: %s line-to-send\n", argv
[0]);
1403 quote1("", argc
, argv
);
1407 * Send a SITE command to the remote machine. The line
1408 * is sent verbatim to the remote machine, except that the
1409 * word "SITE" is added at the front.
1412 site(int argc
, char **argv
)
1415 if (argc
< 2 && !another(&argc
, &argv
, "arguments to SITE command")) {
1416 printf("usage: %s line-to-send\n", argv
[0]);
1420 quote1("SITE ", argc
, argv
);
1424 * Turn argv[1..argc) into a space-separated string, then prepend initial text.
1425 * Send the result as a one-line command and get response.
1428 quote1(char *initial
, int argc
, char **argv
)
1431 char buf
[BUFSIZ
]; /* must be >= sizeof(line) */
1433 strlcpy(buf
, initial
, sizeof(buf
));
1434 for(i
= 1; i
< argc
; i
++) {
1436 strlcat(buf
, " ", sizeof(buf
));
1437 strlcat(buf
, argv
[i
], sizeof(buf
));
1439 if (command("%s", buf
) == PRELIM
) {
1440 while (getreply(0) == PRELIM
)
1446 do_chmod(int argc
, char **argv
)
1449 if (argc
< 2 && !another(&argc
, &argv
, "mode"))
1451 if (argc
< 3 && !another(&argc
, &argv
, "file-name")) {
1453 printf("usage: %s mode file-name\n", argv
[0]);
1457 command("SITE CHMOD %s %s", argv
[1], argv
[2]);
1461 do_umask(int argc
, char **argv
)
1463 int oldverbose
= verbose
;
1466 command(argc
== 1 ? "SITE UMASK" : "SITE UMASK %s", argv
[1]);
1467 verbose
= oldverbose
;
1471 ftp_idle(int argc
, char **argv
)
1473 int oldverbose
= verbose
;
1476 command(argc
== 1 ? "SITE IDLE" : "SITE IDLE %s", argv
[1]);
1477 verbose
= oldverbose
;
1481 * Ask the other side for help.
1484 rmthelp(int argc
, char **argv
)
1486 int oldverbose
= verbose
;
1489 command(argc
== 1 ? "HELP" : "HELP %s", argv
[1]);
1490 verbose
= oldverbose
;
1494 * Terminate session and exit.
1498 quit(int argc
, char **argv
)
1511 * Terminate session, but don't exit.
1514 disconnect(int argc
, char **argv
)
1533 confirm(char *cmd
, char *file
)
1539 printf("%s %s? ", cmd
, file
);
1541 if (fgets(line
, sizeof line
, stdin
) == NULL
)
1543 return (*line
== 'y' || *line
== 'Y');
1554 * Glob a local file name specification with
1555 * the expectation of a single return value.
1556 * Can't control multiple values being expanded
1557 * from the expression, we return only the first.
1560 globulize(char **cpp
)
1568 flags
= GLOB_BRACE
|GLOB_NOCHECK
|GLOB_QUOTE
|GLOB_TILDE
;
1569 memset(&gl
, 0, sizeof(gl
));
1570 if (glob(*cpp
, flags
, NULL
, &gl
) ||
1572 warnx("%s: not found", *cpp
);
1576 *cpp
= strdup(gl
.gl_pathv
[0]); /* XXX - wasted memory */
1582 account(int argc
, char **argv
)
1589 strlcpy (acct
, *argv
, sizeof(acct
));
1593 strlcat(acct
, *argv
, sizeof(acct
));
1597 des_read_pw_string(acct
, sizeof(acct
), "Account:", 0);
1599 command("ACCT %s", acct
);
1618 longjmp(abortprox
,1);
1622 doproxy(int argc
, char **argv
)
1625 RETSIGTYPE (*oldintr
)(int);
1627 if (argc
< 2 && !another(&argc
, &argv
, "command")) {
1628 printf("usage: %s command\n", argv
[0]);
1632 c
= getcmd(argv
[1]);
1633 if (c
== (struct cmd
*) -1) {
1634 printf("?Ambiguous command\n");
1640 printf("?Invalid command\n");
1646 printf("?Invalid proxy command\n");
1651 if (setjmp(abortprox
)) {
1655 oldintr
= signal(SIGINT
, proxabort
);
1657 if (c
->c_conn
&& !connected
) {
1658 printf("Not connected\n");
1661 signal(SIGINT
, oldintr
);
1665 (*c
->c_handler
)(argc
-1, argv
+1);
1673 signal(SIGINT
, oldintr
);
1677 setcase(int argc
, char **argv
)
1681 printf("Case mapping %s.\n", onoff(mcase
));
1686 setcr(int argc
, char **argv
)
1690 printf("Carriage Return stripping %s.\n", onoff(crflag
));
1695 setntrans(int argc
, char **argv
)
1699 printf("Ntrans off.\n");
1705 strlcpy (ntin
, argv
[1], 17);
1710 strlcpy (ntout
, argv
[2], 17);
1716 static char new[MaxPathLen
];
1717 char *cp1
, *cp2
= new;
1718 int i
, ostop
, found
;
1720 for (ostop
= 0; *(ntout
+ ostop
) && ostop
< 16; ostop
++)
1722 for (cp1
= name
; *cp1
; cp1
++) {
1724 for (i
= 0; *(ntin
+ i
) && i
< 16; i
++) {
1725 if (*cp1
== *(ntin
+ i
)) {
1728 *cp2
++ = *(ntout
+ i
);
1742 setnmap(int argc
, char **argv
)
1748 printf("Nmap off.\n");
1752 if (argc
< 3 && !another(&argc
, &argv
, "mapout")) {
1753 printf("Usage: %s [mapin mapout]\n",argv
[0]);
1759 cp
= strchr(altarg
, ' ');
1764 cp
= strchr(altarg
, ' ');
1767 strlcpy(mapin
, altarg
, MaxPathLen
);
1768 while (*++cp
== ' ')
1770 strlcpy(mapout
, cp
, MaxPathLen
);
1776 static char new[MaxPathLen
];
1777 char *cp1
= name
, *cp2
= mapin
;
1778 char *tp
[9], *te
[9];
1779 int i
, toks
[9], toknum
= 0, match
= 1;
1781 for (i
=0; i
< 9; ++i
) {
1784 while (match
&& *cp1
&& *cp2
) {
1787 if (*++cp2
!= *cp1
) {
1792 if (*(cp2
+1) >= '1' && (*cp2
+1) <= '9') {
1793 if (*cp1
!= *(++cp2
+1)) {
1794 toks
[toknum
= *cp2
- '1']++;
1796 while (*++cp1
&& *(cp2
+1)
1810 if (match
&& *cp1
) {
1813 if (match
&& *cp2
) {
1817 if (!match
&& *cp1
) /* last token mismatch */
1834 if (*++cp2
== '$' && isdigit((unsigned char)*(cp2
+1))) {
1835 if (*++cp2
== '0') {
1843 else if (toks
[toknum
= *cp2
- '1']) {
1844 char *cp3
= tp
[toknum
];
1846 while (cp3
!= te
[toknum
]) {
1853 while (*cp2
&& *cp2
!= ',' &&
1858 else if (*cp2
== '$' &&
1859 isdigit((unsigned char)*(cp2
+1))) {
1860 if (*++cp2
== '0') {
1867 else if (toks
[toknum
=
1869 char *cp3
=tp
[toknum
];
1882 printf("nmap: unbalanced brackets\n");
1889 while (*++cp2
&& *cp2
!= ']') {
1890 if (*cp2
== '\\' && *(cp2
+ 1)) {
1895 printf("nmap: unbalanced brackets\n");
1911 if (isdigit((unsigned char)*(cp2
+ 1))) {
1912 if (*++cp2
== '0') {
1919 else if (toks
[toknum
= *cp2
- '1']) {
1920 char *cp3
= tp
[toknum
];
1922 while (cp3
!= te
[toknum
]) {
1928 /* intentional drop through */
1943 setpassive(int argc
, char **argv
)
1946 passivemode
= !passivemode
;
1947 printf("Passive mode %s.\n", onoff(passivemode
));
1952 setsunique(int argc
, char **argv
)
1956 printf("Store unique %s.\n", onoff(sunique
));
1961 setrunique(int argc
, char **argv
)
1965 printf("Receive unique %s.\n", onoff(runique
));
1969 /* change directory to perent directory */
1971 cdup(int argc
, char **argv
)
1974 if (command("CDUP") == ERROR
&& code
== 500) {
1976 printf("CDUP command not recognized, trying XCUP\n");
1981 /* restart transfer at specific point */
1983 restart(int argc
, char **argv
)
1987 printf("restart: offset not specified\n");
1989 restart_point
= atol(argv
[1]);
1990 printf("restarting at %ld. %s\n", (long)restart_point
,
1991 "execute get, put or append to initiate transfer");
1995 /* show remote system type */
1997 syst(int argc
, char **argv
)
2004 macdef(int argc
, char **argv
)
2010 printf("Limit of 16 macros have already been defined\n");
2014 if (argc
< 2 && !another(&argc
, &argv
, "macro name")) {
2015 printf("Usage: %s macro_name\n",argv
[0]);
2020 printf("Enter macro line by line, terminating it with a null line\n");
2022 strlcpy(macros
[macnum
].mac_name
,
2024 sizeof(macros
[macnum
].mac_name
));
2026 macros
[macnum
].mac_start
= macbuf
;
2029 macros
[macnum
].mac_start
= macros
[macnum
- 1].mac_end
+ 1;
2031 tmp
= macros
[macnum
].mac_start
;
2032 while (tmp
!= macbuf
+4096) {
2033 if ((c
= getchar()) == EOF
) {
2034 printf("macdef:end of file encountered\n");
2038 if ((*tmp
= c
) == '\n') {
2039 if (tmp
== macros
[macnum
].mac_start
) {
2040 macros
[macnum
++].mac_end
= tmp
;
2044 if (*(tmp
-1) == '\0') {
2045 macros
[macnum
++].mac_end
= tmp
- 1;
2054 while ((c
= getchar()) != '\n' && c
!= EOF
)
2056 if (c
== EOF
|| getchar() == '\n') {
2057 printf("Macro not defined - 4k buffer exceeded\n");
2065 * get size of file on remote machine
2068 sizecmd(int argc
, char **argv
)
2071 if (argc
< 2 && !another(&argc
, &argv
, "filename")) {
2072 printf("usage: %s filename\n", argv
[0]);
2076 command("SIZE %s", argv
[1]);
2080 * get last modification time of file on remote machine
2083 modtime(int argc
, char **argv
)
2087 if (argc
< 2 && !another(&argc
, &argv
, "filename")) {
2088 printf("usage: %s filename\n", argv
[0]);
2095 if (command("MDTM %s", argv
[1]) == COMPLETE
) {
2096 int yy
, mo
, day
, hour
, min
, sec
;
2097 sscanf(reply_string
, "%*s %04d%02d%02d%02d%02d%02d", &yy
, &mo
,
2098 &day
, &hour
, &min
, &sec
);
2099 /* might want to print this in local time */
2100 printf("%s\t%02d/%02d/%04d %02d:%02d:%02d GMT\n", argv
[1],
2101 mo
, day
, yy
, hour
, min
, sec
);
2103 printf("%s\n", reply_string
);
2108 * show status on reomte machine
2111 rmtstatus(int argc
, char **argv
)
2114 command(argc
> 1 ? "STAT %s" : "STAT" , argv
[1]);
2118 * get file if modtime is more recent than current file
2121 newer(int argc
, char **argv
)
2124 if (getit(argc
, argv
, -1, curtype
== TYPE_I
? "wb" : "w"))
2125 printf("Local file \"%s\" is newer than remote file \"%s\"\n",