cmds.c revision 1.9 1 /* $NetBSD: cmds.c,v 1.9 1996/11/25 05:13:18 lukem Exp $ */
2
3 /*
4 * Copyright (c) 1985, 1989, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36 #ifndef lint
37 #if 0
38 static char sccsid[] = "@(#)cmds.c 8.6 (Berkeley) 10/9/94";
39 #else
40 static char rcsid[] = "$NetBSD: cmds.c,v 1.9 1996/11/25 05:13:18 lukem Exp $";
41 #endif
42 #endif /* not lint */
43
44 /*
45 * FTP User Program -- Command Routines.
46 */
47 #include <sys/param.h>
48 #include <sys/wait.h>
49 #include <sys/stat.h>
50 #include <sys/socket.h>
51 #include <netinet/in.h>
52 #include <arpa/ftp.h>
53
54 #include <ctype.h>
55 #include <err.h>
56 #include <glob.h>
57 #include <netdb.h>
58 #include <signal.h>
59 #include <stdio.h>
60 #include <stdlib.h>
61 #include <string.h>
62 #include <time.h>
63 #include <unistd.h>
64
65 #include "ftp_var.h"
66 #include "pathnames.h"
67
68 jmp_buf jabort;
69 char *mname;
70 char *home = "/";
71
72 /*
73 * `Another' gets another argument, and stores the new argc and argv.
74 * It reverts to the top level (via main.c's intr()) on EOF/error.
75 *
76 * Returns false if no new arguments have been added.
77 */
78 int
79 another(pargc, pargv, prompt)
80 int *pargc;
81 char ***pargv;
82 char *prompt;
83 {
84 int len = strlen(line), ret;
85
86 if (len >= sizeof(line) - 3) {
87 printf("sorry, arguments too long\n");
88 intr();
89 }
90 printf("(%s) ", prompt);
91 line[len++] = ' ';
92 if (fgets(&line[len], sizeof(line) - len, stdin) == NULL)
93 intr();
94 len += strlen(&line[len]);
95 if (len > 0 && line[len - 1] == '\n')
96 line[len - 1] = '\0';
97 makeargv();
98 ret = margc > *pargc;
99 *pargc = margc;
100 *pargv = margv;
101 return (ret);
102 }
103
104 /*
105 * Connect to peer server and
106 * auto-login, if possible.
107 */
108 void
109 setpeer(argc, argv)
110 int argc;
111 char *argv[];
112 {
113 char *host;
114 short port;
115
116 if (connected) {
117 printf("Already connected to %s, use close first.\n",
118 hostname);
119 code = -1;
120 return;
121 }
122 if (argc < 2)
123 (void) another(&argc, &argv, "to");
124 if (argc < 2 || argc > 3) {
125 printf("usage: %s host-name [port]\n", argv[0]);
126 code = -1;
127 return;
128 }
129 port = sp->s_port;
130 if (argc > 2) {
131 port = atoi(argv[2]);
132 if (port <= 0) {
133 printf("%s: bad port number-- %s\n", argv[1], argv[2]);
134 printf ("usage: %s host-name [port]\n", argv[0]);
135 code = -1;
136 return;
137 }
138 port = htons(port);
139 }
140 host = hookup(argv[1], port);
141 if (host) {
142 int overbose;
143
144 connected = 1;
145 /*
146 * Set up defaults for FTP.
147 */
148 (void) strcpy(typename, "ascii"), type = TYPE_A;
149 curtype = TYPE_A;
150 (void) strcpy(formname, "non-print"), form = FORM_N;
151 (void) strcpy(modename, "stream"), mode = MODE_S;
152 (void) strcpy(structname, "file"), stru = STRU_F;
153 (void) strcpy(bytename, "8"), bytesize = 8;
154 if (autologin)
155 (void) login(argv[1]);
156
157 #if defined(unix) && NBBY == 8
158 /*
159 * this ifdef is to keep someone form "porting" this to an incompatible
160 * system and not checking this out. This way they have to think about it.
161 */
162 overbose = verbose;
163 if (debug == 0)
164 verbose = -1;
165 if (command("SYST") == COMPLETE && overbose) {
166 char *cp, c;
167 c = 0;
168 cp = strchr(reply_string+4, ' ');
169 if (cp == NULL)
170 cp = strchr(reply_string+4, '\r');
171 if (cp) {
172 if (cp[-1] == '.')
173 cp--;
174 c = *cp;
175 *cp = '\0';
176 }
177
178 printf("Remote system type is %s.\n",
179 reply_string+4);
180 if (cp)
181 *cp = c;
182 }
183 if (!strncmp(reply_string, "215 UNIX Type: L8", 17)) {
184 if (proxy)
185 unix_proxy = 1;
186 else
187 unix_server = 1;
188 /*
189 * Set type to 0 (not specified by user),
190 * meaning binary by default, but don't bother
191 * telling server. We can use binary
192 * for text files unless changed by the user.
193 */
194 type = 0;
195 (void) strcpy(typename, "binary");
196 if (overbose)
197 printf("Using %s mode to transfer files.\n",
198 typename);
199 } else {
200 if (proxy)
201 unix_proxy = 0;
202 else
203 unix_server = 0;
204 if (overbose &&
205 !strncmp(reply_string, "215 TOPS20", 10))
206 printf(
207 "Remember to set tenex mode when transfering binary files from this machine.\n");
208 }
209 verbose = overbose;
210 #endif /* unix */
211 }
212 }
213
214 struct types {
215 char *t_name;
216 char *t_mode;
217 int t_type;
218 char *t_arg;
219 } types[] = {
220 { "ascii", "A", TYPE_A, 0 },
221 { "binary", "I", TYPE_I, 0 },
222 { "image", "I", TYPE_I, 0 },
223 { "ebcdic", "E", TYPE_E, 0 },
224 { "tenex", "L", TYPE_L, bytename },
225 { NULL }
226 };
227
228 /*
229 * Set transfer type.
230 */
231 void
232 settype(argc, argv)
233 int argc;
234 char *argv[];
235 {
236 struct types *p;
237 int comret;
238
239 if (argc > 2) {
240 char *sep;
241
242 printf("usage: %s [", argv[0]);
243 sep = " ";
244 for (p = types; p->t_name; p++) {
245 printf("%s%s", sep, p->t_name);
246 sep = " | ";
247 }
248 printf(" ]\n");
249 code = -1;
250 return;
251 }
252 if (argc < 2) {
253 printf("Using %s mode to transfer files.\n", typename);
254 code = 0;
255 return;
256 }
257 for (p = types; p->t_name; p++)
258 if (strcmp(argv[1], p->t_name) == 0)
259 break;
260 if (p->t_name == 0) {
261 printf("%s: unknown mode\n", argv[1]);
262 code = -1;
263 return;
264 }
265 if ((p->t_arg != NULL) && (*(p->t_arg) != '\0'))
266 comret = command ("TYPE %s %s", p->t_mode, p->t_arg);
267 else
268 comret = command("TYPE %s", p->t_mode);
269 if (comret == COMPLETE) {
270 (void) strcpy(typename, p->t_name);
271 curtype = type = p->t_type;
272 }
273 }
274
275 /*
276 * Internal form of settype; changes current type in use with server
277 * without changing our notion of the type for data transfers.
278 * Used to change to and from ascii for listings.
279 */
280 void
281 changetype(newtype, show)
282 int newtype, show;
283 {
284 struct types *p;
285 int comret, oldverbose = verbose;
286
287 if (newtype == 0)
288 newtype = TYPE_I;
289 if (newtype == curtype)
290 return;
291 if (debug == 0 && show == 0)
292 verbose = 0;
293 for (p = types; p->t_name; p++)
294 if (newtype == p->t_type)
295 break;
296 if (p->t_name == 0) {
297 printf("ftp: internal error: unknown type %d\n", newtype);
298 return;
299 }
300 if (newtype == TYPE_L && bytename[0] != '\0')
301 comret = command("TYPE %s %s", p->t_mode, bytename);
302 else
303 comret = command("TYPE %s", p->t_mode);
304 if (comret == COMPLETE)
305 curtype = newtype;
306 verbose = oldverbose;
307 }
308
309 char *stype[] = {
310 "type",
311 "",
312 0
313 };
314
315 /*
316 * Set binary transfer type.
317 */
318 /*VARARGS*/
319 void
320 setbinary(argc, argv)
321 int argc;
322 char **argv;
323 {
324
325 stype[1] = "binary";
326 settype(2, stype);
327 }
328
329 /*
330 * Set ascii transfer type.
331 */
332 /*VARARGS*/
333 void
334 setascii(argc, argv)
335 int argc;
336 char *argv[];
337 {
338
339 stype[1] = "ascii";
340 settype(2, stype);
341 }
342
343 /*
344 * Set tenex transfer type.
345 */
346 /*VARARGS*/
347 void
348 settenex(argc, argv)
349 int argc;
350 char *argv[];
351 {
352
353 stype[1] = "tenex";
354 settype(2, stype);
355 }
356
357 /*
358 * Set file transfer mode.
359 */
360 /*ARGSUSED*/
361 void
362 setftmode(argc, argv)
363 int argc;
364 char *argv[];
365 {
366
367 printf("We only support %s mode, sorry.\n", modename);
368 code = -1;
369 }
370
371 /*
372 * Set file transfer format.
373 */
374 /*ARGSUSED*/
375 void
376 setform(argc, argv)
377 int argc;
378 char *argv[];
379 {
380
381 printf("We only support %s format, sorry.\n", formname);
382 code = -1;
383 }
384
385 /*
386 * Set file transfer structure.
387 */
388 /*ARGSUSED*/
389 void
390 setstruct(argc, argv)
391 int argc;
392 char *argv[];
393 {
394
395 printf("We only support %s structure, sorry.\n", structname);
396 code = -1;
397 }
398
399 /*
400 * Send a single file.
401 */
402 void
403 put(argc, argv)
404 int argc;
405 char *argv[];
406 {
407 char *cmd;
408 int loc = 0;
409 char *oldargv1, *oldargv2;
410
411 if (argc == 2) {
412 argc++;
413 argv[2] = argv[1];
414 loc++;
415 }
416 if (argc < 2 && !another(&argc, &argv, "local-file"))
417 goto usage;
418 if (argc < 3 && !another(&argc, &argv, "remote-file")) {
419 usage:
420 printf("usage: %s local-file [ remote-file ]\n", argv[0]);
421 code = -1;
422 return;
423 }
424 oldargv1 = argv[1];
425 oldargv2 = argv[2];
426 if (!globulize(&argv[1])) {
427 code = -1;
428 return;
429 }
430 /*
431 * If "globulize" modifies argv[1], and argv[2] is a copy of
432 * the old argv[1], make it a copy of the new argv[1].
433 */
434 if (argv[1] != oldargv1 && argv[2] == oldargv1) {
435 argv[2] = argv[1];
436 }
437 cmd = (argv[0][0] == 'a') ? "APPE" : ((sunique) ? "STOU" : "STOR");
438 if (loc && ntflag) {
439 argv[2] = dotrans(argv[2]);
440 }
441 if (loc && mapflag) {
442 argv[2] = domap(argv[2]);
443 }
444 sendrequest(cmd, argv[1], argv[2],
445 argv[1] != oldargv1 || argv[2] != oldargv2);
446 }
447
448 /*
449 * Send multiple files.
450 */
451 void
452 mput(argc, argv)
453 int argc;
454 char **argv;
455 {
456 int i;
457 sig_t oldintr;
458 int ointer;
459 char *tp;
460
461 if (argc < 2 && !another(&argc, &argv, "local-files")) {
462 printf("usage: %s local-files\n", argv[0]);
463 code = -1;
464 return;
465 }
466 mname = argv[0];
467 mflag = 1;
468 oldintr = signal(SIGINT, mabort);
469 (void) setjmp(jabort);
470 if (proxy) {
471 char *cp, *tp2, tmpbuf[MAXPATHLEN];
472
473 while ((cp = remglob(argv,0)) != NULL) {
474 if (*cp == 0) {
475 mflag = 0;
476 continue;
477 }
478 if (mflag && confirm(argv[0], cp)) {
479 tp = cp;
480 if (mcase) {
481 while (*tp && !islower(*tp)) {
482 tp++;
483 }
484 if (!*tp) {
485 tp = cp;
486 tp2 = tmpbuf;
487 while ((*tp2 = *tp) != NULL) {
488 if (isupper(*tp2)) {
489 *tp2 = 'a' + *tp2 - 'A';
490 }
491 tp++;
492 tp2++;
493 }
494 }
495 tp = tmpbuf;
496 }
497 if (ntflag) {
498 tp = dotrans(tp);
499 }
500 if (mapflag) {
501 tp = domap(tp);
502 }
503 sendrequest((sunique) ? "STOU" : "STOR",
504 cp, tp, cp != tp || !interactive);
505 if (!mflag && fromatty) {
506 ointer = interactive;
507 interactive = 1;
508 if (confirm("Continue with","mput")) {
509 mflag++;
510 }
511 interactive = ointer;
512 }
513 }
514 }
515 (void) signal(SIGINT, oldintr);
516 mflag = 0;
517 return;
518 }
519 for (i = 1; i < argc; i++) {
520 char **cpp;
521 glob_t gl;
522 int flags;
523
524 if (!doglob) {
525 if (mflag && confirm(argv[0], argv[i])) {
526 tp = (ntflag) ? dotrans(argv[i]) : argv[i];
527 tp = (mapflag) ? domap(tp) : tp;
528 sendrequest((sunique) ? "STOU" : "STOR",
529 argv[i], tp, tp != argv[i] || !interactive);
530 if (!mflag && fromatty) {
531 ointer = interactive;
532 interactive = 1;
533 if (confirm("Continue with","mput")) {
534 mflag++;
535 }
536 interactive = ointer;
537 }
538 }
539 continue;
540 }
541
542 memset(&gl, 0, sizeof(gl));
543 flags = GLOB_BRACE|GLOB_NOCHECK|GLOB_QUOTE|GLOB_TILDE;
544 if (glob(argv[i], flags, NULL, &gl) || gl.gl_pathc == 0) {
545 warnx("%s: not found", argv[i]);
546 globfree(&gl);
547 continue;
548 }
549 for (cpp = gl.gl_pathv; cpp && *cpp != NULL; cpp++) {
550 if (mflag && confirm(argv[0], *cpp)) {
551 tp = (ntflag) ? dotrans(*cpp) : *cpp;
552 tp = (mapflag) ? domap(tp) : tp;
553 sendrequest((sunique) ? "STOU" : "STOR",
554 *cpp, tp, *cpp != tp || !interactive);
555 if (!mflag && fromatty) {
556 ointer = interactive;
557 interactive = 1;
558 if (confirm("Continue with","mput")) {
559 mflag++;
560 }
561 interactive = ointer;
562 }
563 }
564 }
565 globfree(&gl);
566 }
567 (void) signal(SIGINT, oldintr);
568 mflag = 0;
569 }
570
571 void
572 reget(argc, argv)
573 int argc;
574 char *argv[];
575 {
576
577 (void) getit(argc, argv, 1, "r+w");
578 }
579
580 void
581 get(argc, argv)
582 int argc;
583 char *argv[];
584 {
585
586 (void) getit(argc, argv, 0, restart_point ? "r+w" : "w" );
587 }
588
589 /*
590 * Receive one file.
591 */
592 int
593 getit(argc, argv, restartit, mode)
594 int argc;
595 char *argv[];
596 char *mode;
597 int restartit;
598 {
599 int loc = 0;
600 char *oldargv1, *oldargv2;
601
602 if (argc == 2) {
603 argc++;
604 argv[2] = argv[1];
605 loc++;
606 }
607 if (argc < 2 && !another(&argc, &argv, "remote-file"))
608 goto usage;
609 if (argc < 3 && !another(&argc, &argv, "local-file")) {
610 usage:
611 printf("usage: %s remote-file [ local-file ]\n", argv[0]);
612 code = -1;
613 return (0);
614 }
615 oldargv1 = argv[1];
616 oldargv2 = argv[2];
617 if (!globulize(&argv[2])) {
618 code = -1;
619 return (0);
620 }
621 if (loc && mcase) {
622 char *tp = argv[1], *tp2, tmpbuf[MAXPATHLEN];
623
624 while (*tp && !islower(*tp)) {
625 tp++;
626 }
627 if (!*tp) {
628 tp = argv[2];
629 tp2 = tmpbuf;
630 while ((*tp2 = *tp) != NULL) {
631 if (isupper(*tp2)) {
632 *tp2 = 'a' + *tp2 - 'A';
633 }
634 tp++;
635 tp2++;
636 }
637 argv[2] = tmpbuf;
638 }
639 }
640 if (loc && ntflag)
641 argv[2] = dotrans(argv[2]);
642 if (loc && mapflag)
643 argv[2] = domap(argv[2]);
644 if (restartit) {
645 struct stat stbuf;
646 int ret;
647
648 ret = stat(argv[2], &stbuf);
649 if (restartit == 1) {
650 if (ret < 0) {
651 warn("local: %s", argv[2]);
652 return (0);
653 }
654 restart_point = stbuf.st_size;
655 } else {
656 if (ret == 0) {
657 int overbose;
658
659 overbose = verbose;
660 if (debug == 0)
661 verbose = -1;
662 if (command("MDTM %s", argv[1]) == COMPLETE) {
663 int yy, mo, day, hour, min, sec;
664 struct tm *tm;
665 verbose = overbose;
666 sscanf(reply_string,
667 "%*s %04d%02d%02d%02d%02d%02d",
668 &yy, &mo, &day, &hour, &min, &sec);
669 tm = gmtime(&stbuf.st_mtime);
670 tm->tm_mon++;
671 if (tm->tm_year > yy%100)
672 return (1);
673 if ((tm->tm_year == yy%100 &&
674 tm->tm_mon > mo) ||
675 (tm->tm_mon == mo &&
676 tm->tm_mday > day) ||
677 (tm->tm_mday == day &&
678 tm->tm_hour > hour) ||
679 (tm->tm_hour == hour &&
680 tm->tm_min > min) ||
681 (tm->tm_min == min &&
682 tm->tm_sec > sec))
683 return (1);
684 } else {
685 printf("%s\n", reply_string);
686 verbose = overbose;
687 return (0);
688 }
689 }
690 }
691 }
692
693 recvrequest("RETR", argv[2], argv[1], mode,
694 argv[1] != oldargv1 || argv[2] != oldargv2);
695 restart_point = 0;
696 return (0);
697 }
698
699 /* ARGSUSED */
700 void
701 mabort(signo)
702 int signo;
703 {
704 int ointer;
705
706 printf("\n");
707 (void) fflush(stdout);
708 if (mflag && fromatty) {
709 ointer = interactive;
710 interactive = 1;
711 if (confirm("Continue with", mname)) {
712 interactive = ointer;
713 longjmp(jabort,0);
714 }
715 interactive = ointer;
716 }
717 mflag = 0;
718 longjmp(jabort,0);
719 }
720
721 /*
722 * Get multiple files.
723 */
724 void
725 mget(argc, argv)
726 int argc;
727 char **argv;
728 {
729 sig_t oldintr;
730 int ch, ointer;
731 char *cp, *tp, *tp2, tmpbuf[MAXPATHLEN];
732
733 if (argc < 2 && !another(&argc, &argv, "remote-files")) {
734 printf("usage: %s remote-files\n", argv[0]);
735 code = -1;
736 return;
737 }
738 mname = argv[0];
739 mflag = 1;
740 oldintr = signal(SIGINT, mabort);
741 (void) setjmp(jabort);
742 while ((cp = remglob(argv,proxy)) != NULL) {
743 if (*cp == '\0') {
744 mflag = 0;
745 continue;
746 }
747 if (mflag && confirm(argv[0], cp)) {
748 tp = cp;
749 if (mcase) {
750 for (tp2 = tmpbuf; (ch = *tp++) != NULL; )
751 *tp2++ = isupper(ch) ? tolower(ch) : ch;
752 *tp2 = '\0';
753 tp = tmpbuf;
754 }
755 if (ntflag) {
756 tp = dotrans(tp);
757 }
758 if (mapflag) {
759 tp = domap(tp);
760 }
761 recvrequest("RETR", tp, cp, "w",
762 tp != cp || !interactive);
763 if (!mflag && fromatty) {
764 ointer = interactive;
765 interactive = 1;
766 if (confirm("Continue with","mget")) {
767 mflag++;
768 }
769 interactive = ointer;
770 }
771 }
772 }
773 (void) signal(SIGINT,oldintr);
774 mflag = 0;
775 }
776
777 char *
778 remglob(argv,doswitch)
779 char *argv[];
780 int doswitch;
781 {
782 char temp[16];
783 static char buf[MAXPATHLEN];
784 static FILE *ftemp = NULL;
785 static char **args;
786 int oldverbose, oldhash;
787 char *cp, *mode;
788
789 if (!mflag) {
790 if (!doglob) {
791 args = NULL;
792 }
793 else {
794 if (ftemp) {
795 (void) fclose(ftemp);
796 ftemp = NULL;
797 }
798 }
799 return (NULL);
800 }
801 if (!doglob) {
802 if (args == NULL)
803 args = argv;
804 if ((cp = *++args) == NULL)
805 args = NULL;
806 return (cp);
807 }
808 if (ftemp == NULL) {
809 (void) strcpy(temp, _PATH_TMP);
810 (void) mktemp(temp);
811 oldverbose = verbose, verbose = 0;
812 oldhash = hash, hash = 0;
813 if (doswitch) {
814 pswitch(!proxy);
815 }
816 for (mode = "w"; *++argv != NULL; mode = "a")
817 recvrequest ("NLST", temp, *argv, mode, 0);
818 if (doswitch) {
819 pswitch(!proxy);
820 }
821 verbose = oldverbose; hash = oldhash;
822 ftemp = fopen(temp, "r");
823 (void) unlink(temp);
824 if (ftemp == NULL) {
825 printf("can't find list of remote files, oops\n");
826 return (NULL);
827 }
828 }
829 if (fgets(buf, sizeof (buf), ftemp) == NULL) {
830 (void) fclose(ftemp), ftemp = NULL;
831 return (NULL);
832 }
833 if ((cp = strchr(buf, '\n')) != NULL)
834 *cp = '\0';
835 return (buf);
836 }
837
838 char *
839 onoff(bool)
840 int bool;
841 {
842
843 return (bool ? "on" : "off");
844 }
845
846 /*
847 * Show status.
848 */
849 /*ARGSUSED*/
850 void
851 status(argc, argv)
852 int argc;
853 char *argv[];
854 {
855 int i;
856
857 if (connected)
858 printf("Connected to %s.\n", hostname);
859 else
860 printf("Not connected.\n");
861 if (!proxy) {
862 pswitch(1);
863 if (connected) {
864 printf("Connected for proxy commands to %s.\n", hostname);
865 }
866 else {
867 printf("No proxy connection.\n");
868 }
869 pswitch(0);
870 }
871 printf("Mode: %s; Type: %s; Form: %s; Structure: %s\n",
872 modename, typename, formname, structname);
873 printf("Verbose: %s; Bell: %s; Prompting: %s; Globbing: %s\n",
874 onoff(verbose), onoff(bell), onoff(interactive),
875 onoff(doglob));
876 printf("Store unique: %s; Receive unique: %s\n", onoff(sunique),
877 onoff(runique));
878 printf("Case: %s; CR stripping: %s\n",onoff(mcase),onoff(crflag));
879 if (ntflag) {
880 printf("Ntrans: (in) %s (out) %s\n", ntin,ntout);
881 }
882 else {
883 printf("Ntrans: off\n");
884 }
885 if (mapflag) {
886 printf("Nmap: (in) %s (out) %s\n", mapin, mapout);
887 }
888 else {
889 printf("Nmap: off\n");
890 }
891 printf("Hash mark printing: %s; Mark count: %d\n", onoff(hash), mark);
892 printf("Use of PORT cmds: %s\n", onoff(sendport));
893 if (macnum > 0) {
894 printf("Macros:\n");
895 for (i=0; i<macnum; i++) {
896 printf("\t%s\n",macros[i].mac_name);
897 }
898 }
899 code = 0;
900 }
901
902 /*
903 * Set beep on cmd completed mode.
904 */
905 /*VARARGS*/
906 void
907 setbell(argc, argv)
908 int argc;
909 char *argv[];
910 {
911
912 bell = !bell;
913 printf("Bell mode %s.\n", onoff(bell));
914 code = bell;
915 }
916
917 /*
918 * Turn on packet tracing.
919 */
920 /*VARARGS*/
921 void
922 settrace(argc, argv)
923 int argc;
924 char *argv[];
925 {
926
927 trace = !trace;
928 printf("Packet tracing %s.\n", onoff(trace));
929 code = trace;
930 }
931
932 /*
933 * Toggle hash mark printing during transfers, or set hash mark bytecount.
934 */
935 /*VARARGS*/
936 void
937 sethash(argc, argv)
938 int argc;
939 char *argv[];
940 {
941 if (argc == 1) {
942 hash = !hash;
943 printf("Hash mark printing %s", onoff(hash));
944 code = hash;
945 if (hash)
946 printf(" (%d bytes/hash mark)", mark);
947 printf(".\n");
948 } else if (argc != 2) {
949 printf("usage: %s [number of bytes].\n", argv[0]);
950 } else {
951 int nmark = atol(argv[1]);
952 if (nmark < 1)
953 printf( "A hash mark bytecount of %d"
954 " is rather pointless...\n", nmark);
955 else {
956 mark = nmark;
957 printf("Hash mark set to %d bytes/hash mark\n", mark);
958 }
959 }
960 }
961
962 /*
963 * Turn on printing of server echo's.
964 */
965 /*VARARGS*/
966 void
967 setverbose(argc, argv)
968 int argc;
969 char *argv[];
970 {
971
972 verbose = !verbose;
973 printf("Verbose mode %s.\n", onoff(verbose));
974 code = verbose;
975 }
976
977 /*
978 * Toggle PORT cmd use before each data connection.
979 */
980 /*VARARGS*/
981 void
982 setport(argc, argv)
983 int argc;
984 char *argv[];
985 {
986
987 sendport = !sendport;
988 printf("Use of PORT cmds %s.\n", onoff(sendport));
989 code = sendport;
990 }
991
992 /*
993 * Turn on interactive prompting
994 * during mget, mput, and mdelete.
995 */
996 /*VARARGS*/
997 void
998 setprompt(argc, argv)
999 int argc;
1000 char *argv[];
1001 {
1002
1003 interactive = !interactive;
1004 printf("Interactive mode %s.\n", onoff(interactive));
1005 code = interactive;
1006 }
1007
1008 /*
1009 * Toggle metacharacter interpretation
1010 * on local file names.
1011 */
1012 /*VARARGS*/
1013 void
1014 setglob(argc, argv)
1015 int argc;
1016 char *argv[];
1017 {
1018
1019 doglob = !doglob;
1020 printf("Globbing %s.\n", onoff(doglob));
1021 code = doglob;
1022 }
1023
1024 /*
1025 * Set debugging mode on/off and/or
1026 * set level of debugging.
1027 */
1028 /*VARARGS*/
1029 void
1030 setdebug(argc, argv)
1031 int argc;
1032 char *argv[];
1033 {
1034 int val;
1035
1036 if (argc > 1) {
1037 val = atoi(argv[1]);
1038 if (val < 0) {
1039 printf("%s: bad debugging value.\n", argv[1]);
1040 code = -1;
1041 return;
1042 }
1043 } else
1044 val = !debug;
1045 debug = val;
1046 if (debug)
1047 options |= SO_DEBUG;
1048 else
1049 options &= ~SO_DEBUG;
1050 printf("Debugging %s (debug=%d).\n", onoff(debug), debug);
1051 code = debug > 0;
1052 }
1053
1054 /*
1055 * Set current working directory
1056 * on remote machine.
1057 */
1058 void
1059 cd(argc, argv)
1060 int argc;
1061 char *argv[];
1062 {
1063
1064 if (argc < 2 && !another(&argc, &argv, "remote-directory")) {
1065 printf("usage: %s remote-directory\n", argv[0]);
1066 code = -1;
1067 return;
1068 }
1069 if (command("CWD %s", argv[1]) == ERROR && code == 500) {
1070 if (verbose)
1071 printf("CWD command not recognized, trying XCWD\n");
1072 (void) command("XCWD %s", argv[1]);
1073 }
1074 }
1075
1076 /*
1077 * Set current working directory
1078 * on local machine.
1079 */
1080 void
1081 lcd(argc, argv)
1082 int argc;
1083 char *argv[];
1084 {
1085 char buf[MAXPATHLEN];
1086
1087 if (argc < 2)
1088 argc++, argv[1] = home;
1089 if (argc != 2) {
1090 printf("usage: %s local-directory\n", argv[0]);
1091 code = -1;
1092 return;
1093 }
1094 if (!globulize(&argv[1])) {
1095 code = -1;
1096 return;
1097 }
1098 if (chdir(argv[1]) < 0) {
1099 warn("local: %s", argv[1]);
1100 code = -1;
1101 return;
1102 }
1103 if (getwd(buf) != NULL)
1104 printf("Local directory now %s\n", buf);
1105 else
1106 warnx("getwd: %s", buf);
1107 code = 0;
1108 }
1109
1110 /*
1111 * Delete a single file.
1112 */
1113 void
1114 delete(argc, argv)
1115 int argc;
1116 char *argv[];
1117 {
1118
1119 if (argc < 2 && !another(&argc, &argv, "remote-file")) {
1120 printf("usage: %s remote-file\n", argv[0]);
1121 code = -1;
1122 return;
1123 }
1124 (void) command("DELE %s", argv[1]);
1125 }
1126
1127 /*
1128 * Delete multiple files.
1129 */
1130 void
1131 mdelete(argc, argv)
1132 int argc;
1133 char **argv;
1134 {
1135 sig_t oldintr;
1136 int ointer;
1137 char *cp;
1138
1139 if (argc < 2 && !another(&argc, &argv, "remote-files")) {
1140 printf("usage: %s remote-files\n", argv[0]);
1141 code = -1;
1142 return;
1143 }
1144 mname = argv[0];
1145 mflag = 1;
1146 oldintr = signal(SIGINT, mabort);
1147 (void) setjmp(jabort);
1148 while ((cp = remglob(argv,0)) != NULL) {
1149 if (*cp == '\0') {
1150 mflag = 0;
1151 continue;
1152 }
1153 if (mflag && confirm(argv[0], cp)) {
1154 (void) command("DELE %s", cp);
1155 if (!mflag && fromatty) {
1156 ointer = interactive;
1157 interactive = 1;
1158 if (confirm("Continue with", "mdelete")) {
1159 mflag++;
1160 }
1161 interactive = ointer;
1162 }
1163 }
1164 }
1165 (void) signal(SIGINT, oldintr);
1166 mflag = 0;
1167 }
1168
1169 /*
1170 * Rename a remote file.
1171 */
1172 void
1173 renamefile(argc, argv)
1174 int argc;
1175 char *argv[];
1176 {
1177
1178 if (argc < 2 && !another(&argc, &argv, "from-name"))
1179 goto usage;
1180 if (argc < 3 && !another(&argc, &argv, "to-name")) {
1181 usage:
1182 printf("%s from-name to-name\n", argv[0]);
1183 code = -1;
1184 return;
1185 }
1186 if (command("RNFR %s", argv[1]) == CONTINUE)
1187 (void) command("RNTO %s", argv[2]);
1188 }
1189
1190 /*
1191 * Get a directory listing
1192 * of remote files.
1193 */
1194 void
1195 ls(argc, argv)
1196 int argc;
1197 char *argv[];
1198 {
1199 char *cmd;
1200
1201 if (argc < 2)
1202 argc++, argv[1] = NULL;
1203 if (argc < 3)
1204 argc++, argv[2] = "-";
1205 if (argc > 3) {
1206 printf("usage: %s remote-directory local-file\n", argv[0]);
1207 code = -1;
1208 return;
1209 }
1210 cmd = argv[0][0] == 'n' ? "NLST" : "LIST";
1211 if (strcmp(argv[2], "-") && !globulize(&argv[2])) {
1212 code = -1;
1213 return;
1214 }
1215 if (strcmp(argv[2], "-") && *argv[2] != '|')
1216 if (!globulize(&argv[2]) || !confirm("output to local-file:", argv[2])) {
1217 code = -1;
1218 return;
1219 }
1220 recvrequest(cmd, argv[2], argv[1], "w", 0);
1221
1222 /* flush results in case commands are coming from a pipe */
1223 fflush(stdout);
1224 }
1225
1226 /*
1227 * Get a directory listing
1228 * of multiple remote files.
1229 */
1230 void
1231 mls(argc, argv)
1232 int argc;
1233 char **argv;
1234 {
1235 sig_t oldintr;
1236 int ointer, i;
1237 char *cmd, mode[1], *dest;
1238
1239 if (argc < 2 && !another(&argc, &argv, "remote-files"))
1240 goto usage;
1241 if (argc < 3 && !another(&argc, &argv, "local-file")) {
1242 usage:
1243 printf("usage: %s remote-files local-file\n", argv[0]);
1244 code = -1;
1245 return;
1246 }
1247 dest = argv[argc - 1];
1248 argv[argc - 1] = NULL;
1249 if (strcmp(dest, "-") && *dest != '|')
1250 if (!globulize(&dest) ||
1251 !confirm("output to local-file:", dest)) {
1252 code = -1;
1253 return;
1254 }
1255 cmd = argv[0][1] == 'l' ? "NLST" : "LIST";
1256 mname = argv[0];
1257 mflag = 1;
1258 oldintr = signal(SIGINT, mabort);
1259 (void) setjmp(jabort);
1260 for (i = 1; mflag && i < argc-1; ++i) {
1261 *mode = (i == 1) ? 'w' : 'a';
1262 recvrequest(cmd, dest, argv[i], mode, 0);
1263 if (!mflag && fromatty) {
1264 ointer = interactive;
1265 interactive = 1;
1266 if (confirm("Continue with", argv[0])) {
1267 mflag ++;
1268 }
1269 interactive = ointer;
1270 }
1271 }
1272 (void) signal(SIGINT, oldintr);
1273 mflag = 0;
1274 }
1275
1276 /*
1277 * Do a shell escape
1278 */
1279 /*ARGSUSED*/
1280 void
1281 shell(argc, argv)
1282 int argc;
1283 char **argv;
1284 {
1285 pid_t pid;
1286 sig_t old1, old2;
1287 char shellnam[40], *shell, *namep;
1288 union wait status;
1289
1290 old1 = signal (SIGINT, SIG_IGN);
1291 old2 = signal (SIGQUIT, SIG_IGN);
1292 if ((pid = fork()) == 0) {
1293 for (pid = 3; pid < 20; pid++)
1294 (void) close(pid);
1295 (void) signal(SIGINT, SIG_DFL);
1296 (void) signal(SIGQUIT, SIG_DFL);
1297 shell = getenv("SHELL");
1298 if (shell == NULL)
1299 shell = _PATH_BSHELL;
1300 namep = strrchr(shell,'/');
1301 if (namep == NULL)
1302 namep = shell;
1303 (void) strcpy(shellnam,"-");
1304 (void) strcat(shellnam, ++namep);
1305 if (strcmp(namep, "sh") != 0)
1306 shellnam[0] = '+';
1307 if (debug) {
1308 printf ("%s\n", shell);
1309 (void) fflush (stdout);
1310 }
1311 if (argc > 1) {
1312 execl(shell,shellnam,"-c",altarg,(char *)0);
1313 }
1314 else {
1315 execl(shell,shellnam,(char *)0);
1316 }
1317 warn("%s", shell);
1318 code = -1;
1319 exit(1);
1320 }
1321 if (pid > 0)
1322 while (wait((int *)&status) != pid)
1323 ;
1324 (void) signal(SIGINT, old1);
1325 (void) signal(SIGQUIT, old2);
1326 if (pid == -1) {
1327 warn("%s", "Try again later");
1328 code = -1;
1329 }
1330 else {
1331 code = 0;
1332 }
1333 }
1334
1335 /*
1336 * Send new user information (re-login)
1337 */
1338 void
1339 user(argc, argv)
1340 int argc;
1341 char **argv;
1342 {
1343 char acct[80];
1344 int n, aflag = 0;
1345
1346 if (argc < 2)
1347 (void) another(&argc, &argv, "username");
1348 if (argc < 2 || argc > 4) {
1349 printf("usage: %s username [password] [account]\n", argv[0]);
1350 code = -1;
1351 return;
1352 }
1353 n = command("USER %s", argv[1]);
1354 if (n == CONTINUE) {
1355 if (argc < 3 )
1356 argv[2] = getpass("Password: "), argc++;
1357 n = command("PASS %s", argv[2]);
1358 }
1359 if (n == CONTINUE) {
1360 if (argc < 4) {
1361 printf("Account: "); (void) fflush(stdout);
1362 (void) fgets(acct, sizeof(acct) - 1, stdin);
1363 acct[strlen(acct) - 1] = '\0';
1364 argv[3] = acct; argc++;
1365 }
1366 n = command("ACCT %s", argv[3]);
1367 aflag++;
1368 }
1369 if (n != COMPLETE) {
1370 fprintf(stdout, "Login failed.\n");
1371 return;
1372 }
1373 if (!aflag && argc == 4) {
1374 (void) command("ACCT %s", argv[3]);
1375 }
1376 }
1377
1378 /*
1379 * Print working directory.
1380 */
1381 /*VARARGS*/
1382 void
1383 pwd(argc, argv)
1384 int argc;
1385 char *argv[];
1386 {
1387 int oldverbose = verbose;
1388
1389 /*
1390 * If we aren't verbose, this doesn't do anything!
1391 */
1392 verbose = 1;
1393 if (command("PWD") == ERROR && code == 500) {
1394 printf("PWD command not recognized, trying XPWD\n");
1395 (void) command("XPWD");
1396 }
1397 verbose = oldverbose;
1398 }
1399
1400 /*
1401 * Make a directory.
1402 */
1403 void
1404 makedir(argc, argv)
1405 int argc;
1406 char *argv[];
1407 {
1408
1409 if (argc < 2 && !another(&argc, &argv, "directory-name")) {
1410 printf("usage: %s directory-name\n", argv[0]);
1411 code = -1;
1412 return;
1413 }
1414 if (command("MKD %s", argv[1]) == ERROR && code == 500) {
1415 if (verbose)
1416 printf("MKD command not recognized, trying XMKD\n");
1417 (void) command("XMKD %s", argv[1]);
1418 }
1419 }
1420
1421 /*
1422 * Remove a directory.
1423 */
1424 void
1425 removedir(argc, argv)
1426 int argc;
1427 char *argv[];
1428 {
1429
1430 if (argc < 2 && !another(&argc, &argv, "directory-name")) {
1431 printf("usage: %s directory-name\n", argv[0]);
1432 code = -1;
1433 return;
1434 }
1435 if (command("RMD %s", argv[1]) == ERROR && code == 500) {
1436 if (verbose)
1437 printf("RMD command not recognized, trying XRMD\n");
1438 (void) command("XRMD %s", argv[1]);
1439 }
1440 }
1441
1442 /*
1443 * Send a line, verbatim, to the remote machine.
1444 */
1445 void
1446 quote(argc, argv)
1447 int argc;
1448 char *argv[];
1449 {
1450
1451 if (argc < 2 && !another(&argc, &argv, "command line to send")) {
1452 printf("usage: %s line-to-send\n", argv[0]);
1453 code = -1;
1454 return;
1455 }
1456 quote1("", argc, argv);
1457 }
1458
1459 /*
1460 * Send a SITE command to the remote machine. The line
1461 * is sent verbatim to the remote machine, except that the
1462 * word "SITE" is added at the front.
1463 */
1464 void
1465 site(argc, argv)
1466 int argc;
1467 char *argv[];
1468 {
1469
1470 if (argc < 2 && !another(&argc, &argv, "arguments to SITE command")) {
1471 printf("usage: %s line-to-send\n", argv[0]);
1472 code = -1;
1473 return;
1474 }
1475 quote1("SITE ", argc, argv);
1476 }
1477
1478 /*
1479 * Turn argv[1..argc) into a space-separated string, then prepend initial text.
1480 * Send the result as a one-line command and get response.
1481 */
1482 void
1483 quote1(initial, argc, argv)
1484 char *initial;
1485 int argc;
1486 char **argv;
1487 {
1488 int i, len;
1489 char buf[BUFSIZ]; /* must be >= sizeof(line) */
1490
1491 (void) strcpy(buf, initial);
1492 if (argc > 1) {
1493 len = strlen(buf);
1494 len += strlen(strcpy(&buf[len], argv[1]));
1495 for (i = 2; i < argc; i++) {
1496 buf[len++] = ' ';
1497 len += strlen(strcpy(&buf[len], argv[i]));
1498 }
1499 }
1500 if (command(buf) == PRELIM) {
1501 while (getreply(0) == PRELIM)
1502 continue;
1503 }
1504 }
1505
1506 void
1507 do_chmod(argc, argv)
1508 int argc;
1509 char *argv[];
1510 {
1511
1512 if (argc < 2 && !another(&argc, &argv, "mode"))
1513 goto usage;
1514 if (argc < 3 && !another(&argc, &argv, "file-name")) {
1515 usage:
1516 printf("usage: %s mode file-name\n", argv[0]);
1517 code = -1;
1518 return;
1519 }
1520 (void) command("SITE CHMOD %s %s", argv[1], argv[2]);
1521 }
1522
1523 void
1524 do_umask(argc, argv)
1525 int argc;
1526 char *argv[];
1527 {
1528 int oldverbose = verbose;
1529
1530 verbose = 1;
1531 (void) command(argc == 1 ? "SITE UMASK" : "SITE UMASK %s", argv[1]);
1532 verbose = oldverbose;
1533 }
1534
1535 void
1536 idle(argc, argv)
1537 int argc;
1538 char *argv[];
1539 {
1540 int oldverbose = verbose;
1541
1542 verbose = 1;
1543 (void) command(argc == 1 ? "SITE IDLE" : "SITE IDLE %s", argv[1]);
1544 verbose = oldverbose;
1545 }
1546
1547 /*
1548 * Ask the other side for help.
1549 */
1550 void
1551 rmthelp(argc, argv)
1552 int argc;
1553 char *argv[];
1554 {
1555 int oldverbose = verbose;
1556
1557 verbose = 1;
1558 (void) command(argc == 1 ? "HELP" : "HELP %s", argv[1]);
1559 verbose = oldverbose;
1560 }
1561
1562 /*
1563 * Terminate session and exit.
1564 */
1565 /*VARARGS*/
1566 void
1567 quit(argc, argv)
1568 int argc;
1569 char *argv[];
1570 {
1571
1572 if (connected)
1573 disconnect(0, 0);
1574 pswitch(1);
1575 if (connected) {
1576 disconnect(0, 0);
1577 }
1578 exit(0);
1579 }
1580
1581 /*
1582 * Terminate session, but don't exit.
1583 */
1584 void
1585 disconnect(argc, argv)
1586 int argc;
1587 char *argv[];
1588 {
1589
1590 if (!connected)
1591 return;
1592 (void) command("QUIT");
1593 if (cout) {
1594 (void) fclose(cout);
1595 }
1596 cout = NULL;
1597 connected = 0;
1598 data = -1;
1599 if (!proxy) {
1600 macnum = 0;
1601 }
1602 }
1603
1604 int
1605 confirm(cmd, file)
1606 char *cmd, *file;
1607 {
1608 char line[BUFSIZ];
1609
1610 if (!interactive)
1611 return (1);
1612 printf("%s %s? ", cmd, file);
1613 (void) fflush(stdout);
1614 if (fgets(line, sizeof line, stdin) == NULL)
1615 return (0);
1616 return (*line != 'n' && *line != 'N');
1617 }
1618
1619 void
1620 fatal(msg)
1621 char *msg;
1622 {
1623
1624 errx(1, "%s", msg);
1625 }
1626
1627 /*
1628 * Glob a local file name specification with
1629 * the expectation of a single return value.
1630 * Can't control multiple values being expanded
1631 * from the expression, we return only the first.
1632 */
1633 int
1634 globulize(cpp)
1635 char **cpp;
1636 {
1637 glob_t gl;
1638 int flags;
1639
1640 if (!doglob)
1641 return (1);
1642
1643 flags = GLOB_BRACE|GLOB_NOCHECK|GLOB_QUOTE|GLOB_TILDE;
1644 memset(&gl, 0, sizeof(gl));
1645 if (glob(*cpp, flags, NULL, &gl) ||
1646 gl.gl_pathc == 0) {
1647 warnx("%s: not found", *cpp);
1648 globfree(&gl);
1649 return (0);
1650 }
1651 *cpp = strdup(gl.gl_pathv[0]); /* XXX - wasted memory */
1652 globfree(&gl);
1653 return (1);
1654 }
1655
1656 void
1657 account(argc,argv)
1658 int argc;
1659 char **argv;
1660 {
1661 char acct[50], *ap;
1662
1663 if (argc > 1) {
1664 ++argv;
1665 --argc;
1666 (void) strncpy(acct,*argv,49);
1667 acct[49] = '\0';
1668 while (argc > 1) {
1669 --argc;
1670 ++argv;
1671 (void) strncat(acct,*argv, 49-strlen(acct));
1672 }
1673 ap = acct;
1674 }
1675 else {
1676 ap = getpass("Account:");
1677 }
1678 (void) command("ACCT %s", ap);
1679 }
1680
1681 jmp_buf abortprox;
1682
1683 void
1684 proxabort()
1685 {
1686
1687 if (!proxy) {
1688 pswitch(1);
1689 }
1690 if (connected) {
1691 proxflag = 1;
1692 }
1693 else {
1694 proxflag = 0;
1695 }
1696 pswitch(0);
1697 longjmp(abortprox,1);
1698 }
1699
1700 void
1701 doproxy(argc, argv)
1702 int argc;
1703 char *argv[];
1704 {
1705 struct cmd *c;
1706 sig_t oldintr;
1707
1708 if (argc < 2 && !another(&argc, &argv, "command")) {
1709 printf("usage: %s command\n", argv[0]);
1710 code = -1;
1711 return;
1712 }
1713 c = getcmd(argv[1]);
1714 if (c == (struct cmd *) -1) {
1715 printf("?Ambiguous command\n");
1716 (void) fflush(stdout);
1717 code = -1;
1718 return;
1719 }
1720 if (c == 0) {
1721 printf("?Invalid command\n");
1722 (void) fflush(stdout);
1723 code = -1;
1724 return;
1725 }
1726 if (!c->c_proxy) {
1727 printf("?Invalid proxy command\n");
1728 (void) fflush(stdout);
1729 code = -1;
1730 return;
1731 }
1732 if (setjmp(abortprox)) {
1733 code = -1;
1734 return;
1735 }
1736 oldintr = signal(SIGINT, proxabort);
1737 pswitch(1);
1738 if (c->c_conn && !connected) {
1739 printf("Not connected\n");
1740 (void) fflush(stdout);
1741 pswitch(0);
1742 (void) signal(SIGINT, oldintr);
1743 code = -1;
1744 return;
1745 }
1746 (*c->c_handler)(argc-1, argv+1);
1747 if (connected) {
1748 proxflag = 1;
1749 }
1750 else {
1751 proxflag = 0;
1752 }
1753 pswitch(0);
1754 (void) signal(SIGINT, oldintr);
1755 }
1756
1757 void
1758 setcase(argc, argv)
1759 int argc;
1760 char *argv[];
1761 {
1762
1763 mcase = !mcase;
1764 printf("Case mapping %s.\n", onoff(mcase));
1765 code = mcase;
1766 }
1767
1768 void
1769 setcr(argc, argv)
1770 int argc;
1771 char *argv[];
1772 {
1773
1774 crflag = !crflag;
1775 printf("Carriage Return stripping %s.\n", onoff(crflag));
1776 code = crflag;
1777 }
1778
1779 void
1780 setntrans(argc,argv)
1781 int argc;
1782 char *argv[];
1783 {
1784 if (argc == 1) {
1785 ntflag = 0;
1786 printf("Ntrans off.\n");
1787 code = ntflag;
1788 return;
1789 }
1790 ntflag++;
1791 code = ntflag;
1792 (void) strncpy(ntin, argv[1], 16);
1793 ntin[16] = '\0';
1794 if (argc == 2) {
1795 ntout[0] = '\0';
1796 return;
1797 }
1798 (void) strncpy(ntout, argv[2], 16);
1799 ntout[16] = '\0';
1800 }
1801
1802 char *
1803 dotrans(name)
1804 char *name;
1805 {
1806 static char new[MAXPATHLEN];
1807 char *cp1, *cp2 = new;
1808 int i, ostop, found;
1809
1810 for (ostop = 0; *(ntout + ostop) && ostop < 16; ostop++)
1811 continue;
1812 for (cp1 = name; *cp1; cp1++) {
1813 found = 0;
1814 for (i = 0; *(ntin + i) && i < 16; i++) {
1815 if (*cp1 == *(ntin + i)) {
1816 found++;
1817 if (i < ostop) {
1818 *cp2++ = *(ntout + i);
1819 }
1820 break;
1821 }
1822 }
1823 if (!found) {
1824 *cp2++ = *cp1;
1825 }
1826 }
1827 *cp2 = '\0';
1828 return (new);
1829 }
1830
1831 void
1832 setnmap(argc, argv)
1833 int argc;
1834 char *argv[];
1835 {
1836 char *cp;
1837
1838 if (argc == 1) {
1839 mapflag = 0;
1840 printf("Nmap off.\n");
1841 code = mapflag;
1842 return;
1843 }
1844 if (argc < 3 && !another(&argc, &argv, "mapout")) {
1845 printf("Usage: %s [mapin mapout]\n",argv[0]);
1846 code = -1;
1847 return;
1848 }
1849 mapflag = 1;
1850 code = 1;
1851 cp = strchr(altarg, ' ');
1852 if (proxy) {
1853 while(*++cp == ' ')
1854 continue;
1855 altarg = cp;
1856 cp = strchr(altarg, ' ');
1857 }
1858 *cp = '\0';
1859 (void) strncpy(mapin, altarg, MAXPATHLEN - 1);
1860 while (*++cp == ' ')
1861 continue;
1862 (void) strncpy(mapout, cp, MAXPATHLEN - 1);
1863 }
1864
1865 char *
1866 domap(name)
1867 char *name;
1868 {
1869 static char new[MAXPATHLEN];
1870 char *cp1 = name, *cp2 = mapin;
1871 char *tp[9], *te[9];
1872 int i, toks[9], toknum = 0, match = 1;
1873
1874 for (i=0; i < 9; ++i) {
1875 toks[i] = 0;
1876 }
1877 while (match && *cp1 && *cp2) {
1878 switch (*cp2) {
1879 case '\\':
1880 if (*++cp2 != *cp1) {
1881 match = 0;
1882 }
1883 break;
1884 case '$':
1885 if (*(cp2+1) >= '1' && (*cp2+1) <= '9') {
1886 if (*cp1 != *(++cp2+1)) {
1887 toks[toknum = *cp2 - '1']++;
1888 tp[toknum] = cp1;
1889 while (*++cp1 && *(cp2+1)
1890 != *cp1);
1891 te[toknum] = cp1;
1892 }
1893 cp2++;
1894 break;
1895 }
1896 /* FALLTHROUGH */
1897 default:
1898 if (*cp2 != *cp1) {
1899 match = 0;
1900 }
1901 break;
1902 }
1903 if (match && *cp1) {
1904 cp1++;
1905 }
1906 if (match && *cp2) {
1907 cp2++;
1908 }
1909 }
1910 if (!match && *cp1) /* last token mismatch */
1911 {
1912 toks[toknum] = 0;
1913 }
1914 cp1 = new;
1915 *cp1 = '\0';
1916 cp2 = mapout;
1917 while (*cp2) {
1918 match = 0;
1919 switch (*cp2) {
1920 case '\\':
1921 if (*(cp2 + 1)) {
1922 *cp1++ = *++cp2;
1923 }
1924 break;
1925 case '[':
1926 LOOP:
1927 if (*++cp2 == '$' && isdigit(*(cp2+1))) {
1928 if (*++cp2 == '0') {
1929 char *cp3 = name;
1930
1931 while (*cp3) {
1932 *cp1++ = *cp3++;
1933 }
1934 match = 1;
1935 }
1936 else if (toks[toknum = *cp2 - '1']) {
1937 char *cp3 = tp[toknum];
1938
1939 while (cp3 != te[toknum]) {
1940 *cp1++ = *cp3++;
1941 }
1942 match = 1;
1943 }
1944 }
1945 else {
1946 while (*cp2 && *cp2 != ',' &&
1947 *cp2 != ']') {
1948 if (*cp2 == '\\') {
1949 cp2++;
1950 }
1951 else if (*cp2 == '$' &&
1952 isdigit(*(cp2+1))) {
1953 if (*++cp2 == '0') {
1954 char *cp3 = name;
1955
1956 while (*cp3) {
1957 *cp1++ = *cp3++;
1958 }
1959 }
1960 else if (toks[toknum =
1961 *cp2 - '1']) {
1962 char *cp3=tp[toknum];
1963
1964 while (cp3 !=
1965 te[toknum]) {
1966 *cp1++ = *cp3++;
1967 }
1968 }
1969 }
1970 else if (*cp2) {
1971 *cp1++ = *cp2++;
1972 }
1973 }
1974 if (!*cp2) {
1975 printf("nmap: unbalanced brackets\n");
1976 return (name);
1977 }
1978 match = 1;
1979 cp2--;
1980 }
1981 if (match) {
1982 while (*++cp2 && *cp2 != ']') {
1983 if (*cp2 == '\\' && *(cp2 + 1)) {
1984 cp2++;
1985 }
1986 }
1987 if (!*cp2) {
1988 printf("nmap: unbalanced brackets\n");
1989 return (name);
1990 }
1991 break;
1992 }
1993 switch (*++cp2) {
1994 case ',':
1995 goto LOOP;
1996 case ']':
1997 break;
1998 default:
1999 cp2--;
2000 goto LOOP;
2001 }
2002 break;
2003 case '$':
2004 if (isdigit(*(cp2 + 1))) {
2005 if (*++cp2 == '0') {
2006 char *cp3 = name;
2007
2008 while (*cp3) {
2009 *cp1++ = *cp3++;
2010 }
2011 }
2012 else if (toks[toknum = *cp2 - '1']) {
2013 char *cp3 = tp[toknum];
2014
2015 while (cp3 != te[toknum]) {
2016 *cp1++ = *cp3++;
2017 }
2018 }
2019 break;
2020 }
2021 /* intentional drop through */
2022 default:
2023 *cp1++ = *cp2;
2024 break;
2025 }
2026 cp2++;
2027 }
2028 *cp1 = '\0';
2029 if (!*new) {
2030 return (name);
2031 }
2032 return (new);
2033 }
2034
2035 void
2036 setpassive(argc, argv)
2037 int argc;
2038 char *argv[];
2039 {
2040
2041 passivemode = !passivemode;
2042 printf("Passive mode %s.\n", onoff(passivemode));
2043 code = passivemode;
2044 }
2045
2046 void
2047 setsunique(argc, argv)
2048 int argc;
2049 char *argv[];
2050 {
2051
2052 sunique = !sunique;
2053 printf("Store unique %s.\n", onoff(sunique));
2054 code = sunique;
2055 }
2056
2057 void
2058 setrunique(argc, argv)
2059 int argc;
2060 char *argv[];
2061 {
2062
2063 runique = !runique;
2064 printf("Receive unique %s.\n", onoff(runique));
2065 code = runique;
2066 }
2067
2068 /* change directory to perent directory */
2069 void
2070 cdup(argc, argv)
2071 int argc;
2072 char *argv[];
2073 {
2074
2075 if (command("CDUP") == ERROR && code == 500) {
2076 if (verbose)
2077 printf("CDUP command not recognized, trying XCUP\n");
2078 (void) command("XCUP");
2079 }
2080 }
2081
2082 /* restart transfer at specific point */
2083 void
2084 restart(argc, argv)
2085 int argc;
2086 char *argv[];
2087 {
2088
2089 if (argc != 2)
2090 printf("restart: offset not specified\n");
2091 else {
2092 restart_point = atol(argv[1]);
2093 printf("restarting at %qd. %s\n", restart_point,
2094 "execute get, put or append to initiate transfer");
2095 }
2096 }
2097
2098 /* show remote system type */
2099 void
2100 syst(argc, argv)
2101 int argc;
2102 char *argv[];
2103 {
2104
2105 (void) command("SYST");
2106 }
2107
2108 void
2109 macdef(argc, argv)
2110 int argc;
2111 char *argv[];
2112 {
2113 char *tmp;
2114 int c;
2115
2116 if (macnum == 16) {
2117 printf("Limit of 16 macros have already been defined\n");
2118 code = -1;
2119 return;
2120 }
2121 if (argc < 2 && !another(&argc, &argv, "macro name")) {
2122 printf("Usage: %s macro_name\n",argv[0]);
2123 code = -1;
2124 return;
2125 }
2126 if (interactive) {
2127 printf("Enter macro line by line, terminating it with a null line\n");
2128 }
2129 (void) strncpy(macros[macnum].mac_name, argv[1], 8);
2130 if (macnum == 0) {
2131 macros[macnum].mac_start = macbuf;
2132 }
2133 else {
2134 macros[macnum].mac_start = macros[macnum - 1].mac_end + 1;
2135 }
2136 tmp = macros[macnum].mac_start;
2137 while (tmp != macbuf+4096) {
2138 if ((c = getchar()) == EOF) {
2139 printf("macdef:end of file encountered\n");
2140 code = -1;
2141 return;
2142 }
2143 if ((*tmp = c) == '\n') {
2144 if (tmp == macros[macnum].mac_start) {
2145 macros[macnum++].mac_end = tmp;
2146 code = 0;
2147 return;
2148 }
2149 if (*(tmp-1) == '\0') {
2150 macros[macnum++].mac_end = tmp - 1;
2151 code = 0;
2152 return;
2153 }
2154 *tmp = '\0';
2155 }
2156 tmp++;
2157 }
2158 while (1) {
2159 while ((c = getchar()) != '\n' && c != EOF)
2160 /* LOOP */;
2161 if (c == EOF || getchar() == '\n') {
2162 printf("Macro not defined - 4k buffer exceeded\n");
2163 code = -1;
2164 return;
2165 }
2166 }
2167 }
2168
2169 /*
2170 * get size of file on remote machine
2171 */
2172 void
2173 sizecmd(argc, argv)
2174 int argc;
2175 char *argv[];
2176 {
2177
2178 if (argc < 2 && !another(&argc, &argv, "filename")) {
2179 printf("usage: %s filename\n", argv[0]);
2180 code = -1;
2181 return;
2182 }
2183 (void) command("SIZE %s", argv[1]);
2184 }
2185
2186 /*
2187 * get last modification time of file on remote machine
2188 */
2189 void
2190 modtime(argc, argv)
2191 int argc;
2192 char *argv[];
2193 {
2194 int overbose;
2195
2196 if (argc < 2 && !another(&argc, &argv, "filename")) {
2197 printf("usage: %s filename\n", argv[0]);
2198 code = -1;
2199 return;
2200 }
2201 overbose = verbose;
2202 if (debug == 0)
2203 verbose = -1;
2204 if (command("MDTM %s", argv[1]) == COMPLETE) {
2205 int yy, mo, day, hour, min, sec;
2206 sscanf(reply_string, "%*s %04d%02d%02d%02d%02d%02d", &yy, &mo,
2207 &day, &hour, &min, &sec);
2208 /* might want to print this in local time */
2209 printf("%s\t%02d/%02d/%04d %02d:%02d:%02d GMT\n", argv[1],
2210 mo, day, yy, hour, min, sec);
2211 } else
2212 printf("%s\n", reply_string);
2213 verbose = overbose;
2214 }
2215
2216 /*
2217 * show status on reomte machine
2218 */
2219 void
2220 rmtstatus(argc, argv)
2221 int argc;
2222 char *argv[];
2223 {
2224
2225 (void) command(argc > 1 ? "STAT %s" : "STAT" , argv[1]);
2226 }
2227
2228 /*
2229 * get file if modtime is more recent than current file
2230 */
2231 void
2232 newer(argc, argv)
2233 int argc;
2234 char *argv[];
2235 {
2236
2237 if (getit(argc, argv, -1, "w"))
2238 printf("Local file \"%s\" is newer than remote file \"%s\"\n",
2239 argv[2], argv[1]);
2240 }
2241