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