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.36 1999/09/16 20:37:28 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 port = atoi(argv[2]);
107 printf("%s: bad port number-- %s\n", argv[1], argv[2]);
108 printf ("usage: %s host-name [port]\n", argv[0]);
114 host = hookup(argv[1], port);
120 * Set up defaults for FTP.
122 strlcpy(typename, "ascii", sizeof(typename));
125 strlcpy(formname, "non-print", sizeof(formname));
127 strlcpy(modename, "stream", sizeof(modename));
129 strlcpy(structname, "file", sizeof(structname));
131 strlcpy(bytename, "8", sizeof(bytename));
136 #if (defined(unix) || defined(__unix__) || defined(__unix) || defined(_AIX) || defined(_CRAY)) && NBBY == 8
138 * this ifdef is to keep someone form "porting" this to an incompatible
139 * system and not checking this out. This way they have to think about it.
144 if (command("SYST") == COMPLETE && overbose) {
146 cp = strchr(reply_string+4, ' ');
148 cp = strchr(reply_string+4, '\r');
156 printf("Remote system type is %s.\n",
161 if (!strncmp(reply_string, "215 UNIX Type: L8", 17)) {
167 * Set type to 0 (not specified by user),
168 * meaning binary by default, but don't bother
169 * telling server. We can use binary
170 * for text files unless changed by the user.
173 strlcpy(typename, "binary", sizeof(typename));
175 printf("Using %s mode to transfer files.\n",
183 !strncmp(reply_string, "215 TOPS20", 10))
185 "Remember to set tenex mode when transfering binary files from this machine.\n");
198 { "ascii", "A", TYPE_A, 0 },
199 { "binary", "I", TYPE_I, 0 },
200 { "image", "I", TYPE_I, 0 },
201 { "ebcdic", "E", TYPE_E, 0 },
202 { "tenex", "L", TYPE_L, bytename },
210 settype(int argc, char **argv)
218 printf("usage: %s [", argv[0]);
220 for (p = types; p->t_name; p++) {
221 printf("%s%s", sep, p->t_name);
229 printf("Using %s mode to transfer files.\n", typename);
233 for (p = types; p->t_name; p++)
234 if (strcmp(argv[1], p->t_name) == 0)
236 if (p->t_name == 0) {
237 printf("%s: unknown mode\n", argv[1]);
241 if ((p->t_arg != NULL) && (*(p->t_arg) != '\0'))
242 comret = command ("TYPE %s %s", p->t_mode, p->t_arg);
244 comret = command("TYPE %s", p->t_mode);
245 if (comret == COMPLETE) {
246 strlcpy(typename, p->t_name, sizeof(typename));
247 curtype = type = p->t_type;
252 * Internal form of settype; changes current type in use with server
253 * without changing our notion of the type for data transfers.
254 * Used to change to and from ascii for listings.
257 changetype(int newtype, int show)
260 int comret, oldverbose = verbose;
264 if (newtype == curtype)
266 if (debug == 0 && show == 0)
268 for (p = types; p->t_name; p++)
269 if (newtype == p->t_type)
271 if (p->t_name == 0) {
272 printf("ftp: internal error: unknown type %d\n", newtype);
275 if (newtype == TYPE_L && bytename[0] != '\0')
276 comret = command("TYPE %s %s", p->t_mode, bytename);
278 comret = command("TYPE %s", p->t_mode);
279 if (comret == COMPLETE)
281 verbose = oldverbose;
291 * Set binary transfer type.
295 setbinary(int argc, char **argv)
303 * Set ascii transfer type.
307 setascii(int argc, char **argv)
315 * Set tenex transfer type.
319 settenex(int argc, char **argv)
327 * Set file transfer mode.
331 setftmode(int argc, char **argv)
334 printf("We only support %s mode, sorry.\n", modename);
339 * Set file transfer format.
343 setform(int argc, char **argv)
346 printf("We only support %s format, sorry.\n", formname);
351 * Set file transfer structure.
355 setstruct(int argc, char **argv)
358 printf("We only support %s structure, sorry.\n", structname);
363 * Send a single file.
366 put(int argc, char **argv)
370 char *oldargv1, *oldargv2;
377 if (argc < 2 && !another(&argc, &argv, "local-file"))
379 if (argc < 3 && !another(&argc, &argv, "remote-file")) {
381 printf("usage: %s local-file remote-file\n", argv[0]);
387 if (!globulize(&argv[1])) {
392 * If "globulize" modifies argv[1], and argv[2] is a copy of
393 * the old argv[1], make it a copy of the new argv[1].
395 if (argv[1] != oldargv1 && argv[2] == oldargv1) {
398 cmd = (argv[0][0] == 'a') ? "APPE" : ((sunique) ? "STOU" : "STOR");
400 argv[2] = dotrans(argv[2]);
402 if (loc && mapflag) {
403 argv[2] = domap(argv[2]);
405 sendrequest(cmd, argv[1], argv[2],
406 curtype == TYPE_I ? "rb" : "r",
407 argv[1] != oldargv1 || argv[2] != oldargv2);
418 if (mflag && fromatty) {
419 ointer = interactive;
421 if (confirm("Continue with", mname)) {
422 interactive = ointer;
425 interactive = ointer;
432 * Send multiple files.
435 mput(int argc, char **argv)
438 RETSIGTYPE (*oldintr)();
442 if (argc < 2 && !another(&argc, &argv, "local-files")) {
443 printf("usage: %s local-files\n", argv[0]);
449 oldintr = signal(SIGINT, mabort);
452 char *cp, *tp2, tmpbuf[MaxPathLen];
454 while ((cp = remglob(argv,0)) != NULL) {
459 if (mflag && confirm(argv[0], cp)) {
462 while (*tp && !islower(*tp)) {
468 while ((*tp2 = *tp) != '\0') {
470 *tp2 = 'a' + *tp2 - 'A';
484 sendrequest((sunique) ? "STOU" : "STOR",
486 curtype == TYPE_I ? "rb" : "r",
487 cp != tp || !interactive);
488 if (!mflag && fromatty) {
489 ointer = interactive;
491 if (confirm("Continue with","mput")) {
494 interactive = ointer;
498 signal(SIGINT, oldintr);
502 for (i = 1; i < argc; i++) {
508 if (mflag && confirm(argv[0], argv[i])) {
509 tp = (ntflag) ? dotrans(argv[i]) : argv[i];
510 tp = (mapflag) ? domap(tp) : tp;
511 sendrequest((sunique) ? "STOU" : "STOR",
513 curtype == TYPE_I ? "rb" : "r",
514 tp, tp != argv[i] || !interactive);
515 if (!mflag && fromatty) {
516 ointer = interactive;
518 if (confirm("Continue with","mput")) {
521 interactive = ointer;
527 memset(&gl, 0, sizeof(gl));
528 flags = GLOB_BRACE|GLOB_NOCHECK|GLOB_QUOTE|GLOB_TILDE;
529 if (glob(argv[i], flags, NULL, &gl) || gl.gl_pathc == 0) {
530 warnx("%s: not found", argv[i]);
534 for (cpp = gl.gl_pathv; cpp && *cpp != NULL; cpp++) {
535 if (mflag && confirm(argv[0], *cpp)) {
536 tp = (ntflag) ? dotrans(*cpp) : *cpp;
537 tp = (mapflag) ? domap(tp) : tp;
538 sendrequest((sunique) ? "STOU" : "STOR",
540 curtype == TYPE_I ? "rb" : "r",
541 *cpp != tp || !interactive);
542 if (!mflag && fromatty) {
543 ointer = interactive;
545 if (confirm("Continue with","mput")) {
548 interactive = ointer;
554 signal(SIGINT, oldintr);
559 reget(int argc, char **argv)
561 getit(argc, argv, 1, curtype == TYPE_I ? "r+wb" : "r+w");
565 get(int argc, char **argv)
570 if (curtype == TYPE_I)
575 if (curtype == TYPE_I)
580 getit(argc, argv, 0, mode);
587 getit(int argc, char **argv, int restartit, char *mode)
591 char *oldargv1, *oldargv2;
599 if ((argc < 2 && !another(&argc, &argv, "remote-file")) ||
600 (argc < 3 && !another(&argc, &argv, "local-file"))) {
601 printf("usage: %s remote-file [ local-file ]\n", argv[0]);
607 if (!globulize(&argv[2])) {
612 char *tp = argv[1], *tp2, tmpbuf[MaxPathLen];
614 while (*tp && !islower(*tp)) {
620 while ((*tp2 = *tp) != '\0') {
622 *tp2 = 'a' + *tp2 - 'A';
631 argv[2] = dotrans(argv[2]);
633 argv[2] = domap(argv[2]);
638 ret = stat(argv[2], &stbuf);
639 if (restartit == 1) {
641 warn("local: %s", argv[2]);
644 restart_point = stbuf.st_size;
645 } else if (ret == 0) {
648 int yy, mo, day, hour, min, sec;
654 cmdret = command("MDTM %s", argv[1]);
656 if (cmdret != COMPLETE) {
657 printf("%s\n", reply_string);
660 if (sscanf(reply_string,
661 "%*s %04d%02d%02d%02d%02d%02d",
662 &yy, &mo, &day, &hour, &min, &sec)
664 printf ("bad MDTM result\n");
668 tm = gmtime(&stbuf.st_mtime);
672 if ((tm->tm_year > yy) ||
673 (tm->tm_year == yy &&
676 tm->tm_mday > day) ||
677 (tm->tm_mday == day &&
678 tm->tm_hour > hour) ||
679 (tm->tm_hour == hour &&
681 (tm->tm_min == min &&
687 recvrequest("RETR", argv[2], argv[1], mode,
688 argv[1] != oldargv1 || argv[2] != oldargv2, local_given);
694 suspicious_filename(const char *fn)
696 return strstr(fn, "../") != NULL || *fn == '/';
700 * Get multiple files.
703 mget(int argc, char **argv)
707 char *cp, *tp, *tp2, tmpbuf[MaxPathLen];
709 if (argc < 2 && !another(&argc, &argv, "remote-files")) {
710 printf("usage: %s remote-files\n", argv[0]);
716 oldintr = signal(SIGINT, mabort);
718 while ((cp = remglob(argv,proxy)) != NULL) {
723 if (mflag && suspicious_filename(cp))
724 printf("*** Suspicious filename: %s\n", cp);
725 if (mflag && confirm(argv[0], cp)) {
728 for (tp2 = tmpbuf; (ch = *tp++);)
729 *tp2++ = isupper(ch) ? tolower(ch) : ch;
739 recvrequest("RETR", tp, cp,
740 curtype == TYPE_I ? "wb" : "w",
741 tp != cp || !interactive, 0);
742 if (!mflag && fromatty) {
743 ointer = interactive;
745 if (confirm("Continue with","mget")) {
748 interactive = ointer;
752 signal(SIGINT,oldintr);
757 remglob(char **argv, int doswitch)
760 static char buf[MaxPathLen];
761 static FILE *ftemp = NULL;
763 int oldverbose, oldhash;
781 if ((cp = *++args) == NULL)
787 strlcpy(temp, _PATH_TMP_XXX, sizeof(temp));
790 warn("unable to create temporary file %s", temp);
794 oldverbose = verbose, verbose = 0;
795 oldhash = hash, hash = 0;
799 for (mode = "w"; *++argv != NULL; mode = "a")
800 recvrequest ("NLST", temp, *argv, mode, 0, 0);
804 verbose = oldverbose; hash = oldhash;
805 ftemp = fopen(temp, "r");
808 printf("can't find list of remote files, oops\n");
812 while(fgets(buf, sizeof (buf), ftemp)) {
813 if ((cp = strchr(buf, '\n')) != NULL)
815 if(!interactive && suspicious_filename(buf)){
816 printf("Ignoring remote globbed file `%s'\n", buf);
830 return (bool ? "on" : "off");
838 status(int argc, char **argv)
843 printf("Connected to %s.\n", hostname);
845 printf("Not connected.\n");
849 printf("Connected for proxy commands to %s.\n", hostname);
852 printf("No proxy connection.\n");
857 printf("Mode: %s; Type: %s; Form: %s; Structure: %s\n",
858 modename, typename, formname, structname);
859 printf("Verbose: %s; Bell: %s; Prompting: %s; Globbing: %s\n",
860 onoff(verbose), onoff(bell), onoff(interactive),
862 printf("Store unique: %s; Receive unique: %s\n", onoff(sunique),
864 printf("Case: %s; CR stripping: %s\n",onoff(mcase),onoff(crflag));
866 printf("Ntrans: (in) %s (out) %s\n", ntin,ntout);
869 printf("Ntrans: off\n");
872 printf("Nmap: (in) %s (out) %s\n", mapin, mapout);
875 printf("Nmap: off\n");
877 printf("Hash mark printing: %s; Use of PORT cmds: %s\n",
878 onoff(hash), onoff(sendport));
881 for (i=0; i<macnum; i++) {
882 printf("\t%s\n",macros[i].mac_name);
889 * Set beep on cmd completed mode.
893 setbell(int argc, char **argv)
897 printf("Bell mode %s.\n", onoff(bell));
902 * Turn on packet tracing.
906 settrace(int argc, char **argv)
910 printf("Packet tracing %s.\n", onoff(trace));
915 * Toggle hash mark printing during transfers.
919 sethash(int argc, char **argv)
923 printf("Hash mark printing %s", onoff(hash));
926 printf(" (%d bytes/hash mark)", 1024);
931 * Turn on printing of server echo's.
935 setverbose(int argc, char **argv)
939 printf("Verbose mode %s.\n", onoff(verbose));
944 * Toggle PORT cmd use before each data connection.
948 setport(int argc, char **argv)
951 sendport = !sendport;
952 printf("Use of PORT cmds %s.\n", onoff(sendport));
957 * Turn on interactive prompting
958 * during mget, mput, and mdelete.
962 setprompt(int argc, char **argv)
965 interactive = !interactive;
966 printf("Interactive mode %s.\n", onoff(interactive));
971 * Toggle metacharacter interpretation
972 * on local file names.
976 setglob(int argc, char **argv)
980 printf("Globbing %s.\n", onoff(doglob));
985 * Set debugging mode on/off and/or
986 * set level of debugging.
990 setdebug(int argc, char **argv)
997 printf("%s: bad debugging value.\n", argv[1]);
1005 options |= SO_DEBUG;
1007 options &= ~SO_DEBUG;
1008 printf("Debugging %s (debug=%d).\n", onoff(debug), debug);
1013 * Set current working directory
1014 * on remote machine.
1017 cd(int argc, char **argv)
1020 if (argc < 2 && !another(&argc, &argv, "remote-directory")) {
1021 printf("usage: %s remote-directory\n", argv[0]);
1025 if (command("CWD %s", argv[1]) == ERROR && code == 500) {
1027 printf("CWD command not recognized, trying XCWD\n");
1028 command("XCWD %s", argv[1]);
1033 * Set current working directory
1037 lcd(int argc, char **argv)
1039 char buf[MaxPathLen];
1042 argc++, argv[1] = home;
1044 printf("usage: %s local-directory\n", argv[0]);
1048 if (!globulize(&argv[1])) {
1052 if (chdir(argv[1]) < 0) {
1053 warn("local: %s", argv[1]);
1057 if (getcwd(buf, sizeof(buf)) != NULL)
1058 printf("Local directory now %s\n", buf);
1060 warnx("getwd: %s", buf);
1065 * Delete a single file.
1068 delete(int argc, char **argv)
1071 if (argc < 2 && !another(&argc, &argv, "remote-file")) {
1072 printf("usage: %s remote-file\n", argv[0]);
1076 command("DELE %s", argv[1]);
1080 * Delete multiple files.
1083 mdelete(int argc, char **argv)
1089 if (argc < 2 && !another(&argc, &argv, "remote-files")) {
1090 printf("usage: %s remote-files\n", argv[0]);
1096 oldintr = signal(SIGINT, mabort);
1098 while ((cp = remglob(argv,0)) != NULL) {
1103 if (mflag && confirm(argv[0], cp)) {
1104 command("DELE %s", cp);
1105 if (!mflag && fromatty) {
1106 ointer = interactive;
1108 if (confirm("Continue with", "mdelete")) {
1111 interactive = ointer;
1115 signal(SIGINT, oldintr);
1120 * Rename a remote file.
1123 renamefile(int argc, char **argv)
1126 if (argc < 2 && !another(&argc, &argv, "from-name"))
1128 if (argc < 3 && !another(&argc, &argv, "to-name")) {
1130 printf("%s from-name to-name\n", argv[0]);
1134 if (command("RNFR %s", argv[1]) == CONTINUE)
1135 command("RNTO %s", argv[2]);
1139 * Get a directory listing
1143 ls(int argc, char **argv)
1148 argc++, argv[1] = NULL;
1150 argc++, argv[2] = "-";
1152 printf("usage: %s remote-directory local-file\n", argv[0]);
1156 cmd = argv[0][0] == 'n' ? "NLST" : "LIST";
1157 if (strcmp(argv[2], "-") && !globulize(&argv[2])) {
1161 if (strcmp(argv[2], "-") && *argv[2] != '|')
1162 if (!globulize(&argv[2]) || !confirm("output to local-file:",
1167 recvrequest(cmd, argv[2], argv[1], "w", 0, 1);
1171 * Get a directory listing
1172 * of multiple remote files.
1175 mls(int argc, char **argv)
1179 char *cmd, mode[1], *dest;
1181 if (argc < 2 && !another(&argc, &argv, "remote-files"))
1183 if (argc < 3 && !another(&argc, &argv, "local-file")) {
1185 printf("usage: %s remote-files local-file\n", argv[0]);
1189 dest = argv[argc - 1];
1190 argv[argc - 1] = NULL;
1191 if (strcmp(dest, "-") && *dest != '|')
1192 if (!globulize(&dest) ||
1193 !confirm("output to local-file:", dest)) {
1197 cmd = argv[0][1] == 'l' ? "NLST" : "LIST";
1200 oldintr = signal(SIGINT, mabort);
1202 for (i = 1; mflag && i < argc-1; ++i) {
1203 *mode = (i == 1) ? 'w' : 'a';
1204 recvrequest(cmd, dest, argv[i], mode, 0, 1);
1205 if (!mflag && fromatty) {
1206 ointer = interactive;
1208 if (confirm("Continue with", argv[0])) {
1211 interactive = ointer;
1214 signal(SIGINT, oldintr);
1223 shell(int argc, char **argv)
1226 RETSIGTYPE (*old1)(), (*old2)();
1227 char shellnam[40], *shell, *namep;
1230 old1 = signal (SIGINT, SIG_IGN);
1231 old2 = signal (SIGQUIT, SIG_IGN);
1232 if ((pid = fork()) == 0) {
1233 for (pid = 3; pid < 20; pid++)
1235 signal(SIGINT, SIG_DFL);
1236 signal(SIGQUIT, SIG_DFL);
1237 shell = getenv("SHELL");
1239 shell = _PATH_BSHELL;
1240 namep = strrchr(shell,'/');
1243 snprintf (shellnam, sizeof(shellnam),
1245 if (strcmp(namep, "sh") != 0)
1248 printf ("%s\n", shell);
1252 execl(shell,shellnam,"-c",altarg,(char *)0);
1255 execl(shell,shellnam,(char *)0);
1262 while (waitpid(-1, &status, 0) != pid)
1264 signal(SIGINT, old1);
1265 signal(SIGQUIT, old2);
1267 warn("%s", "Try again later");
1276 * Send new user information (re-login)
1279 user(int argc, char **argv)
1286 another(&argc, &argv, "username");
1287 if (argc < 2 || argc > 4) {
1288 printf("usage: %s username [password] [account]\n", argv[0]);
1292 n = command("USER %s", argv[1]);
1293 if (n == CONTINUE) {
1295 des_read_pw_string (tmp,
1301 n = command("PASS %s", argv[2]);
1303 if (n == CONTINUE) {
1305 printf("Account: "); fflush(stdout);
1306 fgets(acct, sizeof(acct) - 1, stdin);
1307 acct[strlen(acct) - 1] = '\0';
1308 argv[3] = acct; argc++;
1310 n = command("ACCT %s", argv[3]);
1313 if (n != COMPLETE) {
1314 fprintf(stdout, "Login failed.\n");
1317 if (!aflag && argc == 4) {
1318 command("ACCT %s", argv[3]);
1323 * Print working directory.
1327 pwd(int argc, char **argv)
1329 int oldverbose = verbose;
1332 * If we aren't verbose, this doesn't do anything!
1335 if (command("PWD") == ERROR && code == 500) {
1336 printf("PWD command not recognized, trying XPWD\n");
1339 verbose = oldverbose;
1346 makedir(int argc, char **argv)
1349 if (argc < 2 && !another(&argc, &argv, "directory-name")) {
1350 printf("usage: %s directory-name\n", argv[0]);
1354 if (command("MKD %s", argv[1]) == ERROR && code == 500) {
1356 printf("MKD command not recognized, trying XMKD\n");
1357 command("XMKD %s", argv[1]);
1362 * Remove a directory.
1365 removedir(int argc, char **argv)
1368 if (argc < 2 && !another(&argc, &argv, "directory-name")) {
1369 printf("usage: %s directory-name\n", argv[0]);
1373 if (command("RMD %s", argv[1]) == ERROR && code == 500) {
1375 printf("RMD command not recognized, trying XRMD\n");
1376 command("XRMD %s", argv[1]);
1381 * Send a line, verbatim, to the remote machine.
1384 quote(int argc, char **argv)
1387 if (argc < 2 && !another(&argc, &argv, "command line to send")) {
1388 printf("usage: %s line-to-send\n", argv[0]);
1392 quote1("", argc, argv);
1396 * Send a SITE command to the remote machine. The line
1397 * is sent verbatim to the remote machine, except that the
1398 * word "SITE" is added at the front.
1401 site(int argc, char **argv)
1404 if (argc < 2 && !another(&argc, &argv, "arguments to SITE command")) {
1405 printf("usage: %s line-to-send\n", argv[0]);
1409 quote1("SITE ", argc, argv);
1413 * Turn argv[1..argc) into a space-separated string, then prepend initial text.
1414 * Send the result as a one-line command and get response.
1417 quote1(char *initial, int argc, char **argv)
1420 char buf[BUFSIZ]; /* must be >= sizeof(line) */
1422 strlcpy(buf, initial, sizeof(buf));
1423 for(i = 1; i < argc; i++) {
1425 strlcat(buf, " ", sizeof(buf));
1426 strlcat(buf, argv[i], sizeof(buf));
1428 if (command("%s", buf) == PRELIM) {
1429 while (getreply(0) == PRELIM)
1435 do_chmod(int argc, char **argv)
1438 if (argc < 2 && !another(&argc, &argv, "mode"))
1440 if (argc < 3 && !another(&argc, &argv, "file-name")) {
1442 printf("usage: %s mode file-name\n", argv[0]);
1446 command("SITE CHMOD %s %s", argv[1], argv[2]);
1450 do_umask(int argc, char **argv)
1452 int oldverbose = verbose;
1455 command(argc == 1 ? "SITE UMASK" : "SITE UMASK %s", argv[1]);
1456 verbose = oldverbose;
1460 ftp_idle(int argc, char **argv)
1462 int oldverbose = verbose;
1465 command(argc == 1 ? "SITE IDLE" : "SITE IDLE %s", argv[1]);
1466 verbose = oldverbose;
1470 * Ask the other side for help.
1473 rmthelp(int argc, char **argv)
1475 int oldverbose = verbose;
1478 command(argc == 1 ? "HELP" : "HELP %s", argv[1]);
1479 verbose = oldverbose;
1483 * Terminate session and exit.
1487 quit(int argc, char **argv)
1500 * Terminate session, but don't exit.
1503 disconnect(int argc, char **argv)
1522 confirm(char *cmd, char *file)
1528 printf("%s %s? ", cmd, file);
1530 if (fgets(line, sizeof line, stdin) == NULL)
1532 return (*line == 'y' || *line == 'Y');
1543 * Glob a local file name specification with
1544 * the expectation of a single return value.
1545 * Can't control multiple values being expanded
1546 * from the expression, we return only the first.
1549 globulize(char **cpp)
1557 flags = GLOB_BRACE|GLOB_NOCHECK|GLOB_QUOTE|GLOB_TILDE;
1558 memset(&gl, 0, sizeof(gl));
1559 if (glob(*cpp, flags, NULL, &gl) ||
1561 warnx("%s: not found", *cpp);
1565 *cpp = strdup(gl.gl_pathv[0]); /* XXX - wasted memory */
1571 account(int argc, char **argv)
1578 strlcpy (acct, *argv, sizeof(acct));
1582 strlcat(acct, *argv, sizeof(acct));
1586 des_read_pw_string(acct, sizeof(acct), "Account:", 0);
1588 command("ACCT %s", acct);
1607 longjmp(abortprox,1);
1611 doproxy(int argc, char **argv)
1614 RETSIGTYPE (*oldintr)();
1616 if (argc < 2 && !another(&argc, &argv, "command")) {
1617 printf("usage: %s command\n", argv[0]);
1621 c = getcmd(argv[1]);
1622 if (c == (struct cmd *) -1) {
1623 printf("?Ambiguous command\n");
1629 printf("?Invalid command\n");
1635 printf("?Invalid proxy command\n");
1640 if (setjmp(abortprox)) {
1644 oldintr = signal(SIGINT, proxabort);
1646 if (c->c_conn && !connected) {
1647 printf("Not connected\n");
1650 signal(SIGINT, oldintr);
1654 (*c->c_handler)(argc-1, argv+1);
1662 signal(SIGINT, oldintr);
1666 setcase(int argc, char **argv)
1670 printf("Case mapping %s.\n", onoff(mcase));
1675 setcr(int argc, char **argv)
1679 printf("Carriage Return stripping %s.\n", onoff(crflag));
1684 setntrans(int argc, char **argv)
1688 printf("Ntrans off.\n");
1694 strlcpy (ntin, argv[1], 17);
1699 strlcpy (ntout, argv[2], 17);
1705 static char new[MaxPathLen];
1706 char *cp1, *cp2 = new;
1707 int i, ostop, found;
1709 for (ostop = 0; *(ntout + ostop) && ostop < 16; ostop++)
1711 for (cp1 = name; *cp1; cp1++) {
1713 for (i = 0; *(ntin + i) && i < 16; i++) {
1714 if (*cp1 == *(ntin + i)) {
1717 *cp2++ = *(ntout + i);
1731 setnmap(int argc, char **argv)
1737 printf("Nmap off.\n");
1741 if (argc < 3 && !another(&argc, &argv, "mapout")) {
1742 printf("Usage: %s [mapin mapout]\n",argv[0]);
1748 cp = strchr(altarg, ' ');
1753 cp = strchr(altarg, ' ');
1756 strlcpy(mapin, altarg, MaxPathLen);
1757 while (*++cp == ' ')
1759 strlcpy(mapout, cp, MaxPathLen);
1765 static char new[MaxPathLen];
1766 char *cp1 = name, *cp2 = mapin;
1767 char *tp[9], *te[9];
1768 int i, toks[9], toknum = 0, match = 1;
1770 for (i=0; i < 9; ++i) {
1773 while (match && *cp1 && *cp2) {
1776 if (*++cp2 != *cp1) {
1781 if (*(cp2+1) >= '1' && (*cp2+1) <= '9') {
1782 if (*cp1 != *(++cp2+1)) {
1783 toks[toknum = *cp2 - '1']++;
1785 while (*++cp1 && *(cp2+1)
1799 if (match && *cp1) {
1802 if (match && *cp2) {
1806 if (!match && *cp1) /* last token mismatch */
1823 if (*++cp2 == '$' && isdigit(*(cp2+1))) {
1824 if (*++cp2 == '0') {
1832 else if (toks[toknum = *cp2 - '1']) {
1833 char *cp3 = tp[toknum];
1835 while (cp3 != te[toknum]) {
1842 while (*cp2 && *cp2 != ',' &&
1847 else if (*cp2 == '$' &&
1848 isdigit(*(cp2+1))) {
1849 if (*++cp2 == '0') {
1856 else if (toks[toknum =
1858 char *cp3=tp[toknum];
1871 printf("nmap: unbalanced brackets\n");
1878 while (*++cp2 && *cp2 != ']') {
1879 if (*cp2 == '\\' && *(cp2 + 1)) {
1884 printf("nmap: unbalanced brackets\n");
1900 if (isdigit(*(cp2 + 1))) {
1901 if (*++cp2 == '0') {
1908 else if (toks[toknum = *cp2 - '1']) {
1909 char *cp3 = tp[toknum];
1911 while (cp3 != te[toknum]) {
1917 /* intentional drop through */
1932 setpassive(int argc, char **argv)
1935 passivemode = !passivemode;
1936 printf("Passive mode %s.\n", onoff(passivemode));
1941 setsunique(int argc, char **argv)
1945 printf("Store unique %s.\n", onoff(sunique));
1950 setrunique(int argc, char **argv)
1954 printf("Receive unique %s.\n", onoff(runique));
1958 /* change directory to perent directory */
1960 cdup(int argc, char **argv)
1963 if (command("CDUP") == ERROR && code == 500) {
1965 printf("CDUP command not recognized, trying XCUP\n");
1970 /* restart transfer at specific point */
1972 restart(int argc, char **argv)
1976 printf("restart: offset not specified\n");
1978 restart_point = atol(argv[1]);
1979 printf("restarting at %ld. %s\n", (long)restart_point,
1980 "execute get, put or append to initiate transfer");
1984 /* show remote system type */
1986 syst(int argc, char **argv)
1993 macdef(int argc, char **argv)
1999 printf("Limit of 16 macros have already been defined\n");
2003 if (argc < 2 && !another(&argc, &argv, "macro name")) {
2004 printf("Usage: %s macro_name\n",argv[0]);
2009 printf("Enter macro line by line, terminating it with a null line\n");
2011 strlcpy(macros[macnum].mac_name,
2013 sizeof(macros[macnum].mac_name));
2015 macros[macnum].mac_start = macbuf;
2018 macros[macnum].mac_start = macros[macnum - 1].mac_end + 1;
2020 tmp = macros[macnum].mac_start;
2021 while (tmp != macbuf+4096) {
2022 if ((c = getchar()) == EOF) {
2023 printf("macdef:end of file encountered\n");
2027 if ((*tmp = c) == '\n') {
2028 if (tmp == macros[macnum].mac_start) {
2029 macros[macnum++].mac_end = tmp;
2033 if (*(tmp-1) == '\0') {
2034 macros[macnum++].mac_end = tmp - 1;
2043 while ((c = getchar()) != '\n' && c != EOF)
2045 if (c == EOF || getchar() == '\n') {
2046 printf("Macro not defined - 4k buffer exceeded\n");
2054 * get size of file on remote machine
2057 sizecmd(int argc, char **argv)
2060 if (argc < 2 && !another(&argc, &argv, "filename")) {
2061 printf("usage: %s filename\n", argv[0]);
2065 command("SIZE %s", argv[1]);
2069 * get last modification time of file on remote machine
2072 modtime(int argc, char **argv)
2076 if (argc < 2 && !another(&argc, &argv, "filename")) {
2077 printf("usage: %s filename\n", argv[0]);
2084 if (command("MDTM %s", argv[1]) == COMPLETE) {
2085 int yy, mo, day, hour, min, sec;
2086 sscanf(reply_string, "%*s %04d%02d%02d%02d%02d%02d", &yy, &mo,
2087 &day, &hour, &min, &sec);
2088 /* might want to print this in local time */
2089 printf("%s\t%02d/%02d/%04d %02d:%02d:%02d GMT\n", argv[1],
2090 mo, day, yy, hour, min, sec);
2092 printf("%s\n", reply_string);
2097 * show status on reomte machine
2100 rmtstatus(int argc, char **argv)
2103 command(argc > 1 ? "STAT %s" : "STAT" , argv[1]);
2107 * get file if modtime is more recent than current file
2110 newer(int argc, char **argv)
2113 if (getit(argc, argv, -1, curtype == TYPE_I ? "wb" : "w"))
2114 printf("Local file \"%s\" is newer than remote file \"%s\"\n",