17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
50b6880ccSsp149894  * Common Development and Distribution License (the "License").
60b6880ccSsp149894  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
220b6880ccSsp149894  *	Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  *	Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
277c478bd9Sstevel@tonic-gate /*	All Rights Reserved  	*/
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate /*
307c478bd9Sstevel@tonic-gate  *	University Copyright- Copyright (c) 1982, 1986, 1988
317c478bd9Sstevel@tonic-gate  *	The Regents of the University of California
327c478bd9Sstevel@tonic-gate  *	All Rights Reserved
337c478bd9Sstevel@tonic-gate  *
347c478bd9Sstevel@tonic-gate  *	University Acknowledgment- Portions of this document are derived from
357c478bd9Sstevel@tonic-gate  *	software developed by the University of California, Berkeley, and its
367c478bd9Sstevel@tonic-gate  *	contributors.
377c478bd9Sstevel@tonic-gate  */
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate /*
407c478bd9Sstevel@tonic-gate  * FTP User Program -- Command Routines.
417c478bd9Sstevel@tonic-gate  */
427c478bd9Sstevel@tonic-gate #define	FTP_NAMES
437c478bd9Sstevel@tonic-gate #include "ftp_var.h"
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate FILE	*tmp_nlst = NULL;	/* tmp file; holds NLST results for mget, etc */
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate static char *mname;
487c478bd9Sstevel@tonic-gate static jmp_buf jabort;
497c478bd9Sstevel@tonic-gate static jmp_buf abortprox;
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate static char *remglob(char *argv[], int doswitch);
527c478bd9Sstevel@tonic-gate static char *onoff(int bool);
537c478bd9Sstevel@tonic-gate static int confirm(char *cmd, char *file);
547c478bd9Sstevel@tonic-gate static int globulize(char **cpp);
557c478bd9Sstevel@tonic-gate static void proxabort(int sig);
567c478bd9Sstevel@tonic-gate static void mabort(int sig);
577c478bd9Sstevel@tonic-gate static char *dotrans(char *name);
587c478bd9Sstevel@tonic-gate static char *domap(char *name);
597c478bd9Sstevel@tonic-gate static void getit(int argc, char *argv[], int restartit, char *mode);
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate static char *getlevel(int);
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate /* Prompt for command argument, add to buffer with space separator */
647c478bd9Sstevel@tonic-gate static int
prompt_for_arg(char * buffer,int buffer_size,char * prompt)657c478bd9Sstevel@tonic-gate prompt_for_arg(char *buffer, int buffer_size, char *prompt)
667c478bd9Sstevel@tonic-gate {
677c478bd9Sstevel@tonic-gate 	if (strlen(buffer) > buffer_size - 2) {
687c478bd9Sstevel@tonic-gate 		(void) printf("Line too long\n");
697c478bd9Sstevel@tonic-gate 		return (-1);
707c478bd9Sstevel@tonic-gate 	}
717c478bd9Sstevel@tonic-gate 	strcat(buffer, " ");
727c478bd9Sstevel@tonic-gate 	stop_timer();
737c478bd9Sstevel@tonic-gate 	(void) printf("(%s) ", prompt);
747c478bd9Sstevel@tonic-gate 	if (fgets(buffer + strlen(buffer), buffer_size - strlen(buffer), stdin)
757c478bd9Sstevel@tonic-gate 	    == NULL) {
767c478bd9Sstevel@tonic-gate 		reset_timer();
777c478bd9Sstevel@tonic-gate 		return (-1);
787c478bd9Sstevel@tonic-gate 	}
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate 	/* Flush what didn't fit in the buffer */
817c478bd9Sstevel@tonic-gate 	if (buffer[strlen(buffer)-1] != '\n') {
827c478bd9Sstevel@tonic-gate 		while (fgetc(stdin) != '\n' && !ferror(stdin) && !feof(stdin))
837c478bd9Sstevel@tonic-gate 			;
847c478bd9Sstevel@tonic-gate 		(void) printf("Line too long\n");
857c478bd9Sstevel@tonic-gate 		reset_timer();
867c478bd9Sstevel@tonic-gate 		return (-1);
877c478bd9Sstevel@tonic-gate 	} else
887c478bd9Sstevel@tonic-gate 		buffer[strlen(buffer)-1] = 0;
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate 	reset_timer();
917c478bd9Sstevel@tonic-gate 	return (0);
927c478bd9Sstevel@tonic-gate }
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate /*
967c478bd9Sstevel@tonic-gate  * Connect to peer server and
977c478bd9Sstevel@tonic-gate  * auto-login, if possible.
987c478bd9Sstevel@tonic-gate  */
997c478bd9Sstevel@tonic-gate void
setpeer(int argc,char * argv[])1007c478bd9Sstevel@tonic-gate setpeer(int argc, char *argv[])
1017c478bd9Sstevel@tonic-gate {
1027c478bd9Sstevel@tonic-gate 	char *host;
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate 	if (connected) {
1057c478bd9Sstevel@tonic-gate 		(void) printf("Already connected to %s, use close first.\n",
1067c478bd9Sstevel@tonic-gate 			hostname);
1077c478bd9Sstevel@tonic-gate 		code = -1;
1087c478bd9Sstevel@tonic-gate 		return;
1097c478bd9Sstevel@tonic-gate 	}
1107c478bd9Sstevel@tonic-gate 	if (argc < 2) {
1117c478bd9Sstevel@tonic-gate 		if (prompt_for_arg(line, sizeof (line), "to") == -1) {
1127c478bd9Sstevel@tonic-gate 			code = -1;
1137c478bd9Sstevel@tonic-gate 			return;
1147c478bd9Sstevel@tonic-gate 		}
1157c478bd9Sstevel@tonic-gate 		makeargv();
1167c478bd9Sstevel@tonic-gate 		argc = margc;
1177c478bd9Sstevel@tonic-gate 		argv = margv;
1187c478bd9Sstevel@tonic-gate 	}
1197c478bd9Sstevel@tonic-gate 	if (argc > 3 || argc < 2) {
1207c478bd9Sstevel@tonic-gate 		(void) printf("usage: %s host-name [port]\n", argv[0]);
1217c478bd9Sstevel@tonic-gate 		code = -1;
1227c478bd9Sstevel@tonic-gate 		return;
1237c478bd9Sstevel@tonic-gate 	}
1247c478bd9Sstevel@tonic-gate 	strcpy(typename, "ascii");
1257c478bd9Sstevel@tonic-gate 	host = hookup(argv[1], (argc > 2 ? argv[2] : "ftp"));
1267c478bd9Sstevel@tonic-gate 	if (host) {
1277c478bd9Sstevel@tonic-gate 		int overbose;
1287c478bd9Sstevel@tonic-gate 		extern char reply_string[];
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate 		connected = 1;
1317c478bd9Sstevel@tonic-gate 		/*
1327c478bd9Sstevel@tonic-gate 		 * Set up defaults for FTP.
1337c478bd9Sstevel@tonic-gate 		 */
1347c478bd9Sstevel@tonic-gate 		clevel = dlevel = PROT_C;
1357c478bd9Sstevel@tonic-gate 		if (autoauth) {
1367c478bd9Sstevel@tonic-gate 			if (do_auth() && autoencrypt) {
1377c478bd9Sstevel@tonic-gate 			    clevel = PROT_P;
1387c478bd9Sstevel@tonic-gate 			    setpbsz(1<<20);
1397c478bd9Sstevel@tonic-gate 			    if (command("PROT P") == COMPLETE)
1407c478bd9Sstevel@tonic-gate 				dlevel = PROT_P;
1417c478bd9Sstevel@tonic-gate 			    else {
1427c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
1437c478bd9Sstevel@tonic-gate 					"%s: couldn't enable encryption\n",
1447c478bd9Sstevel@tonic-gate 					argv[0]);
1457c478bd9Sstevel@tonic-gate 				/* unable to encrypt command channel, too! */
1467c478bd9Sstevel@tonic-gate 				dlevel = clevel = PROT_C;
1477c478bd9Sstevel@tonic-gate 			    }
1487c478bd9Sstevel@tonic-gate 			}
1497c478bd9Sstevel@tonic-gate 			if ((auth_type != AUTHTYPE_NONE) && (clevel == PROT_C))
1507c478bd9Sstevel@tonic-gate 				clevel = PROT_S;
1517c478bd9Sstevel@tonic-gate 		}
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate 		if (autologin)
1547c478bd9Sstevel@tonic-gate 			(void) login(argv[1]);
1550b6880ccSsp149894 		/* if skipsyst is enabled, then don't send SYST command */
1560b6880ccSsp149894 		if (skipsyst)
1570b6880ccSsp149894 			return;
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate 		overbose = verbose;
1607c478bd9Sstevel@tonic-gate 		if (debug == 0)
1617c478bd9Sstevel@tonic-gate 			verbose = -1;
1627c478bd9Sstevel@tonic-gate 		if (command("SYST") == COMPLETE && overbose) {
1637c478bd9Sstevel@tonic-gate 			char *cp, c;
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate 			cp = index(reply_string+4, ' ');
1667c478bd9Sstevel@tonic-gate 			if (cp == NULL)
1677c478bd9Sstevel@tonic-gate 				cp = index(reply_string+4, '\r');
1687c478bd9Sstevel@tonic-gate 			if (cp) {
1697c478bd9Sstevel@tonic-gate 				if (cp[-1] == '.')
1707c478bd9Sstevel@tonic-gate 					cp--;
1717c478bd9Sstevel@tonic-gate 				c = *cp;
1727c478bd9Sstevel@tonic-gate 				*cp = '\0';
1737c478bd9Sstevel@tonic-gate 			}
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate 			(void) printf("Remote system type is %s.\n",
1767c478bd9Sstevel@tonic-gate 				reply_string+4);
1777c478bd9Sstevel@tonic-gate 			if (cp)
1787c478bd9Sstevel@tonic-gate 				*cp = c;
1797c478bd9Sstevel@tonic-gate 		}
1807c478bd9Sstevel@tonic-gate 		if (strncmp(reply_string, "215 UNIX Type: L8", 17) == 0) {
1817c478bd9Sstevel@tonic-gate 			setbinary(0, NULL);
1827c478bd9Sstevel@tonic-gate 			if (overbose)
1837c478bd9Sstevel@tonic-gate 				(void) printf(
1847c478bd9Sstevel@tonic-gate 				    "Using %s mode to transfer files.\n",
1857c478bd9Sstevel@tonic-gate 				    typename);
1867c478bd9Sstevel@tonic-gate 		} else if (overbose &&
1877c478bd9Sstevel@tonic-gate 		    strncmp(reply_string, "215 TOPS20", 10) == 0) {
1887c478bd9Sstevel@tonic-gate 			(void) printf(
1897c478bd9Sstevel@tonic-gate 			    "Remember to set tenex mode when transfering "
1907c478bd9Sstevel@tonic-gate 			    "binary files from this machine.\n");
1917c478bd9Sstevel@tonic-gate 		}
1927c478bd9Sstevel@tonic-gate 		verbose = overbose;
1937c478bd9Sstevel@tonic-gate 	}
1947c478bd9Sstevel@tonic-gate }
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate static struct types {
1977c478bd9Sstevel@tonic-gate 	char	*t_name;
1987c478bd9Sstevel@tonic-gate 	char	*t_mode;
1997c478bd9Sstevel@tonic-gate 	int	t_type;
2007c478bd9Sstevel@tonic-gate 	char	*t_arg;
2017c478bd9Sstevel@tonic-gate } types[] = {
2027c478bd9Sstevel@tonic-gate 	{ "ascii",	"A",	TYPE_A,	0 },
2037c478bd9Sstevel@tonic-gate 	{ "binary",	"I",	TYPE_I,	0 },
2047c478bd9Sstevel@tonic-gate 	{ "image",	"I",	TYPE_I,	0 },
2057c478bd9Sstevel@tonic-gate 	{ "ebcdic",	"E",	TYPE_E,	0 },
2067c478bd9Sstevel@tonic-gate 	{ "tenex",	"L",	TYPE_L,	bytename },
2077c478bd9Sstevel@tonic-gate 	0
2087c478bd9Sstevel@tonic-gate };
2097c478bd9Sstevel@tonic-gate 
2107c478bd9Sstevel@tonic-gate /*
2117c478bd9Sstevel@tonic-gate  * Set transfer type.
2127c478bd9Sstevel@tonic-gate  */
2137c478bd9Sstevel@tonic-gate void
settype(int argc,char * argv[])2147c478bd9Sstevel@tonic-gate settype(int argc, char *argv[])
2157c478bd9Sstevel@tonic-gate {
2167c478bd9Sstevel@tonic-gate 	struct types *p;
2177c478bd9Sstevel@tonic-gate 	int comret;
2187c478bd9Sstevel@tonic-gate 
2197c478bd9Sstevel@tonic-gate 	if (argc > 2) {
2207c478bd9Sstevel@tonic-gate 		char *sep;
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate 		(void) printf("usage: %s [", argv[0]);
2237c478bd9Sstevel@tonic-gate 		sep = " ";
2247c478bd9Sstevel@tonic-gate 		for (p = types; p->t_name; p++) {
2257c478bd9Sstevel@tonic-gate 			(void) printf("%s%s", sep, p->t_name);
2267c478bd9Sstevel@tonic-gate 			if (*sep == ' ')
2277c478bd9Sstevel@tonic-gate 				sep = " | ";
2287c478bd9Sstevel@tonic-gate 		}
2297c478bd9Sstevel@tonic-gate 		(void) printf(" ]\n");
2307c478bd9Sstevel@tonic-gate 		code = -1;
2317c478bd9Sstevel@tonic-gate 		return;
2327c478bd9Sstevel@tonic-gate 	}
2337c478bd9Sstevel@tonic-gate 	if (argc < 2) {
2347c478bd9Sstevel@tonic-gate 		(void) printf("Using %s mode to transfer files.\n", typename);
2357c478bd9Sstevel@tonic-gate 		code = 0;
2367c478bd9Sstevel@tonic-gate 		return;
2377c478bd9Sstevel@tonic-gate 	}
2387c478bd9Sstevel@tonic-gate 	for (p = types; p->t_name; p++)
2397c478bd9Sstevel@tonic-gate 		if (strcmp(argv[1], p->t_name) == 0)
2407c478bd9Sstevel@tonic-gate 			break;
2417c478bd9Sstevel@tonic-gate 	if (p->t_name == 0) {
2427c478bd9Sstevel@tonic-gate 		(void) printf("%s: unknown mode\n", argv[1]);
2437c478bd9Sstevel@tonic-gate 		code = -1;
2447c478bd9Sstevel@tonic-gate 		return;
2457c478bd9Sstevel@tonic-gate 	}
2467c478bd9Sstevel@tonic-gate 	if ((p->t_arg != NULL) && (*(p->t_arg) != '\0'))
2477c478bd9Sstevel@tonic-gate 		comret = command("TYPE %s %s", p->t_mode, p->t_arg);
2487c478bd9Sstevel@tonic-gate 	else
2497c478bd9Sstevel@tonic-gate 		comret = command("TYPE %s", p->t_mode);
2507c478bd9Sstevel@tonic-gate 	if (comret == COMPLETE) {
2517c478bd9Sstevel@tonic-gate 		(void) strcpy(typename, p->t_name);
2527c478bd9Sstevel@tonic-gate 		type = p->t_type;
2537c478bd9Sstevel@tonic-gate 	}
2547c478bd9Sstevel@tonic-gate }
2557c478bd9Sstevel@tonic-gate 
2567c478bd9Sstevel@tonic-gate /*
2577c478bd9Sstevel@tonic-gate  * Set binary transfer type.
2587c478bd9Sstevel@tonic-gate  */
2597c478bd9Sstevel@tonic-gate /*ARGSUSED*/
2607c478bd9Sstevel@tonic-gate void
setbinary(int argc,char * argv[])2617c478bd9Sstevel@tonic-gate setbinary(int argc, char *argv[])
2627c478bd9Sstevel@tonic-gate {
2637c478bd9Sstevel@tonic-gate 	call(settype, "type", "binary", 0);
2647c478bd9Sstevel@tonic-gate }
2657c478bd9Sstevel@tonic-gate 
2667c478bd9Sstevel@tonic-gate /*
2677c478bd9Sstevel@tonic-gate  * Set ascii transfer type.
2687c478bd9Sstevel@tonic-gate  */
2697c478bd9Sstevel@tonic-gate /*ARGSUSED*/
2707c478bd9Sstevel@tonic-gate void
setascii(int argc,char * argv[])2717c478bd9Sstevel@tonic-gate setascii(int argc, char *argv[])
2727c478bd9Sstevel@tonic-gate {
2737c478bd9Sstevel@tonic-gate 	call(settype, "type", "ascii", 0);
2747c478bd9Sstevel@tonic-gate }
2757c478bd9Sstevel@tonic-gate 
2767c478bd9Sstevel@tonic-gate /*
2777c478bd9Sstevel@tonic-gate  * Set tenex transfer type.
2787c478bd9Sstevel@tonic-gate  */
2797c478bd9Sstevel@tonic-gate /*ARGSUSED*/
2807c478bd9Sstevel@tonic-gate void
settenex(int argc,char * argv[])2817c478bd9Sstevel@tonic-gate settenex(int argc, char *argv[])
2827c478bd9Sstevel@tonic-gate {
2837c478bd9Sstevel@tonic-gate 	call(settype, "type", "tenex", 0);
2847c478bd9Sstevel@tonic-gate }
2857c478bd9Sstevel@tonic-gate 
2867c478bd9Sstevel@tonic-gate /*
2877c478bd9Sstevel@tonic-gate  * Set ebcdic transfer type.
2887c478bd9Sstevel@tonic-gate  */
2897c478bd9Sstevel@tonic-gate /*ARGSUSED*/
2907c478bd9Sstevel@tonic-gate void
setebcdic(int argc,char * argv[])2917c478bd9Sstevel@tonic-gate setebcdic(int argc, char *argv[])
2927c478bd9Sstevel@tonic-gate {
2937c478bd9Sstevel@tonic-gate 	call(settype, "type", "ebcdic", 0);
2947c478bd9Sstevel@tonic-gate }
2957c478bd9Sstevel@tonic-gate 
2967c478bd9Sstevel@tonic-gate /*
2977c478bd9Sstevel@tonic-gate  * Set file transfer mode.
2987c478bd9Sstevel@tonic-gate  */
2997c478bd9Sstevel@tonic-gate /*ARGSUSED*/
3007c478bd9Sstevel@tonic-gate void
setmode(int argc,char * argv[])3017c478bd9Sstevel@tonic-gate setmode(int argc, char *argv[])
3027c478bd9Sstevel@tonic-gate {
3037c478bd9Sstevel@tonic-gate 	(void) printf("We only support %s mode, sorry.\n", modename);
3047c478bd9Sstevel@tonic-gate 	code = -1;
3057c478bd9Sstevel@tonic-gate }
3067c478bd9Sstevel@tonic-gate 
3077c478bd9Sstevel@tonic-gate /*
3087c478bd9Sstevel@tonic-gate  * Set file transfer format.
3097c478bd9Sstevel@tonic-gate  */
3107c478bd9Sstevel@tonic-gate /*ARGSUSED*/
3117c478bd9Sstevel@tonic-gate void
setform(int argc,char * argv[])3127c478bd9Sstevel@tonic-gate setform(int argc, char *argv[])
3137c478bd9Sstevel@tonic-gate {
3147c478bd9Sstevel@tonic-gate 	(void) printf("We only support %s format, sorry.\n", formname);
3157c478bd9Sstevel@tonic-gate 	code = -1;
3167c478bd9Sstevel@tonic-gate }
3177c478bd9Sstevel@tonic-gate 
3187c478bd9Sstevel@tonic-gate /*
3197c478bd9Sstevel@tonic-gate  * Set file transfer structure.
3207c478bd9Sstevel@tonic-gate  */
3217c478bd9Sstevel@tonic-gate /*ARGSUSED*/
3227c478bd9Sstevel@tonic-gate void
setstruct(int argc,char * argv[])3237c478bd9Sstevel@tonic-gate setstruct(int argc, char *argv[])
3247c478bd9Sstevel@tonic-gate {
3257c478bd9Sstevel@tonic-gate 
3267c478bd9Sstevel@tonic-gate 	(void) printf("We only support %s structure, sorry.\n", structname);
3277c478bd9Sstevel@tonic-gate 	code = -1;
3287c478bd9Sstevel@tonic-gate }
3297c478bd9Sstevel@tonic-gate 
3307c478bd9Sstevel@tonic-gate /*
3317c478bd9Sstevel@tonic-gate  * Send a single file.
3327c478bd9Sstevel@tonic-gate  */
3337c478bd9Sstevel@tonic-gate void
put(int argc,char * argv[])3347c478bd9Sstevel@tonic-gate put(int argc, char *argv[])
3357c478bd9Sstevel@tonic-gate {
3367c478bd9Sstevel@tonic-gate 	char *cmd;
3377c478bd9Sstevel@tonic-gate 	int loc = 0;
3387c478bd9Sstevel@tonic-gate 	char *oldargv1;
3397c478bd9Sstevel@tonic-gate 
3407c478bd9Sstevel@tonic-gate 	if (argc == 2) {
3417c478bd9Sstevel@tonic-gate 		argc++;
3427c478bd9Sstevel@tonic-gate 		argv[2] = argv[1];
3437c478bd9Sstevel@tonic-gate 		loc++;
3447c478bd9Sstevel@tonic-gate 	}
3457c478bd9Sstevel@tonic-gate 	if (argc < 2) {
3467c478bd9Sstevel@tonic-gate 		if (prompt_for_arg(line, sizeof (line), "local-file") == -1) {
3477c478bd9Sstevel@tonic-gate 			code = -1;
3487c478bd9Sstevel@tonic-gate 			return;
3497c478bd9Sstevel@tonic-gate 		}
3507c478bd9Sstevel@tonic-gate 		makeargv();
3517c478bd9Sstevel@tonic-gate 		argc = margc;
3527c478bd9Sstevel@tonic-gate 		argv = margv;
3537c478bd9Sstevel@tonic-gate 	}
3547c478bd9Sstevel@tonic-gate 	if (argc < 2) {
3557c478bd9Sstevel@tonic-gate usage:
3567c478bd9Sstevel@tonic-gate 		(void) printf("usage: %s local-file remote-file\n", argv[0]);
3577c478bd9Sstevel@tonic-gate 		code = -1;
3587c478bd9Sstevel@tonic-gate 		return;
3597c478bd9Sstevel@tonic-gate 	}
3607c478bd9Sstevel@tonic-gate 	if (argc < 3) {
3617c478bd9Sstevel@tonic-gate 		if (prompt_for_arg(line, sizeof (line), "remote-file") == -1) {
3627c478bd9Sstevel@tonic-gate 			code = -1;
3637c478bd9Sstevel@tonic-gate 			return;
3647c478bd9Sstevel@tonic-gate 		}
3657c478bd9Sstevel@tonic-gate 		makeargv();
3667c478bd9Sstevel@tonic-gate 		argc = margc;
3677c478bd9Sstevel@tonic-gate 		argv = margv;
3687c478bd9Sstevel@tonic-gate 	}
3697c478bd9Sstevel@tonic-gate 	if (argc < 3)
3707c478bd9Sstevel@tonic-gate 		goto usage;
3717c478bd9Sstevel@tonic-gate 	oldargv1 = argv[1];
3727c478bd9Sstevel@tonic-gate 	if (!globulize(&argv[1])) {
3737c478bd9Sstevel@tonic-gate 		code = -1;
3747c478bd9Sstevel@tonic-gate 		return;
3757c478bd9Sstevel@tonic-gate 	}
3767c478bd9Sstevel@tonic-gate 	/*
3777c478bd9Sstevel@tonic-gate 	 * If "globulize" modifies argv[1], and argv[2] is a copy of
3787c478bd9Sstevel@tonic-gate 	 * the old argv[1], make it a copy of the new argv[1].
3797c478bd9Sstevel@tonic-gate 	 */
3807c478bd9Sstevel@tonic-gate 	if (argv[1] != oldargv1 && argv[2] == oldargv1) {
3817c478bd9Sstevel@tonic-gate 		argv[2] = argv[1];
3827c478bd9Sstevel@tonic-gate 	}
3837c478bd9Sstevel@tonic-gate 	cmd = (argv[0][0] == 'a') ? "APPE" : ((sunique) ? "STOU" : "STOR");
3847c478bd9Sstevel@tonic-gate 	if (loc && ntflag) {
3857c478bd9Sstevel@tonic-gate 		argv[2] = dotrans(argv[2]);
3867c478bd9Sstevel@tonic-gate 	}
3877c478bd9Sstevel@tonic-gate 	if (loc && mapflag) {
3887c478bd9Sstevel@tonic-gate 		argv[2] = domap(argv[2]);
3897c478bd9Sstevel@tonic-gate 	}
3907c478bd9Sstevel@tonic-gate 	sendrequest(cmd, argv[1], argv[2], 1);
3917c478bd9Sstevel@tonic-gate }
3927c478bd9Sstevel@tonic-gate 
3937c478bd9Sstevel@tonic-gate /*ARGSUSED*/
3947c478bd9Sstevel@tonic-gate static void
mabort(int sig)3957c478bd9Sstevel@tonic-gate mabort(int sig)
3967c478bd9Sstevel@tonic-gate {
3977c478bd9Sstevel@tonic-gate 	int ointer;
3987c478bd9Sstevel@tonic-gate 
3997c478bd9Sstevel@tonic-gate 	(void) printf("\n");
4007c478bd9Sstevel@tonic-gate 	(void) fflush(stdout);
4017c478bd9Sstevel@tonic-gate 	if (mflag && fromatty) {
4027c478bd9Sstevel@tonic-gate 		ointer = interactive;
4037c478bd9Sstevel@tonic-gate 		interactive = 1;
4047c478bd9Sstevel@tonic-gate 		if (confirm("Continue with", mname)) {
4057c478bd9Sstevel@tonic-gate 			interactive = ointer;
4067c478bd9Sstevel@tonic-gate 			longjmp(jabort, 0);
4077c478bd9Sstevel@tonic-gate 		}
4087c478bd9Sstevel@tonic-gate 		interactive = ointer;
4097c478bd9Sstevel@tonic-gate 	}
4107c478bd9Sstevel@tonic-gate 	mflag = 0;
4117c478bd9Sstevel@tonic-gate 	longjmp(jabort, 0);
4127c478bd9Sstevel@tonic-gate }
4137c478bd9Sstevel@tonic-gate 
4147c478bd9Sstevel@tonic-gate /*
4157c478bd9Sstevel@tonic-gate  * Send multiple files.
4167c478bd9Sstevel@tonic-gate  */
4177c478bd9Sstevel@tonic-gate void
mput(int argc,char * argv[])4187c478bd9Sstevel@tonic-gate mput(int argc, char *argv[])
4197c478bd9Sstevel@tonic-gate {
4207c478bd9Sstevel@tonic-gate 	int i;
4217c478bd9Sstevel@tonic-gate 	int ointer;
4227c478bd9Sstevel@tonic-gate 	void (*oldintr)();
4237c478bd9Sstevel@tonic-gate 	char *tp;
4247c478bd9Sstevel@tonic-gate 	int	len;
4257c478bd9Sstevel@tonic-gate 
4267c478bd9Sstevel@tonic-gate 	if (argc < 2) {
4277c478bd9Sstevel@tonic-gate 		if (prompt_for_arg(line, sizeof (line), "local-files") == -1) {
4287c478bd9Sstevel@tonic-gate 			code = -1;
4297c478bd9Sstevel@tonic-gate 			return;
4307c478bd9Sstevel@tonic-gate 		}
4317c478bd9Sstevel@tonic-gate 		makeargv();
4327c478bd9Sstevel@tonic-gate 		argc = margc;
4337c478bd9Sstevel@tonic-gate 		argv = margv;
4347c478bd9Sstevel@tonic-gate 	}
4357c478bd9Sstevel@tonic-gate 	if (argc < 2) {
4367c478bd9Sstevel@tonic-gate 		(void) printf("usage: %s local-files\n", argv[0]);
4377c478bd9Sstevel@tonic-gate 		code = -1;
4387c478bd9Sstevel@tonic-gate 		return;
4397c478bd9Sstevel@tonic-gate 	}
4407c478bd9Sstevel@tonic-gate 	mname = argv[0];
4417c478bd9Sstevel@tonic-gate 	mflag = 1;
4427c478bd9Sstevel@tonic-gate 	oldintr = signal(SIGINT, mabort);
4437c478bd9Sstevel@tonic-gate 	(void) setjmp(jabort);
4447c478bd9Sstevel@tonic-gate 	if (proxy) {
4457c478bd9Sstevel@tonic-gate 		char *cp, *tp2, tmpbuf[MAXPATHLEN];
4467c478bd9Sstevel@tonic-gate 
4477c478bd9Sstevel@tonic-gate 		while ((cp = remglob(argv, 0)) != NULL) {
4487c478bd9Sstevel@tonic-gate 			if (*cp == 0) {
4497c478bd9Sstevel@tonic-gate 				mflag = 0;
4507c478bd9Sstevel@tonic-gate 				continue;
4517c478bd9Sstevel@tonic-gate 			}
4527c478bd9Sstevel@tonic-gate 			if (mflag && confirm(argv[0], cp)) {
4537c478bd9Sstevel@tonic-gate 				tp = cp;
4547c478bd9Sstevel@tonic-gate 				if (mcase) {
4557c478bd9Sstevel@tonic-gate 					while (*tp) {
4567c478bd9Sstevel@tonic-gate 						if ((len =
4577c478bd9Sstevel@tonic-gate 						    mblen(tp, MB_CUR_MAX)) <= 0)
4587c478bd9Sstevel@tonic-gate 							len = 1;
4597c478bd9Sstevel@tonic-gate 						if (islower(*tp))
4607c478bd9Sstevel@tonic-gate 							break;
4617c478bd9Sstevel@tonic-gate 						tp += len;
4627c478bd9Sstevel@tonic-gate 					}
4637c478bd9Sstevel@tonic-gate 					if (!*tp) {
4647c478bd9Sstevel@tonic-gate 						tp = cp;
4657c478bd9Sstevel@tonic-gate 						tp2 = tmpbuf;
4667c478bd9Sstevel@tonic-gate 						while (*tp) {
4677c478bd9Sstevel@tonic-gate 							if ((len = mblen(tp,
4687c478bd9Sstevel@tonic-gate 							    MB_CUR_MAX)) <= 0)
4697c478bd9Sstevel@tonic-gate 								len = 1;
4707c478bd9Sstevel@tonic-gate 							memcpy(tp2, tp, len);
4717c478bd9Sstevel@tonic-gate 							if (isupper(*tp2)) {
4727c478bd9Sstevel@tonic-gate 								*tp2 = 'a' +
4737c478bd9Sstevel@tonic-gate 								    *tp2 - 'A';
4747c478bd9Sstevel@tonic-gate 							}
4757c478bd9Sstevel@tonic-gate 							tp += len;
4767c478bd9Sstevel@tonic-gate 							tp2 += len;
4777c478bd9Sstevel@tonic-gate 						}
4787c478bd9Sstevel@tonic-gate 						*tp2 = 0;
4797c478bd9Sstevel@tonic-gate 						tp = tmpbuf;
4807c478bd9Sstevel@tonic-gate 					}
4817c478bd9Sstevel@tonic-gate 				}
4827c478bd9Sstevel@tonic-gate 				if (ntflag) {
4837c478bd9Sstevel@tonic-gate 					tp = dotrans(tp);
4847c478bd9Sstevel@tonic-gate 				}
4857c478bd9Sstevel@tonic-gate 				if (mapflag) {
4867c478bd9Sstevel@tonic-gate 					tp = domap(tp);
4877c478bd9Sstevel@tonic-gate 				}
4887c478bd9Sstevel@tonic-gate 				sendrequest((sunique) ? "STOU" : "STOR",
4897c478bd9Sstevel@tonic-gate 				    cp, tp, 0);
4907c478bd9Sstevel@tonic-gate 				if (!mflag && fromatty) {
4917c478bd9Sstevel@tonic-gate 					ointer = interactive;
4927c478bd9Sstevel@tonic-gate 					interactive = 1;
4937c478bd9Sstevel@tonic-gate 					if (confirm("Continue with", "mput")) {
4947c478bd9Sstevel@tonic-gate 						mflag++;
4957c478bd9Sstevel@tonic-gate 					}
4967c478bd9Sstevel@tonic-gate 					interactive = ointer;
4977c478bd9Sstevel@tonic-gate 				}
4987c478bd9Sstevel@tonic-gate 			}
4997c478bd9Sstevel@tonic-gate 		}
5007c478bd9Sstevel@tonic-gate 		(void) signal(SIGINT, oldintr);
5017c478bd9Sstevel@tonic-gate 		mflag = 0;
5027c478bd9Sstevel@tonic-gate 		return;
5037c478bd9Sstevel@tonic-gate 	}
5047c478bd9Sstevel@tonic-gate 	for (i = 1; i < argc; i++) {
5057c478bd9Sstevel@tonic-gate 		char **cpp, **gargs;
5067c478bd9Sstevel@tonic-gate 
5077c478bd9Sstevel@tonic-gate 		if (!doglob) {
5087c478bd9Sstevel@tonic-gate 			if (mflag && confirm(argv[0], argv[i])) {
5097c478bd9Sstevel@tonic-gate 				tp = (ntflag) ? dotrans(argv[i]) : argv[i];
5107c478bd9Sstevel@tonic-gate 				tp = (mapflag) ? domap(tp) : tp;
5117c478bd9Sstevel@tonic-gate 				sendrequest((sunique) ? "STOU" : "STOR",
5127c478bd9Sstevel@tonic-gate 				    argv[i], tp, 1);
5137c478bd9Sstevel@tonic-gate 				if (!mflag && fromatty) {
5147c478bd9Sstevel@tonic-gate 					ointer = interactive;
5157c478bd9Sstevel@tonic-gate 					interactive = 1;
5167c478bd9Sstevel@tonic-gate 					if (confirm("Continue with", "mput")) {
5177c478bd9Sstevel@tonic-gate 						mflag++;
5187c478bd9Sstevel@tonic-gate 					}
5197c478bd9Sstevel@tonic-gate 					interactive = ointer;
5207c478bd9Sstevel@tonic-gate 				}
5217c478bd9Sstevel@tonic-gate 			}
5227c478bd9Sstevel@tonic-gate 			continue;
5237c478bd9Sstevel@tonic-gate 		}
5247c478bd9Sstevel@tonic-gate 		gargs = glob(argv[i]);
5257c478bd9Sstevel@tonic-gate 		if (globerr != NULL) {
5267c478bd9Sstevel@tonic-gate 			(void) printf("%s\n", globerr);
5277c478bd9Sstevel@tonic-gate 			if (gargs)
5287c478bd9Sstevel@tonic-gate 				blkfree(gargs);
5297c478bd9Sstevel@tonic-gate 			continue;
5307c478bd9Sstevel@tonic-gate 		}
5317c478bd9Sstevel@tonic-gate 		for (cpp = gargs; cpp && *cpp != NULL; cpp++) {
5327c478bd9Sstevel@tonic-gate 			if (mflag && confirm(argv[0], *cpp)) {
5337c478bd9Sstevel@tonic-gate 				tp = (ntflag) ? dotrans(*cpp) : *cpp;
5347c478bd9Sstevel@tonic-gate 				tp = (mapflag) ? domap(tp) : tp;
5357c478bd9Sstevel@tonic-gate 				sendrequest((sunique) ? "STOU" : "STOR",
5367c478bd9Sstevel@tonic-gate 				    *cpp, tp, 0);
5377c478bd9Sstevel@tonic-gate 				if (!mflag && fromatty) {
5387c478bd9Sstevel@tonic-gate 					ointer = interactive;
5397c478bd9Sstevel@tonic-gate 					interactive = 1;
5407c478bd9Sstevel@tonic-gate 					if (confirm("Continue with", "mput")) {
5417c478bd9Sstevel@tonic-gate 						mflag++;
5427c478bd9Sstevel@tonic-gate 					}
5437c478bd9Sstevel@tonic-gate 					interactive = ointer;
5447c478bd9Sstevel@tonic-gate 				}
5457c478bd9Sstevel@tonic-gate 			}
5467c478bd9Sstevel@tonic-gate 		}
5477c478bd9Sstevel@tonic-gate 		if (gargs != NULL)
5487c478bd9Sstevel@tonic-gate 			blkfree(gargs);
5497c478bd9Sstevel@tonic-gate 	}
5507c478bd9Sstevel@tonic-gate 	(void) signal(SIGINT, oldintr);
5517c478bd9Sstevel@tonic-gate 	mflag = 0;
5527c478bd9Sstevel@tonic-gate }
5537c478bd9Sstevel@tonic-gate 
5547c478bd9Sstevel@tonic-gate /*
5557c478bd9Sstevel@tonic-gate  * Restart transfer at a specific offset.
5567c478bd9Sstevel@tonic-gate  */
5577c478bd9Sstevel@tonic-gate void
restart(int argc,char * argv[])5587c478bd9Sstevel@tonic-gate restart(int argc, char *argv[])
5597c478bd9Sstevel@tonic-gate {
5607c478bd9Sstevel@tonic-gate 	off_t orestart_point = restart_point;
5617c478bd9Sstevel@tonic-gate 
5627c478bd9Sstevel@tonic-gate 	if (argc > 2) {
5637c478bd9Sstevel@tonic-gate 		(void) printf("usage: %s [marker]\n", argv[0]);
5647c478bd9Sstevel@tonic-gate 		code = -1;
5657c478bd9Sstevel@tonic-gate 		return;
5667c478bd9Sstevel@tonic-gate 	}
5677c478bd9Sstevel@tonic-gate 	if (argc == 2) {
5687c478bd9Sstevel@tonic-gate 		longlong_t rp;
5697c478bd9Sstevel@tonic-gate 		char *endp;
5707c478bd9Sstevel@tonic-gate 
5717c478bd9Sstevel@tonic-gate 		errno = 0;
5727c478bd9Sstevel@tonic-gate 		rp = strtoll(argv[1], &endp, 10);
5737c478bd9Sstevel@tonic-gate 		if (errno || rp < 0 || *endp != '\0')
5747c478bd9Sstevel@tonic-gate 			(void) printf("%s: Invalid offset `%s'\n",
5757c478bd9Sstevel@tonic-gate 				argv[0], argv[1]);
5767c478bd9Sstevel@tonic-gate 		else
5777c478bd9Sstevel@tonic-gate 			restart_point = rp;
5787c478bd9Sstevel@tonic-gate 	}
5797c478bd9Sstevel@tonic-gate 	if (restart_point == 0) {
5807c478bd9Sstevel@tonic-gate 		if (orestart_point == 0)
5817c478bd9Sstevel@tonic-gate 			(void) printf("No restart marker defined\n");
5827c478bd9Sstevel@tonic-gate 		else
5837c478bd9Sstevel@tonic-gate 			(void) printf("Restart marker cleared\n");
5847c478bd9Sstevel@tonic-gate 	} else
5857c478bd9Sstevel@tonic-gate 		(void) printf(
5867c478bd9Sstevel@tonic-gate 			"Restarting at %lld for next get, put or append\n",
5877c478bd9Sstevel@tonic-gate 			(longlong_t)restart_point);
5887c478bd9Sstevel@tonic-gate }
5897c478bd9Sstevel@tonic-gate 
5907c478bd9Sstevel@tonic-gate void
reget(int argc,char * argv[])5917c478bd9Sstevel@tonic-gate reget(int argc, char *argv[])
5927c478bd9Sstevel@tonic-gate {
5937c478bd9Sstevel@tonic-gate 	getit(argc, argv, 1, "r+w");
5947c478bd9Sstevel@tonic-gate }
5957c478bd9Sstevel@tonic-gate 
5967c478bd9Sstevel@tonic-gate void
get(int argc,char * argv[])5977c478bd9Sstevel@tonic-gate get(int argc, char *argv[])
5987c478bd9Sstevel@tonic-gate {
5997c478bd9Sstevel@tonic-gate 	getit(argc, argv, 0, restart_point ? "r+w" : "w");
6007c478bd9Sstevel@tonic-gate }
6017c478bd9Sstevel@tonic-gate 
6027c478bd9Sstevel@tonic-gate /*
6037c478bd9Sstevel@tonic-gate  * Receive one file.
6047c478bd9Sstevel@tonic-gate  */
6057c478bd9Sstevel@tonic-gate static void
getit(int argc,char * argv[],int restartit,char * mode)6067c478bd9Sstevel@tonic-gate getit(int argc, char *argv[], int restartit, char *mode)
6077c478bd9Sstevel@tonic-gate {
6087c478bd9Sstevel@tonic-gate 	int loc = 0;
6097c478bd9Sstevel@tonic-gate 	int len;
6107c478bd9Sstevel@tonic-gate 	int allowpipe = 1;
6117c478bd9Sstevel@tonic-gate 
6127c478bd9Sstevel@tonic-gate 	if (argc == 2) {
6137c478bd9Sstevel@tonic-gate 		argc++;
6147c478bd9Sstevel@tonic-gate 		argv[2] = argv[1];
6157c478bd9Sstevel@tonic-gate 		/* Only permit !file if two arguments. */
6167c478bd9Sstevel@tonic-gate 		allowpipe = 0;
6177c478bd9Sstevel@tonic-gate 		loc++;
6187c478bd9Sstevel@tonic-gate 	}
6197c478bd9Sstevel@tonic-gate 	if (argc < 2) {
6207c478bd9Sstevel@tonic-gate 		if (prompt_for_arg(line, sizeof (line), "remote-file") == -1) {
6217c478bd9Sstevel@tonic-gate 			code = -1;
6227c478bd9Sstevel@tonic-gate 			return;
6237c478bd9Sstevel@tonic-gate 		}
6247c478bd9Sstevel@tonic-gate 		makeargv();
6257c478bd9Sstevel@tonic-gate 		argc = margc;
6267c478bd9Sstevel@tonic-gate 		argv = margv;
6277c478bd9Sstevel@tonic-gate 	}
6287c478bd9Sstevel@tonic-gate 	if (argc < 2) {
6297c478bd9Sstevel@tonic-gate usage:
6307c478bd9Sstevel@tonic-gate 		(void) printf("usage: %s remote-file [ local-file ]\n",
6317c478bd9Sstevel@tonic-gate 			argv[0]);
6327c478bd9Sstevel@tonic-gate 		code = -1;
6337c478bd9Sstevel@tonic-gate 		return;
6347c478bd9Sstevel@tonic-gate 	}
6357c478bd9Sstevel@tonic-gate 	if (argc < 3) {
6367c478bd9Sstevel@tonic-gate 		if (prompt_for_arg(line, sizeof (line), "local-file") == -1) {
6377c478bd9Sstevel@tonic-gate 			code = -1;
6387c478bd9Sstevel@tonic-gate 			return;
6397c478bd9Sstevel@tonic-gate 		}
6407c478bd9Sstevel@tonic-gate 		makeargv();
6417c478bd9Sstevel@tonic-gate 		argc = margc;
6427c478bd9Sstevel@tonic-gate 		argv = margv;
6437c478bd9Sstevel@tonic-gate 	}
6447c478bd9Sstevel@tonic-gate 	if (argc < 3)
6457c478bd9Sstevel@tonic-gate 		goto usage;
6467c478bd9Sstevel@tonic-gate 	if (!globulize(&argv[2])) {
6477c478bd9Sstevel@tonic-gate 		code = -1;
6487c478bd9Sstevel@tonic-gate 		return;
6497c478bd9Sstevel@tonic-gate 	}
6507c478bd9Sstevel@tonic-gate 	if (loc && mcase) {
6517c478bd9Sstevel@tonic-gate 		char *tp = argv[1], *tp2, tmpbuf[MAXPATHLEN];
6527c478bd9Sstevel@tonic-gate 
6537c478bd9Sstevel@tonic-gate 		while (*tp) {
6547c478bd9Sstevel@tonic-gate 			if ((len = mblen(tp, MB_CUR_MAX)) <= 0)
6557c478bd9Sstevel@tonic-gate 				len = 1;
6567c478bd9Sstevel@tonic-gate 			if (islower(*tp))
6577c478bd9Sstevel@tonic-gate 				break;
6587c478bd9Sstevel@tonic-gate 			tp += len;
6597c478bd9Sstevel@tonic-gate 		}
6607c478bd9Sstevel@tonic-gate 		if (!*tp) {
6617c478bd9Sstevel@tonic-gate 			tp = argv[2];
6627c478bd9Sstevel@tonic-gate 			tp2 = tmpbuf;
6637c478bd9Sstevel@tonic-gate 			while (*tp) {
6647c478bd9Sstevel@tonic-gate 				if ((len = mblen(tp, MB_CUR_MAX)) <= 0)
6657c478bd9Sstevel@tonic-gate 					len = 1;
6667c478bd9Sstevel@tonic-gate 				memcpy(tp2, tp, len);
6677c478bd9Sstevel@tonic-gate 				if (isupper(*tp2))
6687c478bd9Sstevel@tonic-gate 					*tp2 = 'a' + *tp2 - 'A';
6697c478bd9Sstevel@tonic-gate 				tp += len;
6707c478bd9Sstevel@tonic-gate 				tp2 += len;
6717c478bd9Sstevel@tonic-gate 			}
6727c478bd9Sstevel@tonic-gate 			*tp2 = 0;
6737c478bd9Sstevel@tonic-gate 			argv[2] = tmpbuf;
6747c478bd9Sstevel@tonic-gate 		}
6757c478bd9Sstevel@tonic-gate 	}
6767c478bd9Sstevel@tonic-gate 	if (loc && ntflag) {
6777c478bd9Sstevel@tonic-gate 		argv[2] = dotrans(argv[2]);
6787c478bd9Sstevel@tonic-gate 	}
6797c478bd9Sstevel@tonic-gate 	if (loc && mapflag) {
6807c478bd9Sstevel@tonic-gate 		argv[2] = domap(argv[2]);
6817c478bd9Sstevel@tonic-gate 	}
6827c478bd9Sstevel@tonic-gate 	if (restartit) {
6837c478bd9Sstevel@tonic-gate 		struct stat stbuf;
6847c478bd9Sstevel@tonic-gate 
6857c478bd9Sstevel@tonic-gate 		if (stat(argv[2], &stbuf) < 0) {
6867c478bd9Sstevel@tonic-gate 			perror(argv[2]);
6877c478bd9Sstevel@tonic-gate 			code = -1;
6887c478bd9Sstevel@tonic-gate 			return;
6897c478bd9Sstevel@tonic-gate 		}
6907c478bd9Sstevel@tonic-gate 		restart_point = stbuf.st_size;
6917c478bd9Sstevel@tonic-gate 	}
6927c478bd9Sstevel@tonic-gate 	recvrequest("RETR", argv[2], argv[1], mode, allowpipe);
6937c478bd9Sstevel@tonic-gate 	restart_point = 0;
6947c478bd9Sstevel@tonic-gate }
6957c478bd9Sstevel@tonic-gate 
6967c478bd9Sstevel@tonic-gate /*
6977c478bd9Sstevel@tonic-gate  * Get multiple files.
6987c478bd9Sstevel@tonic-gate  */
6997c478bd9Sstevel@tonic-gate void
mget(int argc,char * argv[])7007c478bd9Sstevel@tonic-gate mget(int argc, char *argv[])
7017c478bd9Sstevel@tonic-gate {
7027c478bd9Sstevel@tonic-gate 	char *cp, *tp, *tp2, tmpbuf[MAXPATHLEN];
7037c478bd9Sstevel@tonic-gate 	int ointer;
7047c478bd9Sstevel@tonic-gate 	void (*oldintr)();
7057c478bd9Sstevel@tonic-gate 	int need_convert;
7067c478bd9Sstevel@tonic-gate 	int	len;
7077c478bd9Sstevel@tonic-gate 
7087c478bd9Sstevel@tonic-gate 	if (argc < 2) {
7097c478bd9Sstevel@tonic-gate 		if (prompt_for_arg(line, sizeof (line), "remote-files") < 0) {
7107c478bd9Sstevel@tonic-gate 			code = -1;
7117c478bd9Sstevel@tonic-gate 			return;
7127c478bd9Sstevel@tonic-gate 		}
7137c478bd9Sstevel@tonic-gate 		makeargv();
7147c478bd9Sstevel@tonic-gate 		argc = margc;
7157c478bd9Sstevel@tonic-gate 		argv = margv;
7167c478bd9Sstevel@tonic-gate 	}
7177c478bd9Sstevel@tonic-gate 	if (argc < 2) {
7187c478bd9Sstevel@tonic-gate 		(void) printf("usage: %s remote-files\n", argv[0]);
7197c478bd9Sstevel@tonic-gate 		code = -1;
7207c478bd9Sstevel@tonic-gate 		return;
7217c478bd9Sstevel@tonic-gate 	}
7227c478bd9Sstevel@tonic-gate 	mname = argv[0];
7237c478bd9Sstevel@tonic-gate 	mflag = 1;
7247c478bd9Sstevel@tonic-gate 	oldintr = signal(SIGINT, mabort);
7257c478bd9Sstevel@tonic-gate 	(void) setjmp(jabort);
7267c478bd9Sstevel@tonic-gate 	while ((cp = remglob(argv, proxy)) != NULL) {
7277c478bd9Sstevel@tonic-gate 		if (*cp == '\0') {
7287c478bd9Sstevel@tonic-gate 			mflag = 0;
7297c478bd9Sstevel@tonic-gate 			continue;
7307c478bd9Sstevel@tonic-gate 		}
7317c478bd9Sstevel@tonic-gate 		if (mflag && confirm(argv[0], cp)) {
7327c478bd9Sstevel@tonic-gate 			strcpy(tmpbuf, cp);
7337c478bd9Sstevel@tonic-gate 			tp =  tmpbuf;
7347c478bd9Sstevel@tonic-gate 			need_convert = 1;
7357c478bd9Sstevel@tonic-gate 			if (mcase) {
7367c478bd9Sstevel@tonic-gate 				tp2 = tp;
7377c478bd9Sstevel@tonic-gate 				while (*tp2 && need_convert) {
7387c478bd9Sstevel@tonic-gate 				/* Need any case convert? */
7397c478bd9Sstevel@tonic-gate 					if (islower(*tp2))
7407c478bd9Sstevel@tonic-gate 						need_convert = 0;
7417c478bd9Sstevel@tonic-gate 					if ((len = mblen(tp2, MB_CUR_MAX)) <= 0)
7427c478bd9Sstevel@tonic-gate 						len = 1;
7437c478bd9Sstevel@tonic-gate 					tp2 += len;
7447c478bd9Sstevel@tonic-gate 				}
7457c478bd9Sstevel@tonic-gate 				tp2 = tp;
7467c478bd9Sstevel@tonic-gate 				while (need_convert && *tp2) {
7477c478bd9Sstevel@tonic-gate 				/* Convert to lower case */
7487c478bd9Sstevel@tonic-gate 					if (isupper(*tp2))
7497c478bd9Sstevel@tonic-gate 						*tp2 = tolower(*tp2);
7507c478bd9Sstevel@tonic-gate 					if ((len = mblen(tp2, MB_CUR_MAX)) <= 0)
7517c478bd9Sstevel@tonic-gate 						len = 1;
7527c478bd9Sstevel@tonic-gate 					tp2 += len;
7537c478bd9Sstevel@tonic-gate 				}
7547c478bd9Sstevel@tonic-gate 			}
7557c478bd9Sstevel@tonic-gate 
7567c478bd9Sstevel@tonic-gate 			if (ntflag) {
7577c478bd9Sstevel@tonic-gate 				tp = dotrans(tp);
7587c478bd9Sstevel@tonic-gate 			}
7597c478bd9Sstevel@tonic-gate 			if (mapflag) {
7607c478bd9Sstevel@tonic-gate 				tp = domap(tp);
7617c478bd9Sstevel@tonic-gate 			}
7627c478bd9Sstevel@tonic-gate 			recvrequest("RETR", tp, cp, "w", 0);
7637c478bd9Sstevel@tonic-gate 			restart_point = 0;
7647c478bd9Sstevel@tonic-gate 			if (!mflag && fromatty) {
7657c478bd9Sstevel@tonic-gate 				ointer = interactive;
7667c478bd9Sstevel@tonic-gate 				interactive = 1;
7677c478bd9Sstevel@tonic-gate 				if (confirm("Continue with", "mget")) {
7687c478bd9Sstevel@tonic-gate 					mflag++;
7697c478bd9Sstevel@tonic-gate 				}
7707c478bd9Sstevel@tonic-gate 				interactive = ointer;
7717c478bd9Sstevel@tonic-gate 			}
7727c478bd9Sstevel@tonic-gate 		}
7737c478bd9Sstevel@tonic-gate 	}
7747c478bd9Sstevel@tonic-gate 	(void) signal(SIGINT, oldintr);
7757c478bd9Sstevel@tonic-gate 	mflag = 0;
7767c478bd9Sstevel@tonic-gate }
7777c478bd9Sstevel@tonic-gate 
7787c478bd9Sstevel@tonic-gate static char *
remglob(char * argv[],int doswitch)7797c478bd9Sstevel@tonic-gate remglob(char *argv[], int doswitch)
7807c478bd9Sstevel@tonic-gate {
7817c478bd9Sstevel@tonic-gate 	static char buf[MAXPATHLEN];
7827c478bd9Sstevel@tonic-gate 	static char **args;
7837c478bd9Sstevel@tonic-gate 	int oldverbose, oldhash;
7847c478bd9Sstevel@tonic-gate 	char *cp;
7857c478bd9Sstevel@tonic-gate 
7867c478bd9Sstevel@tonic-gate 	if (!mflag) {
7877c478bd9Sstevel@tonic-gate 		if (!doglob) {
7887c478bd9Sstevel@tonic-gate 			args = NULL;
7897c478bd9Sstevel@tonic-gate 		} else {
7907c478bd9Sstevel@tonic-gate 			if (tmp_nlst != NULL) {
7917c478bd9Sstevel@tonic-gate 				(void) fclose(tmp_nlst);
7927c478bd9Sstevel@tonic-gate 				tmp_nlst = NULL;
7937c478bd9Sstevel@tonic-gate 			}
7947c478bd9Sstevel@tonic-gate 		}
7957c478bd9Sstevel@tonic-gate 		return (NULL);
7967c478bd9Sstevel@tonic-gate 	}
7977c478bd9Sstevel@tonic-gate 	if (!doglob) {
7987c478bd9Sstevel@tonic-gate 		if (args == NULL)
7997c478bd9Sstevel@tonic-gate 			args = argv;
8007c478bd9Sstevel@tonic-gate 		if ((cp = *++args) == NULL)
8017c478bd9Sstevel@tonic-gate 			args = NULL;
8027c478bd9Sstevel@tonic-gate 		return (cp);
8037c478bd9Sstevel@tonic-gate 	}
8047c478bd9Sstevel@tonic-gate 	if (tmp_nlst == NULL) {
8057c478bd9Sstevel@tonic-gate 		if ((tmp_nlst = tmpfile()) == NULL) {
8067c478bd9Sstevel@tonic-gate 			(void) printf("%s\n", strerror(errno));
8077c478bd9Sstevel@tonic-gate 			return (NULL);
8087c478bd9Sstevel@tonic-gate 		}
8097c478bd9Sstevel@tonic-gate 		oldverbose = verbose, verbose = 0;
8107c478bd9Sstevel@tonic-gate 		oldhash = hash, hash = 0;
8117c478bd9Sstevel@tonic-gate 		if (doswitch) {
8127c478bd9Sstevel@tonic-gate 			pswitch(!proxy);
8137c478bd9Sstevel@tonic-gate 		}
8147c478bd9Sstevel@tonic-gate 		for (; *++argv != NULL; )
8157c478bd9Sstevel@tonic-gate 			recvrequest("NLST", NULL, *argv, "", 0);
8167c478bd9Sstevel@tonic-gate 		rewind(tmp_nlst);
8177c478bd9Sstevel@tonic-gate 		if (doswitch) {
8187c478bd9Sstevel@tonic-gate 			pswitch(!proxy);
8197c478bd9Sstevel@tonic-gate 		}
8207c478bd9Sstevel@tonic-gate 		verbose = oldverbose; hash = oldhash;
8217c478bd9Sstevel@tonic-gate 	}
8227c478bd9Sstevel@tonic-gate 	reset_timer();
8237c478bd9Sstevel@tonic-gate 	if (fgets(buf, sizeof (buf), tmp_nlst) == NULL) {
8247c478bd9Sstevel@tonic-gate 		(void) fclose(tmp_nlst), tmp_nlst = NULL;
8257c478bd9Sstevel@tonic-gate 		return (NULL);
8267c478bd9Sstevel@tonic-gate 	}
8277c478bd9Sstevel@tonic-gate 	if ((cp = index(buf, '\n')) != NULL)
8287c478bd9Sstevel@tonic-gate 		*cp = '\0';
8297c478bd9Sstevel@tonic-gate 	return (buf);
8307c478bd9Sstevel@tonic-gate }
8317c478bd9Sstevel@tonic-gate 
8327c478bd9Sstevel@tonic-gate static char *
onoff(int bool)8337c478bd9Sstevel@tonic-gate onoff(int bool)
8347c478bd9Sstevel@tonic-gate {
8357c478bd9Sstevel@tonic-gate 	return (bool ? "on" : "off");
8367c478bd9Sstevel@tonic-gate }
8377c478bd9Sstevel@tonic-gate 
8387c478bd9Sstevel@tonic-gate /*
8397c478bd9Sstevel@tonic-gate  * Show status.
8407c478bd9Sstevel@tonic-gate  */
8417c478bd9Sstevel@tonic-gate /*ARGSUSED*/
8427c478bd9Sstevel@tonic-gate void
status(int argc,char * argv[])8437c478bd9Sstevel@tonic-gate status(int argc, char *argv[])
8447c478bd9Sstevel@tonic-gate {
8457c478bd9Sstevel@tonic-gate 	int i;
8467c478bd9Sstevel@tonic-gate 	char *levelp;
8477c478bd9Sstevel@tonic-gate 
8487c478bd9Sstevel@tonic-gate 	if (connected)
8497c478bd9Sstevel@tonic-gate 		(void) printf("Connected to %s.\n", hostname);
8507c478bd9Sstevel@tonic-gate 	else
8517c478bd9Sstevel@tonic-gate 		(void) printf("Not connected.\n");
8527c478bd9Sstevel@tonic-gate 	if (!proxy) {
8537c478bd9Sstevel@tonic-gate 		pswitch(1);
8547c478bd9Sstevel@tonic-gate 		if (connected) {
8557c478bd9Sstevel@tonic-gate 			(void) printf("Connected for proxy commands to %s.\n",
8567c478bd9Sstevel@tonic-gate 			    hostname);
8577c478bd9Sstevel@tonic-gate 		} else {
8587c478bd9Sstevel@tonic-gate 			(void) printf("No proxy connection.\n");
8597c478bd9Sstevel@tonic-gate 		}
8607c478bd9Sstevel@tonic-gate 		pswitch(0);
8617c478bd9Sstevel@tonic-gate 	}
8627c478bd9Sstevel@tonic-gate 
8637c478bd9Sstevel@tonic-gate 	if (auth_type != AUTHTYPE_NONE)
8647c478bd9Sstevel@tonic-gate 		(void) printf("Authentication type: %s\n",
8657c478bd9Sstevel@tonic-gate 			GSS_AUTHTYPE_NAME(auth_type));
8667c478bd9Sstevel@tonic-gate 	else
8677c478bd9Sstevel@tonic-gate 		(void) printf("Not authenticated.\n");
8687c478bd9Sstevel@tonic-gate 	(void) printf("Mechanism: %s\n", mechstr);
8697c478bd9Sstevel@tonic-gate 	(void) printf("Autoauth: %s; Autologin: %s\n",
8707c478bd9Sstevel@tonic-gate 		onoff(autoauth), onoff(autologin));
8717c478bd9Sstevel@tonic-gate 	levelp = getlevel(clevel);
8727c478bd9Sstevel@tonic-gate 	(void) printf("Control Channel Protection Level: %s\n",
8737c478bd9Sstevel@tonic-gate 		levelp ? levelp : "<unknown>");
8747c478bd9Sstevel@tonic-gate 	levelp = getlevel(dlevel);
8757c478bd9Sstevel@tonic-gate 	(void) printf("Data Channel Protection Level: %s\n",
8767c478bd9Sstevel@tonic-gate 		levelp ? levelp : "<unknown>");
8777c478bd9Sstevel@tonic-gate 
8787c478bd9Sstevel@tonic-gate 	(void) printf("Passive mode: %s.\n", onoff(passivemode));
8797c478bd9Sstevel@tonic-gate 	(void) printf("Mode: %s; Type: %s; Form: %s; Structure: %s\n",
8807c478bd9Sstevel@tonic-gate 		modename, typename, formname, structname);
8817c478bd9Sstevel@tonic-gate 	(void) printf("Verbose: %s; Bell: %s; Prompting: %s; Globbing: %s\n",
8827c478bd9Sstevel@tonic-gate 		onoff(verbose), onoff(bell), onoff(interactive),
8837c478bd9Sstevel@tonic-gate 		onoff(doglob));
8847c478bd9Sstevel@tonic-gate 	(void) printf("Store unique: %s; Receive unique: %s\n", onoff(sunique),
8857c478bd9Sstevel@tonic-gate 		onoff(runique));
8867c478bd9Sstevel@tonic-gate 	(void) printf("Case: %s; CR stripping: %s\n",
8877c478bd9Sstevel@tonic-gate 		onoff(mcase), onoff(crflag));
8887c478bd9Sstevel@tonic-gate 	if (ntflag) {
8897c478bd9Sstevel@tonic-gate 		(void) printf("Ntrans: (in) %s (out) %s\n", ntin, ntout);
8907c478bd9Sstevel@tonic-gate 	} else {
8917c478bd9Sstevel@tonic-gate 		(void) printf("Ntrans: off\n");
8927c478bd9Sstevel@tonic-gate 	}
8937c478bd9Sstevel@tonic-gate 	if (mapflag) {
8947c478bd9Sstevel@tonic-gate 		(void) printf("Nmap: (in) %s (out) %s\n", mapin, mapout);
8957c478bd9Sstevel@tonic-gate 	} else {
8967c478bd9Sstevel@tonic-gate 		(void) printf("Nmap: off\n");
8977c478bd9Sstevel@tonic-gate 	}
8987c478bd9Sstevel@tonic-gate 	(void) printf("Hash mark printing: %s; Use of PORT cmds: %s\n",
8997c478bd9Sstevel@tonic-gate 		onoff(hash), onoff(sendport));
9007c478bd9Sstevel@tonic-gate 	if (macnum > 0) {
9017c478bd9Sstevel@tonic-gate 		(void) printf("Macros:\n");
9027c478bd9Sstevel@tonic-gate 		for (i = 0; i < macnum; i++) {
9037c478bd9Sstevel@tonic-gate 			(void) printf("\t%s\n", macros[i].mac_name);
9047c478bd9Sstevel@tonic-gate 		}
9057c478bd9Sstevel@tonic-gate 	}
9067c478bd9Sstevel@tonic-gate 	code = 0;
9077c478bd9Sstevel@tonic-gate }
9087c478bd9Sstevel@tonic-gate 
9097c478bd9Sstevel@tonic-gate /*
9107c478bd9Sstevel@tonic-gate  * Set beep on cmd completed mode.
9117c478bd9Sstevel@tonic-gate  */
9127c478bd9Sstevel@tonic-gate /*ARGSUSED*/
9137c478bd9Sstevel@tonic-gate void
setbell(int argc,char * argv[])9147c478bd9Sstevel@tonic-gate setbell(int argc, char *argv[])
9157c478bd9Sstevel@tonic-gate {
9167c478bd9Sstevel@tonic-gate 	bell = !bell;
9177c478bd9Sstevel@tonic-gate 	(void) printf("Bell mode %s.\n", onoff(bell));
9187c478bd9Sstevel@tonic-gate 	code = bell;
9197c478bd9Sstevel@tonic-gate }
9207c478bd9Sstevel@tonic-gate 
9217c478bd9Sstevel@tonic-gate /*
9227c478bd9Sstevel@tonic-gate  * Turn on packet tracing.
9237c478bd9Sstevel@tonic-gate  */
9247c478bd9Sstevel@tonic-gate /*ARGSUSED*/
9257c478bd9Sstevel@tonic-gate void
settrace(int argc,char * argv[])9267c478bd9Sstevel@tonic-gate settrace(int argc, char *argv[])
9277c478bd9Sstevel@tonic-gate {
9287c478bd9Sstevel@tonic-gate 	trace = !trace;
9297c478bd9Sstevel@tonic-gate 	(void) printf("Packet tracing %s.\n", onoff(trace));
9307c478bd9Sstevel@tonic-gate 	code = trace;
9317c478bd9Sstevel@tonic-gate }
9327c478bd9Sstevel@tonic-gate 
9337c478bd9Sstevel@tonic-gate /*
9347c478bd9Sstevel@tonic-gate  * Toggle hash mark printing during transfers.
9357c478bd9Sstevel@tonic-gate  */
9367c478bd9Sstevel@tonic-gate /*ARGSUSED*/
9377c478bd9Sstevel@tonic-gate void
sethash(int argc,char * argv[])9387c478bd9Sstevel@tonic-gate sethash(int argc, char *argv[])
9397c478bd9Sstevel@tonic-gate {
9407c478bd9Sstevel@tonic-gate 	hash = !hash;
9417c478bd9Sstevel@tonic-gate 	(void) printf("Hash mark printing %s", onoff(hash));
9427c478bd9Sstevel@tonic-gate 	code = hash;
9437c478bd9Sstevel@tonic-gate 	if (hash)
9447c478bd9Sstevel@tonic-gate 		(void) printf(" (%d bytes/hash mark)", HASHSIZ);
9457c478bd9Sstevel@tonic-gate 	(void) printf(".\n");
9467c478bd9Sstevel@tonic-gate }
9477c478bd9Sstevel@tonic-gate 
9487c478bd9Sstevel@tonic-gate /*
9497c478bd9Sstevel@tonic-gate  * Turn on printing of server echo's.
9507c478bd9Sstevel@tonic-gate  */
9517c478bd9Sstevel@tonic-gate /*ARGSUSED*/
9527c478bd9Sstevel@tonic-gate void
setverbose(int argc,char * argv[])9537c478bd9Sstevel@tonic-gate setverbose(int argc, char *argv[])
9547c478bd9Sstevel@tonic-gate {
9557c478bd9Sstevel@tonic-gate 	verbose = !verbose;
9567c478bd9Sstevel@tonic-gate 	(void) printf("Verbose mode %s.\n", onoff(verbose));
9577c478bd9Sstevel@tonic-gate 	code = verbose;
9587c478bd9Sstevel@tonic-gate }
9597c478bd9Sstevel@tonic-gate 
9607c478bd9Sstevel@tonic-gate /*
9617c478bd9Sstevel@tonic-gate  * Toggle PORT cmd use before each data connection.
9627c478bd9Sstevel@tonic-gate  */
9637c478bd9Sstevel@tonic-gate /*ARGSUSED*/
9647c478bd9Sstevel@tonic-gate void
setport(int argc,char * argv[])9657c478bd9Sstevel@tonic-gate setport(int argc, char *argv[])
9667c478bd9Sstevel@tonic-gate {
9677c478bd9Sstevel@tonic-gate 	sendport = !sendport;
9687c478bd9Sstevel@tonic-gate 	(void) printf("Use of PORT cmds %s.\n", onoff(sendport));
9697c478bd9Sstevel@tonic-gate 	code = sendport;
9707c478bd9Sstevel@tonic-gate }
9717c478bd9Sstevel@tonic-gate 
9727c478bd9Sstevel@tonic-gate /*
9737c478bd9Sstevel@tonic-gate  * Turn on interactive prompting
9747c478bd9Sstevel@tonic-gate  * during mget, mput, and mdelete.
9757c478bd9Sstevel@tonic-gate  */
9767c478bd9Sstevel@tonic-gate /*ARGSUSED*/
9777c478bd9Sstevel@tonic-gate void
setprompt(int argc,char * argv[])9787c478bd9Sstevel@tonic-gate setprompt(int argc, char *argv[])
9797c478bd9Sstevel@tonic-gate {
9807c478bd9Sstevel@tonic-gate 	interactive = !interactive;
9817c478bd9Sstevel@tonic-gate 	(void) printf("Interactive mode %s.\n", onoff(interactive));
9827c478bd9Sstevel@tonic-gate 	code = interactive;
9837c478bd9Sstevel@tonic-gate }
9847c478bd9Sstevel@tonic-gate 
9857c478bd9Sstevel@tonic-gate /*
9867c478bd9Sstevel@tonic-gate  * Toggle metacharacter interpretation
9877c478bd9Sstevel@tonic-gate  * on local file names.
9887c478bd9Sstevel@tonic-gate  */
9897c478bd9Sstevel@tonic-gate /*ARGSUSED*/
9907c478bd9Sstevel@tonic-gate void
setglob(int argc,char * argv[])9917c478bd9Sstevel@tonic-gate setglob(int argc, char *argv[])
9927c478bd9Sstevel@tonic-gate {
9937c478bd9Sstevel@tonic-gate 	doglob = !doglob;
9947c478bd9Sstevel@tonic-gate 	(void) printf("Globbing %s.\n", onoff(doglob));
9957c478bd9Sstevel@tonic-gate 	code = doglob;
9967c478bd9Sstevel@tonic-gate }
9977c478bd9Sstevel@tonic-gate 
9987c478bd9Sstevel@tonic-gate /*
9997c478bd9Sstevel@tonic-gate  * Set debugging mode on/off and/or
10007c478bd9Sstevel@tonic-gate  * set level of debugging.
10017c478bd9Sstevel@tonic-gate  */
10027c478bd9Sstevel@tonic-gate void
setdebug(int argc,char * argv[])10037c478bd9Sstevel@tonic-gate setdebug(int argc, char *argv[])
10047c478bd9Sstevel@tonic-gate {
10057c478bd9Sstevel@tonic-gate 	int val;
10067c478bd9Sstevel@tonic-gate 
10077c478bd9Sstevel@tonic-gate 	if (argc > 1) {
10087c478bd9Sstevel@tonic-gate 		val = atoi(argv[1]);
10097c478bd9Sstevel@tonic-gate 		if (val < 0) {
10107c478bd9Sstevel@tonic-gate 			(void) printf("%s: bad debugging value.\n", argv[1]);
10117c478bd9Sstevel@tonic-gate 			code = -1;
10127c478bd9Sstevel@tonic-gate 			return;
10137c478bd9Sstevel@tonic-gate 		}
10147c478bd9Sstevel@tonic-gate 	} else
10157c478bd9Sstevel@tonic-gate 		val = !debug;
10167c478bd9Sstevel@tonic-gate 	debug = val;
10177c478bd9Sstevel@tonic-gate 	if (debug)
10187c478bd9Sstevel@tonic-gate 		options |= SO_DEBUG;
10197c478bd9Sstevel@tonic-gate 	else
10207c478bd9Sstevel@tonic-gate 		options &= ~SO_DEBUG;
10217c478bd9Sstevel@tonic-gate 	(void) printf("Debugging %s (debug=%d).\n", onoff(debug), debug);
10227c478bd9Sstevel@tonic-gate 	code = debug > 0;
10237c478bd9Sstevel@tonic-gate }
10247c478bd9Sstevel@tonic-gate 
10257c478bd9Sstevel@tonic-gate /*
10267c478bd9Sstevel@tonic-gate  * Set current working directory
10277c478bd9Sstevel@tonic-gate  * on remote machine.
10287c478bd9Sstevel@tonic-gate  */
10297c478bd9Sstevel@tonic-gate void
cd(int argc,char * argv[])10307c478bd9Sstevel@tonic-gate cd(int argc, char *argv[])
10317c478bd9Sstevel@tonic-gate {
10327c478bd9Sstevel@tonic-gate 	if (argc < 2) {
10337c478bd9Sstevel@tonic-gate 		if (prompt_for_arg(line, sizeof (line), "remote-directory") <
10347c478bd9Sstevel@tonic-gate 		    0) {
10357c478bd9Sstevel@tonic-gate 			code = -1;
10367c478bd9Sstevel@tonic-gate 			return;
10377c478bd9Sstevel@tonic-gate 		}
10387c478bd9Sstevel@tonic-gate 		makeargv();
10397c478bd9Sstevel@tonic-gate 		argc = margc;
10407c478bd9Sstevel@tonic-gate 		argv = margv;
10417c478bd9Sstevel@tonic-gate 	}
10427c478bd9Sstevel@tonic-gate 	if (argc < 2) {
10437c478bd9Sstevel@tonic-gate 		(void) printf("usage: %s remote-directory\n", argv[0]);
10447c478bd9Sstevel@tonic-gate 		code = -1;
10457c478bd9Sstevel@tonic-gate 		return;
10467c478bd9Sstevel@tonic-gate 	}
10477c478bd9Sstevel@tonic-gate 	(void) command("CWD %s", argv[1]);
10487c478bd9Sstevel@tonic-gate }
10497c478bd9Sstevel@tonic-gate 
10507c478bd9Sstevel@tonic-gate /*
10517c478bd9Sstevel@tonic-gate  * Set current working directory
10527c478bd9Sstevel@tonic-gate  * on local machine.
10537c478bd9Sstevel@tonic-gate  */
10547c478bd9Sstevel@tonic-gate void
lcd(int argc,char * argv[])10557c478bd9Sstevel@tonic-gate lcd(int argc, char *argv[])
10567c478bd9Sstevel@tonic-gate {
10577c478bd9Sstevel@tonic-gate 	char buf[MAXPATHLEN], *bufptr;
10587c478bd9Sstevel@tonic-gate 
10597c478bd9Sstevel@tonic-gate 	if (argc < 2)
10607c478bd9Sstevel@tonic-gate 		argc++, argv[1] = home;
10617c478bd9Sstevel@tonic-gate 	if (argc != 2) {
10627c478bd9Sstevel@tonic-gate 		(void) printf("usage: %s local-directory\n", argv[0]);
10637c478bd9Sstevel@tonic-gate 		code = -1;
10647c478bd9Sstevel@tonic-gate 		return;
10657c478bd9Sstevel@tonic-gate 	}
10667c478bd9Sstevel@tonic-gate 	if (!globulize(&argv[1])) {
10677c478bd9Sstevel@tonic-gate 		code = -1;
10687c478bd9Sstevel@tonic-gate 		return;
10697c478bd9Sstevel@tonic-gate 	}
10707c478bd9Sstevel@tonic-gate 	if (chdir(argv[1]) < 0) {
10717c478bd9Sstevel@tonic-gate 		perror(argv[1]);
10727c478bd9Sstevel@tonic-gate 		code = -1;
10737c478bd9Sstevel@tonic-gate 		return;
10747c478bd9Sstevel@tonic-gate 	}
10757c478bd9Sstevel@tonic-gate 	bufptr = getcwd(buf, MAXPATHLEN);
10767c478bd9Sstevel@tonic-gate 	/*
10777c478bd9Sstevel@tonic-gate 	 * Even though chdir may succeed, getcwd may fail if a component
10787c478bd9Sstevel@tonic-gate 	 * of the pwd is unreadable. In this case, print the argument to
10797c478bd9Sstevel@tonic-gate 	 * chdir as the resultant directory, since we know it succeeded above.
10807c478bd9Sstevel@tonic-gate 	 */
10817c478bd9Sstevel@tonic-gate 	(void) printf("Local directory now %s\n", (bufptr ? bufptr : argv[1]));
10827c478bd9Sstevel@tonic-gate 	code = 0;
10837c478bd9Sstevel@tonic-gate }
10847c478bd9Sstevel@tonic-gate 
10857c478bd9Sstevel@tonic-gate /*
10867c478bd9Sstevel@tonic-gate  * Delete a single file.
10877c478bd9Sstevel@tonic-gate  */
10887c478bd9Sstevel@tonic-gate void
delete(int argc,char * argv[])10897c478bd9Sstevel@tonic-gate delete(int argc, char *argv[])
10907c478bd9Sstevel@tonic-gate {
10917c478bd9Sstevel@tonic-gate 
10927c478bd9Sstevel@tonic-gate 	if (argc < 2) {
10937c478bd9Sstevel@tonic-gate 		if (prompt_for_arg(line, sizeof (line), "remote-file") < 0) {
10947c478bd9Sstevel@tonic-gate 			code = -1;
10957c478bd9Sstevel@tonic-gate 			return;
10967c478bd9Sstevel@tonic-gate 		}
10977c478bd9Sstevel@tonic-gate 		makeargv();
10987c478bd9Sstevel@tonic-gate 		argc = margc;
10997c478bd9Sstevel@tonic-gate 		argv = margv;
11007c478bd9Sstevel@tonic-gate 	}
11017c478bd9Sstevel@tonic-gate 	if (argc < 2) {
11027c478bd9Sstevel@tonic-gate 		(void) printf("usage: %s remote-file\n", argv[0]);
11037c478bd9Sstevel@tonic-gate 		code = -1;
11047c478bd9Sstevel@tonic-gate 		return;
11057c478bd9Sstevel@tonic-gate 	}
11067c478bd9Sstevel@tonic-gate 	(void) command("DELE %s", argv[1]);
11077c478bd9Sstevel@tonic-gate }
11087c478bd9Sstevel@tonic-gate 
11097c478bd9Sstevel@tonic-gate /*
11107c478bd9Sstevel@tonic-gate  * Delete multiple files.
11117c478bd9Sstevel@tonic-gate  */
11127c478bd9Sstevel@tonic-gate void
mdelete(int argc,char * argv[])11137c478bd9Sstevel@tonic-gate mdelete(int argc, char *argv[])
11147c478bd9Sstevel@tonic-gate {
11157c478bd9Sstevel@tonic-gate 	char *cp;
11167c478bd9Sstevel@tonic-gate 	int ointer;
11177c478bd9Sstevel@tonic-gate 	void (*oldintr)();
11187c478bd9Sstevel@tonic-gate 
11197c478bd9Sstevel@tonic-gate 	if (argc < 2) {
11207c478bd9Sstevel@tonic-gate 		if (prompt_for_arg(line, sizeof (line), "remote-files") < 0) {
11217c478bd9Sstevel@tonic-gate 			code = -1;
11227c478bd9Sstevel@tonic-gate 			return;
11237c478bd9Sstevel@tonic-gate 		}
11247c478bd9Sstevel@tonic-gate 		makeargv();
11257c478bd9Sstevel@tonic-gate 		argc = margc;
11267c478bd9Sstevel@tonic-gate 		argv = margv;
11277c478bd9Sstevel@tonic-gate 	}
11287c478bd9Sstevel@tonic-gate 	if (argc < 2) {
11297c478bd9Sstevel@tonic-gate 		(void) printf("usage: %s remote-files\n", argv[0]);
11307c478bd9Sstevel@tonic-gate 		code = -1;
11317c478bd9Sstevel@tonic-gate 		return;
11327c478bd9Sstevel@tonic-gate 	}
11337c478bd9Sstevel@tonic-gate 	mname = argv[0];
11347c478bd9Sstevel@tonic-gate 	mflag = 1;
11357c478bd9Sstevel@tonic-gate 	oldintr = signal(SIGINT, mabort);
11367c478bd9Sstevel@tonic-gate 	(void) setjmp(jabort);
11377c478bd9Sstevel@tonic-gate 	while ((cp = remglob(argv, 0)) != NULL) {
11387c478bd9Sstevel@tonic-gate 		if (*cp == '\0') {
11397c478bd9Sstevel@tonic-gate 			mflag = 0;
11407c478bd9Sstevel@tonic-gate 			continue;
11417c478bd9Sstevel@tonic-gate 		}
11427c478bd9Sstevel@tonic-gate 		if (mflag && confirm(argv[0], cp)) {
11437c478bd9Sstevel@tonic-gate 			(void) command("DELE %s", cp);
11447c478bd9Sstevel@tonic-gate 			if (!mflag && fromatty) {
11457c478bd9Sstevel@tonic-gate 				ointer = interactive;
11467c478bd9Sstevel@tonic-gate 				interactive = 1;
11477c478bd9Sstevel@tonic-gate 				if (confirm("Continue with", "mdelete")) {
11487c478bd9Sstevel@tonic-gate 					mflag++;
11497c478bd9Sstevel@tonic-gate 				}
11507c478bd9Sstevel@tonic-gate 				interactive = ointer;
11517c478bd9Sstevel@tonic-gate 			}
11527c478bd9Sstevel@tonic-gate 		}
11537c478bd9Sstevel@tonic-gate 	}
11547c478bd9Sstevel@tonic-gate 	(void) signal(SIGINT, oldintr);
11557c478bd9Sstevel@tonic-gate 	mflag = 0;
11567c478bd9Sstevel@tonic-gate }
11577c478bd9Sstevel@tonic-gate 
11587c478bd9Sstevel@tonic-gate /*
11597c478bd9Sstevel@tonic-gate  * Rename a remote file.
11607c478bd9Sstevel@tonic-gate  */
11617c478bd9Sstevel@tonic-gate void
renamefile(int argc,char * argv[])11627c478bd9Sstevel@tonic-gate renamefile(int argc, char *argv[])
11637c478bd9Sstevel@tonic-gate {
11647c478bd9Sstevel@tonic-gate 
11657c478bd9Sstevel@tonic-gate 	if (argc < 2) {
11667c478bd9Sstevel@tonic-gate 		if (prompt_for_arg(line, sizeof (line), "from-name") < 0) {
11677c478bd9Sstevel@tonic-gate 			code = -1;
11687c478bd9Sstevel@tonic-gate 			return;
11697c478bd9Sstevel@tonic-gate 		}
11707c478bd9Sstevel@tonic-gate 		makeargv();
11717c478bd9Sstevel@tonic-gate 		argc = margc;
11727c478bd9Sstevel@tonic-gate 		argv = margv;
11737c478bd9Sstevel@tonic-gate 	}
11747c478bd9Sstevel@tonic-gate 	if (argc < 2) {
11757c478bd9Sstevel@tonic-gate usage:
11767c478bd9Sstevel@tonic-gate 		(void) printf("%s from-name to-name\n", argv[0]);
11777c478bd9Sstevel@tonic-gate 		code = -1;
11787c478bd9Sstevel@tonic-gate 		return;
11797c478bd9Sstevel@tonic-gate 	}
11807c478bd9Sstevel@tonic-gate 	if (argc < 3) {
11817c478bd9Sstevel@tonic-gate 		if (prompt_for_arg(line, sizeof (line), "to-name") < 0) {
11827c478bd9Sstevel@tonic-gate 			code = -1;
11837c478bd9Sstevel@tonic-gate 			return;
11847c478bd9Sstevel@tonic-gate 		}
11857c478bd9Sstevel@tonic-gate 		makeargv();
11867c478bd9Sstevel@tonic-gate 		argc = margc;
11877c478bd9Sstevel@tonic-gate 		argv = margv;
11887c478bd9Sstevel@tonic-gate 	}
11897c478bd9Sstevel@tonic-gate 	if (argc < 3)
11907c478bd9Sstevel@tonic-gate 		goto usage;
11917c478bd9Sstevel@tonic-gate 	if (command("RNFR %s", argv[1]) == CONTINUE)
11927c478bd9Sstevel@tonic-gate 		(void) command("RNTO %s", argv[2]);
11937c478bd9Sstevel@tonic-gate }
11947c478bd9Sstevel@tonic-gate 
11957c478bd9Sstevel@tonic-gate /*
11967c478bd9Sstevel@tonic-gate  * Get a directory listing
11977c478bd9Sstevel@tonic-gate  * of remote files.
11987c478bd9Sstevel@tonic-gate  */
11997c478bd9Sstevel@tonic-gate void
ls(int argc,char * argv[])12007c478bd9Sstevel@tonic-gate ls(int argc, char *argv[])
12017c478bd9Sstevel@tonic-gate {
12027c478bd9Sstevel@tonic-gate 	char *cmd;
12037c478bd9Sstevel@tonic-gate 
12047c478bd9Sstevel@tonic-gate 	if (argc < 2)
12057c478bd9Sstevel@tonic-gate 		argc++, argv[1] = NULL;
12067c478bd9Sstevel@tonic-gate 	if (argc < 3)
12077c478bd9Sstevel@tonic-gate 		argc++, argv[2] = "-";
12087c478bd9Sstevel@tonic-gate 	if (argc > 3) {
12097c478bd9Sstevel@tonic-gate 		(void) printf("usage: %s remote-directory local-file\n",
12107c478bd9Sstevel@tonic-gate 			argv[0]);
12117c478bd9Sstevel@tonic-gate 		code = -1;
12127c478bd9Sstevel@tonic-gate 		return;
12137c478bd9Sstevel@tonic-gate 	}
12147c478bd9Sstevel@tonic-gate 	if (ls_invokes_NLST) {
12157c478bd9Sstevel@tonic-gate 		cmd = ((argv[0][0] == 'l' || argv[0][0] == 'n') ?
12167c478bd9Sstevel@tonic-gate 		    "NLST" : "LIST");
12177c478bd9Sstevel@tonic-gate 	} else {
12187c478bd9Sstevel@tonic-gate 		cmd = ((argv[0][0] == 'n') ? "NLST" : "LIST");
12197c478bd9Sstevel@tonic-gate 	}
12207c478bd9Sstevel@tonic-gate 	if (strcmp(argv[2], "-") && !globulize(&argv[2])) {
12217c478bd9Sstevel@tonic-gate 		code = -1;
12227c478bd9Sstevel@tonic-gate 		return;
12237c478bd9Sstevel@tonic-gate 	}
12247c478bd9Sstevel@tonic-gate 	recvrequest(cmd, argv[2], argv[1], "w", 1);
12257c478bd9Sstevel@tonic-gate }
12267c478bd9Sstevel@tonic-gate 
12277c478bd9Sstevel@tonic-gate /*
12287c478bd9Sstevel@tonic-gate  * Get a directory listing
12297c478bd9Sstevel@tonic-gate  * of multiple remote files.
12307c478bd9Sstevel@tonic-gate  */
12317c478bd9Sstevel@tonic-gate void
mls(int argc,char * argv[])12327c478bd9Sstevel@tonic-gate mls(int argc, char *argv[])
12337c478bd9Sstevel@tonic-gate {
12347c478bd9Sstevel@tonic-gate 	char *cmd, mode[1], *dest;
12357c478bd9Sstevel@tonic-gate 	int ointer, i;
12367c478bd9Sstevel@tonic-gate 	void (*oldintr)();
12377c478bd9Sstevel@tonic-gate 
12387c478bd9Sstevel@tonic-gate 	if (argc < 2) {
12397c478bd9Sstevel@tonic-gate 		if (prompt_for_arg(line, sizeof (line), "remote-files") < 0) {
12407c478bd9Sstevel@tonic-gate 			code = -1;
12417c478bd9Sstevel@tonic-gate 			return;
12427c478bd9Sstevel@tonic-gate 		}
12437c478bd9Sstevel@tonic-gate 		makeargv();
12447c478bd9Sstevel@tonic-gate 		argc = margc;
12457c478bd9Sstevel@tonic-gate 		argv = margv;
12467c478bd9Sstevel@tonic-gate 	}
12477c478bd9Sstevel@tonic-gate 	if (argc < 3) {
12487c478bd9Sstevel@tonic-gate 		if (prompt_for_arg(line, sizeof (line), "local-file") < 0) {
12497c478bd9Sstevel@tonic-gate 			code = -1;
12507c478bd9Sstevel@tonic-gate 			return;
12517c478bd9Sstevel@tonic-gate 		}
12527c478bd9Sstevel@tonic-gate 		makeargv();
12537c478bd9Sstevel@tonic-gate 		argc = margc;
12547c478bd9Sstevel@tonic-gate 		argv = margv;
12557c478bd9Sstevel@tonic-gate 	}
12567c478bd9Sstevel@tonic-gate 	if (argc < 3) {
12577c478bd9Sstevel@tonic-gate 		(void) printf("usage: %s remote-files local-file\n", argv[0]);
12587c478bd9Sstevel@tonic-gate 		code = -1;
12597c478bd9Sstevel@tonic-gate 		return;
12607c478bd9Sstevel@tonic-gate 	}
12617c478bd9Sstevel@tonic-gate 	dest = argv[argc - 1];
12627c478bd9Sstevel@tonic-gate 	argv[argc - 1] = NULL;
12637c478bd9Sstevel@tonic-gate 	if (strcmp(dest, "-") && *dest != '|')
12647c478bd9Sstevel@tonic-gate 		if (!globulize(&dest) ||
12657c478bd9Sstevel@tonic-gate 		    !confirm("output to local-file:", dest)) {
12667c478bd9Sstevel@tonic-gate 			code = -1;
12677c478bd9Sstevel@tonic-gate 			return;
12687c478bd9Sstevel@tonic-gate 		}
12697c478bd9Sstevel@tonic-gate 	cmd = argv[0][1] == 'l' ? "NLST" : "LIST";
12707c478bd9Sstevel@tonic-gate 	mname = argv[0];
12717c478bd9Sstevel@tonic-gate 	mflag = 1;
12727c478bd9Sstevel@tonic-gate 	oldintr = signal(SIGINT, mabort);
12737c478bd9Sstevel@tonic-gate 	(void) setjmp(jabort);
12747c478bd9Sstevel@tonic-gate 	for (i = 1; mflag && i < argc-1; ++i) {
12757c478bd9Sstevel@tonic-gate 		*mode = (i == 1) ? 'w' : 'a';
12767c478bd9Sstevel@tonic-gate 		recvrequest(cmd, dest, argv[i], mode, 1);
12777c478bd9Sstevel@tonic-gate 		if (!mflag && fromatty) {
12787c478bd9Sstevel@tonic-gate 			ointer = interactive;
12797c478bd9Sstevel@tonic-gate 			interactive = 1;
12807c478bd9Sstevel@tonic-gate 			if (confirm("Continue with", argv[0])) {
12817c478bd9Sstevel@tonic-gate 				mflag ++;
12827c478bd9Sstevel@tonic-gate 			}
12837c478bd9Sstevel@tonic-gate 			interactive = ointer;
12847c478bd9Sstevel@tonic-gate 		}
12857c478bd9Sstevel@tonic-gate 	}
12867c478bd9Sstevel@tonic-gate 	(void) signal(SIGINT, oldintr);
12877c478bd9Sstevel@tonic-gate 	mflag = 0;
12887c478bd9Sstevel@tonic-gate }
12897c478bd9Sstevel@tonic-gate 
12907c478bd9Sstevel@tonic-gate /*
12917c478bd9Sstevel@tonic-gate  * Do a shell escape
12927c478bd9Sstevel@tonic-gate  */
12937c478bd9Sstevel@tonic-gate /*ARGSUSED*/
12947c478bd9Sstevel@tonic-gate void
shell(int argc,char * argv[])12957c478bd9Sstevel@tonic-gate shell(int argc, char *argv[])
12967c478bd9Sstevel@tonic-gate {
12977c478bd9Sstevel@tonic-gate 	pid_t pid;
12987c478bd9Sstevel@tonic-gate 	void (*old1)(), (*old2)();
12997c478bd9Sstevel@tonic-gate 	char *shellstring, *namep;
13007c478bd9Sstevel@tonic-gate 	int status;
13017c478bd9Sstevel@tonic-gate 
13027c478bd9Sstevel@tonic-gate 	stop_timer();
13037c478bd9Sstevel@tonic-gate 	old1 = signal(SIGINT, SIG_IGN);
13047c478bd9Sstevel@tonic-gate 	old2 = signal(SIGQUIT, SIG_IGN);
13057c478bd9Sstevel@tonic-gate 	if ((pid = fork()) == 0) {
13067c478bd9Sstevel@tonic-gate 		closefrom(STDERR_FILENO + 1);
13077c478bd9Sstevel@tonic-gate 		(void) signal(SIGINT, SIG_DFL);
13087c478bd9Sstevel@tonic-gate 		(void) signal(SIGQUIT, SIG_DFL);
13097c478bd9Sstevel@tonic-gate 		shellstring = getenv("SHELL");
13107c478bd9Sstevel@tonic-gate 		if (shellstring == NULL)
13117c478bd9Sstevel@tonic-gate 			shellstring = "/bin/sh";
13127c478bd9Sstevel@tonic-gate 		namep = rindex(shellstring, '/');
13137c478bd9Sstevel@tonic-gate 		if (namep == NULL)
13147c478bd9Sstevel@tonic-gate 			namep = shellstring;
13157c478bd9Sstevel@tonic-gate 		if (argc > 1) {
13167c478bd9Sstevel@tonic-gate 			if (debug) {
13177c478bd9Sstevel@tonic-gate 				(void) printf("%s -c %s\n", shellstring,
13187c478bd9Sstevel@tonic-gate 					altarg);
13197c478bd9Sstevel@tonic-gate 				(void) fflush(stdout);
13207c478bd9Sstevel@tonic-gate 			}
13217c478bd9Sstevel@tonic-gate 			execl(shellstring, namep, "-c", altarg, (char *)0);
13227c478bd9Sstevel@tonic-gate 		} else {
13237c478bd9Sstevel@tonic-gate 			if (debug) {
13247c478bd9Sstevel@tonic-gate 				(void) printf("%s\n", shellstring);
13257c478bd9Sstevel@tonic-gate 				(void) fflush(stdout);
13267c478bd9Sstevel@tonic-gate 			}
13277c478bd9Sstevel@tonic-gate 			execl(shellstring, namep, (char *)0);
13287c478bd9Sstevel@tonic-gate 		}
13297c478bd9Sstevel@tonic-gate 		perror(shellstring);
13307c478bd9Sstevel@tonic-gate 		code = -1;
13317c478bd9Sstevel@tonic-gate 		exit(1);
13327c478bd9Sstevel@tonic-gate 		}
13337c478bd9Sstevel@tonic-gate 	if (pid > 0)
13347c478bd9Sstevel@tonic-gate 		while (wait(&status) != pid)
13357c478bd9Sstevel@tonic-gate 			;
13367c478bd9Sstevel@tonic-gate 	(void) signal(SIGINT, old1);
13377c478bd9Sstevel@tonic-gate 	(void) signal(SIGQUIT, old2);
13387c478bd9Sstevel@tonic-gate 	reset_timer();
13397c478bd9Sstevel@tonic-gate 	if (pid == (pid_t)-1) {
13407c478bd9Sstevel@tonic-gate 		perror("Try again later");
13417c478bd9Sstevel@tonic-gate 		code = -1;
13427c478bd9Sstevel@tonic-gate 	} else {
13437c478bd9Sstevel@tonic-gate 		code = 0;
13447c478bd9Sstevel@tonic-gate 	}
13457c478bd9Sstevel@tonic-gate }
13467c478bd9Sstevel@tonic-gate 
13477c478bd9Sstevel@tonic-gate /*
13487c478bd9Sstevel@tonic-gate  * Send new user information (re-login)
13497c478bd9Sstevel@tonic-gate  */
13507c478bd9Sstevel@tonic-gate void
user(int argc,char * argv[])13517c478bd9Sstevel@tonic-gate user(int argc, char *argv[])
13527c478bd9Sstevel@tonic-gate {
13537c478bd9Sstevel@tonic-gate 	char acct[80];
13547c478bd9Sstevel@tonic-gate 	int n, aflag = 0;
13557c478bd9Sstevel@tonic-gate 
13567c478bd9Sstevel@tonic-gate 	if (argc < 2) {
13577c478bd9Sstevel@tonic-gate 		if (prompt_for_arg(line, sizeof (line), "username") < 0) {
13587c478bd9Sstevel@tonic-gate 			code = -1;
13597c478bd9Sstevel@tonic-gate 			return;
13607c478bd9Sstevel@tonic-gate 		}
13617c478bd9Sstevel@tonic-gate 		makeargv();
13627c478bd9Sstevel@tonic-gate 		argc = margc;
13637c478bd9Sstevel@tonic-gate 		argv = margv;
13647c478bd9Sstevel@tonic-gate 	}
13657c478bd9Sstevel@tonic-gate 	if (argc > 4) {
13667c478bd9Sstevel@tonic-gate 		(void) printf("usage: %s username [password] [account]\n",
13677c478bd9Sstevel@tonic-gate 			argv[0]);
13687c478bd9Sstevel@tonic-gate 		code = -1;
13697c478bd9Sstevel@tonic-gate 		return;
13707c478bd9Sstevel@tonic-gate 	}
13717c478bd9Sstevel@tonic-gate 	if (argv[1] == 0) {
13727c478bd9Sstevel@tonic-gate 		(void) printf("access for user (nil) denied\n");
13737c478bd9Sstevel@tonic-gate 		code = -1;
13747c478bd9Sstevel@tonic-gate 		return;
13757c478bd9Sstevel@tonic-gate 	}
13767c478bd9Sstevel@tonic-gate 	n = command("USER %s", argv[1]);
13777c478bd9Sstevel@tonic-gate 	if (n == CONTINUE) {
13787c478bd9Sstevel@tonic-gate 		int oldclevel;
13797c478bd9Sstevel@tonic-gate 		if (argc < 3)
13807c478bd9Sstevel@tonic-gate 			argv[2] = mygetpass("Password: "), argc++;
13817c478bd9Sstevel@tonic-gate 		if ((oldclevel = clevel) == PROT_S)
13827c478bd9Sstevel@tonic-gate 			clevel = PROT_P;
13837c478bd9Sstevel@tonic-gate 		n = command("PASS %s", argv[2]);
13847c478bd9Sstevel@tonic-gate 		/* level may have changed */
13857c478bd9Sstevel@tonic-gate 		if (clevel == PROT_P)
13867c478bd9Sstevel@tonic-gate 			clevel = oldclevel;
13877c478bd9Sstevel@tonic-gate 	}
13887c478bd9Sstevel@tonic-gate 	if (n == CONTINUE) {
13897c478bd9Sstevel@tonic-gate 		if (argc < 4) {
13907c478bd9Sstevel@tonic-gate 			(void) printf("Account: "); (void) fflush(stdout);
13917c478bd9Sstevel@tonic-gate 			stop_timer();
13927c478bd9Sstevel@tonic-gate 			(void) fgets(acct, sizeof (acct) - 1, stdin);
13937c478bd9Sstevel@tonic-gate 			reset_timer();
13947c478bd9Sstevel@tonic-gate 			acct[strlen(acct) - 1] = '\0';
13957c478bd9Sstevel@tonic-gate 			argv[3] = acct; argc++;
13967c478bd9Sstevel@tonic-gate 		}
13977c478bd9Sstevel@tonic-gate 		n = command("ACCT %s", argv[3]);
13987c478bd9Sstevel@tonic-gate 		aflag++;
13997c478bd9Sstevel@tonic-gate 	}
14007c478bd9Sstevel@tonic-gate 	if (n != COMPLETE) {
14017c478bd9Sstevel@tonic-gate 		(void) fprintf(stdout, "Login failed.\n");
14027c478bd9Sstevel@tonic-gate 		return;
14037c478bd9Sstevel@tonic-gate 	}
14047c478bd9Sstevel@tonic-gate 	if (!aflag && argc == 4) {
14057c478bd9Sstevel@tonic-gate 		(void) command("ACCT %s", argv[3]);
14067c478bd9Sstevel@tonic-gate 	}
14077c478bd9Sstevel@tonic-gate }
14087c478bd9Sstevel@tonic-gate 
14097c478bd9Sstevel@tonic-gate /*
14107c478bd9Sstevel@tonic-gate  * Print working directory.
14117c478bd9Sstevel@tonic-gate  */
14127c478bd9Sstevel@tonic-gate /*ARGSUSED*/
14137c478bd9Sstevel@tonic-gate void
pwd(int argc,char * argv[])14147c478bd9Sstevel@tonic-gate pwd(int argc, char *argv[])
14157c478bd9Sstevel@tonic-gate {
14167c478bd9Sstevel@tonic-gate 	(void) command("PWD");
14177c478bd9Sstevel@tonic-gate }
14187c478bd9Sstevel@tonic-gate 
14197c478bd9Sstevel@tonic-gate /*
14207c478bd9Sstevel@tonic-gate  * Make a directory.
14217c478bd9Sstevel@tonic-gate  */
14227c478bd9Sstevel@tonic-gate void
makedir(int argc,char * argv[])14237c478bd9Sstevel@tonic-gate makedir(int argc, char *argv[])
14247c478bd9Sstevel@tonic-gate {
14257c478bd9Sstevel@tonic-gate 	if (argc < 2) {
14267c478bd9Sstevel@tonic-gate 		if (prompt_for_arg(line, sizeof (line), "directory-name") <
14277c478bd9Sstevel@tonic-gate 		    0) {
14287c478bd9Sstevel@tonic-gate 			code = -1;
14297c478bd9Sstevel@tonic-gate 			return;
14307c478bd9Sstevel@tonic-gate 		}
14317c478bd9Sstevel@tonic-gate 		makeargv();
14327c478bd9Sstevel@tonic-gate 		argc = margc;
14337c478bd9Sstevel@tonic-gate 		argv = margv;
14347c478bd9Sstevel@tonic-gate 	}
14357c478bd9Sstevel@tonic-gate 	if (argc < 2) {
14367c478bd9Sstevel@tonic-gate 		(void) printf("usage: %s directory-name\n", argv[0]);
14377c478bd9Sstevel@tonic-gate 		code = -1;
14387c478bd9Sstevel@tonic-gate 		return;
14397c478bd9Sstevel@tonic-gate 	}
14407c478bd9Sstevel@tonic-gate 	(void) command("MKD %s", argv[1]);
14417c478bd9Sstevel@tonic-gate }
14427c478bd9Sstevel@tonic-gate 
14437c478bd9Sstevel@tonic-gate /*
14447c478bd9Sstevel@tonic-gate  * Remove a directory.
14457c478bd9Sstevel@tonic-gate  */
14467c478bd9Sstevel@tonic-gate void
removedir(int argc,char * argv[])14477c478bd9Sstevel@tonic-gate removedir(int argc, char *argv[])
14487c478bd9Sstevel@tonic-gate {
14497c478bd9Sstevel@tonic-gate 	if (argc < 2) {
14507c478bd9Sstevel@tonic-gate 		if (prompt_for_arg(line, sizeof (line), "directory-name") <
14517c478bd9Sstevel@tonic-gate 		    0) {
14527c478bd9Sstevel@tonic-gate 			code = -1;
14537c478bd9Sstevel@tonic-gate 			return;
14547c478bd9Sstevel@tonic-gate 		}
14557c478bd9Sstevel@tonic-gate 		makeargv();
14567c478bd9Sstevel@tonic-gate 		argc = margc;
14577c478bd9Sstevel@tonic-gate 		argv = margv;
14587c478bd9Sstevel@tonic-gate 	}
14597c478bd9Sstevel@tonic-gate 	if (argc < 2) {
14607c478bd9Sstevel@tonic-gate 		(void) printf("usage: %s directory-name\n", argv[0]);
14617c478bd9Sstevel@tonic-gate 		code = -1;
14627c478bd9Sstevel@tonic-gate 		return;
14637c478bd9Sstevel@tonic-gate 	}
14647c478bd9Sstevel@tonic-gate 	(void) command("RMD %s", argv[1]);
14657c478bd9Sstevel@tonic-gate }
14667c478bd9Sstevel@tonic-gate 
14677c478bd9Sstevel@tonic-gate /*
14687c478bd9Sstevel@tonic-gate  * Send a line, verbatim, to the remote machine.
14697c478bd9Sstevel@tonic-gate  */
14707c478bd9Sstevel@tonic-gate void
quote(int argc,char * argv[])14717c478bd9Sstevel@tonic-gate quote(int argc, char *argv[])
14727c478bd9Sstevel@tonic-gate {
14737c478bd9Sstevel@tonic-gate 	int i, n, len;
14747c478bd9Sstevel@tonic-gate 	char buf[FTPBUFSIZ];
14757c478bd9Sstevel@tonic-gate 
14767c478bd9Sstevel@tonic-gate 	if (argc < 2) {
14777c478bd9Sstevel@tonic-gate 		if (prompt_for_arg(line, sizeof (line),
14787c478bd9Sstevel@tonic-gate 		    "command line to send") == -1) {
14797c478bd9Sstevel@tonic-gate 			code = -1;
14807c478bd9Sstevel@tonic-gate 			return;
14817c478bd9Sstevel@tonic-gate 		}
14827c478bd9Sstevel@tonic-gate 		makeargv();
14837c478bd9Sstevel@tonic-gate 		argc = margc;
14847c478bd9Sstevel@tonic-gate 		argv = margv;
14857c478bd9Sstevel@tonic-gate 	}
14867c478bd9Sstevel@tonic-gate 	if (argc < 2) {
14877c478bd9Sstevel@tonic-gate 		(void) printf("usage: %s line-to-send\n", argv[0]);
14887c478bd9Sstevel@tonic-gate 		code = -1;
14897c478bd9Sstevel@tonic-gate 		return;
14907c478bd9Sstevel@tonic-gate 	}
14917c478bd9Sstevel@tonic-gate 	len = snprintf(buf, sizeof (buf), "%s", argv[1]);
14927c478bd9Sstevel@tonic-gate 	if (len >= 0 && len < sizeof (buf) - 1) {
14937c478bd9Sstevel@tonic-gate 		for (i = 2; i < argc; i++) {
14947c478bd9Sstevel@tonic-gate 			n = snprintf(&buf[len], sizeof (buf) - len, " %s",
14957c478bd9Sstevel@tonic-gate 					argv[i]);
14967c478bd9Sstevel@tonic-gate 			if (n < 0 || n >= sizeof (buf) - len)
14977c478bd9Sstevel@tonic-gate 				break;
14987c478bd9Sstevel@tonic-gate 			len += n;
14997c478bd9Sstevel@tonic-gate 		}
15007c478bd9Sstevel@tonic-gate 	}
15017c478bd9Sstevel@tonic-gate 	if (command("%s", buf) == PRELIM) {
15027c478bd9Sstevel@tonic-gate 		while (getreply(0) == PRELIM)
15037c478bd9Sstevel@tonic-gate 			;
15047c478bd9Sstevel@tonic-gate 	}
15057c478bd9Sstevel@tonic-gate }
15067c478bd9Sstevel@tonic-gate 
15077c478bd9Sstevel@tonic-gate /*
15087c478bd9Sstevel@tonic-gate  * Send a line, verbatim, to the remote machine as a SITE command.
15097c478bd9Sstevel@tonic-gate  */
15107c478bd9Sstevel@tonic-gate void
site(int argc,char * argv[])15117c478bd9Sstevel@tonic-gate site(int argc, char *argv[])
15127c478bd9Sstevel@tonic-gate {
15137c478bd9Sstevel@tonic-gate 	int i, n, len;
15147c478bd9Sstevel@tonic-gate 	char buf[FTPBUFSIZ];
15157c478bd9Sstevel@tonic-gate 
15167c478bd9Sstevel@tonic-gate 	if (argc < 2) {
15177c478bd9Sstevel@tonic-gate 		if (prompt_for_arg(line, sizeof (line),
15187c478bd9Sstevel@tonic-gate 		    "arguments to SITE command") == -1) {
15197c478bd9Sstevel@tonic-gate 			code = -1;
15207c478bd9Sstevel@tonic-gate 			return;
15217c478bd9Sstevel@tonic-gate 		}
15227c478bd9Sstevel@tonic-gate 		makeargv();
15237c478bd9Sstevel@tonic-gate 		argc = margc;
15247c478bd9Sstevel@tonic-gate 		argv = margv;
15257c478bd9Sstevel@tonic-gate 	}
15267c478bd9Sstevel@tonic-gate 	if (argc < 2) {
15277c478bd9Sstevel@tonic-gate 		(void) printf("usage: %s arg1 [arg2] ...\n", argv[0]);
15287c478bd9Sstevel@tonic-gate 		code = -1;
15297c478bd9Sstevel@tonic-gate 		return;
15307c478bd9Sstevel@tonic-gate 	}
15317c478bd9Sstevel@tonic-gate 	len = snprintf(buf, sizeof (buf), "%s", argv[1]);
15327c478bd9Sstevel@tonic-gate 	if (len >= 0 && len < sizeof (buf) - 1) {
15337c478bd9Sstevel@tonic-gate 		for (i = 2; i < argc; i++) {
15347c478bd9Sstevel@tonic-gate 			n = snprintf(&buf[len], sizeof (buf) - len, " %s",
15357c478bd9Sstevel@tonic-gate 					argv[i]);
15367c478bd9Sstevel@tonic-gate 			if (n < 0 || n >= sizeof (buf) - len)
15377c478bd9Sstevel@tonic-gate 				break;
15387c478bd9Sstevel@tonic-gate 			len += n;
15397c478bd9Sstevel@tonic-gate 		}
15407c478bd9Sstevel@tonic-gate 	}
15417c478bd9Sstevel@tonic-gate 	if (command("SITE %s", buf) == PRELIM) {
15427c478bd9Sstevel@tonic-gate 		while (getreply(0) == PRELIM)
15437c478bd9Sstevel@tonic-gate 			;
15447c478bd9Sstevel@tonic-gate 	}
15457c478bd9Sstevel@tonic-gate }
15467c478bd9Sstevel@tonic-gate 
15477c478bd9Sstevel@tonic-gate /*
15487c478bd9Sstevel@tonic-gate  * Ask the other side for help.
15497c478bd9Sstevel@tonic-gate  */
15507c478bd9Sstevel@tonic-gate void
rmthelp(int argc,char * argv[])15517c478bd9Sstevel@tonic-gate rmthelp(int argc, char *argv[])
15527c478bd9Sstevel@tonic-gate {
15537c478bd9Sstevel@tonic-gate 	int oldverbose = verbose;
15547c478bd9Sstevel@tonic-gate 
15557c478bd9Sstevel@tonic-gate 	verbose = 1;
15567c478bd9Sstevel@tonic-gate 	(void) command(argc == 1 ? "HELP" : "HELP %s", argv[1]);
15577c478bd9Sstevel@tonic-gate 	verbose = oldverbose;
15587c478bd9Sstevel@tonic-gate }
15597c478bd9Sstevel@tonic-gate 
15607c478bd9Sstevel@tonic-gate /*
15617c478bd9Sstevel@tonic-gate  * Terminate session and exit.
15627c478bd9Sstevel@tonic-gate  */
15637c478bd9Sstevel@tonic-gate /*ARGSUSED*/
15647c478bd9Sstevel@tonic-gate void
quit(int argc,char * argv[])15657c478bd9Sstevel@tonic-gate quit(int argc, char *argv[])
15667c478bd9Sstevel@tonic-gate {
15677c478bd9Sstevel@tonic-gate 	if (connected)
15687c478bd9Sstevel@tonic-gate 		disconnect(0, NULL);
15697c478bd9Sstevel@tonic-gate 	pswitch(1);
15707c478bd9Sstevel@tonic-gate 	if (connected) {
15717c478bd9Sstevel@tonic-gate 		disconnect(0, NULL);
15727c478bd9Sstevel@tonic-gate 	}
15737c478bd9Sstevel@tonic-gate 	exit(0);
15747c478bd9Sstevel@tonic-gate }
15757c478bd9Sstevel@tonic-gate 
15767c478bd9Sstevel@tonic-gate /*
15777c478bd9Sstevel@tonic-gate  * Terminate session, but don't exit.
15787c478bd9Sstevel@tonic-gate  */
15797c478bd9Sstevel@tonic-gate /*ARGSUSED*/
15807c478bd9Sstevel@tonic-gate void
disconnect(int argc,char * argv[])15817c478bd9Sstevel@tonic-gate disconnect(int argc, char *argv[])
15827c478bd9Sstevel@tonic-gate {
15837c478bd9Sstevel@tonic-gate 	extern FILE *ctrl_in, *ctrl_out;
15847c478bd9Sstevel@tonic-gate 	extern int data;
15857c478bd9Sstevel@tonic-gate 
15867c478bd9Sstevel@tonic-gate 	if (!connected)
15877c478bd9Sstevel@tonic-gate 		return;
15887c478bd9Sstevel@tonic-gate 	(void) command("QUIT");
15897c478bd9Sstevel@tonic-gate 	if (ctrl_in) {
15907c478bd9Sstevel@tonic-gate 		reset_timer();
15917c478bd9Sstevel@tonic-gate 		(void) fclose(ctrl_in);
15927c478bd9Sstevel@tonic-gate 	}
15937c478bd9Sstevel@tonic-gate 	if (ctrl_out) {
15947c478bd9Sstevel@tonic-gate 		reset_timer();
15957c478bd9Sstevel@tonic-gate 		(void) fclose(ctrl_out);
15967c478bd9Sstevel@tonic-gate 	}
15977c478bd9Sstevel@tonic-gate 	ctrl_out = ctrl_in = NULL;
15987c478bd9Sstevel@tonic-gate 	connected = 0;
15997c478bd9Sstevel@tonic-gate 	data = -1;
16007c478bd9Sstevel@tonic-gate 	if (!proxy) {
16017c478bd9Sstevel@tonic-gate 		macnum = 0;
16027c478bd9Sstevel@tonic-gate 	}
16037c478bd9Sstevel@tonic-gate 
16047c478bd9Sstevel@tonic-gate 	auth_type = AUTHTYPE_NONE;
16057c478bd9Sstevel@tonic-gate 	clevel = dlevel = PROT_C;
16067c478bd9Sstevel@tonic-gate 	goteof = 0;
16077c478bd9Sstevel@tonic-gate }
16087c478bd9Sstevel@tonic-gate 
16097c478bd9Sstevel@tonic-gate static int
confirm(char * cmd,char * file)16107c478bd9Sstevel@tonic-gate confirm(char *cmd, char *file)
16117c478bd9Sstevel@tonic-gate {
16127c478bd9Sstevel@tonic-gate 	char line[FTPBUFSIZ];
16137c478bd9Sstevel@tonic-gate 
16147c478bd9Sstevel@tonic-gate 	if (!interactive)
16157c478bd9Sstevel@tonic-gate 		return (1);
16167c478bd9Sstevel@tonic-gate 	stop_timer();
16177c478bd9Sstevel@tonic-gate 	(void) printf("%s %s? ", cmd, file);
16187c478bd9Sstevel@tonic-gate 	(void) fflush(stdout);
16197c478bd9Sstevel@tonic-gate 	*line = '\0';
16207c478bd9Sstevel@tonic-gate 	(void) fgets(line, sizeof (line), stdin);
16217c478bd9Sstevel@tonic-gate 	reset_timer();
16227c478bd9Sstevel@tonic-gate 	return (*line != 'n' && *line != 'N');
16237c478bd9Sstevel@tonic-gate }
16247c478bd9Sstevel@tonic-gate 
16257c478bd9Sstevel@tonic-gate void
fatal(char * msg)16267c478bd9Sstevel@tonic-gate fatal(char *msg)
16277c478bd9Sstevel@tonic-gate {
16287c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "ftp: %s\n", msg);
16297c478bd9Sstevel@tonic-gate 	exit(1);
16307c478bd9Sstevel@tonic-gate }
16317c478bd9Sstevel@tonic-gate 
16327c478bd9Sstevel@tonic-gate /*
16337c478bd9Sstevel@tonic-gate  * Glob a local file name specification with
16347c478bd9Sstevel@tonic-gate  * the expectation of a single return value.
16357c478bd9Sstevel@tonic-gate  * Can't control multiple values being expanded
16367c478bd9Sstevel@tonic-gate  * from the expression, we return only the first.
16377c478bd9Sstevel@tonic-gate  */
16387c478bd9Sstevel@tonic-gate static int
globulize(char ** cpp)16397c478bd9Sstevel@tonic-gate globulize(char **cpp)
16407c478bd9Sstevel@tonic-gate {
16417c478bd9Sstevel@tonic-gate 	char **globbed;
16427c478bd9Sstevel@tonic-gate 
16437c478bd9Sstevel@tonic-gate 	if (!doglob)
16447c478bd9Sstevel@tonic-gate 		return (1);
16457c478bd9Sstevel@tonic-gate 	globbed = glob(*cpp);
16467c478bd9Sstevel@tonic-gate 	if (globbed != NULL && *globbed == NULL && globerr == NULL)
16477c478bd9Sstevel@tonic-gate 		globerr = "No match";
16487c478bd9Sstevel@tonic-gate 	if (globerr != NULL) {
16497c478bd9Sstevel@tonic-gate 		(void) printf("%s: %s\n", *cpp, globerr);
16507c478bd9Sstevel@tonic-gate 		if (globbed)
16517c478bd9Sstevel@tonic-gate 			blkfree(globbed);
16527c478bd9Sstevel@tonic-gate 		return (0);
16537c478bd9Sstevel@tonic-gate 	}
16547c478bd9Sstevel@tonic-gate 	if (globbed) {
16557c478bd9Sstevel@tonic-gate 		*cpp = strdup(*globbed);
16567c478bd9Sstevel@tonic-gate 		blkfree(globbed);
16577c478bd9Sstevel@tonic-gate 		if (!*cpp)
16587c478bd9Sstevel@tonic-gate 			return (0);
16597c478bd9Sstevel@tonic-gate 	}
16607c478bd9Sstevel@tonic-gate 	return (1);
16617c478bd9Sstevel@tonic-gate }
16627c478bd9Sstevel@tonic-gate 
16637c478bd9Sstevel@tonic-gate void
account(int argc,char * argv[])16647c478bd9Sstevel@tonic-gate account(int argc, char *argv[])
16657c478bd9Sstevel@tonic-gate {
16667c478bd9Sstevel@tonic-gate 	char acct[50], *ap;
16677c478bd9Sstevel@tonic-gate 
16687c478bd9Sstevel@tonic-gate 	if (argc > 1) {
16697c478bd9Sstevel@tonic-gate 		++argv;
16707c478bd9Sstevel@tonic-gate 		--argc;
16717c478bd9Sstevel@tonic-gate 		(void) strncpy(acct, *argv, 49);
16727c478bd9Sstevel@tonic-gate 		acct[49] = '\0';
16737c478bd9Sstevel@tonic-gate 		while (argc > 1) {
16747c478bd9Sstevel@tonic-gate 			--argc;
16757c478bd9Sstevel@tonic-gate 			++argv;
16767c478bd9Sstevel@tonic-gate 			(void) strncat(acct, *argv, 49 - strlen(acct));
16777c478bd9Sstevel@tonic-gate 		}
16787c478bd9Sstevel@tonic-gate 		ap = acct;
16797c478bd9Sstevel@tonic-gate 	} else {
16807c478bd9Sstevel@tonic-gate 		ap = mygetpass("Account:");
16817c478bd9Sstevel@tonic-gate 	}
16827c478bd9Sstevel@tonic-gate 	(void) command("ACCT %s", ap);
16837c478bd9Sstevel@tonic-gate }
16847c478bd9Sstevel@tonic-gate 
16857c478bd9Sstevel@tonic-gate /*ARGSUSED*/
16867c478bd9Sstevel@tonic-gate static void
proxabort(int sig)16877c478bd9Sstevel@tonic-gate proxabort(int sig)
16887c478bd9Sstevel@tonic-gate {
16897c478bd9Sstevel@tonic-gate 	extern int proxy;
16907c478bd9Sstevel@tonic-gate 
16917c478bd9Sstevel@tonic-gate 	if (!proxy) {
16927c478bd9Sstevel@tonic-gate 		pswitch(1);
16937c478bd9Sstevel@tonic-gate 	}
16947c478bd9Sstevel@tonic-gate 	if (connected) {
16957c478bd9Sstevel@tonic-gate 		proxflag = 1;
16967c478bd9Sstevel@tonic-gate 	} else {
16977c478bd9Sstevel@tonic-gate 		proxflag = 0;
16987c478bd9Sstevel@tonic-gate 	}
16997c478bd9Sstevel@tonic-gate 	pswitch(0);
17007c478bd9Sstevel@tonic-gate 	longjmp(abortprox, 1);
17017c478bd9Sstevel@tonic-gate }
17027c478bd9Sstevel@tonic-gate 
17037c478bd9Sstevel@tonic-gate void
doproxy(int argc,char * argv[])17047c478bd9Sstevel@tonic-gate doproxy(int argc, char *argv[])
17057c478bd9Sstevel@tonic-gate {
17067c478bd9Sstevel@tonic-gate 	void (*oldintr)();
17077c478bd9Sstevel@tonic-gate 	struct cmd *c;
17087c478bd9Sstevel@tonic-gate 
17097c478bd9Sstevel@tonic-gate 	if (argc < 2) {
17107c478bd9Sstevel@tonic-gate 		if (prompt_for_arg(line, sizeof (line), "command") == -1) {
17117c478bd9Sstevel@tonic-gate 			code = -1;
17127c478bd9Sstevel@tonic-gate 			return;
17137c478bd9Sstevel@tonic-gate 		}
17147c478bd9Sstevel@tonic-gate 		makeargv();
17157c478bd9Sstevel@tonic-gate 		argc = margc;
17167c478bd9Sstevel@tonic-gate 		argv = margv;
17177c478bd9Sstevel@tonic-gate 	}
17187c478bd9Sstevel@tonic-gate 	if (argc < 2) {
17197c478bd9Sstevel@tonic-gate 		(void) printf("usage: %s command\n", argv[0]);
17207c478bd9Sstevel@tonic-gate 		code = -1;
17217c478bd9Sstevel@tonic-gate 		return;
17227c478bd9Sstevel@tonic-gate 	}
17237c478bd9Sstevel@tonic-gate 	c = getcmd(argv[1]);
17247c478bd9Sstevel@tonic-gate 	if (c == (struct cmd *)-1) {
17257c478bd9Sstevel@tonic-gate 		(void) printf("?Ambiguous command\n");
17267c478bd9Sstevel@tonic-gate 		(void) fflush(stdout);
17277c478bd9Sstevel@tonic-gate 		code = -1;
17287c478bd9Sstevel@tonic-gate 		return;
17297c478bd9Sstevel@tonic-gate 	}
17307c478bd9Sstevel@tonic-gate 	if (c == 0) {
17317c478bd9Sstevel@tonic-gate 		(void) printf("?Invalid command\n");
17327c478bd9Sstevel@tonic-gate 		(void) fflush(stdout);
17337c478bd9Sstevel@tonic-gate 		code = -1;
17347c478bd9Sstevel@tonic-gate 		return;
17357c478bd9Sstevel@tonic-gate 	}
17367c478bd9Sstevel@tonic-gate 	if (!c->c_proxy) {
17377c478bd9Sstevel@tonic-gate 		(void) printf("?Invalid proxy command\n");
17387c478bd9Sstevel@tonic-gate 		(void) fflush(stdout);
17397c478bd9Sstevel@tonic-gate 		code = -1;
17407c478bd9Sstevel@tonic-gate 		return;
17417c478bd9Sstevel@tonic-gate 	}
17427c478bd9Sstevel@tonic-gate 	if (setjmp(abortprox)) {
17437c478bd9Sstevel@tonic-gate 		code = -1;
17447c478bd9Sstevel@tonic-gate 		return;
17457c478bd9Sstevel@tonic-gate 	}
17467c478bd9Sstevel@tonic-gate 	oldintr = signal(SIGINT, (void (*)())proxabort);
17477c478bd9Sstevel@tonic-gate 	pswitch(1);
17487c478bd9Sstevel@tonic-gate 	if (c->c_conn && !connected) {
17497c478bd9Sstevel@tonic-gate 		(void) printf("Not connected\n");
17507c478bd9Sstevel@tonic-gate 		(void) fflush(stdout);
17517c478bd9Sstevel@tonic-gate 		pswitch(0);
17527c478bd9Sstevel@tonic-gate 		(void) signal(SIGINT, oldintr);
17537c478bd9Sstevel@tonic-gate 		code = -1;
17547c478bd9Sstevel@tonic-gate 		return;
17557c478bd9Sstevel@tonic-gate 	}
17567c478bd9Sstevel@tonic-gate 	(*c->c_handler)(argc-1, argv+1);
17577c478bd9Sstevel@tonic-gate 	if (connected) {
17587c478bd9Sstevel@tonic-gate 		proxflag = 1;
17597c478bd9Sstevel@tonic-gate 	} else {
17607c478bd9Sstevel@tonic-gate 		proxflag = 0;
17617c478bd9Sstevel@tonic-gate 	}
17627c478bd9Sstevel@tonic-gate 	pswitch(0);
17637c478bd9Sstevel@tonic-gate 	(void) signal(SIGINT, oldintr);
17647c478bd9Sstevel@tonic-gate }
17657c478bd9Sstevel@tonic-gate 
17667c478bd9Sstevel@tonic-gate /*ARGSUSED*/
17677c478bd9Sstevel@tonic-gate void
setcase(int argc,char * argv[])17687c478bd9Sstevel@tonic-gate setcase(int argc, char *argv[])
17697c478bd9Sstevel@tonic-gate {
17707c478bd9Sstevel@tonic-gate 	mcase = !mcase;
17717c478bd9Sstevel@tonic-gate 	(void) printf("Case mapping %s.\n", onoff(mcase));
17727c478bd9Sstevel@tonic-gate 	code = mcase;
17737c478bd9Sstevel@tonic-gate }
17747c478bd9Sstevel@tonic-gate 
17757c478bd9Sstevel@tonic-gate /*ARGSUSED*/
17767c478bd9Sstevel@tonic-gate void
setcr(int argc,char * argv[])17777c478bd9Sstevel@tonic-gate setcr(int argc, char *argv[])
17787c478bd9Sstevel@tonic-gate {
17797c478bd9Sstevel@tonic-gate 	crflag = !crflag;
17807c478bd9Sstevel@tonic-gate 	(void) printf("Carriage Return stripping %s.\n", onoff(crflag));
17817c478bd9Sstevel@tonic-gate 	code = crflag;
17827c478bd9Sstevel@tonic-gate }
17837c478bd9Sstevel@tonic-gate 
17847c478bd9Sstevel@tonic-gate void
setntrans(int argc,char * argv[])17857c478bd9Sstevel@tonic-gate setntrans(int argc, char *argv[])
17867c478bd9Sstevel@tonic-gate {
17877c478bd9Sstevel@tonic-gate 	if (argc == 1) {
17887c478bd9Sstevel@tonic-gate 		ntflag = 0;
17897c478bd9Sstevel@tonic-gate 		(void) printf("Ntrans off.\n");
17907c478bd9Sstevel@tonic-gate 		code = ntflag;
17917c478bd9Sstevel@tonic-gate 		return;
17927c478bd9Sstevel@tonic-gate 	}
17937c478bd9Sstevel@tonic-gate 	ntflag++;
17947c478bd9Sstevel@tonic-gate 	code = ntflag;
17957c478bd9Sstevel@tonic-gate 	(void) strncpy(ntin, argv[1], 16);
17967c478bd9Sstevel@tonic-gate 	ntin[16] = '\0';
17977c478bd9Sstevel@tonic-gate 	if (argc == 2) {
17987c478bd9Sstevel@tonic-gate 		ntout[0] = '\0';
17997c478bd9Sstevel@tonic-gate 		return;
18007c478bd9Sstevel@tonic-gate 	}
18017c478bd9Sstevel@tonic-gate 	(void) strncpy(ntout, argv[2], 16);
18027c478bd9Sstevel@tonic-gate 	ntout[16] = '\0';
18037c478bd9Sstevel@tonic-gate }
18047c478bd9Sstevel@tonic-gate 
18057c478bd9Sstevel@tonic-gate static char *
dotrans(char * name)18067c478bd9Sstevel@tonic-gate dotrans(char *name)
18077c478bd9Sstevel@tonic-gate {
18087c478bd9Sstevel@tonic-gate 	static char new[MAXPATHLEN];
18097c478bd9Sstevel@tonic-gate 	char *cp1, *cp2 = new;
18107c478bd9Sstevel@tonic-gate 	int i, ostop, found;
18117c478bd9Sstevel@tonic-gate 
18127c478bd9Sstevel@tonic-gate 	for (ostop = 0; *(ntout + ostop) && ostop < 16; ostop++)
18137c478bd9Sstevel@tonic-gate 		;
18147c478bd9Sstevel@tonic-gate 	for (cp1 = name; *cp1; cp1++) {
18157c478bd9Sstevel@tonic-gate 		found = 0;
18167c478bd9Sstevel@tonic-gate 		for (i = 0; *(ntin + i) && i < 16; i++) {
18177c478bd9Sstevel@tonic-gate 			if (*cp1 == *(ntin + i)) {
18187c478bd9Sstevel@tonic-gate 				found++;
18197c478bd9Sstevel@tonic-gate 				if (i < ostop) {
18207c478bd9Sstevel@tonic-gate 					*cp2++ = *(ntout + i);
18217c478bd9Sstevel@tonic-gate 				}
18227c478bd9Sstevel@tonic-gate 				break;
18237c478bd9Sstevel@tonic-gate 			}
18247c478bd9Sstevel@tonic-gate 		}
18257c478bd9Sstevel@tonic-gate 		if (!found) {
18267c478bd9Sstevel@tonic-gate 			*cp2++ = *cp1;
18277c478bd9Sstevel@tonic-gate 		}
18287c478bd9Sstevel@tonic-gate 	}
18297c478bd9Sstevel@tonic-gate 	*cp2 = '\0';
18307c478bd9Sstevel@tonic-gate 	return (new);
18317c478bd9Sstevel@tonic-gate }
18327c478bd9Sstevel@tonic-gate 
18337c478bd9Sstevel@tonic-gate void
setnmap(int argc,char * argv[])18347c478bd9Sstevel@tonic-gate setnmap(int argc, char *argv[])
18357c478bd9Sstevel@tonic-gate {
18367c478bd9Sstevel@tonic-gate 	char *cp;
18377c478bd9Sstevel@tonic-gate 
18387c478bd9Sstevel@tonic-gate 	if (argc == 1) {
18397c478bd9Sstevel@tonic-gate 		mapflag = 0;
18407c478bd9Sstevel@tonic-gate 		(void) printf("Nmap off.\n");
18417c478bd9Sstevel@tonic-gate 		code = mapflag;
18427c478bd9Sstevel@tonic-gate 		return;
18437c478bd9Sstevel@tonic-gate 	}
18447c478bd9Sstevel@tonic-gate 	if (argc < 3) {
18457c478bd9Sstevel@tonic-gate 		if (prompt_for_arg(line, sizeof (line), "mapout") == -1) {
18467c478bd9Sstevel@tonic-gate 			code = -1;
18477c478bd9Sstevel@tonic-gate 			return;
18487c478bd9Sstevel@tonic-gate 		}
18497c478bd9Sstevel@tonic-gate 		makeargv();
18507c478bd9Sstevel@tonic-gate 		argc = margc;
18517c478bd9Sstevel@tonic-gate 		argv = margv;
18527c478bd9Sstevel@tonic-gate 	}
18537c478bd9Sstevel@tonic-gate 	if (argc < 3) {
18547c478bd9Sstevel@tonic-gate 		(void) printf("Usage: %s [mapin mapout]\n", argv[0]);
18557c478bd9Sstevel@tonic-gate 		code = -1;
18567c478bd9Sstevel@tonic-gate 		return;
18577c478bd9Sstevel@tonic-gate 	}
18587c478bd9Sstevel@tonic-gate 	mapflag = 1;
18597c478bd9Sstevel@tonic-gate 	code = 1;
18607c478bd9Sstevel@tonic-gate 	cp = index(altarg, ' ');
18617c478bd9Sstevel@tonic-gate 	if (proxy) {
18627c478bd9Sstevel@tonic-gate 		while (*++cp == ' ')
18637c478bd9Sstevel@tonic-gate 			/* NULL */;
18647c478bd9Sstevel@tonic-gate 		altarg = cp;
18657c478bd9Sstevel@tonic-gate 		cp = index(altarg, ' ');
18667c478bd9Sstevel@tonic-gate 	}
18677c478bd9Sstevel@tonic-gate 	*cp = '\0';
18687c478bd9Sstevel@tonic-gate 	(void) strncpy(mapin, altarg, MAXPATHLEN - 1);
18697c478bd9Sstevel@tonic-gate 	while (*++cp == ' ')
18707c478bd9Sstevel@tonic-gate 		/* NULL */;
18717c478bd9Sstevel@tonic-gate 	(void) strncpy(mapout, cp, MAXPATHLEN - 1);
18727c478bd9Sstevel@tonic-gate }
18737c478bd9Sstevel@tonic-gate 
18747c478bd9Sstevel@tonic-gate static char *
domap(char * name)18757c478bd9Sstevel@tonic-gate domap(char *name)
18767c478bd9Sstevel@tonic-gate {
18777c478bd9Sstevel@tonic-gate 	static char new[MAXPATHLEN];
18787c478bd9Sstevel@tonic-gate 	char *cp1 = name, *cp2 = mapin;
18797c478bd9Sstevel@tonic-gate 	char *tp[9], *te[9];
18807c478bd9Sstevel@tonic-gate 	int i, toks[9], toknum, match = 1;
18817c478bd9Sstevel@tonic-gate 	wchar_t	wc1, wc2;
18827c478bd9Sstevel@tonic-gate 	int	len1, len2;
18837c478bd9Sstevel@tonic-gate 
18847c478bd9Sstevel@tonic-gate 	for (i = 0; i < 9; ++i) {
18857c478bd9Sstevel@tonic-gate 		toks[i] = 0;
18867c478bd9Sstevel@tonic-gate 	}
18877c478bd9Sstevel@tonic-gate 	while (match && *cp1 && *cp2) {
18887c478bd9Sstevel@tonic-gate 		if ((len1 = mbtowc(&wc1, cp1, MB_CUR_MAX)) <= 0) {
18897c478bd9Sstevel@tonic-gate 			wc1 = (unsigned char)*cp1;
18907c478bd9Sstevel@tonic-gate 			len1 = 1;
18917c478bd9Sstevel@tonic-gate 		}
18927c478bd9Sstevel@tonic-gate 		cp1 += len1;
18937c478bd9Sstevel@tonic-gate 		if ((len2 = mbtowc(&wc2, cp2, MB_CUR_MAX)) <= 0) {
18947c478bd9Sstevel@tonic-gate 			wc2 = (unsigned char)*cp2;
18957c478bd9Sstevel@tonic-gate 			len2 = 1;
18967c478bd9Sstevel@tonic-gate 		}
18977c478bd9Sstevel@tonic-gate 		cp2 += len2;
18987c478bd9Sstevel@tonic-gate 
18997c478bd9Sstevel@tonic-gate 		switch (wc2) {
19007c478bd9Sstevel@tonic-gate 		case '\\':
19017c478bd9Sstevel@tonic-gate 			if ((len2 = mbtowc(&wc2, cp2, MB_CUR_MAX)) <= 0) {
19027c478bd9Sstevel@tonic-gate 				wc2 = (unsigned char)*cp2;
19037c478bd9Sstevel@tonic-gate 				len2 = 1;
19047c478bd9Sstevel@tonic-gate 			}
19057c478bd9Sstevel@tonic-gate 			cp2 += len2;
19067c478bd9Sstevel@tonic-gate 			if (wc2 != wc1)
19077c478bd9Sstevel@tonic-gate 				match = 0;
19087c478bd9Sstevel@tonic-gate 			break;
19097c478bd9Sstevel@tonic-gate 
19107c478bd9Sstevel@tonic-gate 		case '$':
19117c478bd9Sstevel@tonic-gate 			if (*cp2 >= '1' && *cp2 <= '9') {
19127c478bd9Sstevel@tonic-gate 				if ((len2 =
19137c478bd9Sstevel@tonic-gate 				    mbtowc(&wc2, cp2 + 1, MB_CUR_MAX)) <= 0) {
19147c478bd9Sstevel@tonic-gate 					wc2 = (unsigned char)*(cp2 + 1);
19157c478bd9Sstevel@tonic-gate 					len2 = 1;
19167c478bd9Sstevel@tonic-gate 				}
19177c478bd9Sstevel@tonic-gate 				if (wc1 != wc2) {
19187c478bd9Sstevel@tonic-gate 					toks[toknum = *cp2 - '1']++;
19197c478bd9Sstevel@tonic-gate 					tp[toknum] = cp1 - len1;
19207c478bd9Sstevel@tonic-gate 					while (*cp1) {
19217c478bd9Sstevel@tonic-gate 						if ((len1 = mbtowc(&wc1,
19227c478bd9Sstevel@tonic-gate 						    cp1, MB_CUR_MAX)) <= 0) {
19237c478bd9Sstevel@tonic-gate 							wc1 =
19247c478bd9Sstevel@tonic-gate 							    (unsigned char)*cp1;
19257c478bd9Sstevel@tonic-gate 							len1 = 1;
19267c478bd9Sstevel@tonic-gate 						}
19277c478bd9Sstevel@tonic-gate 						cp1 += len1;
19287c478bd9Sstevel@tonic-gate 						if (wc2 == wc1)
19297c478bd9Sstevel@tonic-gate 							break;
19307c478bd9Sstevel@tonic-gate 					}
19317c478bd9Sstevel@tonic-gate 					if (*cp1 == 0 && wc2 != wc1)
19327c478bd9Sstevel@tonic-gate 						te[toknum] = cp1;
19337c478bd9Sstevel@tonic-gate 					else
19347c478bd9Sstevel@tonic-gate 						te[toknum] = cp1 - len1;
19357c478bd9Sstevel@tonic-gate 				}
19367c478bd9Sstevel@tonic-gate 				cp2++;			/* Consume the digit */
19377c478bd9Sstevel@tonic-gate 				if (wc2)
19387c478bd9Sstevel@tonic-gate 					cp2 += len2;	/* Consume wide char */
19397c478bd9Sstevel@tonic-gate 				break;
19407c478bd9Sstevel@tonic-gate 			}
1941*c2b69decSToomas Soome 			/* FALLTHROUGH */
19427c478bd9Sstevel@tonic-gate 		default:
19437c478bd9Sstevel@tonic-gate 			if (wc2 != wc1)
19447c478bd9Sstevel@tonic-gate 				match = 0;
19457c478bd9Sstevel@tonic-gate 			break;
19467c478bd9Sstevel@tonic-gate 		}
19477c478bd9Sstevel@tonic-gate 	}
19487c478bd9Sstevel@tonic-gate 
19497c478bd9Sstevel@tonic-gate 	cp1 = new;
19507c478bd9Sstevel@tonic-gate 	*cp1 = '\0';
19517c478bd9Sstevel@tonic-gate 	cp2 = mapout;
19527c478bd9Sstevel@tonic-gate 	while (*cp2) {
19537c478bd9Sstevel@tonic-gate 		match = 0;
19547c478bd9Sstevel@tonic-gate 		switch (*cp2) {
19557c478bd9Sstevel@tonic-gate 		case '\\':
19567c478bd9Sstevel@tonic-gate 			cp2++;
19577c478bd9Sstevel@tonic-gate 			if (*cp2) {
19587c478bd9Sstevel@tonic-gate 				if ((len2 = mblen(cp2, MB_CUR_MAX)) <= 0)
19597c478bd9Sstevel@tonic-gate 					len2 = 1;
19607c478bd9Sstevel@tonic-gate 				memcpy(cp1, cp2, len2);
19617c478bd9Sstevel@tonic-gate 				cp1 += len2;
19627c478bd9Sstevel@tonic-gate 				cp2 += len2;
19637c478bd9Sstevel@tonic-gate 			}
19647c478bd9Sstevel@tonic-gate 			break;
19657c478bd9Sstevel@tonic-gate 
19667c478bd9Sstevel@tonic-gate 		case '[':
19677c478bd9Sstevel@tonic-gate LOOP:
19687c478bd9Sstevel@tonic-gate 			cp2++;
19697c478bd9Sstevel@tonic-gate 			if (*cp2 == '$' && isdigit(*(cp2+1))) {
19707c478bd9Sstevel@tonic-gate 				if (*++cp2 == '0') {
19717c478bd9Sstevel@tonic-gate 					char *cp3 = name;
19727c478bd9Sstevel@tonic-gate 
19737c478bd9Sstevel@tonic-gate 					while (*cp3) {
19747c478bd9Sstevel@tonic-gate 						*cp1++ = *cp3++;
19757c478bd9Sstevel@tonic-gate 					}
19767c478bd9Sstevel@tonic-gate 					match = 1;
19777c478bd9Sstevel@tonic-gate 				} else if (toks[toknum = *cp2 - '1']) {
19787c478bd9Sstevel@tonic-gate 					char *cp3 = tp[toknum];
19797c478bd9Sstevel@tonic-gate 
19807c478bd9Sstevel@tonic-gate 					while (cp3 != te[toknum]) {
19817c478bd9Sstevel@tonic-gate 						*cp1++ = *cp3++;
19827c478bd9Sstevel@tonic-gate 					}
19837c478bd9Sstevel@tonic-gate 					match = 1;
19847c478bd9Sstevel@tonic-gate 				}
19857c478bd9Sstevel@tonic-gate 			} else {
19867c478bd9Sstevel@tonic-gate 				while (*cp2 && *cp2 != ',' && *cp2 != ']') {
19877c478bd9Sstevel@tonic-gate 					if (*cp2 == '\\') {
19887c478bd9Sstevel@tonic-gate 						cp2++;
19897c478bd9Sstevel@tonic-gate 						continue;
19907c478bd9Sstevel@tonic-gate 					}
19917c478bd9Sstevel@tonic-gate 
19927c478bd9Sstevel@tonic-gate 					if (*cp2 == '$' && isdigit(*(cp2+1))) {
19937c478bd9Sstevel@tonic-gate 						if (*++cp2 == '0') {
19947c478bd9Sstevel@tonic-gate 							char *cp3 = name;
19957c478bd9Sstevel@tonic-gate 
19967c478bd9Sstevel@tonic-gate 							while (*cp3)
19977c478bd9Sstevel@tonic-gate 								*cp1++ = *cp3++;
19987c478bd9Sstevel@tonic-gate 							continue;
19997c478bd9Sstevel@tonic-gate 						}
20007c478bd9Sstevel@tonic-gate 						if (toks[toknum = *cp2 - '1']) {
20017c478bd9Sstevel@tonic-gate 							char *cp3 = tp[toknum];
20027c478bd9Sstevel@tonic-gate 
20037c478bd9Sstevel@tonic-gate 							while (cp3 !=
20047c478bd9Sstevel@tonic-gate 							    te[toknum])
20057c478bd9Sstevel@tonic-gate 								*cp1++ = *cp3++;
20067c478bd9Sstevel@tonic-gate 						}
20077c478bd9Sstevel@tonic-gate 						continue;
20087c478bd9Sstevel@tonic-gate 					}
20097c478bd9Sstevel@tonic-gate 					if (*cp2) {
20107c478bd9Sstevel@tonic-gate 						if ((len2 =
20117c478bd9Sstevel@tonic-gate 						    mblen(cp2, MB_CUR_MAX)) <=
20127c478bd9Sstevel@tonic-gate 						    0) {
20137c478bd9Sstevel@tonic-gate 							len2 = 1;
20147c478bd9Sstevel@tonic-gate 						}
20157c478bd9Sstevel@tonic-gate 						memcpy(cp1, cp2, len2);
20167c478bd9Sstevel@tonic-gate 						cp1 += len2;
20177c478bd9Sstevel@tonic-gate 						cp2 += len2;
20187c478bd9Sstevel@tonic-gate 					}
20197c478bd9Sstevel@tonic-gate 				}
20207c478bd9Sstevel@tonic-gate 				if (!*cp2) {
20217c478bd9Sstevel@tonic-gate 					(void) printf(
20227c478bd9Sstevel@tonic-gate 						"nmap: unbalanced brackets\n");
20237c478bd9Sstevel@tonic-gate 					return (name);
20247c478bd9Sstevel@tonic-gate 				}
20257c478bd9Sstevel@tonic-gate 				match = 1;
20267c478bd9Sstevel@tonic-gate 			}
20277c478bd9Sstevel@tonic-gate 			if (match) {
20287c478bd9Sstevel@tonic-gate 				while (*cp2 && *cp2 != ']') {
20297c478bd9Sstevel@tonic-gate 					if (*cp2 == '\\' && *(cp2 + 1)) {
20307c478bd9Sstevel@tonic-gate 						cp2++;
20317c478bd9Sstevel@tonic-gate 					}
20327c478bd9Sstevel@tonic-gate 					if ((len2 = mblen(cp2, MB_CUR_MAX)) <=
20337c478bd9Sstevel@tonic-gate 					    0)
20347c478bd9Sstevel@tonic-gate 						len2 = 1;
20357c478bd9Sstevel@tonic-gate 					cp2 += len2;
20367c478bd9Sstevel@tonic-gate 				}
20377c478bd9Sstevel@tonic-gate 				if (!*cp2) {
20387c478bd9Sstevel@tonic-gate 					(void) printf(
20397c478bd9Sstevel@tonic-gate 						"nmap: unbalanced brackets\n");
20407c478bd9Sstevel@tonic-gate 					return (name);
20417c478bd9Sstevel@tonic-gate 				}
20427c478bd9Sstevel@tonic-gate 				cp2++;
20437c478bd9Sstevel@tonic-gate 				break;
20447c478bd9Sstevel@tonic-gate 			}
20457c478bd9Sstevel@tonic-gate 			switch (*++cp2) {
20467c478bd9Sstevel@tonic-gate 				case ',':
20477c478bd9Sstevel@tonic-gate 					goto LOOP;
20487c478bd9Sstevel@tonic-gate 				case ']':
20497c478bd9Sstevel@tonic-gate 					break;
20507c478bd9Sstevel@tonic-gate 				default:
20517c478bd9Sstevel@tonic-gate 					cp2--;
20527c478bd9Sstevel@tonic-gate 					goto LOOP;
20537c478bd9Sstevel@tonic-gate 			}
20547c478bd9Sstevel@tonic-gate 			cp2++;
20557c478bd9Sstevel@tonic-gate 			break;
20567c478bd9Sstevel@tonic-gate 		case '$':
20577c478bd9Sstevel@tonic-gate 			if (isdigit(*(cp2 + 1))) {
20587c478bd9Sstevel@tonic-gate 				if (*++cp2 == '0') {
20597c478bd9Sstevel@tonic-gate 					char *cp3 = name;
20607c478bd9Sstevel@tonic-gate 
20617c478bd9Sstevel@tonic-gate 					while (*cp3) {
20627c478bd9Sstevel@tonic-gate 						*cp1++ = *cp3++;
20637c478bd9Sstevel@tonic-gate 					}
20647c478bd9Sstevel@tonic-gate 				} else if (toks[toknum = *cp2 - '1']) {
20657c478bd9Sstevel@tonic-gate 					char *cp3 = tp[toknum];
20667c478bd9Sstevel@tonic-gate 
20677c478bd9Sstevel@tonic-gate 					while (cp3 != te[toknum]) {
20687c478bd9Sstevel@tonic-gate 						*cp1++ = *cp3++;
20697c478bd9Sstevel@tonic-gate 					}
20707c478bd9Sstevel@tonic-gate 				}
20717c478bd9Sstevel@tonic-gate 				cp2++;
20727c478bd9Sstevel@tonic-gate 				break;
20737c478bd9Sstevel@tonic-gate 			}
2074*c2b69decSToomas Soome 			/* FALLTHROUGH */
20757c478bd9Sstevel@tonic-gate 		default:
20767c478bd9Sstevel@tonic-gate 			if ((len2 = mblen(cp2, MB_CUR_MAX)) <= 0)
20777c478bd9Sstevel@tonic-gate 				len2 = 1;
20787c478bd9Sstevel@tonic-gate 			memcpy(cp1, cp2, len2);
20797c478bd9Sstevel@tonic-gate 			cp1 += len2;
20807c478bd9Sstevel@tonic-gate 			cp2 += len2;
20817c478bd9Sstevel@tonic-gate 			break;
20827c478bd9Sstevel@tonic-gate 		}
20837c478bd9Sstevel@tonic-gate 	}
20847c478bd9Sstevel@tonic-gate 	*cp1 = '\0';
20857c478bd9Sstevel@tonic-gate 	if (!*new) {
20867c478bd9Sstevel@tonic-gate 		return (name);
20877c478bd9Sstevel@tonic-gate 	}
20887c478bd9Sstevel@tonic-gate 	return (new);
20897c478bd9Sstevel@tonic-gate }
20907c478bd9Sstevel@tonic-gate 
20917c478bd9Sstevel@tonic-gate /*ARGSUSED*/
20927c478bd9Sstevel@tonic-gate void
setsunique(int argc,char * argv[])20937c478bd9Sstevel@tonic-gate setsunique(int argc, char *argv[])
20947c478bd9Sstevel@tonic-gate {
20957c478bd9Sstevel@tonic-gate 	sunique = !sunique;
20967c478bd9Sstevel@tonic-gate 	(void) printf("Store unique %s.\n", onoff(sunique));
20977c478bd9Sstevel@tonic-gate 	code = sunique;
20987c478bd9Sstevel@tonic-gate }
20997c478bd9Sstevel@tonic-gate 
21007c478bd9Sstevel@tonic-gate /*ARGSUSED*/
21017c478bd9Sstevel@tonic-gate void
setrunique(int argc,char * argv[])21027c478bd9Sstevel@tonic-gate setrunique(int argc, char *argv[])
21037c478bd9Sstevel@tonic-gate {
21047c478bd9Sstevel@tonic-gate 	runique = !runique;
21057c478bd9Sstevel@tonic-gate 	(void) printf("Receive unique %s.\n", onoff(runique));
21067c478bd9Sstevel@tonic-gate 	code = runique;
21077c478bd9Sstevel@tonic-gate }
21087c478bd9Sstevel@tonic-gate 
21097c478bd9Sstevel@tonic-gate /*ARGSUSED*/
21107c478bd9Sstevel@tonic-gate void
setpassive(int argc,char * argv[])21117c478bd9Sstevel@tonic-gate setpassive(int argc, char *argv[])
21127c478bd9Sstevel@tonic-gate {
21137c478bd9Sstevel@tonic-gate 	passivemode = !passivemode;
21147c478bd9Sstevel@tonic-gate 	(void) printf("Passive mode %s.\n", onoff(passivemode));
21157c478bd9Sstevel@tonic-gate 	code = passivemode;
21167c478bd9Sstevel@tonic-gate }
21177c478bd9Sstevel@tonic-gate 
21187c478bd9Sstevel@tonic-gate void
settcpwindow(int argc,char * argv[])21197c478bd9Sstevel@tonic-gate settcpwindow(int argc, char *argv[])
21207c478bd9Sstevel@tonic-gate {
21217c478bd9Sstevel@tonic-gate 	int owindowsize = tcpwindowsize;
21227c478bd9Sstevel@tonic-gate 
21237c478bd9Sstevel@tonic-gate 	if (argc > 2) {
21247c478bd9Sstevel@tonic-gate 		(void) printf("usage: %s [size]\n", argv[0]);
21257c478bd9Sstevel@tonic-gate 		code = -1;
21267c478bd9Sstevel@tonic-gate 		return;
21277c478bd9Sstevel@tonic-gate 	}
21287c478bd9Sstevel@tonic-gate 	if (argc == 2) {
21297c478bd9Sstevel@tonic-gate 		int window;
21307c478bd9Sstevel@tonic-gate 		char *endp;
21317c478bd9Sstevel@tonic-gate 
21327c478bd9Sstevel@tonic-gate 		errno = 0;
21337c478bd9Sstevel@tonic-gate 		window = (int)strtol(argv[1], &endp, 10);
21347c478bd9Sstevel@tonic-gate 		if (errno || window < 0 || *endp != '\0')
21357c478bd9Sstevel@tonic-gate 			(void) printf("%s: Invalid size `%s'\n",
21367c478bd9Sstevel@tonic-gate 				argv[0], argv[1]);
21377c478bd9Sstevel@tonic-gate 		else
21387c478bd9Sstevel@tonic-gate 			tcpwindowsize = window;
21397c478bd9Sstevel@tonic-gate 	}
21407c478bd9Sstevel@tonic-gate 	if (tcpwindowsize == 0) {
21417c478bd9Sstevel@tonic-gate 		if (owindowsize == 0)
21427c478bd9Sstevel@tonic-gate 			(void) printf("No TCP window size defined\n");
21437c478bd9Sstevel@tonic-gate 		else
21447c478bd9Sstevel@tonic-gate 			(void) printf("TCP window size cleared\n");
21457c478bd9Sstevel@tonic-gate 	} else
21467c478bd9Sstevel@tonic-gate 		(void) printf("TCP window size is set to %d\n", tcpwindowsize);
21477c478bd9Sstevel@tonic-gate }
21487c478bd9Sstevel@tonic-gate 
21497c478bd9Sstevel@tonic-gate /* change directory to parent directory */
21507c478bd9Sstevel@tonic-gate /*ARGSUSED*/
21517c478bd9Sstevel@tonic-gate void
cdup(int argc,char * argv[])21527c478bd9Sstevel@tonic-gate cdup(int argc, char *argv[])
21537c478bd9Sstevel@tonic-gate {
21547c478bd9Sstevel@tonic-gate 	(void) command("CDUP");
21557c478bd9Sstevel@tonic-gate }
21567c478bd9Sstevel@tonic-gate 
21577c478bd9Sstevel@tonic-gate void
macdef(int argc,char * argv[])21587c478bd9Sstevel@tonic-gate macdef(int argc, char *argv[])
21597c478bd9Sstevel@tonic-gate {
21607c478bd9Sstevel@tonic-gate 	char *tmp;
21617c478bd9Sstevel@tonic-gate 	int c;
21627c478bd9Sstevel@tonic-gate 
21637c478bd9Sstevel@tonic-gate 	if (macnum == 16) {
21647c478bd9Sstevel@tonic-gate 		(void) printf("Limit of 16 macros have already been defined\n");
21657c478bd9Sstevel@tonic-gate 		code = -1;
21667c478bd9Sstevel@tonic-gate 		return;
21677c478bd9Sstevel@tonic-gate 	}
21687c478bd9Sstevel@tonic-gate 	if (argc < 2) {
21697c478bd9Sstevel@tonic-gate 		if (prompt_for_arg(line, sizeof (line), "macro name") == -1) {
21707c478bd9Sstevel@tonic-gate 			code = -1;
21717c478bd9Sstevel@tonic-gate 			return;
21727c478bd9Sstevel@tonic-gate 		}
21737c478bd9Sstevel@tonic-gate 		makeargv();
21747c478bd9Sstevel@tonic-gate 		argc = margc;
21757c478bd9Sstevel@tonic-gate 		argv = margv;
21767c478bd9Sstevel@tonic-gate 	}
21777c478bd9Sstevel@tonic-gate 	if (argc != 2) {
21787c478bd9Sstevel@tonic-gate 		(void) printf("Usage: %s macro_name\n", argv[0]);
21797c478bd9Sstevel@tonic-gate 		code = -1;
21807c478bd9Sstevel@tonic-gate 		return;
21817c478bd9Sstevel@tonic-gate 	}
21827c478bd9Sstevel@tonic-gate 	if (interactive) {
21837c478bd9Sstevel@tonic-gate 		(void) printf("Enter macro line by line, terminating "
21847c478bd9Sstevel@tonic-gate 		    "it with a null line\n");
21857c478bd9Sstevel@tonic-gate 	}
21867c478bd9Sstevel@tonic-gate 	(void) strncpy(macros[macnum].mac_name, argv[1], 8);
21877c478bd9Sstevel@tonic-gate 	if (macnum == 0) {
21887c478bd9Sstevel@tonic-gate 		macros[macnum].mac_start = macbuf;
21897c478bd9Sstevel@tonic-gate 	} else {
21907c478bd9Sstevel@tonic-gate 		macros[macnum].mac_start = macros[macnum - 1].mac_end + 1;
21917c478bd9Sstevel@tonic-gate 	}
21927c478bd9Sstevel@tonic-gate 	tmp = macros[macnum].mac_start;
21937c478bd9Sstevel@tonic-gate 	while (tmp != macbuf+4096) {
21947c478bd9Sstevel@tonic-gate 		if ((c = getchar()) == EOF) {
21957c478bd9Sstevel@tonic-gate 			(void) printf("macdef:end of file encountered\n");
21967c478bd9Sstevel@tonic-gate 			code = -1;
21977c478bd9Sstevel@tonic-gate 			return;
21987c478bd9Sstevel@tonic-gate 		}
21997c478bd9Sstevel@tonic-gate 		if ((*tmp = c) == '\n') {
22007c478bd9Sstevel@tonic-gate 			if (tmp == macros[macnum].mac_start) {
22017c478bd9Sstevel@tonic-gate 				macros[macnum++].mac_end = tmp;
22027c478bd9Sstevel@tonic-gate 				code = 0;
22037c478bd9Sstevel@tonic-gate 				return;
22047c478bd9Sstevel@tonic-gate 			}
22057c478bd9Sstevel@tonic-gate 			if (*(tmp-1) == '\0') {
22067c478bd9Sstevel@tonic-gate 				macros[macnum++].mac_end = tmp - 1;
22077c478bd9Sstevel@tonic-gate 				code = 0;
22087c478bd9Sstevel@tonic-gate 				return;
22097c478bd9Sstevel@tonic-gate 			}
22107c478bd9Sstevel@tonic-gate 			*tmp = '\0';
22117c478bd9Sstevel@tonic-gate 		}
22127c478bd9Sstevel@tonic-gate 		tmp++;
22137c478bd9Sstevel@tonic-gate 	}
22147c478bd9Sstevel@tonic-gate 	for (;;) {
22157c478bd9Sstevel@tonic-gate 		while ((c = getchar()) != '\n' && c != EOF)
22167c478bd9Sstevel@tonic-gate 			/* NULL */;
22177c478bd9Sstevel@tonic-gate 		if (c == EOF || getchar() == '\n') {
22187c478bd9Sstevel@tonic-gate 			(void) printf(
22197c478bd9Sstevel@tonic-gate 				"Macro not defined - 4k buffer exceeded\n");
22207c478bd9Sstevel@tonic-gate 			code = -1;
22217c478bd9Sstevel@tonic-gate 			return;
22227c478bd9Sstevel@tonic-gate 		}
22237c478bd9Sstevel@tonic-gate 	}
22247c478bd9Sstevel@tonic-gate }
22257c478bd9Sstevel@tonic-gate 
22267c478bd9Sstevel@tonic-gate /*
22277c478bd9Sstevel@tonic-gate  * The p_name strings are for the getlevel and setlevel commands.
22287c478bd9Sstevel@tonic-gate  * The name strings for printing are in the arpa/ftp.h file in the
22297c478bd9Sstevel@tonic-gate  * protnames[] array of strings.
22307c478bd9Sstevel@tonic-gate  */
22317c478bd9Sstevel@tonic-gate static	struct	levels {
22327c478bd9Sstevel@tonic-gate 	char	*p_name;
22337c478bd9Sstevel@tonic-gate 	char	*p_mode;
22347c478bd9Sstevel@tonic-gate 	int	p_level;
22357c478bd9Sstevel@tonic-gate } levels[] = {
22367c478bd9Sstevel@tonic-gate 	{ "clear",	"C",	PROT_C },
22377c478bd9Sstevel@tonic-gate 	{ "safe",	"S",	PROT_S },
22387c478bd9Sstevel@tonic-gate 	{ "private",	"P",	PROT_P },
22397c478bd9Sstevel@tonic-gate 	NULL
22407c478bd9Sstevel@tonic-gate };
22417c478bd9Sstevel@tonic-gate 
22427c478bd9Sstevel@tonic-gate /*
22437c478bd9Sstevel@tonic-gate  * Return a pointer to a string which is the readable version of the
22447c478bd9Sstevel@tonic-gate  * protection level, or NULL if the input level is not found.
22457c478bd9Sstevel@tonic-gate  */
22467c478bd9Sstevel@tonic-gate static char *
getlevel(int level)22477c478bd9Sstevel@tonic-gate getlevel(int level)
22487c478bd9Sstevel@tonic-gate {
22497c478bd9Sstevel@tonic-gate 	struct levels *p;
22507c478bd9Sstevel@tonic-gate 
22517c478bd9Sstevel@tonic-gate 	for (p = levels; (p != NULL) && (p->p_level != level); p++)
22527c478bd9Sstevel@tonic-gate 		;
22537c478bd9Sstevel@tonic-gate 	return (p ? p->p_name : NULL);
22547c478bd9Sstevel@tonic-gate }
22557c478bd9Sstevel@tonic-gate 
22567c478bd9Sstevel@tonic-gate static char *plevel[] = {
22577c478bd9Sstevel@tonic-gate 	"protect",
22587c478bd9Sstevel@tonic-gate 	"",
22597c478bd9Sstevel@tonic-gate 	NULL
22607c478bd9Sstevel@tonic-gate };
22617c478bd9Sstevel@tonic-gate 
22627c478bd9Sstevel@tonic-gate /*
22637c478bd9Sstevel@tonic-gate  * Set control channel protection level.
22647c478bd9Sstevel@tonic-gate  */
22657c478bd9Sstevel@tonic-gate void
setclevel(int argc,char * argv[])22667c478bd9Sstevel@tonic-gate setclevel(int argc, char *argv[])
22677c478bd9Sstevel@tonic-gate {
22687c478bd9Sstevel@tonic-gate 	struct levels *p;
22697c478bd9Sstevel@tonic-gate 	char *levelp;
22707c478bd9Sstevel@tonic-gate 	int comret;
22717c478bd9Sstevel@tonic-gate 
22727c478bd9Sstevel@tonic-gate 	if (argc > 2) {
22737c478bd9Sstevel@tonic-gate 		char *sep;
22747c478bd9Sstevel@tonic-gate 
22757c478bd9Sstevel@tonic-gate 		(void) printf("usage: %s [", argv[0]);
22767c478bd9Sstevel@tonic-gate 		sep = " ";
22777c478bd9Sstevel@tonic-gate 		for (p = levels; p->p_name; p++) {
22787c478bd9Sstevel@tonic-gate 			(void) printf("%s%s", sep, p->p_name);
22797c478bd9Sstevel@tonic-gate 			if (*sep == ' ')
22807c478bd9Sstevel@tonic-gate 				sep = " | ";
22817c478bd9Sstevel@tonic-gate 		}
22827c478bd9Sstevel@tonic-gate 		(void) printf(" ]\n");
22837c478bd9Sstevel@tonic-gate 		code = -1;
22847c478bd9Sstevel@tonic-gate 		return;
22857c478bd9Sstevel@tonic-gate 	}
22867c478bd9Sstevel@tonic-gate 	if (argc < 2) {
22877c478bd9Sstevel@tonic-gate 		levelp = getlevel(clevel);
22887c478bd9Sstevel@tonic-gate 		(void) printf("Using %s protection level for commands.\n",
22897c478bd9Sstevel@tonic-gate 			levelp ? levelp : "<unknown>");
22907c478bd9Sstevel@tonic-gate 		code = 0;
22917c478bd9Sstevel@tonic-gate 		return;
22927c478bd9Sstevel@tonic-gate 	}
22937c478bd9Sstevel@tonic-gate 	for (p = levels; (p != NULL) && (p->p_name); p++)
22947c478bd9Sstevel@tonic-gate 		if (strcmp(argv[1], p->p_name) == 0)
22957c478bd9Sstevel@tonic-gate 			break;
22967c478bd9Sstevel@tonic-gate 	if (p->p_name == 0) {
22977c478bd9Sstevel@tonic-gate 		(void) printf("%s: unknown protection level\n", argv[1]);
22987c478bd9Sstevel@tonic-gate 		code = -1;
22997c478bd9Sstevel@tonic-gate 		return;
23007c478bd9Sstevel@tonic-gate 	}
23017c478bd9Sstevel@tonic-gate 	if (auth_type == AUTHTYPE_NONE) {
23027c478bd9Sstevel@tonic-gate 		if (strcmp(p->p_name, "clear"))
23037c478bd9Sstevel@tonic-gate 			(void) printf("Cannot set protection level to %s\n",
23047c478bd9Sstevel@tonic-gate 				argv[1]);
23057c478bd9Sstevel@tonic-gate 		return;
23067c478bd9Sstevel@tonic-gate 	}
23077c478bd9Sstevel@tonic-gate 	if (strcmp(p->p_name, "clear") == 0) {
23087c478bd9Sstevel@tonic-gate 		comret = command("CCC");
23097c478bd9Sstevel@tonic-gate 		if (comret == COMPLETE)
23107c478bd9Sstevel@tonic-gate 			clevel = PROT_C;
23117c478bd9Sstevel@tonic-gate 		return;
23127c478bd9Sstevel@tonic-gate 	}
23137c478bd9Sstevel@tonic-gate 	clevel = p->p_level;
23147c478bd9Sstevel@tonic-gate 	(void) printf("Control channel protection level set to %s.\n",
23157c478bd9Sstevel@tonic-gate 		p->p_name);
23167c478bd9Sstevel@tonic-gate }
23177c478bd9Sstevel@tonic-gate 
23187c478bd9Sstevel@tonic-gate /*
23197c478bd9Sstevel@tonic-gate  * Set data channel protection level.
23207c478bd9Sstevel@tonic-gate  */
23217c478bd9Sstevel@tonic-gate void
setdlevel(int argc,char * argv[])23227c478bd9Sstevel@tonic-gate setdlevel(int argc, char *argv[])
23237c478bd9Sstevel@tonic-gate {
23247c478bd9Sstevel@tonic-gate 	struct levels *p;
23257c478bd9Sstevel@tonic-gate 	int comret;
23267c478bd9Sstevel@tonic-gate 
23277c478bd9Sstevel@tonic-gate 	if (argc != 2) {
23287c478bd9Sstevel@tonic-gate 		char *sep;
23297c478bd9Sstevel@tonic-gate 
23307c478bd9Sstevel@tonic-gate 		(void) printf("usage: %s [", argv[0]);
23317c478bd9Sstevel@tonic-gate 		sep = " ";
23327c478bd9Sstevel@tonic-gate 		for (p = levels; p->p_name; p++) {
23337c478bd9Sstevel@tonic-gate 			(void) printf("%s%s", sep, p->p_name);
23347c478bd9Sstevel@tonic-gate 			if (*sep == ' ')
23357c478bd9Sstevel@tonic-gate 				sep = " | ";
23367c478bd9Sstevel@tonic-gate 		}
23377c478bd9Sstevel@tonic-gate 		(void) printf(" ]\n");
23387c478bd9Sstevel@tonic-gate 		code = -1;
23397c478bd9Sstevel@tonic-gate 		return;
23407c478bd9Sstevel@tonic-gate 	}
23417c478bd9Sstevel@tonic-gate 	for (p = levels; p->p_name; p++)
23427c478bd9Sstevel@tonic-gate 		if (strcmp(argv[1], p->p_name) == 0)
23437c478bd9Sstevel@tonic-gate 			break;
23447c478bd9Sstevel@tonic-gate 	if (p->p_name == 0) {
23457c478bd9Sstevel@tonic-gate 		(void) printf("%s: unknown protection level\n", argv[1]);
23467c478bd9Sstevel@tonic-gate 		code = -1;
23477c478bd9Sstevel@tonic-gate 		return;
23487c478bd9Sstevel@tonic-gate 	}
23497c478bd9Sstevel@tonic-gate 	if (auth_type == AUTHTYPE_NONE) {
23507c478bd9Sstevel@tonic-gate 		if (strcmp(p->p_name, "clear"))
23517c478bd9Sstevel@tonic-gate 			(void) printf("Cannot set protection level to %s\n",
23527c478bd9Sstevel@tonic-gate 				argv[1]);
23537c478bd9Sstevel@tonic-gate 		return;
23547c478bd9Sstevel@tonic-gate 	}
23557c478bd9Sstevel@tonic-gate 	/* Start with a PBSZ of 1 meg */
23567c478bd9Sstevel@tonic-gate 	if (p->p_level != PROT_C)
23577c478bd9Sstevel@tonic-gate 		setpbsz(1<<20);
23587c478bd9Sstevel@tonic-gate 	comret = command("PROT %s", p->p_mode);
23597c478bd9Sstevel@tonic-gate 	if (comret == COMPLETE)
23607c478bd9Sstevel@tonic-gate 		dlevel = p->p_level;
23617c478bd9Sstevel@tonic-gate }
23627c478bd9Sstevel@tonic-gate 
23637c478bd9Sstevel@tonic-gate /*
23647c478bd9Sstevel@tonic-gate  * Set clear command protection level.
23657c478bd9Sstevel@tonic-gate  */
23667c478bd9Sstevel@tonic-gate /* VARARGS */
23677c478bd9Sstevel@tonic-gate void
ccc(int argc,char * argv[])23687c478bd9Sstevel@tonic-gate ccc(int argc, char *argv[])
23697c478bd9Sstevel@tonic-gate {
23707c478bd9Sstevel@tonic-gate 	plevel[1] = "clear";
23717c478bd9Sstevel@tonic-gate 	setclevel(2, plevel);
23727c478bd9Sstevel@tonic-gate }
23737c478bd9Sstevel@tonic-gate 
23747c478bd9Sstevel@tonic-gate /*
23757c478bd9Sstevel@tonic-gate  * Set clear data protection level.
23767c478bd9Sstevel@tonic-gate  */
23777c478bd9Sstevel@tonic-gate /* VARARGS */
23787c478bd9Sstevel@tonic-gate void
setclear(int argc,char * argv[])23797c478bd9Sstevel@tonic-gate setclear(int argc, char *argv[])
23807c478bd9Sstevel@tonic-gate {
23817c478bd9Sstevel@tonic-gate 	plevel[1] = "clear";
23827c478bd9Sstevel@tonic-gate 	setdlevel(2, plevel);
23837c478bd9Sstevel@tonic-gate }
23847c478bd9Sstevel@tonic-gate 
23857c478bd9Sstevel@tonic-gate /*
23867c478bd9Sstevel@tonic-gate  * Set safe data protection level.
23877c478bd9Sstevel@tonic-gate  */
23887c478bd9Sstevel@tonic-gate /* VARARGS */
23897c478bd9Sstevel@tonic-gate void
setsafe(int argc,char * argv[])23907c478bd9Sstevel@tonic-gate setsafe(int argc, char *argv[])
23917c478bd9Sstevel@tonic-gate {
23927c478bd9Sstevel@tonic-gate 	plevel[1] = "safe";
23937c478bd9Sstevel@tonic-gate 	setdlevel(2, plevel);
23947c478bd9Sstevel@tonic-gate }
23957c478bd9Sstevel@tonic-gate 
23967c478bd9Sstevel@tonic-gate /*
23977c478bd9Sstevel@tonic-gate  * Set private data protection level.
23987c478bd9Sstevel@tonic-gate  */
23997c478bd9Sstevel@tonic-gate /* VARARGS */
24007c478bd9Sstevel@tonic-gate void
setprivate(int argc,char * argv[])24017c478bd9Sstevel@tonic-gate setprivate(int argc, char *argv[])
24027c478bd9Sstevel@tonic-gate {
24037c478bd9Sstevel@tonic-gate 	plevel[1] = "private";
24047c478bd9Sstevel@tonic-gate 	setdlevel(2, plevel);
24057c478bd9Sstevel@tonic-gate }
24067c478bd9Sstevel@tonic-gate 
24077c478bd9Sstevel@tonic-gate /*
24087c478bd9Sstevel@tonic-gate  * Set mechanism type
24097c478bd9Sstevel@tonic-gate  */
24107c478bd9Sstevel@tonic-gate void
setmech(int argc,char * argv[])24117c478bd9Sstevel@tonic-gate setmech(int  argc, char *argv[])
24127c478bd9Sstevel@tonic-gate {
24137c478bd9Sstevel@tonic-gate 	char	tempmech[MECH_SZ];
24147c478bd9Sstevel@tonic-gate 
24157c478bd9Sstevel@tonic-gate 	if (argc < 2) {
24167c478bd9Sstevel@tonic-gate 		if (prompt_for_arg(line, sizeof (line), "mech-type") == -1) {
24177c478bd9Sstevel@tonic-gate 			code = -1;
24187c478bd9Sstevel@tonic-gate 			return;
24197c478bd9Sstevel@tonic-gate 		}
24207c478bd9Sstevel@tonic-gate 		makeargv();
24217c478bd9Sstevel@tonic-gate 		argc = margc;
24227c478bd9Sstevel@tonic-gate 		argv = margv;
24237c478bd9Sstevel@tonic-gate 	}
24247c478bd9Sstevel@tonic-gate 
24257c478bd9Sstevel@tonic-gate 	if (argc != 2) {
24267c478bd9Sstevel@tonic-gate 		(void) printf("usage: %s [ mechanism type ]\n", argv[0]);
24277c478bd9Sstevel@tonic-gate 		code = -1;
24287c478bd9Sstevel@tonic-gate 		return;
24297c478bd9Sstevel@tonic-gate 	}
24307c478bd9Sstevel@tonic-gate 
24317c478bd9Sstevel@tonic-gate 	if ((strlcpy(tempmech, argv[1], MECH_SZ) >= MECH_SZ) ||
24327c478bd9Sstevel@tonic-gate 		__gss_mech_to_oid(tempmech, (gss_OID*)&mechoid) !=
24337c478bd9Sstevel@tonic-gate 			GSS_S_COMPLETE) {
24347c478bd9Sstevel@tonic-gate 		(void) printf("%s: %s: not a valid security mechanism\n",
24357c478bd9Sstevel@tonic-gate 			argv[0], tempmech);
24367c478bd9Sstevel@tonic-gate 		code = -1;
24377c478bd9Sstevel@tonic-gate 		return;
24387c478bd9Sstevel@tonic-gate 	} else {
24397c478bd9Sstevel@tonic-gate 		(void) strlcpy(mechstr, tempmech, MECH_SZ);
24407c478bd9Sstevel@tonic-gate 		(void) printf("Using %s mechanism type\n", mechstr);
24417c478bd9Sstevel@tonic-gate 		code = 0;
24427c478bd9Sstevel@tonic-gate 		return;
24437c478bd9Sstevel@tonic-gate 	}
24447c478bd9Sstevel@tonic-gate }
2445