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.2.2 2000/06/23 02:43:49 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;
650 time_t mtime = stbuf.st_mtime;
655 cmdret = command("MDTM %s", argv[1]);
657 if (cmdret != COMPLETE) {
658 printf("%s\n", reply_string);
661 if (sscanf(reply_string,
662 "%*s %04d%02d%02d%02d%02d%02d",
663 &yy, &mo, &day, &hour, &min, &sec)
665 printf ("bad MDTM result\n");
673 if ((tm->tm_year > yy) ||
674 (tm->tm_year == yy &&
677 tm->tm_mday > day) ||
678 (tm->tm_mday == day &&
679 tm->tm_hour > hour) ||
680 (tm->tm_hour == hour &&
682 (tm->tm_min == min &&
688 recvrequest("RETR", argv[2], argv[1], mode,
689 argv[1] != oldargv1 || argv[2] != oldargv2, local_given);
695 suspicious_filename(const char *fn)
697 return strstr(fn, "../") != NULL || *fn == '/';
701 * Get multiple files.
704 mget(int argc, char **argv)
708 char *cp, *tp, *tp2, tmpbuf[MaxPathLen];
710 if (argc < 2 && !another(&argc, &argv, "remote-files")) {
711 printf("usage: %s remote-files\n", argv[0]);
717 oldintr = signal(SIGINT, mabort);
719 while ((cp = remglob(argv,proxy)) != NULL) {
724 if (mflag && suspicious_filename(cp))
725 printf("*** Suspicious filename: %s\n", cp);
726 if (mflag && confirm(argv[0], cp)) {
729 for (tp2 = tmpbuf; (ch = *tp++);)
730 *tp2++ = isupper(ch) ? tolower(ch) : ch;
740 recvrequest("RETR", tp, cp,
741 curtype == TYPE_I ? "wb" : "w",
742 tp != cp || !interactive, 0);
743 if (!mflag && fromatty) {
744 ointer = interactive;
746 if (confirm("Continue with","mget")) {
749 interactive = ointer;
753 signal(SIGINT,oldintr);
758 remglob(char **argv, int doswitch)
761 static char buf[MaxPathLen];
762 static FILE *ftemp = NULL;
764 int oldverbose, oldhash;
782 if ((cp = *++args) == NULL)
788 strlcpy(temp, _PATH_TMP_XXX, sizeof(temp));
791 warn("unable to create temporary file %s", temp);
795 oldverbose = verbose, verbose = 0;
796 oldhash = hash, hash = 0;
800 for (mode = "w"; *++argv != NULL; mode = "a")
801 recvrequest ("NLST", temp, *argv, mode, 0, 0);
805 verbose = oldverbose; hash = oldhash;
806 ftemp = fopen(temp, "r");
809 printf("can't find list of remote files, oops\n");
813 while(fgets(buf, sizeof (buf), ftemp)) {
814 if ((cp = strchr(buf, '\n')) != NULL)
816 if(!interactive && suspicious_filename(buf)){
817 printf("Ignoring remote globbed file `%s'\n", buf);
831 return (bool ? "on" : "off");
839 status(int argc, char **argv)
844 printf("Connected to %s.\n", hostname);
846 printf("Not connected.\n");
850 printf("Connected for proxy commands to %s.\n", hostname);
853 printf("No proxy connection.\n");
858 printf("Mode: %s; Type: %s; Form: %s; Structure: %s\n",
859 modename, typename, formname, structname);
860 printf("Verbose: %s; Bell: %s; Prompting: %s; Globbing: %s\n",
861 onoff(verbose), onoff(bell), onoff(interactive),
863 printf("Store unique: %s; Receive unique: %s\n", onoff(sunique),
865 printf("Case: %s; CR stripping: %s\n",onoff(mcase),onoff(crflag));
867 printf("Ntrans: (in) %s (out) %s\n", ntin,ntout);
870 printf("Ntrans: off\n");
873 printf("Nmap: (in) %s (out) %s\n", mapin, mapout);
876 printf("Nmap: off\n");
878 printf("Hash mark printing: %s; Use of PORT cmds: %s\n",
879 onoff(hash), onoff(sendport));
882 for (i=0; i<macnum; i++) {
883 printf("\t%s\n",macros[i].mac_name);
890 * Set beep on cmd completed mode.
894 setbell(int argc, char **argv)
898 printf("Bell mode %s.\n", onoff(bell));
903 * Turn on packet tracing.
907 settrace(int argc, char **argv)
911 printf("Packet tracing %s.\n", onoff(trace));
916 * Toggle hash mark printing during transfers.
920 sethash(int argc, char **argv)
924 printf("Hash mark printing %s", onoff(hash));
927 printf(" (%d bytes/hash mark)", 1024);
932 * Turn on printing of server echo's.
936 setverbose(int argc, char **argv)
940 printf("Verbose mode %s.\n", onoff(verbose));
945 * Toggle PORT cmd use before each data connection.
949 setport(int argc, char **argv)
952 sendport = !sendport;
953 printf("Use of PORT cmds %s.\n", onoff(sendport));
958 * Turn on interactive prompting
959 * during mget, mput, and mdelete.
963 setprompt(int argc, char **argv)
966 interactive = !interactive;
967 printf("Interactive mode %s.\n", onoff(interactive));
972 * Toggle metacharacter interpretation
973 * on local file names.
977 setglob(int argc, char **argv)
981 printf("Globbing %s.\n", onoff(doglob));
986 * Set debugging mode on/off and/or
987 * set level of debugging.
991 setdebug(int argc, char **argv)
998 printf("%s: bad debugging value.\n", argv[1]);
1006 options |= SO_DEBUG;
1008 options &= ~SO_DEBUG;
1009 printf("Debugging %s (debug=%d).\n", onoff(debug), debug);
1014 * Set current working directory
1015 * on remote machine.
1018 cd(int argc, char **argv)
1021 if (argc < 2 && !another(&argc, &argv, "remote-directory")) {
1022 printf("usage: %s remote-directory\n", argv[0]);
1026 if (command("CWD %s", argv[1]) == ERROR && code == 500) {
1028 printf("CWD command not recognized, trying XCWD\n");
1029 command("XCWD %s", argv[1]);
1034 * Set current working directory
1038 lcd(int argc, char **argv)
1040 char buf[MaxPathLen];
1043 argc++, argv[1] = home;
1045 printf("usage: %s local-directory\n", argv[0]);
1049 if (!globulize(&argv[1])) {
1053 if (chdir(argv[1]) < 0) {
1054 warn("local: %s", argv[1]);
1058 if (getcwd(buf, sizeof(buf)) != NULL)
1059 printf("Local directory now %s\n", buf);
1061 warnx("getwd: %s", buf);
1066 * Delete a single file.
1069 delete(int argc, char **argv)
1072 if (argc < 2 && !another(&argc, &argv, "remote-file")) {
1073 printf("usage: %s remote-file\n", argv[0]);
1077 command("DELE %s", argv[1]);
1081 * Delete multiple files.
1084 mdelete(int argc, char **argv)
1090 if (argc < 2 && !another(&argc, &argv, "remote-files")) {
1091 printf("usage: %s remote-files\n", argv[0]);
1097 oldintr = signal(SIGINT, mabort);
1099 while ((cp = remglob(argv,0)) != NULL) {
1104 if (mflag && confirm(argv[0], cp)) {
1105 command("DELE %s", cp);
1106 if (!mflag && fromatty) {
1107 ointer = interactive;
1109 if (confirm("Continue with", "mdelete")) {
1112 interactive = ointer;
1116 signal(SIGINT, oldintr);
1121 * Rename a remote file.
1124 renamefile(int argc, char **argv)
1127 if (argc < 2 && !another(&argc, &argv, "from-name"))
1129 if (argc < 3 && !another(&argc, &argv, "to-name")) {
1131 printf("%s from-name to-name\n", argv[0]);
1135 if (command("RNFR %s", argv[1]) == CONTINUE)
1136 command("RNTO %s", argv[2]);
1140 * Get a directory listing
1144 ls(int argc, char **argv)
1149 argc++, argv[1] = NULL;
1151 argc++, argv[2] = "-";
1153 printf("usage: %s remote-directory local-file\n", argv[0]);
1157 cmd = argv[0][0] == 'n' ? "NLST" : "LIST";
1158 if (strcmp(argv[2], "-") && !globulize(&argv[2])) {
1162 if (strcmp(argv[2], "-") && *argv[2] != '|')
1163 if (!globulize(&argv[2]) || !confirm("output to local-file:",
1168 recvrequest(cmd, argv[2], argv[1], "w", 0, 1);
1172 * Get a directory listing
1173 * of multiple remote files.
1176 mls(int argc, char **argv)
1180 char *cmd, mode[1], *dest;
1182 if (argc < 2 && !another(&argc, &argv, "remote-files"))
1184 if (argc < 3 && !another(&argc, &argv, "local-file")) {
1186 printf("usage: %s remote-files local-file\n", argv[0]);
1190 dest = argv[argc - 1];
1191 argv[argc - 1] = NULL;
1192 if (strcmp(dest, "-") && *dest != '|')
1193 if (!globulize(&dest) ||
1194 !confirm("output to local-file:", dest)) {
1198 cmd = argv[0][1] == 'l' ? "NLST" : "LIST";
1201 oldintr = signal(SIGINT, mabort);
1203 for (i = 1; mflag && i < argc-1; ++i) {
1204 *mode = (i == 1) ? 'w' : 'a';
1205 recvrequest(cmd, dest, argv[i], mode, 0, 1);
1206 if (!mflag && fromatty) {
1207 ointer = interactive;
1209 if (confirm("Continue with", argv[0])) {
1212 interactive = ointer;
1215 signal(SIGINT, oldintr);
1224 shell(int argc, char **argv)
1227 RETSIGTYPE (*old1)(), (*old2)();
1228 char shellnam[40], *shell, *namep;
1231 old1 = signal (SIGINT, SIG_IGN);
1232 old2 = signal (SIGQUIT, SIG_IGN);
1233 if ((pid = fork()) == 0) {
1234 for (pid = 3; pid < 20; pid++)
1236 signal(SIGINT, SIG_DFL);
1237 signal(SIGQUIT, SIG_DFL);
1238 shell = getenv("SHELL");
1240 shell = _PATH_BSHELL;
1241 namep = strrchr(shell,'/');
1244 snprintf (shellnam, sizeof(shellnam),
1246 if (strcmp(namep, "sh") != 0)
1249 printf ("%s\n", shell);
1253 execl(shell,shellnam,"-c",altarg,(char *)0);
1256 execl(shell,shellnam,(char *)0);
1263 while (waitpid(-1, &status, 0) != pid)
1265 signal(SIGINT, old1);
1266 signal(SIGQUIT, old2);
1268 warn("%s", "Try again later");
1277 * Send new user information (re-login)
1280 user(int argc, char **argv)
1287 another(&argc, &argv, "username");
1288 if (argc < 2 || argc > 4) {
1289 printf("usage: %s username [password] [account]\n", argv[0]);
1293 n = command("USER %s", argv[1]);
1294 if (n == CONTINUE) {
1296 des_read_pw_string (tmp,
1302 n = command("PASS %s", argv[2]);
1304 if (n == CONTINUE) {
1306 printf("Account: "); fflush(stdout);
1307 fgets(acct, sizeof(acct) - 1, stdin);
1308 acct[strlen(acct) - 1] = '\0';
1309 argv[3] = acct; argc++;
1311 n = command("ACCT %s", argv[3]);
1314 if (n != COMPLETE) {
1315 fprintf(stdout, "Login failed.\n");
1318 if (!aflag && argc == 4) {
1319 command("ACCT %s", argv[3]);
1324 * Print working directory.
1328 pwd(int argc, char **argv)
1330 int oldverbose = verbose;
1333 * If we aren't verbose, this doesn't do anything!
1336 if (command("PWD") == ERROR && code == 500) {
1337 printf("PWD command not recognized, trying XPWD\n");
1340 verbose = oldverbose;
1347 makedir(int argc, char **argv)
1350 if (argc < 2 && !another(&argc, &argv, "directory-name")) {
1351 printf("usage: %s directory-name\n", argv[0]);
1355 if (command("MKD %s", argv[1]) == ERROR && code == 500) {
1357 printf("MKD command not recognized, trying XMKD\n");
1358 command("XMKD %s", argv[1]);
1363 * Remove a directory.
1366 removedir(int argc, char **argv)
1369 if (argc < 2 && !another(&argc, &argv, "directory-name")) {
1370 printf("usage: %s directory-name\n", argv[0]);
1374 if (command("RMD %s", argv[1]) == ERROR && code == 500) {
1376 printf("RMD command not recognized, trying XRMD\n");
1377 command("XRMD %s", argv[1]);
1382 * Send a line, verbatim, to the remote machine.
1385 quote(int argc, char **argv)
1388 if (argc < 2 && !another(&argc, &argv, "command line to send")) {
1389 printf("usage: %s line-to-send\n", argv[0]);
1393 quote1("", argc, argv);
1397 * Send a SITE command to the remote machine. The line
1398 * is sent verbatim to the remote machine, except that the
1399 * word "SITE" is added at the front.
1402 site(int argc, char **argv)
1405 if (argc < 2 && !another(&argc, &argv, "arguments to SITE command")) {
1406 printf("usage: %s line-to-send\n", argv[0]);
1410 quote1("SITE ", argc, argv);
1414 * Turn argv[1..argc) into a space-separated string, then prepend initial text.
1415 * Send the result as a one-line command and get response.
1418 quote1(char *initial, int argc, char **argv)
1421 char buf[BUFSIZ]; /* must be >= sizeof(line) */
1423 strlcpy(buf, initial, sizeof(buf));
1424 for(i = 1; i < argc; i++) {
1426 strlcat(buf, " ", sizeof(buf));
1427 strlcat(buf, argv[i], sizeof(buf));
1429 if (command("%s", buf) == PRELIM) {
1430 while (getreply(0) == PRELIM)
1436 do_chmod(int argc, char **argv)
1439 if (argc < 2 && !another(&argc, &argv, "mode"))
1441 if (argc < 3 && !another(&argc, &argv, "file-name")) {
1443 printf("usage: %s mode file-name\n", argv[0]);
1447 command("SITE CHMOD %s %s", argv[1], argv[2]);
1451 do_umask(int argc, char **argv)
1453 int oldverbose = verbose;
1456 command(argc == 1 ? "SITE UMASK" : "SITE UMASK %s", argv[1]);
1457 verbose = oldverbose;
1461 ftp_idle(int argc, char **argv)
1463 int oldverbose = verbose;
1466 command(argc == 1 ? "SITE IDLE" : "SITE IDLE %s", argv[1]);
1467 verbose = oldverbose;
1471 * Ask the other side for help.
1474 rmthelp(int argc, char **argv)
1476 int oldverbose = verbose;
1479 command(argc == 1 ? "HELP" : "HELP %s", argv[1]);
1480 verbose = oldverbose;
1484 * Terminate session and exit.
1488 quit(int argc, char **argv)
1501 * Terminate session, but don't exit.
1504 disconnect(int argc, char **argv)
1523 confirm(char *cmd, char *file)
1529 printf("%s %s? ", cmd, file);
1531 if (fgets(line, sizeof line, stdin) == NULL)
1533 return (*line == 'y' || *line == 'Y');
1544 * Glob a local file name specification with
1545 * the expectation of a single return value.
1546 * Can't control multiple values being expanded
1547 * from the expression, we return only the first.
1550 globulize(char **cpp)
1558 flags = GLOB_BRACE|GLOB_NOCHECK|GLOB_QUOTE|GLOB_TILDE;
1559 memset(&gl, 0, sizeof(gl));
1560 if (glob(*cpp, flags, NULL, &gl) ||
1562 warnx("%s: not found", *cpp);
1566 *cpp = strdup(gl.gl_pathv[0]); /* XXX - wasted memory */
1572 account(int argc, char **argv)
1579 strlcpy (acct, *argv, sizeof(acct));
1583 strlcat(acct, *argv, sizeof(acct));
1587 des_read_pw_string(acct, sizeof(acct), "Account:", 0);
1589 command("ACCT %s", acct);
1608 longjmp(abortprox,1);
1612 doproxy(int argc, char **argv)
1615 RETSIGTYPE (*oldintr)();
1617 if (argc < 2 && !another(&argc, &argv, "command")) {
1618 printf("usage: %s command\n", argv[0]);
1622 c = getcmd(argv[1]);
1623 if (c == (struct cmd *) -1) {
1624 printf("?Ambiguous command\n");
1630 printf("?Invalid command\n");
1636 printf("?Invalid proxy command\n");
1641 if (setjmp(abortprox)) {
1645 oldintr = signal(SIGINT, proxabort);
1647 if (c->c_conn && !connected) {
1648 printf("Not connected\n");
1651 signal(SIGINT, oldintr);
1655 (*c->c_handler)(argc-1, argv+1);
1663 signal(SIGINT, oldintr);
1667 setcase(int argc, char **argv)
1671 printf("Case mapping %s.\n", onoff(mcase));
1676 setcr(int argc, char **argv)
1680 printf("Carriage Return stripping %s.\n", onoff(crflag));
1685 setntrans(int argc, char **argv)
1689 printf("Ntrans off.\n");
1695 strlcpy (ntin, argv[1], 17);
1700 strlcpy (ntout, argv[2], 17);
1706 static char new[MaxPathLen];
1707 char *cp1, *cp2 = new;
1708 int i, ostop, found;
1710 for (ostop = 0; *(ntout + ostop) && ostop < 16; ostop++)
1712 for (cp1 = name; *cp1; cp1++) {
1714 for (i = 0; *(ntin + i) && i < 16; i++) {
1715 if (*cp1 == *(ntin + i)) {
1718 *cp2++ = *(ntout + i);
1732 setnmap(int argc, char **argv)
1738 printf("Nmap off.\n");
1742 if (argc < 3 && !another(&argc, &argv, "mapout")) {
1743 printf("Usage: %s [mapin mapout]\n",argv[0]);
1749 cp = strchr(altarg, ' ');
1754 cp = strchr(altarg, ' ');
1757 strlcpy(mapin, altarg, MaxPathLen);
1758 while (*++cp == ' ')
1760 strlcpy(mapout, cp, MaxPathLen);
1766 static char new[MaxPathLen];
1767 char *cp1 = name, *cp2 = mapin;
1768 char *tp[9], *te[9];
1769 int i, toks[9], toknum = 0, match = 1;
1771 for (i=0; i < 9; ++i) {
1774 while (match && *cp1 && *cp2) {
1777 if (*++cp2 != *cp1) {
1782 if (*(cp2+1) >= '1' && (*cp2+1) <= '9') {
1783 if (*cp1 != *(++cp2+1)) {
1784 toks[toknum = *cp2 - '1']++;
1786 while (*++cp1 && *(cp2+1)
1800 if (match && *cp1) {
1803 if (match && *cp2) {
1807 if (!match && *cp1) /* last token mismatch */
1824 if (*++cp2 == '$' && isdigit(*(cp2+1))) {
1825 if (*++cp2 == '0') {
1833 else if (toks[toknum = *cp2 - '1']) {
1834 char *cp3 = tp[toknum];
1836 while (cp3 != te[toknum]) {
1843 while (*cp2 && *cp2 != ',' &&
1848 else if (*cp2 == '$' &&
1849 isdigit(*(cp2+1))) {
1850 if (*++cp2 == '0') {
1857 else if (toks[toknum =
1859 char *cp3=tp[toknum];
1872 printf("nmap: unbalanced brackets\n");
1879 while (*++cp2 && *cp2 != ']') {
1880 if (*cp2 == '\\' && *(cp2 + 1)) {
1885 printf("nmap: unbalanced brackets\n");
1901 if (isdigit(*(cp2 + 1))) {
1902 if (*++cp2 == '0') {
1909 else if (toks[toknum = *cp2 - '1']) {
1910 char *cp3 = tp[toknum];
1912 while (cp3 != te[toknum]) {
1918 /* intentional drop through */
1933 setpassive(int argc, char **argv)
1936 passivemode = !passivemode;
1937 printf("Passive mode %s.\n", onoff(passivemode));
1942 setsunique(int argc, char **argv)
1946 printf("Store unique %s.\n", onoff(sunique));
1951 setrunique(int argc, char **argv)
1955 printf("Receive unique %s.\n", onoff(runique));
1959 /* change directory to perent directory */
1961 cdup(int argc, char **argv)
1964 if (command("CDUP") == ERROR && code == 500) {
1966 printf("CDUP command not recognized, trying XCUP\n");
1971 /* restart transfer at specific point */
1973 restart(int argc, char **argv)
1977 printf("restart: offset not specified\n");
1979 restart_point = atol(argv[1]);
1980 printf("restarting at %ld. %s\n", (long)restart_point,
1981 "execute get, put or append to initiate transfer");
1985 /* show remote system type */
1987 syst(int argc, char **argv)
1994 macdef(int argc, char **argv)
2000 printf("Limit of 16 macros have already been defined\n");
2004 if (argc < 2 && !another(&argc, &argv, "macro name")) {
2005 printf("Usage: %s macro_name\n",argv[0]);
2010 printf("Enter macro line by line, terminating it with a null line\n");
2012 strlcpy(macros[macnum].mac_name,
2014 sizeof(macros[macnum].mac_name));
2016 macros[macnum].mac_start = macbuf;
2019 macros[macnum].mac_start = macros[macnum - 1].mac_end + 1;
2021 tmp = macros[macnum].mac_start;
2022 while (tmp != macbuf+4096) {
2023 if ((c = getchar()) == EOF) {
2024 printf("macdef:end of file encountered\n");
2028 if ((*tmp = c) == '\n') {
2029 if (tmp == macros[macnum].mac_start) {
2030 macros[macnum++].mac_end = tmp;
2034 if (*(tmp-1) == '\0') {
2035 macros[macnum++].mac_end = tmp - 1;
2044 while ((c = getchar()) != '\n' && c != EOF)
2046 if (c == EOF || getchar() == '\n') {
2047 printf("Macro not defined - 4k buffer exceeded\n");
2055 * get size of file on remote machine
2058 sizecmd(int argc, char **argv)
2061 if (argc < 2 && !another(&argc, &argv, "filename")) {
2062 printf("usage: %s filename\n", argv[0]);
2066 command("SIZE %s", argv[1]);
2070 * get last modification time of file on remote machine
2073 modtime(int argc, char **argv)
2077 if (argc < 2 && !another(&argc, &argv, "filename")) {
2078 printf("usage: %s filename\n", argv[0]);
2085 if (command("MDTM %s", argv[1]) == COMPLETE) {
2086 int yy, mo, day, hour, min, sec;
2087 sscanf(reply_string, "%*s %04d%02d%02d%02d%02d%02d", &yy, &mo,
2088 &day, &hour, &min, &sec);
2089 /* might want to print this in local time */
2090 printf("%s\t%02d/%02d/%04d %02d:%02d:%02d GMT\n", argv[1],
2091 mo, day, yy, hour, min, sec);
2093 printf("%s\n", reply_string);
2098 * show status on reomte machine
2101 rmtstatus(int argc, char **argv)
2104 command(argc > 1 ? "STAT %s" : "STAT" , argv[1]);
2108 * get file if modtime is more recent than current file
2111 newer(int argc, char **argv)
2114 if (getit(argc, argv, -1, curtype == TYPE_I ? "wb" : "w"))
2115 printf("Local file \"%s\" is newer than remote file \"%s\"\n",