xref: /onnv-gate/usr/src/cmd/sh/bltin.c (revision 2256:a9ca1c675600)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*2256Sna195498  * Common Development and Distribution License (the "License").
6*2256Sna195498  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
21527Schin 
22527Schin /*
23*2256Sna195498  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
24527Schin  * Use is subject to license terms.
25527Schin  */
26527Schin 
270Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
280Sstevel@tonic-gate /*	  All Rights Reserved  	*/
290Sstevel@tonic-gate 
30527Schin #pragma ident	"%Z%%M%	%I%	%E% SMI"
310Sstevel@tonic-gate /*
320Sstevel@tonic-gate  *
330Sstevel@tonic-gate  * UNIX shell
340Sstevel@tonic-gate  *
350Sstevel@tonic-gate  */
360Sstevel@tonic-gate 
370Sstevel@tonic-gate 
380Sstevel@tonic-gate #include	"defs.h"
390Sstevel@tonic-gate #include	<errno.h>
400Sstevel@tonic-gate #include	"sym.h"
410Sstevel@tonic-gate #include	"hash.h"
420Sstevel@tonic-gate #include	<sys/types.h>
430Sstevel@tonic-gate #include	<sys/times.h>
440Sstevel@tonic-gate 
45527Schin void
builtin(int type,int argc,unsigned char ** argv,struct trenod * t)46527Schin builtin(int type, int argc, unsigned char **argv, struct trenod *t)
470Sstevel@tonic-gate {
480Sstevel@tonic-gate 	short index = initio(t->treio, (type != SYSEXEC));
490Sstevel@tonic-gate 	unsigned char *a1 = argv[1];
500Sstevel@tonic-gate 
510Sstevel@tonic-gate 	switch (type)
520Sstevel@tonic-gate 	{
530Sstevel@tonic-gate 
540Sstevel@tonic-gate 	case SYSSUSP:
550Sstevel@tonic-gate 		syssusp(argc,argv);
560Sstevel@tonic-gate 		break;
570Sstevel@tonic-gate 
580Sstevel@tonic-gate 	case SYSSTOP:
590Sstevel@tonic-gate 		sysstop(argc,argv);
600Sstevel@tonic-gate 		break;
610Sstevel@tonic-gate 
620Sstevel@tonic-gate 	case SYSKILL:
630Sstevel@tonic-gate 		syskill(argc,argv);
640Sstevel@tonic-gate 		break;
650Sstevel@tonic-gate 
660Sstevel@tonic-gate 	case SYSFGBG:
670Sstevel@tonic-gate 		sysfgbg(argc,argv);
680Sstevel@tonic-gate 		break;
690Sstevel@tonic-gate 
700Sstevel@tonic-gate 	case SYSJOBS:
710Sstevel@tonic-gate 		sysjobs(argc,argv);
720Sstevel@tonic-gate 		break;
730Sstevel@tonic-gate 
740Sstevel@tonic-gate 	case SYSDOT:
750Sstevel@tonic-gate 		if (a1)
760Sstevel@tonic-gate 		{
77527Schin 			int	f;
780Sstevel@tonic-gate 
790Sstevel@tonic-gate 			if ((f = pathopen(getpath(a1), a1)) < 0)
800Sstevel@tonic-gate 				failed(a1, notfound);
810Sstevel@tonic-gate 			else
820Sstevel@tonic-gate 				execexp(0, f);
830Sstevel@tonic-gate 		}
840Sstevel@tonic-gate 		break;
850Sstevel@tonic-gate 
860Sstevel@tonic-gate 	case SYSTIMES:
870Sstevel@tonic-gate 		{
880Sstevel@tonic-gate 			struct tms tms;
890Sstevel@tonic-gate 
900Sstevel@tonic-gate 			times(&tms);
910Sstevel@tonic-gate 			prt(tms.tms_cutime);
920Sstevel@tonic-gate 			prc_buff(SPACE);
930Sstevel@tonic-gate 			prt(tms.tms_cstime);
940Sstevel@tonic-gate 			prc_buff(NL);
950Sstevel@tonic-gate 		}
960Sstevel@tonic-gate 		break;
970Sstevel@tonic-gate 
980Sstevel@tonic-gate 	case SYSEXIT:
990Sstevel@tonic-gate 		if ( tried_to_exit++ || endjobs(JOB_STOPPED) ){
1000Sstevel@tonic-gate 			flags |= forcexit;	/* force exit */
1010Sstevel@tonic-gate 			exitsh(a1 ? stoi(a1) : retval);
1020Sstevel@tonic-gate 		}
1030Sstevel@tonic-gate 		break;
1040Sstevel@tonic-gate 
1050Sstevel@tonic-gate 	case SYSNULL:
1060Sstevel@tonic-gate 		t->treio = 0;
1070Sstevel@tonic-gate 		break;
1080Sstevel@tonic-gate 
1090Sstevel@tonic-gate 	case SYSCONT:
1100Sstevel@tonic-gate 		if (loopcnt)
1110Sstevel@tonic-gate 		{
1120Sstevel@tonic-gate 			execbrk = breakcnt = 1;
1130Sstevel@tonic-gate 			if (a1)
1140Sstevel@tonic-gate 				breakcnt = stoi(a1);
1150Sstevel@tonic-gate 			if (breakcnt > loopcnt)
1160Sstevel@tonic-gate 				breakcnt = loopcnt;
1170Sstevel@tonic-gate 			else
1180Sstevel@tonic-gate 				breakcnt = -breakcnt;
1190Sstevel@tonic-gate 		}
1200Sstevel@tonic-gate 		break;
1210Sstevel@tonic-gate 
1220Sstevel@tonic-gate 	case SYSBREAK:
1230Sstevel@tonic-gate 		if (loopcnt)
1240Sstevel@tonic-gate 		{
1250Sstevel@tonic-gate 			execbrk = breakcnt = 1;
1260Sstevel@tonic-gate 			if (a1)
1270Sstevel@tonic-gate 				breakcnt = stoi(a1);
1280Sstevel@tonic-gate 			if (breakcnt > loopcnt)
1290Sstevel@tonic-gate 				breakcnt = loopcnt;
1300Sstevel@tonic-gate 		}
1310Sstevel@tonic-gate 		break;
1320Sstevel@tonic-gate 
1330Sstevel@tonic-gate 	case SYSTRAP:
1340Sstevel@tonic-gate 		systrap(argc,argv);
1350Sstevel@tonic-gate 		break;
1360Sstevel@tonic-gate 
1370Sstevel@tonic-gate 	case SYSEXEC:
1380Sstevel@tonic-gate 		argv++;
1390Sstevel@tonic-gate 		ioset = 0;
1400Sstevel@tonic-gate 		if (a1 == 0) {
1410Sstevel@tonic-gate 			setmode(0);
1420Sstevel@tonic-gate 			break;
1430Sstevel@tonic-gate 		}
1440Sstevel@tonic-gate 		/* FALLTHROUGH */
1450Sstevel@tonic-gate 
1460Sstevel@tonic-gate #ifdef RES	/* Research includes login as part of the shell */
1470Sstevel@tonic-gate 
1480Sstevel@tonic-gate 	case SYSLOGIN:
1490Sstevel@tonic-gate 		if (!endjobs(JOB_STOPPED|JOB_RUNNING))
1500Sstevel@tonic-gate 			break;
1510Sstevel@tonic-gate 		oldsigs();
1520Sstevel@tonic-gate 		execa(argv, -1);
1530Sstevel@tonic-gate 		done(0);
1540Sstevel@tonic-gate #else
1550Sstevel@tonic-gate 
1560Sstevel@tonic-gate 	case SYSNEWGRP:
1570Sstevel@tonic-gate 		if (flags & rshflg)
1580Sstevel@tonic-gate 			failed(argv[0], restricted);
1590Sstevel@tonic-gate 		else if (!endjobs(JOB_STOPPED|JOB_RUNNING))
1600Sstevel@tonic-gate 			break;
1610Sstevel@tonic-gate 		else
1620Sstevel@tonic-gate 		{
1630Sstevel@tonic-gate 			flags |= forcexit; /* bad exec will terminate shell */
1640Sstevel@tonic-gate 			oldsigs();
1650Sstevel@tonic-gate 			rmtemp(0);
1660Sstevel@tonic-gate 			rmfunctmp();
1670Sstevel@tonic-gate #ifdef ACCT
1680Sstevel@tonic-gate 			doacct();
1690Sstevel@tonic-gate #endif
1700Sstevel@tonic-gate 			execa(argv, -1);
1710Sstevel@tonic-gate 			done(0);
1720Sstevel@tonic-gate 		}
1730Sstevel@tonic-gate 
1740Sstevel@tonic-gate #endif
1750Sstevel@tonic-gate 
1760Sstevel@tonic-gate 	case SYSCD:
1770Sstevel@tonic-gate 		if (flags & rshflg)
1780Sstevel@tonic-gate 			failed(argv[0], restricted);
1790Sstevel@tonic-gate 		else if ((a1 && *a1) || (a1 == 0 && (a1 = homenod.namval)))
1800Sstevel@tonic-gate 		{
1810Sstevel@tonic-gate 			unsigned char *cdpath;
1820Sstevel@tonic-gate 			unsigned char *dir;
1830Sstevel@tonic-gate 			int f;
1840Sstevel@tonic-gate 
1850Sstevel@tonic-gate 			if ((cdpath = cdpnod.namval) == 0 ||
1860Sstevel@tonic-gate 			     *a1 == '/' ||
1870Sstevel@tonic-gate 			     cf(a1, ".") == 0 ||
1880Sstevel@tonic-gate 			     cf(a1, "..") == 0 ||
1890Sstevel@tonic-gate 			     (*a1 == '.' && (*(a1+1) == '/' || *(a1+1) == '.' && *(a1+2) == '/')))
1900Sstevel@tonic-gate 				cdpath = (unsigned char *)nullstr;
1910Sstevel@tonic-gate 
1920Sstevel@tonic-gate 			do
1930Sstevel@tonic-gate 			{
1940Sstevel@tonic-gate 				dir = cdpath;
1950Sstevel@tonic-gate 				cdpath = catpath(cdpath,a1);
1960Sstevel@tonic-gate 			}
1970Sstevel@tonic-gate 			while ((f = (chdir((const char *) curstak()) < 0)) &&
1980Sstevel@tonic-gate 			    cdpath);
1990Sstevel@tonic-gate 
2000Sstevel@tonic-gate 			if (f) {
2010Sstevel@tonic-gate 				switch(errno) {
2020Sstevel@tonic-gate 						case EMULTIHOP:
2030Sstevel@tonic-gate 							failed(a1, emultihop);
2040Sstevel@tonic-gate 							break;
2050Sstevel@tonic-gate 						case ENOTDIR:
2060Sstevel@tonic-gate 							failed(a1, enotdir);
2070Sstevel@tonic-gate 							break;
2080Sstevel@tonic-gate 						case ENOENT:
2090Sstevel@tonic-gate 							failed(a1, enoent);
2100Sstevel@tonic-gate 							break;
2110Sstevel@tonic-gate 						case EACCES:
2120Sstevel@tonic-gate 							failed(a1, eacces);
2130Sstevel@tonic-gate 							break;
2140Sstevel@tonic-gate 						case ENOLINK:
2150Sstevel@tonic-gate 							failed(a1, enolink);
2160Sstevel@tonic-gate 							break;
2170Sstevel@tonic-gate 						default:
2180Sstevel@tonic-gate 						failed(a1, baddir);
2190Sstevel@tonic-gate 						break;
2200Sstevel@tonic-gate 						}
2210Sstevel@tonic-gate 			}
2220Sstevel@tonic-gate 			else
2230Sstevel@tonic-gate 			{
2240Sstevel@tonic-gate 				cwd(curstak());
2250Sstevel@tonic-gate 				if (cf(nullstr, dir) &&
2260Sstevel@tonic-gate 				    *dir != ':' &&
2270Sstevel@tonic-gate 					any('/', curstak()) &&
2280Sstevel@tonic-gate 					flags & prompt)
2290Sstevel@tonic-gate 				{
2300Sstevel@tonic-gate 					prs_buff(cwdget());
2310Sstevel@tonic-gate 					prc_buff(NL);
2320Sstevel@tonic-gate 				}
2330Sstevel@tonic-gate 			}
2340Sstevel@tonic-gate 			zapcd();
2350Sstevel@tonic-gate 		}
2360Sstevel@tonic-gate 		else
2370Sstevel@tonic-gate 		{
2380Sstevel@tonic-gate 			if (a1)
2390Sstevel@tonic-gate 				error(nulldir);
2400Sstevel@tonic-gate 			else
2410Sstevel@tonic-gate 				error(nohome);
2420Sstevel@tonic-gate 		}
2430Sstevel@tonic-gate 
2440Sstevel@tonic-gate 		break;
2450Sstevel@tonic-gate 
2460Sstevel@tonic-gate 	case SYSSHFT:
2470Sstevel@tonic-gate 		{
2480Sstevel@tonic-gate 			int places;
2490Sstevel@tonic-gate 
2500Sstevel@tonic-gate 			places = a1 ? stoi(a1) : 1;
2510Sstevel@tonic-gate 
2520Sstevel@tonic-gate 			if ((dolc -= places) < 0)
2530Sstevel@tonic-gate 			{
2540Sstevel@tonic-gate 				dolc = 0;
2550Sstevel@tonic-gate 				error(badshift);
2560Sstevel@tonic-gate 			}
2570Sstevel@tonic-gate 			else
2580Sstevel@tonic-gate 				dolv += places;
2590Sstevel@tonic-gate 		}
2600Sstevel@tonic-gate 
2610Sstevel@tonic-gate 		break;
2620Sstevel@tonic-gate 
2630Sstevel@tonic-gate 	case SYSWAIT:
2640Sstevel@tonic-gate 		syswait(argc,argv);
2650Sstevel@tonic-gate 		break;
2660Sstevel@tonic-gate 
2670Sstevel@tonic-gate 	case SYSREAD:
2680Sstevel@tonic-gate 		if(argc < 2)
2690Sstevel@tonic-gate 			failed(argv[0],mssgargn);
2700Sstevel@tonic-gate 		rwait = 1;
2710Sstevel@tonic-gate 		exitval = readvar(&argv[1]);
2720Sstevel@tonic-gate 		rwait = 0;
2730Sstevel@tonic-gate 		break;
2740Sstevel@tonic-gate 
2750Sstevel@tonic-gate 	case SYSSET:
2760Sstevel@tonic-gate 		if (a1)
2770Sstevel@tonic-gate 		{
2780Sstevel@tonic-gate 			int	cnt;
2790Sstevel@tonic-gate 
2800Sstevel@tonic-gate 			cnt = options(argc, argv);
2810Sstevel@tonic-gate 			if (cnt > 1)
2820Sstevel@tonic-gate 				setargs(argv + argc - cnt);
2830Sstevel@tonic-gate 		}
2840Sstevel@tonic-gate 		else if (comptr(t)->comset == 0)
2850Sstevel@tonic-gate 		{
2860Sstevel@tonic-gate 			/*
2870Sstevel@tonic-gate 			 * scan name chain and print
2880Sstevel@tonic-gate 			 */
2890Sstevel@tonic-gate 			namscan(printnam);
2900Sstevel@tonic-gate 		}
2910Sstevel@tonic-gate 		break;
2920Sstevel@tonic-gate 
2930Sstevel@tonic-gate 	case SYSRDONLY:
2940Sstevel@tonic-gate 		exitval = 0;
2950Sstevel@tonic-gate 		if (a1)
2960Sstevel@tonic-gate 		{
2970Sstevel@tonic-gate 			while (*++argv)
2980Sstevel@tonic-gate 				attrib(lookup(*argv), N_RDONLY);
2990Sstevel@tonic-gate 		}
3000Sstevel@tonic-gate 		else
3010Sstevel@tonic-gate 			namscan(printro);
3020Sstevel@tonic-gate 
3030Sstevel@tonic-gate 		break;
3040Sstevel@tonic-gate 
3050Sstevel@tonic-gate 	case SYSXPORT:
3060Sstevel@tonic-gate 		{
3070Sstevel@tonic-gate 			struct namnod 	*n;
3080Sstevel@tonic-gate 
3090Sstevel@tonic-gate 			exitval = 0;
3100Sstevel@tonic-gate 			if (a1)
3110Sstevel@tonic-gate 			{
3120Sstevel@tonic-gate 				while (*++argv)
3130Sstevel@tonic-gate 				{
3140Sstevel@tonic-gate 					n = lookup(*argv);
3150Sstevel@tonic-gate 					if (n->namflg & N_FUNCTN)
3160Sstevel@tonic-gate 						error(badexport);
3170Sstevel@tonic-gate 					else
3180Sstevel@tonic-gate 						attrib(n, N_EXPORT);
3190Sstevel@tonic-gate 				}
3200Sstevel@tonic-gate 			}
3210Sstevel@tonic-gate 			else
3220Sstevel@tonic-gate 				namscan(printexp);
3230Sstevel@tonic-gate 		}
3240Sstevel@tonic-gate 		break;
3250Sstevel@tonic-gate 
3260Sstevel@tonic-gate 	case SYSEVAL:
3270Sstevel@tonic-gate 		if (a1)
3280Sstevel@tonic-gate 			execexp(a1, &argv[2]);
3290Sstevel@tonic-gate 		break;
3300Sstevel@tonic-gate 
3310Sstevel@tonic-gate #ifndef RES
3320Sstevel@tonic-gate 	case SYSULIMIT:
3330Sstevel@tonic-gate 		sysulimit(argc, argv);
3340Sstevel@tonic-gate 		break;
3350Sstevel@tonic-gate 
3360Sstevel@tonic-gate 	case SYSUMASK:
3370Sstevel@tonic-gate 		if (a1)
3380Sstevel@tonic-gate 		{
3390Sstevel@tonic-gate 			int c;
3400Sstevel@tonic-gate 			mode_t i;
3410Sstevel@tonic-gate 
3420Sstevel@tonic-gate 			i = 0;
3430Sstevel@tonic-gate 			while ((c = *a1++) >= '0' && c <= '7')
3440Sstevel@tonic-gate 				i = (i << 3) + c - '0';
3450Sstevel@tonic-gate 			umask(i);
3460Sstevel@tonic-gate 		}
3470Sstevel@tonic-gate 		else
3480Sstevel@tonic-gate 		{
3490Sstevel@tonic-gate 			mode_t i;
3500Sstevel@tonic-gate 			int j;
3510Sstevel@tonic-gate 
3520Sstevel@tonic-gate 			umask(i = umask(0));
3530Sstevel@tonic-gate 			prc_buff('0');
3540Sstevel@tonic-gate 			for (j = 6; j >= 0; j -= 3)
3550Sstevel@tonic-gate 				prc_buff(((i >> j) & 07) +'0');
3560Sstevel@tonic-gate 			prc_buff(NL);
3570Sstevel@tonic-gate 		}
3580Sstevel@tonic-gate 		break;
3590Sstevel@tonic-gate 
3600Sstevel@tonic-gate #endif
3610Sstevel@tonic-gate 
3620Sstevel@tonic-gate 	case SYSTST:
3630Sstevel@tonic-gate 		exitval = test(argc, argv);
3640Sstevel@tonic-gate 		break;
3650Sstevel@tonic-gate 
3660Sstevel@tonic-gate 	case SYSECHO:
3670Sstevel@tonic-gate 		exitval = echo(argc, argv);
3680Sstevel@tonic-gate 		break;
3690Sstevel@tonic-gate 
3700Sstevel@tonic-gate 	case SYSHASH:
3710Sstevel@tonic-gate 		exitval = 0;
3720Sstevel@tonic-gate 
3730Sstevel@tonic-gate 		if (a1)
3740Sstevel@tonic-gate 		{
3750Sstevel@tonic-gate 			if (a1[0] == '-')
3760Sstevel@tonic-gate 			{
3770Sstevel@tonic-gate 				if (a1[1] == 'r')
3780Sstevel@tonic-gate 					zaphash();
3790Sstevel@tonic-gate 				else
3800Sstevel@tonic-gate 					error(badopt);
3810Sstevel@tonic-gate 			}
3820Sstevel@tonic-gate 			else
3830Sstevel@tonic-gate 			{
3840Sstevel@tonic-gate 				while (*++argv)
3850Sstevel@tonic-gate 				{
3860Sstevel@tonic-gate 					if (hashtype(hash_cmd(*argv)) == NOTFOUND)
3870Sstevel@tonic-gate 						failed(*argv, notfound);
3880Sstevel@tonic-gate 				}
3890Sstevel@tonic-gate 			}
3900Sstevel@tonic-gate 		}
3910Sstevel@tonic-gate 		else
3920Sstevel@tonic-gate 			hashpr();
3930Sstevel@tonic-gate 
3940Sstevel@tonic-gate 		break;
3950Sstevel@tonic-gate 
3960Sstevel@tonic-gate 	case SYSPWD:
3970Sstevel@tonic-gate 		{
3980Sstevel@tonic-gate 			exitval = 0;
3990Sstevel@tonic-gate 			cwdprint();
4000Sstevel@tonic-gate 		}
4010Sstevel@tonic-gate 		break;
4020Sstevel@tonic-gate 
4030Sstevel@tonic-gate 	case SYSRETURN:
4040Sstevel@tonic-gate 		if (funcnt == 0)
4050Sstevel@tonic-gate 			error(badreturn);
4060Sstevel@tonic-gate 
4070Sstevel@tonic-gate 		execbrk = 1;
4080Sstevel@tonic-gate 		exitval = (a1 ? stoi(a1) : retval);
4090Sstevel@tonic-gate 		break;
4100Sstevel@tonic-gate 
4110Sstevel@tonic-gate 	case SYSTYPE:
4120Sstevel@tonic-gate 		exitval = 0;
4130Sstevel@tonic-gate 		if (a1)
4140Sstevel@tonic-gate 		{
4150Sstevel@tonic-gate 			/* return success only if all names are found */
4160Sstevel@tonic-gate 			while (*++argv)
4170Sstevel@tonic-gate 				exitval |= what_is_path(*argv);
4180Sstevel@tonic-gate 		}
4190Sstevel@tonic-gate 		break;
4200Sstevel@tonic-gate 
4210Sstevel@tonic-gate 	case SYSUNS:
4220Sstevel@tonic-gate 		exitval = 0;
4230Sstevel@tonic-gate 		if (a1)
4240Sstevel@tonic-gate 		{
4250Sstevel@tonic-gate 			while (*++argv)
4260Sstevel@tonic-gate 				unset_name(*argv);
4270Sstevel@tonic-gate 		}
4280Sstevel@tonic-gate 		break;
4290Sstevel@tonic-gate 
4300Sstevel@tonic-gate 	case SYSGETOPT: {
4310Sstevel@tonic-gate 		int getoptval;
4320Sstevel@tonic-gate 		struct namnod *n;
4330Sstevel@tonic-gate 		extern unsigned char numbuf[];
4340Sstevel@tonic-gate 		unsigned char *varnam = argv[2];
4350Sstevel@tonic-gate 		unsigned char c[2];
4360Sstevel@tonic-gate 		if(argc < 3) {
4370Sstevel@tonic-gate 			failure(argv[0],mssgargn);
4380Sstevel@tonic-gate 			break;
4390Sstevel@tonic-gate 		}
4400Sstevel@tonic-gate 		exitval = 0;
4410Sstevel@tonic-gate 		n = lookup("OPTIND");
4420Sstevel@tonic-gate 		optind = stoi(n->namval);
4430Sstevel@tonic-gate 		if(argc > 3) {
4440Sstevel@tonic-gate 			argv[2] = dolv[0];
4450Sstevel@tonic-gate 			getoptval = getopt(argc-2, (char **)&argv[2], (char *)argv[1]);
4460Sstevel@tonic-gate 		}
4470Sstevel@tonic-gate 		else
4480Sstevel@tonic-gate 			getoptval = getopt(dolc+1, (char **)dolv, (char *)argv[1]);
4490Sstevel@tonic-gate 		if(getoptval == -1) {
4500Sstevel@tonic-gate 			itos(optind);
4510Sstevel@tonic-gate 			assign(n, numbuf);
4520Sstevel@tonic-gate 			n = lookup(varnam);
453527Schin 			assign(n, (unsigned char *)nullstr);
4540Sstevel@tonic-gate 			exitval = 1;
4550Sstevel@tonic-gate 			break;
4560Sstevel@tonic-gate 		}
4570Sstevel@tonic-gate 		argv[2] = varnam;
4580Sstevel@tonic-gate 		itos(optind);
4590Sstevel@tonic-gate 		assign(n, numbuf);
4600Sstevel@tonic-gate 		c[0] = getoptval;
4610Sstevel@tonic-gate 		c[1] = 0;
4620Sstevel@tonic-gate 		n = lookup(varnam);
4630Sstevel@tonic-gate 		assign(n, c);
4640Sstevel@tonic-gate 		n = lookup("OPTARG");
465527Schin 		assign(n, (unsigned char *)optarg);
4660Sstevel@tonic-gate 		}
4670Sstevel@tonic-gate 		break;
4680Sstevel@tonic-gate 
4690Sstevel@tonic-gate 	default:
470*2256Sna195498 		prs_buff(_gettext("unknown builtin\n"));
4710Sstevel@tonic-gate 	}
4720Sstevel@tonic-gate 
4730Sstevel@tonic-gate 
4740Sstevel@tonic-gate 	flushb();
4750Sstevel@tonic-gate 	restore(index);
4760Sstevel@tonic-gate 	chktrap();
4770Sstevel@tonic-gate }
478