xref: /illumos-gate/usr/src/cmd/sh/bltin.c (revision 965005c8)
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 
237c478bd9Sstevel@tonic-gate /*
24*965005c8Schin  * Copyright 1996 Sun Microsystems, Inc.  All rights reserved.
25*965005c8Schin  * Use is subject to license terms.
267c478bd9Sstevel@tonic-gate  */
277c478bd9Sstevel@tonic-gate 
28*965005c8Schin /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
29*965005c8Schin /*	  All Rights Reserved  	*/
30*965005c8Schin 
31*965005c8Schin #pragma ident	"%Z%%M%	%I%	%E% SMI"
327c478bd9Sstevel@tonic-gate /*
337c478bd9Sstevel@tonic-gate  *
347c478bd9Sstevel@tonic-gate  * UNIX shell
357c478bd9Sstevel@tonic-gate  *
367c478bd9Sstevel@tonic-gate  */
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate #include	"defs.h"
407c478bd9Sstevel@tonic-gate #include	<errno.h>
417c478bd9Sstevel@tonic-gate #include	"sym.h"
427c478bd9Sstevel@tonic-gate #include	"hash.h"
437c478bd9Sstevel@tonic-gate #include	<sys/types.h>
447c478bd9Sstevel@tonic-gate #include	<sys/times.h>
457c478bd9Sstevel@tonic-gate 
46*965005c8Schin void
47*965005c8Schin builtin(int type, int argc, unsigned char **argv, struct trenod *t)
487c478bd9Sstevel@tonic-gate {
497c478bd9Sstevel@tonic-gate 	short index = initio(t->treio, (type != SYSEXEC));
507c478bd9Sstevel@tonic-gate 	unsigned char *a1 = argv[1];
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate 	switch (type)
537c478bd9Sstevel@tonic-gate 	{
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate 	case SYSSUSP:
567c478bd9Sstevel@tonic-gate 		syssusp(argc,argv);
577c478bd9Sstevel@tonic-gate 		break;
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate 	case SYSSTOP:
607c478bd9Sstevel@tonic-gate 		sysstop(argc,argv);
617c478bd9Sstevel@tonic-gate 		break;
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate 	case SYSKILL:
647c478bd9Sstevel@tonic-gate 		syskill(argc,argv);
657c478bd9Sstevel@tonic-gate 		break;
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate 	case SYSFGBG:
687c478bd9Sstevel@tonic-gate 		sysfgbg(argc,argv);
697c478bd9Sstevel@tonic-gate 		break;
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate 	case SYSJOBS:
727c478bd9Sstevel@tonic-gate 		sysjobs(argc,argv);
737c478bd9Sstevel@tonic-gate 		break;
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate 	case SYSDOT:
767c478bd9Sstevel@tonic-gate 		if (a1)
777c478bd9Sstevel@tonic-gate 		{
78*965005c8Schin 			int	f;
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate 			if ((f = pathopen(getpath(a1), a1)) < 0)
817c478bd9Sstevel@tonic-gate 				failed(a1, notfound);
827c478bd9Sstevel@tonic-gate 			else
837c478bd9Sstevel@tonic-gate 				execexp(0, f);
847c478bd9Sstevel@tonic-gate 		}
857c478bd9Sstevel@tonic-gate 		break;
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate 	case SYSTIMES:
887c478bd9Sstevel@tonic-gate 		{
897c478bd9Sstevel@tonic-gate 			struct tms tms;
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate 			times(&tms);
927c478bd9Sstevel@tonic-gate 			prt(tms.tms_cutime);
937c478bd9Sstevel@tonic-gate 			prc_buff(SPACE);
947c478bd9Sstevel@tonic-gate 			prt(tms.tms_cstime);
957c478bd9Sstevel@tonic-gate 			prc_buff(NL);
967c478bd9Sstevel@tonic-gate 		}
977c478bd9Sstevel@tonic-gate 		break;
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate 	case SYSEXIT:
1007c478bd9Sstevel@tonic-gate 		if ( tried_to_exit++ || endjobs(JOB_STOPPED) ){
1017c478bd9Sstevel@tonic-gate 			flags |= forcexit;	/* force exit */
1027c478bd9Sstevel@tonic-gate 			exitsh(a1 ? stoi(a1) : retval);
1037c478bd9Sstevel@tonic-gate 		}
1047c478bd9Sstevel@tonic-gate 		break;
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate 	case SYSNULL:
1077c478bd9Sstevel@tonic-gate 		t->treio = 0;
1087c478bd9Sstevel@tonic-gate 		break;
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate 	case SYSCONT:
1117c478bd9Sstevel@tonic-gate 		if (loopcnt)
1127c478bd9Sstevel@tonic-gate 		{
1137c478bd9Sstevel@tonic-gate 			execbrk = breakcnt = 1;
1147c478bd9Sstevel@tonic-gate 			if (a1)
1157c478bd9Sstevel@tonic-gate 				breakcnt = stoi(a1);
1167c478bd9Sstevel@tonic-gate 			if (breakcnt > loopcnt)
1177c478bd9Sstevel@tonic-gate 				breakcnt = loopcnt;
1187c478bd9Sstevel@tonic-gate 			else
1197c478bd9Sstevel@tonic-gate 				breakcnt = -breakcnt;
1207c478bd9Sstevel@tonic-gate 		}
1217c478bd9Sstevel@tonic-gate 		break;
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate 	case SYSBREAK:
1247c478bd9Sstevel@tonic-gate 		if (loopcnt)
1257c478bd9Sstevel@tonic-gate 		{
1267c478bd9Sstevel@tonic-gate 			execbrk = breakcnt = 1;
1277c478bd9Sstevel@tonic-gate 			if (a1)
1287c478bd9Sstevel@tonic-gate 				breakcnt = stoi(a1);
1297c478bd9Sstevel@tonic-gate 			if (breakcnt > loopcnt)
1307c478bd9Sstevel@tonic-gate 				breakcnt = loopcnt;
1317c478bd9Sstevel@tonic-gate 		}
1327c478bd9Sstevel@tonic-gate 		break;
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate 	case SYSTRAP:
1357c478bd9Sstevel@tonic-gate 		systrap(argc,argv);
1367c478bd9Sstevel@tonic-gate 		break;
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate 	case SYSEXEC:
1397c478bd9Sstevel@tonic-gate 		argv++;
1407c478bd9Sstevel@tonic-gate 		ioset = 0;
1417c478bd9Sstevel@tonic-gate 		if (a1 == 0) {
1427c478bd9Sstevel@tonic-gate 			setmode(0);
1437c478bd9Sstevel@tonic-gate 			break;
1447c478bd9Sstevel@tonic-gate 		}
1457c478bd9Sstevel@tonic-gate 		/* FALLTHROUGH */
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate #ifdef RES	/* Research includes login as part of the shell */
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate 	case SYSLOGIN:
1507c478bd9Sstevel@tonic-gate 		if (!endjobs(JOB_STOPPED|JOB_RUNNING))
1517c478bd9Sstevel@tonic-gate 			break;
1527c478bd9Sstevel@tonic-gate 		oldsigs();
1537c478bd9Sstevel@tonic-gate 		execa(argv, -1);
1547c478bd9Sstevel@tonic-gate 		done(0);
1557c478bd9Sstevel@tonic-gate #else
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate 	case SYSNEWGRP:
1587c478bd9Sstevel@tonic-gate 		if (flags & rshflg)
1597c478bd9Sstevel@tonic-gate 			failed(argv[0], restricted);
1607c478bd9Sstevel@tonic-gate 		else if (!endjobs(JOB_STOPPED|JOB_RUNNING))
1617c478bd9Sstevel@tonic-gate 			break;
1627c478bd9Sstevel@tonic-gate 		else
1637c478bd9Sstevel@tonic-gate 		{
1647c478bd9Sstevel@tonic-gate 			flags |= forcexit; /* bad exec will terminate shell */
1657c478bd9Sstevel@tonic-gate 			oldsigs();
1667c478bd9Sstevel@tonic-gate 			rmtemp(0);
1677c478bd9Sstevel@tonic-gate 			rmfunctmp();
1687c478bd9Sstevel@tonic-gate #ifdef ACCT
1697c478bd9Sstevel@tonic-gate 			doacct();
1707c478bd9Sstevel@tonic-gate #endif
1717c478bd9Sstevel@tonic-gate 			execa(argv, -1);
1727c478bd9Sstevel@tonic-gate 			done(0);
1737c478bd9Sstevel@tonic-gate 		}
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate #endif
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate 	case SYSCD:
1787c478bd9Sstevel@tonic-gate 		if (flags & rshflg)
1797c478bd9Sstevel@tonic-gate 			failed(argv[0], restricted);
1807c478bd9Sstevel@tonic-gate 		else if ((a1 && *a1) || (a1 == 0 && (a1 = homenod.namval)))
1817c478bd9Sstevel@tonic-gate 		{
1827c478bd9Sstevel@tonic-gate 			unsigned char *cdpath;
1837c478bd9Sstevel@tonic-gate 			unsigned char *dir;
1847c478bd9Sstevel@tonic-gate 			int f;
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate 			if ((cdpath = cdpnod.namval) == 0 ||
1877c478bd9Sstevel@tonic-gate 			     *a1 == '/' ||
1887c478bd9Sstevel@tonic-gate 			     cf(a1, ".") == 0 ||
1897c478bd9Sstevel@tonic-gate 			     cf(a1, "..") == 0 ||
1907c478bd9Sstevel@tonic-gate 			     (*a1 == '.' && (*(a1+1) == '/' || *(a1+1) == '.' && *(a1+2) == '/')))
1917c478bd9Sstevel@tonic-gate 				cdpath = (unsigned char *)nullstr;
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate 			do
1947c478bd9Sstevel@tonic-gate 			{
1957c478bd9Sstevel@tonic-gate 				dir = cdpath;
1967c478bd9Sstevel@tonic-gate 				cdpath = catpath(cdpath,a1);
1977c478bd9Sstevel@tonic-gate 			}
1987c478bd9Sstevel@tonic-gate 			while ((f = (chdir((const char *) curstak()) < 0)) &&
1997c478bd9Sstevel@tonic-gate 			    cdpath);
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate 			if (f) {
2027c478bd9Sstevel@tonic-gate 				switch(errno) {
2037c478bd9Sstevel@tonic-gate 						case EMULTIHOP:
2047c478bd9Sstevel@tonic-gate 							failed(a1, emultihop);
2057c478bd9Sstevel@tonic-gate 							break;
2067c478bd9Sstevel@tonic-gate 						case ENOTDIR:
2077c478bd9Sstevel@tonic-gate 							failed(a1, enotdir);
2087c478bd9Sstevel@tonic-gate 							break;
2097c478bd9Sstevel@tonic-gate 						case ENOENT:
2107c478bd9Sstevel@tonic-gate 							failed(a1, enoent);
2117c478bd9Sstevel@tonic-gate 							break;
2127c478bd9Sstevel@tonic-gate 						case EACCES:
2137c478bd9Sstevel@tonic-gate 							failed(a1, eacces);
2147c478bd9Sstevel@tonic-gate 							break;
2157c478bd9Sstevel@tonic-gate 						case ENOLINK:
2167c478bd9Sstevel@tonic-gate 							failed(a1, enolink);
2177c478bd9Sstevel@tonic-gate 							break;
2187c478bd9Sstevel@tonic-gate 						default:
2197c478bd9Sstevel@tonic-gate 						failed(a1, baddir);
2207c478bd9Sstevel@tonic-gate 						break;
2217c478bd9Sstevel@tonic-gate 						}
2227c478bd9Sstevel@tonic-gate 			}
2237c478bd9Sstevel@tonic-gate 			else
2247c478bd9Sstevel@tonic-gate 			{
2257c478bd9Sstevel@tonic-gate 				cwd(curstak());
2267c478bd9Sstevel@tonic-gate 				if (cf(nullstr, dir) &&
2277c478bd9Sstevel@tonic-gate 				    *dir != ':' &&
2287c478bd9Sstevel@tonic-gate 					any('/', curstak()) &&
2297c478bd9Sstevel@tonic-gate 					flags & prompt)
2307c478bd9Sstevel@tonic-gate 				{
2317c478bd9Sstevel@tonic-gate 					prs_buff(cwdget());
2327c478bd9Sstevel@tonic-gate 					prc_buff(NL);
2337c478bd9Sstevel@tonic-gate 				}
2347c478bd9Sstevel@tonic-gate 			}
2357c478bd9Sstevel@tonic-gate 			zapcd();
2367c478bd9Sstevel@tonic-gate 		}
2377c478bd9Sstevel@tonic-gate 		else
2387c478bd9Sstevel@tonic-gate 		{
2397c478bd9Sstevel@tonic-gate 			if (a1)
2407c478bd9Sstevel@tonic-gate 				error(nulldir);
2417c478bd9Sstevel@tonic-gate 			else
2427c478bd9Sstevel@tonic-gate 				error(nohome);
2437c478bd9Sstevel@tonic-gate 		}
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate 		break;
2467c478bd9Sstevel@tonic-gate 
2477c478bd9Sstevel@tonic-gate 	case SYSSHFT:
2487c478bd9Sstevel@tonic-gate 		{
2497c478bd9Sstevel@tonic-gate 			int places;
2507c478bd9Sstevel@tonic-gate 
2517c478bd9Sstevel@tonic-gate 			places = a1 ? stoi(a1) : 1;
2527c478bd9Sstevel@tonic-gate 
2537c478bd9Sstevel@tonic-gate 			if ((dolc -= places) < 0)
2547c478bd9Sstevel@tonic-gate 			{
2557c478bd9Sstevel@tonic-gate 				dolc = 0;
2567c478bd9Sstevel@tonic-gate 				error(badshift);
2577c478bd9Sstevel@tonic-gate 			}
2587c478bd9Sstevel@tonic-gate 			else
2597c478bd9Sstevel@tonic-gate 				dolv += places;
2607c478bd9Sstevel@tonic-gate 		}
2617c478bd9Sstevel@tonic-gate 
2627c478bd9Sstevel@tonic-gate 		break;
2637c478bd9Sstevel@tonic-gate 
2647c478bd9Sstevel@tonic-gate 	case SYSWAIT:
2657c478bd9Sstevel@tonic-gate 		syswait(argc,argv);
2667c478bd9Sstevel@tonic-gate 		break;
2677c478bd9Sstevel@tonic-gate 
2687c478bd9Sstevel@tonic-gate 	case SYSREAD:
2697c478bd9Sstevel@tonic-gate 		if(argc < 2)
2707c478bd9Sstevel@tonic-gate 			failed(argv[0],mssgargn);
2717c478bd9Sstevel@tonic-gate 		rwait = 1;
2727c478bd9Sstevel@tonic-gate 		exitval = readvar(&argv[1]);
2737c478bd9Sstevel@tonic-gate 		rwait = 0;
2747c478bd9Sstevel@tonic-gate 		break;
2757c478bd9Sstevel@tonic-gate 
2767c478bd9Sstevel@tonic-gate 	case SYSSET:
2777c478bd9Sstevel@tonic-gate 		if (a1)
2787c478bd9Sstevel@tonic-gate 		{
2797c478bd9Sstevel@tonic-gate 			int	cnt;
2807c478bd9Sstevel@tonic-gate 
2817c478bd9Sstevel@tonic-gate 			cnt = options(argc, argv);
2827c478bd9Sstevel@tonic-gate 			if (cnt > 1)
2837c478bd9Sstevel@tonic-gate 				setargs(argv + argc - cnt);
2847c478bd9Sstevel@tonic-gate 		}
2857c478bd9Sstevel@tonic-gate 		else if (comptr(t)->comset == 0)
2867c478bd9Sstevel@tonic-gate 		{
2877c478bd9Sstevel@tonic-gate 			/*
2887c478bd9Sstevel@tonic-gate 			 * scan name chain and print
2897c478bd9Sstevel@tonic-gate 			 */
2907c478bd9Sstevel@tonic-gate 			namscan(printnam);
2917c478bd9Sstevel@tonic-gate 		}
2927c478bd9Sstevel@tonic-gate 		break;
2937c478bd9Sstevel@tonic-gate 
2947c478bd9Sstevel@tonic-gate 	case SYSRDONLY:
2957c478bd9Sstevel@tonic-gate 		exitval = 0;
2967c478bd9Sstevel@tonic-gate 		if (a1)
2977c478bd9Sstevel@tonic-gate 		{
2987c478bd9Sstevel@tonic-gate 			while (*++argv)
2997c478bd9Sstevel@tonic-gate 				attrib(lookup(*argv), N_RDONLY);
3007c478bd9Sstevel@tonic-gate 		}
3017c478bd9Sstevel@tonic-gate 		else
3027c478bd9Sstevel@tonic-gate 			namscan(printro);
3037c478bd9Sstevel@tonic-gate 
3047c478bd9Sstevel@tonic-gate 		break;
3057c478bd9Sstevel@tonic-gate 
3067c478bd9Sstevel@tonic-gate 	case SYSXPORT:
3077c478bd9Sstevel@tonic-gate 		{
3087c478bd9Sstevel@tonic-gate 			struct namnod 	*n;
3097c478bd9Sstevel@tonic-gate 
3107c478bd9Sstevel@tonic-gate 			exitval = 0;
3117c478bd9Sstevel@tonic-gate 			if (a1)
3127c478bd9Sstevel@tonic-gate 			{
3137c478bd9Sstevel@tonic-gate 				while (*++argv)
3147c478bd9Sstevel@tonic-gate 				{
3157c478bd9Sstevel@tonic-gate 					n = lookup(*argv);
3167c478bd9Sstevel@tonic-gate 					if (n->namflg & N_FUNCTN)
3177c478bd9Sstevel@tonic-gate 						error(badexport);
3187c478bd9Sstevel@tonic-gate 					else
3197c478bd9Sstevel@tonic-gate 						attrib(n, N_EXPORT);
3207c478bd9Sstevel@tonic-gate 				}
3217c478bd9Sstevel@tonic-gate 			}
3227c478bd9Sstevel@tonic-gate 			else
3237c478bd9Sstevel@tonic-gate 				namscan(printexp);
3247c478bd9Sstevel@tonic-gate 		}
3257c478bd9Sstevel@tonic-gate 		break;
3267c478bd9Sstevel@tonic-gate 
3277c478bd9Sstevel@tonic-gate 	case SYSEVAL:
3287c478bd9Sstevel@tonic-gate 		if (a1)
3297c478bd9Sstevel@tonic-gate 			execexp(a1, &argv[2]);
3307c478bd9Sstevel@tonic-gate 		break;
3317c478bd9Sstevel@tonic-gate 
3327c478bd9Sstevel@tonic-gate #ifndef RES
3337c478bd9Sstevel@tonic-gate 	case SYSULIMIT:
3347c478bd9Sstevel@tonic-gate 		sysulimit(argc, argv);
3357c478bd9Sstevel@tonic-gate 		break;
3367c478bd9Sstevel@tonic-gate 
3377c478bd9Sstevel@tonic-gate 	case SYSUMASK:
3387c478bd9Sstevel@tonic-gate 		if (a1)
3397c478bd9Sstevel@tonic-gate 		{
3407c478bd9Sstevel@tonic-gate 			int c;
3417c478bd9Sstevel@tonic-gate 			mode_t i;
3427c478bd9Sstevel@tonic-gate 
3437c478bd9Sstevel@tonic-gate 			i = 0;
3447c478bd9Sstevel@tonic-gate 			while ((c = *a1++) >= '0' && c <= '7')
3457c478bd9Sstevel@tonic-gate 				i = (i << 3) + c - '0';
3467c478bd9Sstevel@tonic-gate 			umask(i);
3477c478bd9Sstevel@tonic-gate 		}
3487c478bd9Sstevel@tonic-gate 		else
3497c478bd9Sstevel@tonic-gate 		{
3507c478bd9Sstevel@tonic-gate 			mode_t i;
3517c478bd9Sstevel@tonic-gate 			int j;
3527c478bd9Sstevel@tonic-gate 
3537c478bd9Sstevel@tonic-gate 			umask(i = umask(0));
3547c478bd9Sstevel@tonic-gate 			prc_buff('0');
3557c478bd9Sstevel@tonic-gate 			for (j = 6; j >= 0; j -= 3)
3567c478bd9Sstevel@tonic-gate 				prc_buff(((i >> j) & 07) +'0');
3577c478bd9Sstevel@tonic-gate 			prc_buff(NL);
3587c478bd9Sstevel@tonic-gate 		}
3597c478bd9Sstevel@tonic-gate 		break;
3607c478bd9Sstevel@tonic-gate 
3617c478bd9Sstevel@tonic-gate #endif
3627c478bd9Sstevel@tonic-gate 
3637c478bd9Sstevel@tonic-gate 	case SYSTST:
3647c478bd9Sstevel@tonic-gate 		exitval = test(argc, argv);
3657c478bd9Sstevel@tonic-gate 		break;
3667c478bd9Sstevel@tonic-gate 
3677c478bd9Sstevel@tonic-gate 	case SYSECHO:
3687c478bd9Sstevel@tonic-gate 		exitval = echo(argc, argv);
3697c478bd9Sstevel@tonic-gate 		break;
3707c478bd9Sstevel@tonic-gate 
3717c478bd9Sstevel@tonic-gate 	case SYSHASH:
3727c478bd9Sstevel@tonic-gate 		exitval = 0;
3737c478bd9Sstevel@tonic-gate 
3747c478bd9Sstevel@tonic-gate 		if (a1)
3757c478bd9Sstevel@tonic-gate 		{
3767c478bd9Sstevel@tonic-gate 			if (a1[0] == '-')
3777c478bd9Sstevel@tonic-gate 			{
3787c478bd9Sstevel@tonic-gate 				if (a1[1] == 'r')
3797c478bd9Sstevel@tonic-gate 					zaphash();
3807c478bd9Sstevel@tonic-gate 				else
3817c478bd9Sstevel@tonic-gate 					error(badopt);
3827c478bd9Sstevel@tonic-gate 			}
3837c478bd9Sstevel@tonic-gate 			else
3847c478bd9Sstevel@tonic-gate 			{
3857c478bd9Sstevel@tonic-gate 				while (*++argv)
3867c478bd9Sstevel@tonic-gate 				{
3877c478bd9Sstevel@tonic-gate 					if (hashtype(hash_cmd(*argv)) == NOTFOUND)
3887c478bd9Sstevel@tonic-gate 						failed(*argv, notfound);
3897c478bd9Sstevel@tonic-gate 				}
3907c478bd9Sstevel@tonic-gate 			}
3917c478bd9Sstevel@tonic-gate 		}
3927c478bd9Sstevel@tonic-gate 		else
3937c478bd9Sstevel@tonic-gate 			hashpr();
3947c478bd9Sstevel@tonic-gate 
3957c478bd9Sstevel@tonic-gate 		break;
3967c478bd9Sstevel@tonic-gate 
3977c478bd9Sstevel@tonic-gate 	case SYSPWD:
3987c478bd9Sstevel@tonic-gate 		{
3997c478bd9Sstevel@tonic-gate 			exitval = 0;
4007c478bd9Sstevel@tonic-gate 			cwdprint();
4017c478bd9Sstevel@tonic-gate 		}
4027c478bd9Sstevel@tonic-gate 		break;
4037c478bd9Sstevel@tonic-gate 
4047c478bd9Sstevel@tonic-gate 	case SYSRETURN:
4057c478bd9Sstevel@tonic-gate 		if (funcnt == 0)
4067c478bd9Sstevel@tonic-gate 			error(badreturn);
4077c478bd9Sstevel@tonic-gate 
4087c478bd9Sstevel@tonic-gate 		execbrk = 1;
4097c478bd9Sstevel@tonic-gate 		exitval = (a1 ? stoi(a1) : retval);
4107c478bd9Sstevel@tonic-gate 		break;
4117c478bd9Sstevel@tonic-gate 
4127c478bd9Sstevel@tonic-gate 	case SYSTYPE:
4137c478bd9Sstevel@tonic-gate 		exitval = 0;
4147c478bd9Sstevel@tonic-gate 		if (a1)
4157c478bd9Sstevel@tonic-gate 		{
4167c478bd9Sstevel@tonic-gate 			/* return success only if all names are found */
4177c478bd9Sstevel@tonic-gate 			while (*++argv)
4187c478bd9Sstevel@tonic-gate 				exitval |= what_is_path(*argv);
4197c478bd9Sstevel@tonic-gate 		}
4207c478bd9Sstevel@tonic-gate 		break;
4217c478bd9Sstevel@tonic-gate 
4227c478bd9Sstevel@tonic-gate 	case SYSUNS:
4237c478bd9Sstevel@tonic-gate 		exitval = 0;
4247c478bd9Sstevel@tonic-gate 		if (a1)
4257c478bd9Sstevel@tonic-gate 		{
4267c478bd9Sstevel@tonic-gate 			while (*++argv)
4277c478bd9Sstevel@tonic-gate 				unset_name(*argv);
4287c478bd9Sstevel@tonic-gate 		}
4297c478bd9Sstevel@tonic-gate 		break;
4307c478bd9Sstevel@tonic-gate 
4317c478bd9Sstevel@tonic-gate 	case SYSGETOPT: {
4327c478bd9Sstevel@tonic-gate 		int getoptval;
4337c478bd9Sstevel@tonic-gate 		struct namnod *n;
4347c478bd9Sstevel@tonic-gate 		extern unsigned char numbuf[];
4357c478bd9Sstevel@tonic-gate 		unsigned char *varnam = argv[2];
4367c478bd9Sstevel@tonic-gate 		unsigned char c[2];
4377c478bd9Sstevel@tonic-gate 		if(argc < 3) {
4387c478bd9Sstevel@tonic-gate 			failure(argv[0],mssgargn);
4397c478bd9Sstevel@tonic-gate 			break;
4407c478bd9Sstevel@tonic-gate 		}
4417c478bd9Sstevel@tonic-gate 		exitval = 0;
4427c478bd9Sstevel@tonic-gate 		n = lookup("OPTIND");
4437c478bd9Sstevel@tonic-gate 		optind = stoi(n->namval);
4447c478bd9Sstevel@tonic-gate 		if(argc > 3) {
4457c478bd9Sstevel@tonic-gate 			argv[2] = dolv[0];
4467c478bd9Sstevel@tonic-gate 			getoptval = getopt(argc-2, (char **)&argv[2], (char *)argv[1]);
4477c478bd9Sstevel@tonic-gate 		}
4487c478bd9Sstevel@tonic-gate 		else
4497c478bd9Sstevel@tonic-gate 			getoptval = getopt(dolc+1, (char **)dolv, (char *)argv[1]);
4507c478bd9Sstevel@tonic-gate 		if(getoptval == -1) {
4517c478bd9Sstevel@tonic-gate 			itos(optind);
4527c478bd9Sstevel@tonic-gate 			assign(n, numbuf);
4537c478bd9Sstevel@tonic-gate 			n = lookup(varnam);
454*965005c8Schin 			assign(n, (unsigned char *)nullstr);
4557c478bd9Sstevel@tonic-gate 			exitval = 1;
4567c478bd9Sstevel@tonic-gate 			break;
4577c478bd9Sstevel@tonic-gate 		}
4587c478bd9Sstevel@tonic-gate 		argv[2] = varnam;
4597c478bd9Sstevel@tonic-gate 		itos(optind);
4607c478bd9Sstevel@tonic-gate 		assign(n, numbuf);
4617c478bd9Sstevel@tonic-gate 		c[0] = getoptval;
4627c478bd9Sstevel@tonic-gate 		c[1] = 0;
4637c478bd9Sstevel@tonic-gate 		n = lookup(varnam);
4647c478bd9Sstevel@tonic-gate 		assign(n, c);
4657c478bd9Sstevel@tonic-gate 		n = lookup("OPTARG");
466*965005c8Schin 		assign(n, (unsigned char *)optarg);
4677c478bd9Sstevel@tonic-gate 		}
4687c478bd9Sstevel@tonic-gate 		break;
4697c478bd9Sstevel@tonic-gate 
4707c478bd9Sstevel@tonic-gate 	default:
4717c478bd9Sstevel@tonic-gate 		prs_buff("unknown builtin\n");
4727c478bd9Sstevel@tonic-gate 	}
4737c478bd9Sstevel@tonic-gate 
4747c478bd9Sstevel@tonic-gate 
4757c478bd9Sstevel@tonic-gate 	flushb();
4767c478bd9Sstevel@tonic-gate 	restore(index);
4777c478bd9Sstevel@tonic-gate 	chktrap();
4787c478bd9Sstevel@tonic-gate }
479