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