xref: /illumos-gate/usr/src/cmd/sh/main.c (revision 134a1f4e)
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
539e7390aSna  * Common Development and Distribution License (the "License").
639e7390aSna  * 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  */
2139e7390aSna 
227c478bd9Sstevel@tonic-gate /*
23*134a1f4eSCasper H.S. Dik  * Copyright (c) 1988, 2010, Oracle and/or its affiliates. All rights reserved.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
277c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate /*
307c478bd9Sstevel@tonic-gate  * UNIX shell
317c478bd9Sstevel@tonic-gate  */
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate #include	"defs.h"
347c478bd9Sstevel@tonic-gate #include	"sym.h"
357c478bd9Sstevel@tonic-gate #include	"timeout.h"
36965005c8Schin #include	<stdio.h>
377c478bd9Sstevel@tonic-gate #include	<sys/types.h>
387c478bd9Sstevel@tonic-gate #include	<sys/stat.h>
397c478bd9Sstevel@tonic-gate #include	<sys/wait.h>
407c478bd9Sstevel@tonic-gate #include	"dup.h"
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate #ifdef RES
437c478bd9Sstevel@tonic-gate #include	<sgtty.h>
447c478bd9Sstevel@tonic-gate #endif
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate pid_t mypid, mypgid, mysid;
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate static BOOL	beenhere = FALSE;
49965005c8Schin unsigned char	tmpout[TMPOUTSZ];
507c478bd9Sstevel@tonic-gate struct fileblk	stdfile;
517c478bd9Sstevel@tonic-gate struct fileblk *standin = &stdfile;
527c478bd9Sstevel@tonic-gate int mailchk = 0;
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate static unsigned char	*mailp;
557c478bd9Sstevel@tonic-gate static long	*mod_time = 0;
567c478bd9Sstevel@tonic-gate static BOOL login_shell = FALSE;
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate #if vax
597c478bd9Sstevel@tonic-gate char **execargs = (char **)(0x7ffffffc);
607c478bd9Sstevel@tonic-gate #endif
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate #if pdp11
637c478bd9Sstevel@tonic-gate char **execargs = (char **)(-2);
647c478bd9Sstevel@tonic-gate #endif
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate 
67965005c8Schin static void	exfile();
687c478bd9Sstevel@tonic-gate extern unsigned char 	*simple();
69965005c8Schin static void Ldup(int, int);
70965005c8Schin void settmp(void);
71965005c8Schin void chkmail(void);
72965005c8Schin void setmail(unsigned char *);
737c478bd9Sstevel@tonic-gate 
74965005c8Schin int
main(int c,char * v[],char * e[])75965005c8Schin main(int c, char *v[], char *e[])
767c478bd9Sstevel@tonic-gate {
77965005c8Schin 	int		rflag = ttyflg;
787c478bd9Sstevel@tonic-gate 	int		rsflag = 1;	/* local restricted flag */
79965005c8Schin 	unsigned char	*flagc = flagadr;
807c478bd9Sstevel@tonic-gate 	struct namnod	*n;
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate 	mypid = getpid();
837c478bd9Sstevel@tonic-gate 	mypgid = getpgid(mypid);
847c478bd9Sstevel@tonic-gate 	mysid = getsid(mypid);
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate 	/*
877c478bd9Sstevel@tonic-gate 	 * Do locale processing only if /usr is mounted.
887c478bd9Sstevel@tonic-gate 	 */
897c478bd9Sstevel@tonic-gate 	localedir_exists = (access(localedir, F_OK) == 0);
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate 	/*
927c478bd9Sstevel@tonic-gate 	 * initialize storage allocation
937c478bd9Sstevel@tonic-gate 	 */
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate 	if (stakbot == 0) {
967c478bd9Sstevel@tonic-gate 	addblok((unsigned)0);
977c478bd9Sstevel@tonic-gate 	}
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate 	/*
1007c478bd9Sstevel@tonic-gate 	 * If the first character of the last path element of v[0] is "-"
1017c478bd9Sstevel@tonic-gate 	 * (ex. -sh, or /bin/-sh), this is a login shell
1027c478bd9Sstevel@tonic-gate 	 */
1037c478bd9Sstevel@tonic-gate 	if (*simple(v[0]) == '-') {
1047c478bd9Sstevel@tonic-gate 		signal(SIGXCPU, SIG_DFL);
1057c478bd9Sstevel@tonic-gate 		signal(SIGXFSZ, SIG_DFL);
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate 		/*
1087c478bd9Sstevel@tonic-gate 		 * As the previous comment states, this is a login shell.
1097c478bd9Sstevel@tonic-gate 		 * Therefore, we set the login_shell flag to explicitly
1107c478bd9Sstevel@tonic-gate 		 * indicate this condition.
1117c478bd9Sstevel@tonic-gate 		 */
1127c478bd9Sstevel@tonic-gate 		login_shell = TRUE;
1137c478bd9Sstevel@tonic-gate 	}
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate 	stdsigs();
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate 	/*
1187c478bd9Sstevel@tonic-gate 	 * set names from userenv
1197c478bd9Sstevel@tonic-gate 	 */
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate 	setup_env();
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate 	/*
1247c478bd9Sstevel@tonic-gate 	 * LC_MESSAGES is set here so that early error messages will
1257c478bd9Sstevel@tonic-gate 	 * come out in the right style.
1267c478bd9Sstevel@tonic-gate 	 * Note that LC_CTYPE is done later on and is *not*
1277c478bd9Sstevel@tonic-gate 	 * taken from the previous environ
1287c478bd9Sstevel@tonic-gate 	 */
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate 	/*
1317c478bd9Sstevel@tonic-gate 	 * Do locale processing only if /usr is mounted.
1327c478bd9Sstevel@tonic-gate 	 */
1337c478bd9Sstevel@tonic-gate 	if (localedir_exists)
1347c478bd9Sstevel@tonic-gate 		(void) setlocale(LC_ALL, "");
1357c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
1367c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"	/* Use this only if it weren't */
1377c478bd9Sstevel@tonic-gate #endif
1387c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate 	/*
1417c478bd9Sstevel@tonic-gate 	 * 'rsflag' is zero if SHELL variable is
1427c478bd9Sstevel@tonic-gate 	 *  set in environment and
1437c478bd9Sstevel@tonic-gate 	 *  the simple file part of the value.
1447c478bd9Sstevel@tonic-gate 	 *  is rsh
1457c478bd9Sstevel@tonic-gate 	 */
146db397771Schin 	if (n = findnam("SHELL")) {
1477c478bd9Sstevel@tonic-gate 		if (eq("rsh", simple(n->namval)))
1487c478bd9Sstevel@tonic-gate 			rsflag = 0;
1497c478bd9Sstevel@tonic-gate 	}
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate 	/*
1527c478bd9Sstevel@tonic-gate 	 * a shell is also restricted if the simple name of argv(0) is
1537c478bd9Sstevel@tonic-gate 	 * rsh or -rsh in its simple name
1547c478bd9Sstevel@tonic-gate 	 */
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate #ifndef RES
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate 	if (c > 0 && (eq("rsh", simple(*v)) || eq("-rsh", simple(*v))))
1597c478bd9Sstevel@tonic-gate 		rflag = 0;
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate #endif
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate 	if (eq("jsh", simple(*v)) || eq("-jsh", simple(*v)))
1647c478bd9Sstevel@tonic-gate 		flags |= monitorflg;
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate 	hcreate();
1677c478bd9Sstevel@tonic-gate 	set_dotpath();
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate 	/*
1717c478bd9Sstevel@tonic-gate 	 * look for options
1727c478bd9Sstevel@tonic-gate 	 * dolc is $#
1737c478bd9Sstevel@tonic-gate 	 */
1747c478bd9Sstevel@tonic-gate 	dolc = options(c, v);
1757c478bd9Sstevel@tonic-gate 
176db397771Schin 	if (dolc < 2) {
1777c478bd9Sstevel@tonic-gate 		flags |= stdflg;
1787c478bd9Sstevel@tonic-gate 		{
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate 			while (*flagc)
1817c478bd9Sstevel@tonic-gate 				flagc++;
1827c478bd9Sstevel@tonic-gate 			*flagc++ = STDFLG;
1837c478bd9Sstevel@tonic-gate 			*flagc = 0;
1847c478bd9Sstevel@tonic-gate 		}
1857c478bd9Sstevel@tonic-gate 	}
1867c478bd9Sstevel@tonic-gate 	if ((flags & stdflg) == 0)
1877c478bd9Sstevel@tonic-gate 		dolc--;
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate 	if ((flags & privflg) == 0) {
190965005c8Schin 		uid_t euid;
191965005c8Schin 		gid_t egid;
192965005c8Schin 		uid_t ruid;
193965005c8Schin 		gid_t rgid;
1947c478bd9Sstevel@tonic-gate 
1957c478bd9Sstevel@tonic-gate 		/*
1967c478bd9Sstevel@tonic-gate 		 * Determine all of the user's id #'s for this process and
1977c478bd9Sstevel@tonic-gate 		 * then decide if this shell is being entered as a result
1987c478bd9Sstevel@tonic-gate 		 * of a fork/exec.
1997c478bd9Sstevel@tonic-gate 		 * If the effective uid/gid do NOT match and the euid/egid
2007c478bd9Sstevel@tonic-gate 		 * is < 100 and the egid is NOT 1, reset the uid and gid to
2017c478bd9Sstevel@tonic-gate 		 * the user originally calling this process.
2027c478bd9Sstevel@tonic-gate 		 */
2037c478bd9Sstevel@tonic-gate 		euid = geteuid();
2047c478bd9Sstevel@tonic-gate 		ruid = getuid();
2057c478bd9Sstevel@tonic-gate 		egid = getegid();
2067c478bd9Sstevel@tonic-gate 		rgid = getgid();
2077c478bd9Sstevel@tonic-gate 		if ((euid != ruid) && (euid < 100))
2087c478bd9Sstevel@tonic-gate 			setuid(ruid);   /* reset the uid to the orig user */
2097c478bd9Sstevel@tonic-gate 		if ((egid != rgid) && ((egid < 100) && (egid != 1)))
2107c478bd9Sstevel@tonic-gate 			setgid(rgid);   /* reset the gid to the orig user */
2117c478bd9Sstevel@tonic-gate 	}
2127c478bd9Sstevel@tonic-gate 
2137c478bd9Sstevel@tonic-gate 	dolv = (unsigned char **)v + c - dolc;
2147c478bd9Sstevel@tonic-gate 	dolc--;
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate 	/*
2177c478bd9Sstevel@tonic-gate 	 * return here for shell file execution
2187c478bd9Sstevel@tonic-gate 	 * but not for parenthesis subshells
2197c478bd9Sstevel@tonic-gate 	 */
2207c478bd9Sstevel@tonic-gate 	if (setjmp(subshell)) {
2217c478bd9Sstevel@tonic-gate 		freejobs();
2227c478bd9Sstevel@tonic-gate 		flags |= subsh;
2237c478bd9Sstevel@tonic-gate 	}
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate 	/*
2267c478bd9Sstevel@tonic-gate 	 * number of positional parameters
2277c478bd9Sstevel@tonic-gate 	 */
2287c478bd9Sstevel@tonic-gate 	replace(&cmdadr, dolv[0]);	/* cmdadr is $0 */
2297c478bd9Sstevel@tonic-gate 
2307c478bd9Sstevel@tonic-gate 	/*
2317c478bd9Sstevel@tonic-gate 	 * set pidname '$$'
2327c478bd9Sstevel@tonic-gate 	 */
2337c478bd9Sstevel@tonic-gate 	assnum(&pidadr, (long)mypid);
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate 	/*
2367c478bd9Sstevel@tonic-gate 	 * set up temp file names
2377c478bd9Sstevel@tonic-gate 	 */
2387c478bd9Sstevel@tonic-gate 	settmp();
2397c478bd9Sstevel@tonic-gate 
2407c478bd9Sstevel@tonic-gate 	/*
2417c478bd9Sstevel@tonic-gate 	 * default internal field separators
2427c478bd9Sstevel@tonic-gate 	 * Do not allow importing of IFS from parent shell.
2437c478bd9Sstevel@tonic-gate 	 * setup_env() may have set anything from parent shell to IFS.
2447c478bd9Sstevel@tonic-gate 	 * Always set the default ifs to IFS.
2457c478bd9Sstevel@tonic-gate 	 */
246965005c8Schin 	assign(&ifsnod, (unsigned char *)sptbnl);
2477c478bd9Sstevel@tonic-gate 
2487c478bd9Sstevel@tonic-gate 	dfault(&mchknod, MAILCHECK);
2497c478bd9Sstevel@tonic-gate 	mailchk = stoi(mchknod.namval);
2507c478bd9Sstevel@tonic-gate 
2517c478bd9Sstevel@tonic-gate 	/* initialize OPTIND for getopt */
2527c478bd9Sstevel@tonic-gate 
2537c478bd9Sstevel@tonic-gate 	n = lookup("OPTIND");
254965005c8Schin 	assign(n, (unsigned char *)"1");
2557c478bd9Sstevel@tonic-gate 	/*
2567c478bd9Sstevel@tonic-gate 	 * make sure that option parsing starts
2577c478bd9Sstevel@tonic-gate 	 * at first character
2587c478bd9Sstevel@tonic-gate 	 */
2597c478bd9Sstevel@tonic-gate 	_sp = 1;
2607c478bd9Sstevel@tonic-gate 
2617c478bd9Sstevel@tonic-gate 	if ((beenhere++) == FALSE)	/* ? profile */
2627c478bd9Sstevel@tonic-gate 	{
2637c478bd9Sstevel@tonic-gate 		if ((login_shell == TRUE) && (flags & privflg) == 0) {
2647c478bd9Sstevel@tonic-gate 
2657c478bd9Sstevel@tonic-gate 			/* system profile */
2667c478bd9Sstevel@tonic-gate 
2677c478bd9Sstevel@tonic-gate #ifndef RES
2687c478bd9Sstevel@tonic-gate 
2697c478bd9Sstevel@tonic-gate 			if ((input = pathopen(nullstr, sysprofile)) >= 0)
2707c478bd9Sstevel@tonic-gate 				exfile(rflag);		/* file exists */
2717c478bd9Sstevel@tonic-gate 
2727c478bd9Sstevel@tonic-gate #endif
2737c478bd9Sstevel@tonic-gate 			/* user profile */
2747c478bd9Sstevel@tonic-gate 
275db397771Schin 			if ((input = pathopen(homenod.namval, profile)) >= 0) {
2767c478bd9Sstevel@tonic-gate 				exfile(rflag);
2777c478bd9Sstevel@tonic-gate 				flags &= ~ttyflg;
2787c478bd9Sstevel@tonic-gate 			}
2797c478bd9Sstevel@tonic-gate 		}
2807c478bd9Sstevel@tonic-gate 		if (rsflag == 0 || rflag == 0) {
2817c478bd9Sstevel@tonic-gate 			if ((flags & rshflg) == 0) {
2827c478bd9Sstevel@tonic-gate 				while (*flagc)
2837c478bd9Sstevel@tonic-gate 					flagc++;
2847c478bd9Sstevel@tonic-gate 				*flagc++ = 'r';
2857c478bd9Sstevel@tonic-gate 				*flagc = '\0';
2867c478bd9Sstevel@tonic-gate 			}
2877c478bd9Sstevel@tonic-gate 			flags |= rshflg;
2887c478bd9Sstevel@tonic-gate 		}
2897c478bd9Sstevel@tonic-gate 
2907c478bd9Sstevel@tonic-gate 		/*
2917c478bd9Sstevel@tonic-gate 		 * open input file if specified
2927c478bd9Sstevel@tonic-gate 		 */
293db397771Schin 		if (comdiv) {
2947c478bd9Sstevel@tonic-gate 			estabf(comdiv);
2957c478bd9Sstevel@tonic-gate 			input = -1;
2967c478bd9Sstevel@tonic-gate 		}
2977c478bd9Sstevel@tonic-gate 		else
2987c478bd9Sstevel@tonic-gate 		{
2997c478bd9Sstevel@tonic-gate 			if (flags & stdflg) {
3007c478bd9Sstevel@tonic-gate 				input = 0;
3017c478bd9Sstevel@tonic-gate 			} else {
3027c478bd9Sstevel@tonic-gate 			/*
3037c478bd9Sstevel@tonic-gate 			 * If the command file specified by 'cmdadr'
3047c478bd9Sstevel@tonic-gate 			 * doesn't exist, chkopen() will fail calling
3057c478bd9Sstevel@tonic-gate 			 * exitsh(). If this is a login shell and
3067c478bd9Sstevel@tonic-gate 			 * the $HOME/.profile file does not exist, the
3077c478bd9Sstevel@tonic-gate 			 * above statement "flags &= ~ttyflg" does not
3087c478bd9Sstevel@tonic-gate 			 * get executed and this makes exitsh() call
3097c478bd9Sstevel@tonic-gate 			 * longjmp() instead of exiting. longjmp() will
3107c478bd9Sstevel@tonic-gate 			 * return to the location specified by the last
3117c478bd9Sstevel@tonic-gate 			 * active jmpbuffer, which is the one set up in
3127c478bd9Sstevel@tonic-gate 			 * the function exfile() called after the system
3137c478bd9Sstevel@tonic-gate 			 * profile file is executed (see lines above).
3147c478bd9Sstevel@tonic-gate 			 * This would cause an infinite loop, because
3157c478bd9Sstevel@tonic-gate 			 * chkopen() will continue to fail and exitsh()
3167c478bd9Sstevel@tonic-gate 			 * to call longjmp(). To make exitsh() exit instead
3177c478bd9Sstevel@tonic-gate 			 * of calling longjmp(), we then set the flag forcexit
3187c478bd9Sstevel@tonic-gate 			 * at this stage.
3197c478bd9Sstevel@tonic-gate 			 */
3207c478bd9Sstevel@tonic-gate 
3217c478bd9Sstevel@tonic-gate 				flags |= forcexit;
3227c478bd9Sstevel@tonic-gate 				input = chkopen(cmdadr, 0);
3237c478bd9Sstevel@tonic-gate 				flags &= ~forcexit;
3247c478bd9Sstevel@tonic-gate 			}
3257c478bd9Sstevel@tonic-gate 
3267c478bd9Sstevel@tonic-gate #ifdef ACCT
3277c478bd9Sstevel@tonic-gate 			if (input != 0)
3287c478bd9Sstevel@tonic-gate 				preacct(cmdadr);
3297c478bd9Sstevel@tonic-gate #endif
3307c478bd9Sstevel@tonic-gate 			comdiv--;
3317c478bd9Sstevel@tonic-gate 		}
3327c478bd9Sstevel@tonic-gate 	}
3337c478bd9Sstevel@tonic-gate #ifdef pdp11
3347c478bd9Sstevel@tonic-gate 	else
3357c478bd9Sstevel@tonic-gate 		*execargs = (char *)dolv;	/* for `ps' cmd */
3367c478bd9Sstevel@tonic-gate #endif
3377c478bd9Sstevel@tonic-gate 
3387c478bd9Sstevel@tonic-gate 
3397c478bd9Sstevel@tonic-gate 	exfile(0);
3407c478bd9Sstevel@tonic-gate 	done(0);
3417c478bd9Sstevel@tonic-gate }
3427c478bd9Sstevel@tonic-gate 
343965005c8Schin static void
exfile(int prof)344965005c8Schin exfile(int prof)
3457c478bd9Sstevel@tonic-gate {
3467c478bd9Sstevel@tonic-gate 	time_t	mailtime = 0;	/* Must not be a register variable */
3477c478bd9Sstevel@tonic-gate 	time_t 	curtime = 0;
3487c478bd9Sstevel@tonic-gate 
3497c478bd9Sstevel@tonic-gate 	/*
3507c478bd9Sstevel@tonic-gate 	 * move input
3517c478bd9Sstevel@tonic-gate 	 */
352db397771Schin 	if (input > 0) {
3537c478bd9Sstevel@tonic-gate 		Ldup(input, INIO);
3547c478bd9Sstevel@tonic-gate 		input = INIO;
3557c478bd9Sstevel@tonic-gate 	}
3567c478bd9Sstevel@tonic-gate 
3577c478bd9Sstevel@tonic-gate 
3587c478bd9Sstevel@tonic-gate 	setmode(prof);
3597c478bd9Sstevel@tonic-gate 
360db397771Schin 	if (setjmp(errshell) && prof) {
3617c478bd9Sstevel@tonic-gate 		close(input);
3627c478bd9Sstevel@tonic-gate 		(void) endjobs(0);
3637c478bd9Sstevel@tonic-gate 		return;
3647c478bd9Sstevel@tonic-gate 	}
3657c478bd9Sstevel@tonic-gate 	/*
3667c478bd9Sstevel@tonic-gate 	 * error return here
3677c478bd9Sstevel@tonic-gate 	 */
3687c478bd9Sstevel@tonic-gate 
3697c478bd9Sstevel@tonic-gate 	loopcnt = peekc = peekn = 0;
3707c478bd9Sstevel@tonic-gate 	fndef = 0;
3717c478bd9Sstevel@tonic-gate 	nohash = 0;
3727c478bd9Sstevel@tonic-gate 	iopend = 0;
3737c478bd9Sstevel@tonic-gate 
3747c478bd9Sstevel@tonic-gate 	if (input >= 0)
3757c478bd9Sstevel@tonic-gate 		initf(input);
3767c478bd9Sstevel@tonic-gate 	/*
3777c478bd9Sstevel@tonic-gate 	 * command loop
3787c478bd9Sstevel@tonic-gate 	 */
379db397771Schin 	for (;;) {
3807c478bd9Sstevel@tonic-gate 		tdystak(0);
3817c478bd9Sstevel@tonic-gate 		stakchk();	/* may reduce sbrk */
3827c478bd9Sstevel@tonic-gate 		exitset();
3837c478bd9Sstevel@tonic-gate 
384db397771Schin 		if ((flags & prompt) && standin->fstak == 0 && !eof) {
3857c478bd9Sstevel@tonic-gate 
386db397771Schin 			if (mailp) {
3877c478bd9Sstevel@tonic-gate 				time(&curtime);
3887c478bd9Sstevel@tonic-gate 
389db397771Schin 				if ((curtime - mailtime) >= mailchk) {
3907c478bd9Sstevel@tonic-gate 					chkmail();
3917c478bd9Sstevel@tonic-gate 					mailtime = curtime;
3927c478bd9Sstevel@tonic-gate 				}
3937c478bd9Sstevel@tonic-gate 			}
3947c478bd9Sstevel@tonic-gate 
3957c478bd9Sstevel@tonic-gate 			/* necessary to print jobs in a timely manner */
3967c478bd9Sstevel@tonic-gate 			if (trapnote & TRAPSET)
3977c478bd9Sstevel@tonic-gate 				chktrap();
3987c478bd9Sstevel@tonic-gate 
3997c478bd9Sstevel@tonic-gate 			prs(ps1nod.namval);
4007c478bd9Sstevel@tonic-gate 
4017c478bd9Sstevel@tonic-gate #ifdef TIME_OUT
4027c478bd9Sstevel@tonic-gate 			alarm(TIMEOUT);
4037c478bd9Sstevel@tonic-gate #endif
4047c478bd9Sstevel@tonic-gate 
4057c478bd9Sstevel@tonic-gate 		}
4067c478bd9Sstevel@tonic-gate 
4077c478bd9Sstevel@tonic-gate 		trapnote = 0;
4087c478bd9Sstevel@tonic-gate 		peekc = readwc();
4097c478bd9Sstevel@tonic-gate 		if (eof) {
4107c478bd9Sstevel@tonic-gate 			if (endjobs(JOB_STOPPED))
4117c478bd9Sstevel@tonic-gate 				return;
4127c478bd9Sstevel@tonic-gate 			eof = 0;
4137c478bd9Sstevel@tonic-gate 		}
4147c478bd9Sstevel@tonic-gate 
4157c478bd9Sstevel@tonic-gate #ifdef TIME_OUT
4167c478bd9Sstevel@tonic-gate 		alarm(0);
4177c478bd9Sstevel@tonic-gate #endif
4187c478bd9Sstevel@tonic-gate 
4197c478bd9Sstevel@tonic-gate 		{
420965005c8Schin 			struct trenod *t;
4217c478bd9Sstevel@tonic-gate 			t = cmd(NL, MTFLG);
4227c478bd9Sstevel@tonic-gate 			if (t == NULL && flags & ttyflg)
4237c478bd9Sstevel@tonic-gate 				freejobs();
4247c478bd9Sstevel@tonic-gate 			else
4257c478bd9Sstevel@tonic-gate 				execute(t, 0, eflag);
4267c478bd9Sstevel@tonic-gate 		}
4277c478bd9Sstevel@tonic-gate 
4287c478bd9Sstevel@tonic-gate 		eof |= (flags & oneflg);
4297c478bd9Sstevel@tonic-gate 
4307c478bd9Sstevel@tonic-gate 	}
4317c478bd9Sstevel@tonic-gate }
4327c478bd9Sstevel@tonic-gate 
433965005c8Schin void
chkpr(void)434965005c8Schin chkpr(void)
4357c478bd9Sstevel@tonic-gate {
4367c478bd9Sstevel@tonic-gate 	if ((flags & prompt) && standin->fstak == 0)
4377c478bd9Sstevel@tonic-gate 		prs(ps2nod.namval);
4387c478bd9Sstevel@tonic-gate }
4397c478bd9Sstevel@tonic-gate 
440965005c8Schin void
settmp(void)441965005c8Schin settmp(void)
4427c478bd9Sstevel@tonic-gate {
443965005c8Schin 	int len;
4447c478bd9Sstevel@tonic-gate 	serial = 0;
445965005c8Schin 	if ((len = snprintf((char *)tmpout, TMPOUTSZ, "/tmp/sh%u", mypid)) >=
446965005c8Schin 	    TMPOUTSZ) {
447965005c8Schin 		/*
448965005c8Schin 		 * TMPOUTSZ should be big enough, but if it isn't,
449965005c8Schin 		 * we'll at least try to create tmp files with
450965005c8Schin 		 * a truncated tmpfile name at tmpout.
451965005c8Schin 		 */
452965005c8Schin 		tmpout_offset = TMPOUTSZ - 1;
453965005c8Schin 	} else {
454965005c8Schin 		tmpout_offset = len;
455965005c8Schin 	}
4567c478bd9Sstevel@tonic-gate }
4577c478bd9Sstevel@tonic-gate 
458965005c8Schin static void
Ldup(int fa,int fb)459965005c8Schin Ldup(int fa, int fb)
4607c478bd9Sstevel@tonic-gate {
4617c478bd9Sstevel@tonic-gate #ifdef RES
4627c478bd9Sstevel@tonic-gate 
4637c478bd9Sstevel@tonic-gate 	dup(fa | DUPFLG, fb);
4647c478bd9Sstevel@tonic-gate 	close(fa);
4657c478bd9Sstevel@tonic-gate 	ioctl(fb, FIOCLEX, 0);
4667c478bd9Sstevel@tonic-gate 
4677c478bd9Sstevel@tonic-gate #else
4687c478bd9Sstevel@tonic-gate 
4697c478bd9Sstevel@tonic-gate 	if (fa >= 0) {
470db397771Schin 		if (fa != fb) {
4717c478bd9Sstevel@tonic-gate 			close(fb);
4727c478bd9Sstevel@tonic-gate 			fcntl(fa, 0, fb); /* normal dup */
4737c478bd9Sstevel@tonic-gate 			close(fa);
4747c478bd9Sstevel@tonic-gate 		}
4757c478bd9Sstevel@tonic-gate 		fcntl(fb, 2, 1);	/* autoclose for fb */
4767c478bd9Sstevel@tonic-gate 	}
4777c478bd9Sstevel@tonic-gate 
4787c478bd9Sstevel@tonic-gate #endif
4797c478bd9Sstevel@tonic-gate }
4807c478bd9Sstevel@tonic-gate 
481965005c8Schin void
chkmail(void)482965005c8Schin chkmail(void)
4837c478bd9Sstevel@tonic-gate {
484965005c8Schin 	unsigned char 	*s = mailp;
485965005c8Schin 	unsigned char	*save;
4867c478bd9Sstevel@tonic-gate 
4877c478bd9Sstevel@tonic-gate 	long	*ptr = mod_time;
4887c478bd9Sstevel@tonic-gate 	unsigned char	*start;
4897c478bd9Sstevel@tonic-gate 	BOOL	flg;
4907c478bd9Sstevel@tonic-gate 	struct stat	statb;
4917c478bd9Sstevel@tonic-gate 
4927c478bd9Sstevel@tonic-gate 	while (*s) {
4937c478bd9Sstevel@tonic-gate 		start = s;
4947c478bd9Sstevel@tonic-gate 		save = 0;
4957c478bd9Sstevel@tonic-gate 		flg = 0;
4967c478bd9Sstevel@tonic-gate 
4977c478bd9Sstevel@tonic-gate 		while (*s) {
4987c478bd9Sstevel@tonic-gate 			if (*s != COLON) {
4997c478bd9Sstevel@tonic-gate 				if (*s == '%' && save == 0)
5007c478bd9Sstevel@tonic-gate 					save = s;
5017c478bd9Sstevel@tonic-gate 
5027c478bd9Sstevel@tonic-gate 				s++;
5037c478bd9Sstevel@tonic-gate 			} else {
5047c478bd9Sstevel@tonic-gate 				flg = 1;
5057c478bd9Sstevel@tonic-gate 				*s = 0;
5067c478bd9Sstevel@tonic-gate 			}
5077c478bd9Sstevel@tonic-gate 		}
5087c478bd9Sstevel@tonic-gate 
5097c478bd9Sstevel@tonic-gate 		if (save)
5107c478bd9Sstevel@tonic-gate 			*save = 0;
5117c478bd9Sstevel@tonic-gate 
5127c478bd9Sstevel@tonic-gate 		if (*start && stat((const char *)start, &statb) >= 0) {
5137c478bd9Sstevel@tonic-gate 			if (statb.st_size && *ptr &&
5147c478bd9Sstevel@tonic-gate 			    statb.st_mtime != *ptr) {
5157c478bd9Sstevel@tonic-gate 				if (save) {
5167c478bd9Sstevel@tonic-gate 					prs(save+1);
5177c478bd9Sstevel@tonic-gate 					newline();
5187c478bd9Sstevel@tonic-gate 				}
5197c478bd9Sstevel@tonic-gate 				else
52039e7390aSna 					prs(_gettext(mailmsg));
5217c478bd9Sstevel@tonic-gate 			}
5227c478bd9Sstevel@tonic-gate 			*ptr = statb.st_mtime;
5237c478bd9Sstevel@tonic-gate 		} else if (*ptr == 0)
5247c478bd9Sstevel@tonic-gate 			*ptr = 1;
5257c478bd9Sstevel@tonic-gate 
5267c478bd9Sstevel@tonic-gate 		if (save)
5277c478bd9Sstevel@tonic-gate 			*save = '%';
5287c478bd9Sstevel@tonic-gate 
5297c478bd9Sstevel@tonic-gate 		if (flg)
5307c478bd9Sstevel@tonic-gate 			*s++ = COLON;
5317c478bd9Sstevel@tonic-gate 
5327c478bd9Sstevel@tonic-gate 		ptr++;
5337c478bd9Sstevel@tonic-gate 	}
5347c478bd9Sstevel@tonic-gate }
5357c478bd9Sstevel@tonic-gate 
536965005c8Schin void
setmail(unsigned char * mailpath)537965005c8Schin setmail(unsigned char *mailpath)
5387c478bd9Sstevel@tonic-gate {
539965005c8Schin 	unsigned char	*s = mailpath;
540965005c8Schin 	int 		cnt = 1;
5417c478bd9Sstevel@tonic-gate 
5427c478bd9Sstevel@tonic-gate 	long	*ptr;
5437c478bd9Sstevel@tonic-gate 
5447c478bd9Sstevel@tonic-gate 	free(mod_time);
545db397771Schin 	if (mailp = mailpath) {
546db397771Schin 		while (*s) {
5477c478bd9Sstevel@tonic-gate 			if (*s == COLON)
5487c478bd9Sstevel@tonic-gate 				cnt += 1;
5497c478bd9Sstevel@tonic-gate 
5507c478bd9Sstevel@tonic-gate 			s++;
5517c478bd9Sstevel@tonic-gate 		}
5527c478bd9Sstevel@tonic-gate 
5537c478bd9Sstevel@tonic-gate 		ptr = mod_time = (long *)alloc(sizeof (long) * cnt);
5547c478bd9Sstevel@tonic-gate 
555db397771Schin 		while (cnt) {
5567c478bd9Sstevel@tonic-gate 			*ptr = 0;
5577c478bd9Sstevel@tonic-gate 			ptr++;
5587c478bd9Sstevel@tonic-gate 			cnt--;
5597c478bd9Sstevel@tonic-gate 		}
5607c478bd9Sstevel@tonic-gate 	}
5617c478bd9Sstevel@tonic-gate }
5627c478bd9Sstevel@tonic-gate 
563965005c8Schin void
setmode(int prof)564965005c8Schin setmode(int prof)
5657c478bd9Sstevel@tonic-gate {
5667c478bd9Sstevel@tonic-gate 	/*
5677c478bd9Sstevel@tonic-gate 	 * decide whether interactive
5687c478bd9Sstevel@tonic-gate 	 */
5697c478bd9Sstevel@tonic-gate 
5707c478bd9Sstevel@tonic-gate 	if ((flags & intflg) ||
5717c478bd9Sstevel@tonic-gate 	    ((flags&oneflg) == 0 &&
5727c478bd9Sstevel@tonic-gate 	    isatty(output) &&
5737c478bd9Sstevel@tonic-gate 	    isatty(input)))
5747c478bd9Sstevel@tonic-gate 
5757c478bd9Sstevel@tonic-gate 	{
5767c478bd9Sstevel@tonic-gate 		dfault(&ps1nod, (geteuid() ? stdprompt : supprompt));
5777c478bd9Sstevel@tonic-gate 		dfault(&ps2nod, readmsg);
5787c478bd9Sstevel@tonic-gate 		flags |= ttyflg | prompt;
5797c478bd9Sstevel@tonic-gate 		if (mailpnod.namflg != N_DEFAULT)
5807c478bd9Sstevel@tonic-gate 			setmail(mailpnod.namval);
5817c478bd9Sstevel@tonic-gate 		else
5827c478bd9Sstevel@tonic-gate 			setmail(mailnod.namval);
5837c478bd9Sstevel@tonic-gate 		startjobs();
5847c478bd9Sstevel@tonic-gate 	}
5857c478bd9Sstevel@tonic-gate 	else
5867c478bd9Sstevel@tonic-gate 	{
5877c478bd9Sstevel@tonic-gate 		flags |= prof;
5887c478bd9Sstevel@tonic-gate 		flags &= ~prompt;
5897c478bd9Sstevel@tonic-gate 	}
5907c478bd9Sstevel@tonic-gate }
591