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