xref: /titanic_52/usr/src/cmd/sh/main.c (revision db397771158a0b9b33b5ab2dee8593e03ee5e994)
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
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
23965005c8Schin  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
287c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate /*
357c478bd9Sstevel@tonic-gate  * UNIX shell
367c478bd9Sstevel@tonic-gate  */
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate #include	"defs.h"
397c478bd9Sstevel@tonic-gate #include	"sym.h"
407c478bd9Sstevel@tonic-gate #include	"timeout.h"
41965005c8Schin #include	<stdio.h>
427c478bd9Sstevel@tonic-gate #include	<sys/types.h>
437c478bd9Sstevel@tonic-gate #include	<sys/stat.h>
447c478bd9Sstevel@tonic-gate #include	<sys/wait.h>
457c478bd9Sstevel@tonic-gate #include	"dup.h"
467c478bd9Sstevel@tonic-gate #include	"sh_policy.h"
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate #ifdef RES
497c478bd9Sstevel@tonic-gate #include	<sgtty.h>
507c478bd9Sstevel@tonic-gate #endif
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate pid_t mypid, mypgid, mysid;
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate static BOOL	beenhere = FALSE;
55965005c8Schin unsigned char	tmpout[TMPOUTSZ];
567c478bd9Sstevel@tonic-gate struct fileblk	stdfile;
577c478bd9Sstevel@tonic-gate struct fileblk *standin = &stdfile;
587c478bd9Sstevel@tonic-gate int mailchk = 0;
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate static unsigned char	*mailp;
617c478bd9Sstevel@tonic-gate static long	*mod_time = 0;
627c478bd9Sstevel@tonic-gate static BOOL login_shell = FALSE;
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate #if vax
657c478bd9Sstevel@tonic-gate char **execargs = (char **)(0x7ffffffc);
667c478bd9Sstevel@tonic-gate #endif
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate #if pdp11
697c478bd9Sstevel@tonic-gate char **execargs = (char **)(-2);
707c478bd9Sstevel@tonic-gate #endif
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate 
73965005c8Schin static void	exfile();
747c478bd9Sstevel@tonic-gate extern unsigned char 	*simple();
75965005c8Schin static void Ldup(int, int);
76965005c8Schin void settmp(void);
77965005c8Schin void chkmail(void);
78965005c8Schin void setmail(unsigned char *);
797c478bd9Sstevel@tonic-gate 
80965005c8Schin int
81965005c8Schin main(int c, char *v[], char *e[])
827c478bd9Sstevel@tonic-gate {
83965005c8Schin 	int		rflag = ttyflg;
847c478bd9Sstevel@tonic-gate 	int		rsflag = 1;	/* local restricted flag */
85965005c8Schin 	unsigned char	*flagc = flagadr;
867c478bd9Sstevel@tonic-gate 	struct namnod	*n;
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate 	mypid = getpid();
897c478bd9Sstevel@tonic-gate 	mypgid = getpgid(mypid);
907c478bd9Sstevel@tonic-gate 	mysid = getsid(mypid);
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate 	/*
937c478bd9Sstevel@tonic-gate 	 * Do locale processing only if /usr is mounted.
947c478bd9Sstevel@tonic-gate 	 */
957c478bd9Sstevel@tonic-gate 	localedir_exists = (access(localedir, F_OK) == 0);
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate 	/*
987c478bd9Sstevel@tonic-gate 	 * initialize storage allocation
997c478bd9Sstevel@tonic-gate 	 */
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate 	if (stakbot == 0) {
1027c478bd9Sstevel@tonic-gate 	addblok((unsigned)0);
1037c478bd9Sstevel@tonic-gate 	}
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate 	/*
1067c478bd9Sstevel@tonic-gate 	 * If the first character of the last path element of v[0] is "-"
1077c478bd9Sstevel@tonic-gate 	 * (ex. -sh, or /bin/-sh), this is a login shell
1087c478bd9Sstevel@tonic-gate 	 */
1097c478bd9Sstevel@tonic-gate 	if (*simple(v[0]) == '-') {
1107c478bd9Sstevel@tonic-gate 		signal(SIGXCPU, SIG_DFL);
1117c478bd9Sstevel@tonic-gate 		signal(SIGXFSZ, SIG_DFL);
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate 		/*
1147c478bd9Sstevel@tonic-gate 		 * As the previous comment states, this is a login shell.
1157c478bd9Sstevel@tonic-gate 		 * Therefore, we set the login_shell flag to explicitly
1167c478bd9Sstevel@tonic-gate 		 * indicate this condition.
1177c478bd9Sstevel@tonic-gate 		 */
1187c478bd9Sstevel@tonic-gate 		login_shell = TRUE;
1197c478bd9Sstevel@tonic-gate 	}
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate 	stdsigs();
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate 	/*
1247c478bd9Sstevel@tonic-gate 	 * set names from userenv
1257c478bd9Sstevel@tonic-gate 	 */
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate 	setup_env();
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate 	/*
1307c478bd9Sstevel@tonic-gate 	 * LC_MESSAGES is set here so that early error messages will
1317c478bd9Sstevel@tonic-gate 	 * come out in the right style.
1327c478bd9Sstevel@tonic-gate 	 * Note that LC_CTYPE is done later on and is *not*
1337c478bd9Sstevel@tonic-gate 	 * taken from the previous environ
1347c478bd9Sstevel@tonic-gate 	 */
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate 	/*
1377c478bd9Sstevel@tonic-gate 	 * Do locale processing only if /usr is mounted.
1387c478bd9Sstevel@tonic-gate 	 */
1397c478bd9Sstevel@tonic-gate 	if (localedir_exists)
1407c478bd9Sstevel@tonic-gate 		(void) setlocale(LC_ALL, "");
1417c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
1427c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"	/* Use this only if it weren't */
1437c478bd9Sstevel@tonic-gate #endif
1447c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate 	/*
1477c478bd9Sstevel@tonic-gate 	 * This is a profile shell if the simple name of argv[0] is
1487c478bd9Sstevel@tonic-gate 	 * pfsh or -pfsh
1497c478bd9Sstevel@tonic-gate 	 */
1507c478bd9Sstevel@tonic-gate 	if (c > 0 && (eq("pfsh", simple(*v)) || eq("-pfsh", simple(*v)))) {
1517c478bd9Sstevel@tonic-gate 		flags |= pfshflg;
1527c478bd9Sstevel@tonic-gate 		secpolicy_init();
1537c478bd9Sstevel@tonic-gate 	}
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate 	/*
1567c478bd9Sstevel@tonic-gate 	 * 'rsflag' is zero if SHELL variable is
1577c478bd9Sstevel@tonic-gate 	 *  set in environment and
1587c478bd9Sstevel@tonic-gate 	 *  the simple file part of the value.
1597c478bd9Sstevel@tonic-gate 	 *  is rsh
1607c478bd9Sstevel@tonic-gate 	 */
161*db397771Schin 	if (n = findnam("SHELL")) {
1627c478bd9Sstevel@tonic-gate 		if (eq("rsh", simple(n->namval)))
1637c478bd9Sstevel@tonic-gate 			rsflag = 0;
1647c478bd9Sstevel@tonic-gate 	}
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate 	/*
1677c478bd9Sstevel@tonic-gate 	 * a shell is also restricted if the simple name of argv(0) is
1687c478bd9Sstevel@tonic-gate 	 * rsh or -rsh in its simple name
1697c478bd9Sstevel@tonic-gate 	 */
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate #ifndef RES
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate 	if (c > 0 && (eq("rsh", simple(*v)) || eq("-rsh", simple(*v))))
1747c478bd9Sstevel@tonic-gate 		rflag = 0;
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate #endif
1777c478bd9Sstevel@tonic-gate 
1787c478bd9Sstevel@tonic-gate 	if (eq("jsh", simple(*v)) || eq("-jsh", simple(*v)))
1797c478bd9Sstevel@tonic-gate 		flags |= monitorflg;
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate 	hcreate();
1827c478bd9Sstevel@tonic-gate 	set_dotpath();
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate 
1857c478bd9Sstevel@tonic-gate 	/*
1867c478bd9Sstevel@tonic-gate 	 * look for options
1877c478bd9Sstevel@tonic-gate 	 * dolc is $#
1887c478bd9Sstevel@tonic-gate 	 */
1897c478bd9Sstevel@tonic-gate 	dolc = options(c, v);
1907c478bd9Sstevel@tonic-gate 
191*db397771Schin 	if (dolc < 2) {
1927c478bd9Sstevel@tonic-gate 		flags |= stdflg;
1937c478bd9Sstevel@tonic-gate 		{
1947c478bd9Sstevel@tonic-gate 
1957c478bd9Sstevel@tonic-gate 			while (*flagc)
1967c478bd9Sstevel@tonic-gate 				flagc++;
1977c478bd9Sstevel@tonic-gate 			*flagc++ = STDFLG;
1987c478bd9Sstevel@tonic-gate 			*flagc = 0;
1997c478bd9Sstevel@tonic-gate 		}
2007c478bd9Sstevel@tonic-gate 	}
2017c478bd9Sstevel@tonic-gate 	if ((flags & stdflg) == 0)
2027c478bd9Sstevel@tonic-gate 		dolc--;
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate 	if ((flags & privflg) == 0) {
205965005c8Schin 		uid_t euid;
206965005c8Schin 		gid_t egid;
207965005c8Schin 		uid_t ruid;
208965005c8Schin 		gid_t rgid;
2097c478bd9Sstevel@tonic-gate 
2107c478bd9Sstevel@tonic-gate 		/*
2117c478bd9Sstevel@tonic-gate 		 * Determine all of the user's id #'s for this process and
2127c478bd9Sstevel@tonic-gate 		 * then decide if this shell is being entered as a result
2137c478bd9Sstevel@tonic-gate 		 * of a fork/exec.
2147c478bd9Sstevel@tonic-gate 		 * If the effective uid/gid do NOT match and the euid/egid
2157c478bd9Sstevel@tonic-gate 		 * is < 100 and the egid is NOT 1, reset the uid and gid to
2167c478bd9Sstevel@tonic-gate 		 * the user originally calling this process.
2177c478bd9Sstevel@tonic-gate 		 */
2187c478bd9Sstevel@tonic-gate 		euid = geteuid();
2197c478bd9Sstevel@tonic-gate 		ruid = getuid();
2207c478bd9Sstevel@tonic-gate 		egid = getegid();
2217c478bd9Sstevel@tonic-gate 		rgid = getgid();
2227c478bd9Sstevel@tonic-gate 		if ((euid != ruid) && (euid < 100))
2237c478bd9Sstevel@tonic-gate 			setuid(ruid);   /* reset the uid to the orig user */
2247c478bd9Sstevel@tonic-gate 		if ((egid != rgid) && ((egid < 100) && (egid != 1)))
2257c478bd9Sstevel@tonic-gate 			setgid(rgid);   /* reset the gid to the orig user */
2267c478bd9Sstevel@tonic-gate 	}
2277c478bd9Sstevel@tonic-gate 
2287c478bd9Sstevel@tonic-gate 	dolv = (unsigned char **)v + c - dolc;
2297c478bd9Sstevel@tonic-gate 	dolc--;
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate 	/*
2327c478bd9Sstevel@tonic-gate 	 * return here for shell file execution
2337c478bd9Sstevel@tonic-gate 	 * but not for parenthesis subshells
2347c478bd9Sstevel@tonic-gate 	 */
2357c478bd9Sstevel@tonic-gate 	if (setjmp(subshell)) {
2367c478bd9Sstevel@tonic-gate 		freejobs();
2377c478bd9Sstevel@tonic-gate 		flags |= subsh;
2387c478bd9Sstevel@tonic-gate 	}
2397c478bd9Sstevel@tonic-gate 
2407c478bd9Sstevel@tonic-gate 	/*
2417c478bd9Sstevel@tonic-gate 	 * number of positional parameters
2427c478bd9Sstevel@tonic-gate 	 */
2437c478bd9Sstevel@tonic-gate 	replace(&cmdadr, dolv[0]);	/* cmdadr is $0 */
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate 	/*
2467c478bd9Sstevel@tonic-gate 	 * set pidname '$$'
2477c478bd9Sstevel@tonic-gate 	 */
2487c478bd9Sstevel@tonic-gate 	assnum(&pidadr, (long)mypid);
2497c478bd9Sstevel@tonic-gate 
2507c478bd9Sstevel@tonic-gate 	/*
2517c478bd9Sstevel@tonic-gate 	 * set up temp file names
2527c478bd9Sstevel@tonic-gate 	 */
2537c478bd9Sstevel@tonic-gate 	settmp();
2547c478bd9Sstevel@tonic-gate 
2557c478bd9Sstevel@tonic-gate 	/*
2567c478bd9Sstevel@tonic-gate 	 * default internal field separators
2577c478bd9Sstevel@tonic-gate 	 * Do not allow importing of IFS from parent shell.
2587c478bd9Sstevel@tonic-gate 	 * setup_env() may have set anything from parent shell to IFS.
2597c478bd9Sstevel@tonic-gate 	 * Always set the default ifs to IFS.
2607c478bd9Sstevel@tonic-gate 	 */
261965005c8Schin 	assign(&ifsnod, (unsigned char *)sptbnl);
2627c478bd9Sstevel@tonic-gate 
2637c478bd9Sstevel@tonic-gate 	dfault(&mchknod, MAILCHECK);
2647c478bd9Sstevel@tonic-gate 	mailchk = stoi(mchknod.namval);
2657c478bd9Sstevel@tonic-gate 
2667c478bd9Sstevel@tonic-gate 	/* initialize OPTIND for getopt */
2677c478bd9Sstevel@tonic-gate 
2687c478bd9Sstevel@tonic-gate 	n = lookup("OPTIND");
269965005c8Schin 	assign(n, (unsigned char *)"1");
2707c478bd9Sstevel@tonic-gate 	/*
2717c478bd9Sstevel@tonic-gate 	 * make sure that option parsing starts
2727c478bd9Sstevel@tonic-gate 	 * at first character
2737c478bd9Sstevel@tonic-gate 	 */
2747c478bd9Sstevel@tonic-gate 	_sp = 1;
2757c478bd9Sstevel@tonic-gate 
2767c478bd9Sstevel@tonic-gate 	/* initialize multibyte information */
2777c478bd9Sstevel@tonic-gate 	setwidth();
2787c478bd9Sstevel@tonic-gate 
2797c478bd9Sstevel@tonic-gate 	if ((beenhere++) == FALSE)	/* ? profile */
2807c478bd9Sstevel@tonic-gate 	{
2817c478bd9Sstevel@tonic-gate 		if ((login_shell == TRUE) && (flags & privflg) == 0) {
2827c478bd9Sstevel@tonic-gate 
2837c478bd9Sstevel@tonic-gate 			/* system profile */
2847c478bd9Sstevel@tonic-gate 
2857c478bd9Sstevel@tonic-gate #ifndef RES
2867c478bd9Sstevel@tonic-gate 
2877c478bd9Sstevel@tonic-gate 			if ((input = pathopen(nullstr, sysprofile)) >= 0)
2887c478bd9Sstevel@tonic-gate 				exfile(rflag);		/* file exists */
2897c478bd9Sstevel@tonic-gate 
2907c478bd9Sstevel@tonic-gate #endif
2917c478bd9Sstevel@tonic-gate 			/* user profile */
2927c478bd9Sstevel@tonic-gate 
293*db397771Schin 			if ((input = pathopen(homenod.namval, profile)) >= 0) {
2947c478bd9Sstevel@tonic-gate 				exfile(rflag);
2957c478bd9Sstevel@tonic-gate 				flags &= ~ttyflg;
2967c478bd9Sstevel@tonic-gate 			}
2977c478bd9Sstevel@tonic-gate 		}
2987c478bd9Sstevel@tonic-gate 		if (rsflag == 0 || rflag == 0) {
2997c478bd9Sstevel@tonic-gate 			if ((flags & rshflg) == 0) {
3007c478bd9Sstevel@tonic-gate 				while (*flagc)
3017c478bd9Sstevel@tonic-gate 					flagc++;
3027c478bd9Sstevel@tonic-gate 				*flagc++ = 'r';
3037c478bd9Sstevel@tonic-gate 				*flagc = '\0';
3047c478bd9Sstevel@tonic-gate 			}
3057c478bd9Sstevel@tonic-gate 			flags |= rshflg;
3067c478bd9Sstevel@tonic-gate 		}
3077c478bd9Sstevel@tonic-gate 
3087c478bd9Sstevel@tonic-gate 		/*
3097c478bd9Sstevel@tonic-gate 		 * open input file if specified
3107c478bd9Sstevel@tonic-gate 		 */
311*db397771Schin 		if (comdiv) {
3127c478bd9Sstevel@tonic-gate 			estabf(comdiv);
3137c478bd9Sstevel@tonic-gate 			input = -1;
3147c478bd9Sstevel@tonic-gate 		}
3157c478bd9Sstevel@tonic-gate 		else
3167c478bd9Sstevel@tonic-gate 		{
3177c478bd9Sstevel@tonic-gate 			if (flags & stdflg) {
3187c478bd9Sstevel@tonic-gate 				input = 0;
3197c478bd9Sstevel@tonic-gate 			} else {
3207c478bd9Sstevel@tonic-gate 			/*
3217c478bd9Sstevel@tonic-gate 			 * If the command file specified by 'cmdadr'
3227c478bd9Sstevel@tonic-gate 			 * doesn't exist, chkopen() will fail calling
3237c478bd9Sstevel@tonic-gate 			 * exitsh(). If this is a login shell and
3247c478bd9Sstevel@tonic-gate 			 * the $HOME/.profile file does not exist, the
3257c478bd9Sstevel@tonic-gate 			 * above statement "flags &= ~ttyflg" does not
3267c478bd9Sstevel@tonic-gate 			 * get executed and this makes exitsh() call
3277c478bd9Sstevel@tonic-gate 			 * longjmp() instead of exiting. longjmp() will
3287c478bd9Sstevel@tonic-gate 			 * return to the location specified by the last
3297c478bd9Sstevel@tonic-gate 			 * active jmpbuffer, which is the one set up in
3307c478bd9Sstevel@tonic-gate 			 * the function exfile() called after the system
3317c478bd9Sstevel@tonic-gate 			 * profile file is executed (see lines above).
3327c478bd9Sstevel@tonic-gate 			 * This would cause an infinite loop, because
3337c478bd9Sstevel@tonic-gate 			 * chkopen() will continue to fail and exitsh()
3347c478bd9Sstevel@tonic-gate 			 * to call longjmp(). To make exitsh() exit instead
3357c478bd9Sstevel@tonic-gate 			 * of calling longjmp(), we then set the flag forcexit
3367c478bd9Sstevel@tonic-gate 			 * at this stage.
3377c478bd9Sstevel@tonic-gate 			 */
3387c478bd9Sstevel@tonic-gate 
3397c478bd9Sstevel@tonic-gate 				flags |= forcexit;
3407c478bd9Sstevel@tonic-gate 				input = chkopen(cmdadr, 0);
3417c478bd9Sstevel@tonic-gate 				flags &= ~forcexit;
3427c478bd9Sstevel@tonic-gate 			}
3437c478bd9Sstevel@tonic-gate 
3447c478bd9Sstevel@tonic-gate #ifdef ACCT
3457c478bd9Sstevel@tonic-gate 			if (input != 0)
3467c478bd9Sstevel@tonic-gate 				preacct(cmdadr);
3477c478bd9Sstevel@tonic-gate #endif
3487c478bd9Sstevel@tonic-gate 			comdiv--;
3497c478bd9Sstevel@tonic-gate 		}
3507c478bd9Sstevel@tonic-gate 	}
3517c478bd9Sstevel@tonic-gate #ifdef pdp11
3527c478bd9Sstevel@tonic-gate 	else
3537c478bd9Sstevel@tonic-gate 		*execargs = (char *)dolv;	/* for `ps' cmd */
3547c478bd9Sstevel@tonic-gate #endif
3557c478bd9Sstevel@tonic-gate 
3567c478bd9Sstevel@tonic-gate 
3577c478bd9Sstevel@tonic-gate 	exfile(0);
3587c478bd9Sstevel@tonic-gate 	done(0);
3597c478bd9Sstevel@tonic-gate }
3607c478bd9Sstevel@tonic-gate 
361965005c8Schin static void
362965005c8Schin exfile(int prof)
3637c478bd9Sstevel@tonic-gate {
3647c478bd9Sstevel@tonic-gate 	time_t	mailtime = 0;	/* Must not be a register variable */
3657c478bd9Sstevel@tonic-gate 	time_t 	curtime = 0;
3667c478bd9Sstevel@tonic-gate 
3677c478bd9Sstevel@tonic-gate 	/*
3687c478bd9Sstevel@tonic-gate 	 * move input
3697c478bd9Sstevel@tonic-gate 	 */
370*db397771Schin 	if (input > 0) {
3717c478bd9Sstevel@tonic-gate 		Ldup(input, INIO);
3727c478bd9Sstevel@tonic-gate 		input = INIO;
3737c478bd9Sstevel@tonic-gate 	}
3747c478bd9Sstevel@tonic-gate 
3757c478bd9Sstevel@tonic-gate 
3767c478bd9Sstevel@tonic-gate 	setmode(prof);
3777c478bd9Sstevel@tonic-gate 
378*db397771Schin 	if (setjmp(errshell) && prof) {
3797c478bd9Sstevel@tonic-gate 		close(input);
3807c478bd9Sstevel@tonic-gate 		(void) endjobs(0);
3817c478bd9Sstevel@tonic-gate 		return;
3827c478bd9Sstevel@tonic-gate 	}
3837c478bd9Sstevel@tonic-gate 	/*
3847c478bd9Sstevel@tonic-gate 	 * error return here
3857c478bd9Sstevel@tonic-gate 	 */
3867c478bd9Sstevel@tonic-gate 
3877c478bd9Sstevel@tonic-gate 	loopcnt = peekc = peekn = 0;
3887c478bd9Sstevel@tonic-gate 	fndef = 0;
3897c478bd9Sstevel@tonic-gate 	nohash = 0;
3907c478bd9Sstevel@tonic-gate 	iopend = 0;
3917c478bd9Sstevel@tonic-gate 
3927c478bd9Sstevel@tonic-gate 	if (input >= 0)
3937c478bd9Sstevel@tonic-gate 		initf(input);
3947c478bd9Sstevel@tonic-gate 	/*
3957c478bd9Sstevel@tonic-gate 	 * command loop
3967c478bd9Sstevel@tonic-gate 	 */
397*db397771Schin 	for (;;) {
3987c478bd9Sstevel@tonic-gate 		tdystak(0);
3997c478bd9Sstevel@tonic-gate 		stakchk();	/* may reduce sbrk */
4007c478bd9Sstevel@tonic-gate 		exitset();
4017c478bd9Sstevel@tonic-gate 
402*db397771Schin 		if ((flags & prompt) && standin->fstak == 0 && !eof) {
4037c478bd9Sstevel@tonic-gate 
404*db397771Schin 			if (mailp) {
4057c478bd9Sstevel@tonic-gate 				time(&curtime);
4067c478bd9Sstevel@tonic-gate 
407*db397771Schin 				if ((curtime - mailtime) >= mailchk) {
4087c478bd9Sstevel@tonic-gate 					chkmail();
4097c478bd9Sstevel@tonic-gate 					mailtime = curtime;
4107c478bd9Sstevel@tonic-gate 				}
4117c478bd9Sstevel@tonic-gate 			}
4127c478bd9Sstevel@tonic-gate 
4137c478bd9Sstevel@tonic-gate 			/* necessary to print jobs in a timely manner */
4147c478bd9Sstevel@tonic-gate 			if (trapnote & TRAPSET)
4157c478bd9Sstevel@tonic-gate 				chktrap();
4167c478bd9Sstevel@tonic-gate 
4177c478bd9Sstevel@tonic-gate 			prs(ps1nod.namval);
4187c478bd9Sstevel@tonic-gate 
4197c478bd9Sstevel@tonic-gate #ifdef TIME_OUT
4207c478bd9Sstevel@tonic-gate 			alarm(TIMEOUT);
4217c478bd9Sstevel@tonic-gate #endif
4227c478bd9Sstevel@tonic-gate 
4237c478bd9Sstevel@tonic-gate 		}
4247c478bd9Sstevel@tonic-gate 
4257c478bd9Sstevel@tonic-gate 		trapnote = 0;
4267c478bd9Sstevel@tonic-gate 		peekc = readwc();
4277c478bd9Sstevel@tonic-gate 		if (eof) {
4287c478bd9Sstevel@tonic-gate 			if (endjobs(JOB_STOPPED))
4297c478bd9Sstevel@tonic-gate 				return;
4307c478bd9Sstevel@tonic-gate 			eof = 0;
4317c478bd9Sstevel@tonic-gate 		}
4327c478bd9Sstevel@tonic-gate 
4337c478bd9Sstevel@tonic-gate #ifdef TIME_OUT
4347c478bd9Sstevel@tonic-gate 		alarm(0);
4357c478bd9Sstevel@tonic-gate #endif
4367c478bd9Sstevel@tonic-gate 
4377c478bd9Sstevel@tonic-gate 		{
438965005c8Schin 			struct trenod *t;
4397c478bd9Sstevel@tonic-gate 			t = cmd(NL, MTFLG);
4407c478bd9Sstevel@tonic-gate 			if (t == NULL && flags & ttyflg)
4417c478bd9Sstevel@tonic-gate 				freejobs();
4427c478bd9Sstevel@tonic-gate 			else
4437c478bd9Sstevel@tonic-gate 				execute(t, 0, eflag);
4447c478bd9Sstevel@tonic-gate 		}
4457c478bd9Sstevel@tonic-gate 
4467c478bd9Sstevel@tonic-gate 		eof |= (flags & oneflg);
4477c478bd9Sstevel@tonic-gate 
4487c478bd9Sstevel@tonic-gate 	}
4497c478bd9Sstevel@tonic-gate }
4507c478bd9Sstevel@tonic-gate 
451965005c8Schin void
452965005c8Schin chkpr(void)
4537c478bd9Sstevel@tonic-gate {
4547c478bd9Sstevel@tonic-gate 	if ((flags & prompt) && standin->fstak == 0)
4557c478bd9Sstevel@tonic-gate 		prs(ps2nod.namval);
4567c478bd9Sstevel@tonic-gate }
4577c478bd9Sstevel@tonic-gate 
458965005c8Schin void
459965005c8Schin settmp(void)
4607c478bd9Sstevel@tonic-gate {
461965005c8Schin 	int len;
4627c478bd9Sstevel@tonic-gate 	serial = 0;
463965005c8Schin 	if ((len = snprintf((char *)tmpout, TMPOUTSZ, "/tmp/sh%u", mypid)) >=
464965005c8Schin 	    TMPOUTSZ) {
465965005c8Schin 		/*
466965005c8Schin 		 * TMPOUTSZ should be big enough, but if it isn't,
467965005c8Schin 		 * we'll at least try to create tmp files with
468965005c8Schin 		 * a truncated tmpfile name at tmpout.
469965005c8Schin 		 */
470965005c8Schin 		tmpout_offset = TMPOUTSZ - 1;
471965005c8Schin 	} else {
472965005c8Schin 		tmpout_offset = len;
473965005c8Schin 	}
4747c478bd9Sstevel@tonic-gate }
4757c478bd9Sstevel@tonic-gate 
476965005c8Schin static void
477965005c8Schin Ldup(int fa, int fb)
4787c478bd9Sstevel@tonic-gate {
4797c478bd9Sstevel@tonic-gate #ifdef RES
4807c478bd9Sstevel@tonic-gate 
4817c478bd9Sstevel@tonic-gate 	dup(fa | DUPFLG, fb);
4827c478bd9Sstevel@tonic-gate 	close(fa);
4837c478bd9Sstevel@tonic-gate 	ioctl(fb, FIOCLEX, 0);
4847c478bd9Sstevel@tonic-gate 
4857c478bd9Sstevel@tonic-gate #else
4867c478bd9Sstevel@tonic-gate 
4877c478bd9Sstevel@tonic-gate 	if (fa >= 0) {
488*db397771Schin 		if (fa != fb) {
4897c478bd9Sstevel@tonic-gate 			close(fb);
4907c478bd9Sstevel@tonic-gate 			fcntl(fa, 0, fb); /* normal dup */
4917c478bd9Sstevel@tonic-gate 			close(fa);
4927c478bd9Sstevel@tonic-gate 		}
4937c478bd9Sstevel@tonic-gate 		fcntl(fb, 2, 1);	/* autoclose for fb */
4947c478bd9Sstevel@tonic-gate 	}
4957c478bd9Sstevel@tonic-gate 
4967c478bd9Sstevel@tonic-gate #endif
4977c478bd9Sstevel@tonic-gate }
4987c478bd9Sstevel@tonic-gate 
499965005c8Schin void
500965005c8Schin chkmail(void)
5017c478bd9Sstevel@tonic-gate {
502965005c8Schin 	unsigned char 	*s = mailp;
503965005c8Schin 	unsigned char	*save;
5047c478bd9Sstevel@tonic-gate 
5057c478bd9Sstevel@tonic-gate 	long	*ptr = mod_time;
5067c478bd9Sstevel@tonic-gate 	unsigned char	*start;
5077c478bd9Sstevel@tonic-gate 	BOOL	flg;
5087c478bd9Sstevel@tonic-gate 	struct stat	statb;
5097c478bd9Sstevel@tonic-gate 
5107c478bd9Sstevel@tonic-gate 	while (*s) {
5117c478bd9Sstevel@tonic-gate 		start = s;
5127c478bd9Sstevel@tonic-gate 		save = 0;
5137c478bd9Sstevel@tonic-gate 		flg = 0;
5147c478bd9Sstevel@tonic-gate 
5157c478bd9Sstevel@tonic-gate 		while (*s) {
5167c478bd9Sstevel@tonic-gate 			if (*s != COLON) {
5177c478bd9Sstevel@tonic-gate 				if (*s == '%' && save == 0)
5187c478bd9Sstevel@tonic-gate 					save = s;
5197c478bd9Sstevel@tonic-gate 
5207c478bd9Sstevel@tonic-gate 				s++;
5217c478bd9Sstevel@tonic-gate 			} else {
5227c478bd9Sstevel@tonic-gate 				flg = 1;
5237c478bd9Sstevel@tonic-gate 				*s = 0;
5247c478bd9Sstevel@tonic-gate 			}
5257c478bd9Sstevel@tonic-gate 		}
5267c478bd9Sstevel@tonic-gate 
5277c478bd9Sstevel@tonic-gate 		if (save)
5287c478bd9Sstevel@tonic-gate 			*save = 0;
5297c478bd9Sstevel@tonic-gate 
5307c478bd9Sstevel@tonic-gate 		if (*start && stat((const char *)start, &statb) >= 0) {
5317c478bd9Sstevel@tonic-gate 			if (statb.st_size && *ptr &&
5327c478bd9Sstevel@tonic-gate 			    statb.st_mtime != *ptr) {
5337c478bd9Sstevel@tonic-gate 				if (save) {
5347c478bd9Sstevel@tonic-gate 					prs(save+1);
5357c478bd9Sstevel@tonic-gate 					newline();
5367c478bd9Sstevel@tonic-gate 				}
5377c478bd9Sstevel@tonic-gate 				else
5387c478bd9Sstevel@tonic-gate 					prs(mailmsg);
5397c478bd9Sstevel@tonic-gate 			}
5407c478bd9Sstevel@tonic-gate 			*ptr = statb.st_mtime;
5417c478bd9Sstevel@tonic-gate 		} else if (*ptr == 0)
5427c478bd9Sstevel@tonic-gate 			*ptr = 1;
5437c478bd9Sstevel@tonic-gate 
5447c478bd9Sstevel@tonic-gate 		if (save)
5457c478bd9Sstevel@tonic-gate 			*save = '%';
5467c478bd9Sstevel@tonic-gate 
5477c478bd9Sstevel@tonic-gate 		if (flg)
5487c478bd9Sstevel@tonic-gate 			*s++ = COLON;
5497c478bd9Sstevel@tonic-gate 
5507c478bd9Sstevel@tonic-gate 		ptr++;
5517c478bd9Sstevel@tonic-gate 	}
5527c478bd9Sstevel@tonic-gate }
5537c478bd9Sstevel@tonic-gate 
554965005c8Schin void
555965005c8Schin setmail(unsigned char *mailpath)
5567c478bd9Sstevel@tonic-gate {
557965005c8Schin 	unsigned char	*s = mailpath;
558965005c8Schin 	int 		cnt = 1;
5597c478bd9Sstevel@tonic-gate 
5607c478bd9Sstevel@tonic-gate 	long	*ptr;
5617c478bd9Sstevel@tonic-gate 
5627c478bd9Sstevel@tonic-gate 	free(mod_time);
563*db397771Schin 	if (mailp = mailpath) {
564*db397771Schin 		while (*s) {
5657c478bd9Sstevel@tonic-gate 			if (*s == COLON)
5667c478bd9Sstevel@tonic-gate 				cnt += 1;
5677c478bd9Sstevel@tonic-gate 
5687c478bd9Sstevel@tonic-gate 			s++;
5697c478bd9Sstevel@tonic-gate 		}
5707c478bd9Sstevel@tonic-gate 
5717c478bd9Sstevel@tonic-gate 		ptr = mod_time = (long *)alloc(sizeof (long) * cnt);
5727c478bd9Sstevel@tonic-gate 
573*db397771Schin 		while (cnt) {
5747c478bd9Sstevel@tonic-gate 			*ptr = 0;
5757c478bd9Sstevel@tonic-gate 			ptr++;
5767c478bd9Sstevel@tonic-gate 			cnt--;
5777c478bd9Sstevel@tonic-gate 		}
5787c478bd9Sstevel@tonic-gate 	}
5797c478bd9Sstevel@tonic-gate }
5807c478bd9Sstevel@tonic-gate 
5817c478bd9Sstevel@tonic-gate void
5827c478bd9Sstevel@tonic-gate setwidth()
5837c478bd9Sstevel@tonic-gate {
5847c478bd9Sstevel@tonic-gate 	unsigned char *name = lookup("LC_CTYPE")->namval;
5857c478bd9Sstevel@tonic-gate 	if (!name || !*name)
5867c478bd9Sstevel@tonic-gate 		name = lookup("LANG")->namval;
5877c478bd9Sstevel@tonic-gate 	/*
5887c478bd9Sstevel@tonic-gate 	 * Do locale processing only if /usr is mounted.
5897c478bd9Sstevel@tonic-gate 	 */
5907c478bd9Sstevel@tonic-gate 	if (localedir_exists) {
5917c478bd9Sstevel@tonic-gate 		if (!name || !*name)
5927c478bd9Sstevel@tonic-gate 			(void) setlocale(LC_CTYPE, "C");
5937c478bd9Sstevel@tonic-gate 		else
5947c478bd9Sstevel@tonic-gate 			(void) setlocale(LC_CTYPE, (const char *)name);
5957c478bd9Sstevel@tonic-gate 	}
5967c478bd9Sstevel@tonic-gate }
5977c478bd9Sstevel@tonic-gate 
598965005c8Schin void
599965005c8Schin setmode(int prof)
6007c478bd9Sstevel@tonic-gate {
6017c478bd9Sstevel@tonic-gate 	/*
6027c478bd9Sstevel@tonic-gate 	 * decide whether interactive
6037c478bd9Sstevel@tonic-gate 	 */
6047c478bd9Sstevel@tonic-gate 
6057c478bd9Sstevel@tonic-gate 	if ((flags & intflg) ||
6067c478bd9Sstevel@tonic-gate 	    ((flags&oneflg) == 0 &&
6077c478bd9Sstevel@tonic-gate 	    isatty(output) &&
6087c478bd9Sstevel@tonic-gate 	    isatty(input)))
6097c478bd9Sstevel@tonic-gate 
6107c478bd9Sstevel@tonic-gate 	{
6117c478bd9Sstevel@tonic-gate 		dfault(&ps1nod, (geteuid() ? stdprompt : supprompt));
6127c478bd9Sstevel@tonic-gate 		dfault(&ps2nod, readmsg);
6137c478bd9Sstevel@tonic-gate 		flags |= ttyflg | prompt;
6147c478bd9Sstevel@tonic-gate 		if (mailpnod.namflg != N_DEFAULT)
6157c478bd9Sstevel@tonic-gate 			setmail(mailpnod.namval);
6167c478bd9Sstevel@tonic-gate 		else
6177c478bd9Sstevel@tonic-gate 			setmail(mailnod.namval);
6187c478bd9Sstevel@tonic-gate 		startjobs();
6197c478bd9Sstevel@tonic-gate 	}
6207c478bd9Sstevel@tonic-gate 	else
6217c478bd9Sstevel@tonic-gate 	{
6227c478bd9Sstevel@tonic-gate 		flags |= prof;
6237c478bd9Sstevel@tonic-gate 		flags &= ~prompt;
6247c478bd9Sstevel@tonic-gate 	}
6257c478bd9Sstevel@tonic-gate }
6267c478bd9Sstevel@tonic-gate 
6277c478bd9Sstevel@tonic-gate /*
6287c478bd9Sstevel@tonic-gate  * A generic call back routine to output error messages from the
6297c478bd9Sstevel@tonic-gate  * policy backing functions called by pfsh.
6307c478bd9Sstevel@tonic-gate  *
6317c478bd9Sstevel@tonic-gate  * msg must contain '\n' if a new line is to be printed.
6327c478bd9Sstevel@tonic-gate  */
6337c478bd9Sstevel@tonic-gate void
6347c478bd9Sstevel@tonic-gate secpolicy_print(int level, const char *msg)
6357c478bd9Sstevel@tonic-gate {
6367c478bd9Sstevel@tonic-gate 	switch (level) {
6377c478bd9Sstevel@tonic-gate 	case SECPOLICY_WARN:
6387c478bd9Sstevel@tonic-gate 	default:
6397c478bd9Sstevel@tonic-gate 		prs(msg);	/* prs() does gettext() */
6407c478bd9Sstevel@tonic-gate 		return;
6417c478bd9Sstevel@tonic-gate 	case SECPOLICY_ERROR:
6427c478bd9Sstevel@tonic-gate 		error(msg);
6437c478bd9Sstevel@tonic-gate 		break;
6447c478bd9Sstevel@tonic-gate 	}
6457c478bd9Sstevel@tonic-gate }
646