xref: /titanic_51/usr/src/cmd/sh/bltin.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
23*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
24*7c478bd9Sstevel@tonic-gate 
25*7c478bd9Sstevel@tonic-gate 
26*7c478bd9Sstevel@tonic-gate /*
27*7c478bd9Sstevel@tonic-gate  * Copyright (c) 1996, by Sun Microsystems, Inc.
28*7c478bd9Sstevel@tonic-gate  * All rights reserved.
29*7c478bd9Sstevel@tonic-gate  */
30*7c478bd9Sstevel@tonic-gate 
31*7c478bd9Sstevel@tonic-gate #ident	"%Z%%M%	%I%	%E% SMI"	/* SVr4.0 1.3.8.1	*/
32*7c478bd9Sstevel@tonic-gate /*
33*7c478bd9Sstevel@tonic-gate  *
34*7c478bd9Sstevel@tonic-gate  * UNIX shell
35*7c478bd9Sstevel@tonic-gate  *
36*7c478bd9Sstevel@tonic-gate  */
37*7c478bd9Sstevel@tonic-gate 
38*7c478bd9Sstevel@tonic-gate 
39*7c478bd9Sstevel@tonic-gate #include	"defs.h"
40*7c478bd9Sstevel@tonic-gate #include	<errno.h>
41*7c478bd9Sstevel@tonic-gate #include	"sym.h"
42*7c478bd9Sstevel@tonic-gate #include	"hash.h"
43*7c478bd9Sstevel@tonic-gate #include	<sys/types.h>
44*7c478bd9Sstevel@tonic-gate #include	<sys/times.h>
45*7c478bd9Sstevel@tonic-gate 
46*7c478bd9Sstevel@tonic-gate builtin(type, argc, argv, t)
47*7c478bd9Sstevel@tonic-gate int type, argc;
48*7c478bd9Sstevel@tonic-gate unsigned char **argv;
49*7c478bd9Sstevel@tonic-gate struct trenod	*t;
50*7c478bd9Sstevel@tonic-gate {
51*7c478bd9Sstevel@tonic-gate 	short index = initio(t->treio, (type != SYSEXEC));
52*7c478bd9Sstevel@tonic-gate 	unsigned char *a1 = argv[1];
53*7c478bd9Sstevel@tonic-gate 
54*7c478bd9Sstevel@tonic-gate 	switch (type)
55*7c478bd9Sstevel@tonic-gate 	{
56*7c478bd9Sstevel@tonic-gate 
57*7c478bd9Sstevel@tonic-gate 	case SYSSUSP:
58*7c478bd9Sstevel@tonic-gate 		syssusp(argc,argv);
59*7c478bd9Sstevel@tonic-gate 		break;
60*7c478bd9Sstevel@tonic-gate 
61*7c478bd9Sstevel@tonic-gate 	case SYSSTOP:
62*7c478bd9Sstevel@tonic-gate 		sysstop(argc,argv);
63*7c478bd9Sstevel@tonic-gate 		break;
64*7c478bd9Sstevel@tonic-gate 
65*7c478bd9Sstevel@tonic-gate 	case SYSKILL:
66*7c478bd9Sstevel@tonic-gate 		syskill(argc,argv);
67*7c478bd9Sstevel@tonic-gate 		break;
68*7c478bd9Sstevel@tonic-gate 
69*7c478bd9Sstevel@tonic-gate 	case SYSFGBG:
70*7c478bd9Sstevel@tonic-gate 		sysfgbg(argc,argv);
71*7c478bd9Sstevel@tonic-gate 		break;
72*7c478bd9Sstevel@tonic-gate 
73*7c478bd9Sstevel@tonic-gate 	case SYSJOBS:
74*7c478bd9Sstevel@tonic-gate 		sysjobs(argc,argv);
75*7c478bd9Sstevel@tonic-gate 		break;
76*7c478bd9Sstevel@tonic-gate 
77*7c478bd9Sstevel@tonic-gate 	case SYSDOT:
78*7c478bd9Sstevel@tonic-gate 		if (a1)
79*7c478bd9Sstevel@tonic-gate 		{
80*7c478bd9Sstevel@tonic-gate 			register int	f;
81*7c478bd9Sstevel@tonic-gate 
82*7c478bd9Sstevel@tonic-gate 			if ((f = pathopen(getpath(a1), a1)) < 0)
83*7c478bd9Sstevel@tonic-gate 				failed(a1, notfound);
84*7c478bd9Sstevel@tonic-gate 			else
85*7c478bd9Sstevel@tonic-gate 				execexp(0, f);
86*7c478bd9Sstevel@tonic-gate 		}
87*7c478bd9Sstevel@tonic-gate 		break;
88*7c478bd9Sstevel@tonic-gate 
89*7c478bd9Sstevel@tonic-gate 	case SYSTIMES:
90*7c478bd9Sstevel@tonic-gate 		{
91*7c478bd9Sstevel@tonic-gate 			struct tms tms;
92*7c478bd9Sstevel@tonic-gate 
93*7c478bd9Sstevel@tonic-gate 			times(&tms);
94*7c478bd9Sstevel@tonic-gate 			prt(tms.tms_cutime);
95*7c478bd9Sstevel@tonic-gate 			prc_buff(SPACE);
96*7c478bd9Sstevel@tonic-gate 			prt(tms.tms_cstime);
97*7c478bd9Sstevel@tonic-gate 			prc_buff(NL);
98*7c478bd9Sstevel@tonic-gate 		}
99*7c478bd9Sstevel@tonic-gate 		break;
100*7c478bd9Sstevel@tonic-gate 
101*7c478bd9Sstevel@tonic-gate 	case SYSEXIT:
102*7c478bd9Sstevel@tonic-gate 		if ( tried_to_exit++ || endjobs(JOB_STOPPED) ){
103*7c478bd9Sstevel@tonic-gate 			flags |= forcexit;	/* force exit */
104*7c478bd9Sstevel@tonic-gate 			exitsh(a1 ? stoi(a1) : retval);
105*7c478bd9Sstevel@tonic-gate 		}
106*7c478bd9Sstevel@tonic-gate 		break;
107*7c478bd9Sstevel@tonic-gate 
108*7c478bd9Sstevel@tonic-gate 	case SYSNULL:
109*7c478bd9Sstevel@tonic-gate 		t->treio = 0;
110*7c478bd9Sstevel@tonic-gate 		break;
111*7c478bd9Sstevel@tonic-gate 
112*7c478bd9Sstevel@tonic-gate 	case SYSCONT:
113*7c478bd9Sstevel@tonic-gate 		if (loopcnt)
114*7c478bd9Sstevel@tonic-gate 		{
115*7c478bd9Sstevel@tonic-gate 			execbrk = breakcnt = 1;
116*7c478bd9Sstevel@tonic-gate 			if (a1)
117*7c478bd9Sstevel@tonic-gate 				breakcnt = stoi(a1);
118*7c478bd9Sstevel@tonic-gate 			if (breakcnt > loopcnt)
119*7c478bd9Sstevel@tonic-gate 				breakcnt = loopcnt;
120*7c478bd9Sstevel@tonic-gate 			else
121*7c478bd9Sstevel@tonic-gate 				breakcnt = -breakcnt;
122*7c478bd9Sstevel@tonic-gate 		}
123*7c478bd9Sstevel@tonic-gate 		break;
124*7c478bd9Sstevel@tonic-gate 
125*7c478bd9Sstevel@tonic-gate 	case SYSBREAK:
126*7c478bd9Sstevel@tonic-gate 		if (loopcnt)
127*7c478bd9Sstevel@tonic-gate 		{
128*7c478bd9Sstevel@tonic-gate 			execbrk = breakcnt = 1;
129*7c478bd9Sstevel@tonic-gate 			if (a1)
130*7c478bd9Sstevel@tonic-gate 				breakcnt = stoi(a1);
131*7c478bd9Sstevel@tonic-gate 			if (breakcnt > loopcnt)
132*7c478bd9Sstevel@tonic-gate 				breakcnt = loopcnt;
133*7c478bd9Sstevel@tonic-gate 		}
134*7c478bd9Sstevel@tonic-gate 		break;
135*7c478bd9Sstevel@tonic-gate 
136*7c478bd9Sstevel@tonic-gate 	case SYSTRAP:
137*7c478bd9Sstevel@tonic-gate 		systrap(argc,argv);
138*7c478bd9Sstevel@tonic-gate 		break;
139*7c478bd9Sstevel@tonic-gate 
140*7c478bd9Sstevel@tonic-gate 	case SYSEXEC:
141*7c478bd9Sstevel@tonic-gate 		argv++;
142*7c478bd9Sstevel@tonic-gate 		ioset = 0;
143*7c478bd9Sstevel@tonic-gate 		if (a1 == 0) {
144*7c478bd9Sstevel@tonic-gate 			setmode(0);
145*7c478bd9Sstevel@tonic-gate 			break;
146*7c478bd9Sstevel@tonic-gate 		}
147*7c478bd9Sstevel@tonic-gate 		/* FALLTHROUGH */
148*7c478bd9Sstevel@tonic-gate 
149*7c478bd9Sstevel@tonic-gate #ifdef RES	/* Research includes login as part of the shell */
150*7c478bd9Sstevel@tonic-gate 
151*7c478bd9Sstevel@tonic-gate 	case SYSLOGIN:
152*7c478bd9Sstevel@tonic-gate 		if (!endjobs(JOB_STOPPED|JOB_RUNNING))
153*7c478bd9Sstevel@tonic-gate 			break;
154*7c478bd9Sstevel@tonic-gate 		oldsigs();
155*7c478bd9Sstevel@tonic-gate 		execa(argv, -1);
156*7c478bd9Sstevel@tonic-gate 		done(0);
157*7c478bd9Sstevel@tonic-gate #else
158*7c478bd9Sstevel@tonic-gate 
159*7c478bd9Sstevel@tonic-gate 	case SYSNEWGRP:
160*7c478bd9Sstevel@tonic-gate 		if (flags & rshflg)
161*7c478bd9Sstevel@tonic-gate 			failed(argv[0], restricted);
162*7c478bd9Sstevel@tonic-gate 		else if (!endjobs(JOB_STOPPED|JOB_RUNNING))
163*7c478bd9Sstevel@tonic-gate 			break;
164*7c478bd9Sstevel@tonic-gate 		else
165*7c478bd9Sstevel@tonic-gate 		{
166*7c478bd9Sstevel@tonic-gate 			flags |= forcexit; /* bad exec will terminate shell */
167*7c478bd9Sstevel@tonic-gate 			oldsigs();
168*7c478bd9Sstevel@tonic-gate 			rmtemp(0);
169*7c478bd9Sstevel@tonic-gate 			rmfunctmp();
170*7c478bd9Sstevel@tonic-gate #ifdef ACCT
171*7c478bd9Sstevel@tonic-gate 			doacct();
172*7c478bd9Sstevel@tonic-gate #endif
173*7c478bd9Sstevel@tonic-gate 			execa(argv, -1);
174*7c478bd9Sstevel@tonic-gate 			done(0);
175*7c478bd9Sstevel@tonic-gate 		}
176*7c478bd9Sstevel@tonic-gate 
177*7c478bd9Sstevel@tonic-gate #endif
178*7c478bd9Sstevel@tonic-gate 
179*7c478bd9Sstevel@tonic-gate 	case SYSCD:
180*7c478bd9Sstevel@tonic-gate 		if (flags & rshflg)
181*7c478bd9Sstevel@tonic-gate 			failed(argv[0], restricted);
182*7c478bd9Sstevel@tonic-gate 		else if ((a1 && *a1) || (a1 == 0 && (a1 = homenod.namval)))
183*7c478bd9Sstevel@tonic-gate 		{
184*7c478bd9Sstevel@tonic-gate 			unsigned char *cdpath;
185*7c478bd9Sstevel@tonic-gate 			unsigned char *dir;
186*7c478bd9Sstevel@tonic-gate 			int f;
187*7c478bd9Sstevel@tonic-gate 
188*7c478bd9Sstevel@tonic-gate 			if ((cdpath = cdpnod.namval) == 0 ||
189*7c478bd9Sstevel@tonic-gate 			     *a1 == '/' ||
190*7c478bd9Sstevel@tonic-gate 			     cf(a1, ".") == 0 ||
191*7c478bd9Sstevel@tonic-gate 			     cf(a1, "..") == 0 ||
192*7c478bd9Sstevel@tonic-gate 			     (*a1 == '.' && (*(a1+1) == '/' || *(a1+1) == '.' && *(a1+2) == '/')))
193*7c478bd9Sstevel@tonic-gate 				cdpath = (unsigned char *)nullstr;
194*7c478bd9Sstevel@tonic-gate 
195*7c478bd9Sstevel@tonic-gate 			do
196*7c478bd9Sstevel@tonic-gate 			{
197*7c478bd9Sstevel@tonic-gate 				dir = cdpath;
198*7c478bd9Sstevel@tonic-gate 				cdpath = catpath(cdpath,a1);
199*7c478bd9Sstevel@tonic-gate 			}
200*7c478bd9Sstevel@tonic-gate 			while ((f = (chdir((const char *) curstak()) < 0)) &&
201*7c478bd9Sstevel@tonic-gate 			    cdpath);
202*7c478bd9Sstevel@tonic-gate 
203*7c478bd9Sstevel@tonic-gate 			if (f) {
204*7c478bd9Sstevel@tonic-gate 				switch(errno) {
205*7c478bd9Sstevel@tonic-gate 						case EMULTIHOP:
206*7c478bd9Sstevel@tonic-gate 							failed(a1, emultihop);
207*7c478bd9Sstevel@tonic-gate 							break;
208*7c478bd9Sstevel@tonic-gate 						case ENOTDIR:
209*7c478bd9Sstevel@tonic-gate 							failed(a1, enotdir);
210*7c478bd9Sstevel@tonic-gate 							break;
211*7c478bd9Sstevel@tonic-gate 						case ENOENT:
212*7c478bd9Sstevel@tonic-gate 							failed(a1, enoent);
213*7c478bd9Sstevel@tonic-gate 							break;
214*7c478bd9Sstevel@tonic-gate 						case EACCES:
215*7c478bd9Sstevel@tonic-gate 							failed(a1, eacces);
216*7c478bd9Sstevel@tonic-gate 							break;
217*7c478bd9Sstevel@tonic-gate 						case ENOLINK:
218*7c478bd9Sstevel@tonic-gate 							failed(a1, enolink);
219*7c478bd9Sstevel@tonic-gate 							break;
220*7c478bd9Sstevel@tonic-gate 						default:
221*7c478bd9Sstevel@tonic-gate 						failed(a1, baddir);
222*7c478bd9Sstevel@tonic-gate 						break;
223*7c478bd9Sstevel@tonic-gate 						}
224*7c478bd9Sstevel@tonic-gate 			}
225*7c478bd9Sstevel@tonic-gate 			else
226*7c478bd9Sstevel@tonic-gate 			{
227*7c478bd9Sstevel@tonic-gate 				cwd(curstak());
228*7c478bd9Sstevel@tonic-gate 				if (cf(nullstr, dir) &&
229*7c478bd9Sstevel@tonic-gate 				    *dir != ':' &&
230*7c478bd9Sstevel@tonic-gate 					any('/', curstak()) &&
231*7c478bd9Sstevel@tonic-gate 					flags & prompt)
232*7c478bd9Sstevel@tonic-gate 				{
233*7c478bd9Sstevel@tonic-gate 					prs_buff(cwdget());
234*7c478bd9Sstevel@tonic-gate 					prc_buff(NL);
235*7c478bd9Sstevel@tonic-gate 				}
236*7c478bd9Sstevel@tonic-gate 			}
237*7c478bd9Sstevel@tonic-gate 			zapcd();
238*7c478bd9Sstevel@tonic-gate 		}
239*7c478bd9Sstevel@tonic-gate 		else
240*7c478bd9Sstevel@tonic-gate 		{
241*7c478bd9Sstevel@tonic-gate 			if (a1)
242*7c478bd9Sstevel@tonic-gate 				error(nulldir);
243*7c478bd9Sstevel@tonic-gate 			else
244*7c478bd9Sstevel@tonic-gate 				error(nohome);
245*7c478bd9Sstevel@tonic-gate 		}
246*7c478bd9Sstevel@tonic-gate 
247*7c478bd9Sstevel@tonic-gate 		break;
248*7c478bd9Sstevel@tonic-gate 
249*7c478bd9Sstevel@tonic-gate 	case SYSSHFT:
250*7c478bd9Sstevel@tonic-gate 		{
251*7c478bd9Sstevel@tonic-gate 			int places;
252*7c478bd9Sstevel@tonic-gate 
253*7c478bd9Sstevel@tonic-gate 			places = a1 ? stoi(a1) : 1;
254*7c478bd9Sstevel@tonic-gate 
255*7c478bd9Sstevel@tonic-gate 			if ((dolc -= places) < 0)
256*7c478bd9Sstevel@tonic-gate 			{
257*7c478bd9Sstevel@tonic-gate 				dolc = 0;
258*7c478bd9Sstevel@tonic-gate 				error(badshift);
259*7c478bd9Sstevel@tonic-gate 			}
260*7c478bd9Sstevel@tonic-gate 			else
261*7c478bd9Sstevel@tonic-gate 				dolv += places;
262*7c478bd9Sstevel@tonic-gate 		}
263*7c478bd9Sstevel@tonic-gate 
264*7c478bd9Sstevel@tonic-gate 		break;
265*7c478bd9Sstevel@tonic-gate 
266*7c478bd9Sstevel@tonic-gate 	case SYSWAIT:
267*7c478bd9Sstevel@tonic-gate 		syswait(argc,argv);
268*7c478bd9Sstevel@tonic-gate 		break;
269*7c478bd9Sstevel@tonic-gate 
270*7c478bd9Sstevel@tonic-gate 	case SYSREAD:
271*7c478bd9Sstevel@tonic-gate 		if(argc < 2)
272*7c478bd9Sstevel@tonic-gate 			failed(argv[0],mssgargn);
273*7c478bd9Sstevel@tonic-gate 		rwait = 1;
274*7c478bd9Sstevel@tonic-gate 		exitval = readvar(&argv[1]);
275*7c478bd9Sstevel@tonic-gate 		rwait = 0;
276*7c478bd9Sstevel@tonic-gate 		break;
277*7c478bd9Sstevel@tonic-gate 
278*7c478bd9Sstevel@tonic-gate 	case SYSSET:
279*7c478bd9Sstevel@tonic-gate 		if (a1)
280*7c478bd9Sstevel@tonic-gate 		{
281*7c478bd9Sstevel@tonic-gate 			int	cnt;
282*7c478bd9Sstevel@tonic-gate 
283*7c478bd9Sstevel@tonic-gate 			cnt = options(argc, argv);
284*7c478bd9Sstevel@tonic-gate 			if (cnt > 1)
285*7c478bd9Sstevel@tonic-gate 				setargs(argv + argc - cnt);
286*7c478bd9Sstevel@tonic-gate 		}
287*7c478bd9Sstevel@tonic-gate 		else if (comptr(t)->comset == 0)
288*7c478bd9Sstevel@tonic-gate 		{
289*7c478bd9Sstevel@tonic-gate 			/*
290*7c478bd9Sstevel@tonic-gate 			 * scan name chain and print
291*7c478bd9Sstevel@tonic-gate 			 */
292*7c478bd9Sstevel@tonic-gate 			namscan(printnam);
293*7c478bd9Sstevel@tonic-gate 		}
294*7c478bd9Sstevel@tonic-gate 		break;
295*7c478bd9Sstevel@tonic-gate 
296*7c478bd9Sstevel@tonic-gate 	case SYSRDONLY:
297*7c478bd9Sstevel@tonic-gate 		exitval = 0;
298*7c478bd9Sstevel@tonic-gate 		if (a1)
299*7c478bd9Sstevel@tonic-gate 		{
300*7c478bd9Sstevel@tonic-gate 			while (*++argv)
301*7c478bd9Sstevel@tonic-gate 				attrib(lookup(*argv), N_RDONLY);
302*7c478bd9Sstevel@tonic-gate 		}
303*7c478bd9Sstevel@tonic-gate 		else
304*7c478bd9Sstevel@tonic-gate 			namscan(printro);
305*7c478bd9Sstevel@tonic-gate 
306*7c478bd9Sstevel@tonic-gate 		break;
307*7c478bd9Sstevel@tonic-gate 
308*7c478bd9Sstevel@tonic-gate 	case SYSXPORT:
309*7c478bd9Sstevel@tonic-gate 		{
310*7c478bd9Sstevel@tonic-gate 			struct namnod 	*n;
311*7c478bd9Sstevel@tonic-gate 
312*7c478bd9Sstevel@tonic-gate 			exitval = 0;
313*7c478bd9Sstevel@tonic-gate 			if (a1)
314*7c478bd9Sstevel@tonic-gate 			{
315*7c478bd9Sstevel@tonic-gate 				while (*++argv)
316*7c478bd9Sstevel@tonic-gate 				{
317*7c478bd9Sstevel@tonic-gate 					n = lookup(*argv);
318*7c478bd9Sstevel@tonic-gate 					if (n->namflg & N_FUNCTN)
319*7c478bd9Sstevel@tonic-gate 						error(badexport);
320*7c478bd9Sstevel@tonic-gate 					else
321*7c478bd9Sstevel@tonic-gate 						attrib(n, N_EXPORT);
322*7c478bd9Sstevel@tonic-gate 				}
323*7c478bd9Sstevel@tonic-gate 			}
324*7c478bd9Sstevel@tonic-gate 			else
325*7c478bd9Sstevel@tonic-gate 				namscan(printexp);
326*7c478bd9Sstevel@tonic-gate 		}
327*7c478bd9Sstevel@tonic-gate 		break;
328*7c478bd9Sstevel@tonic-gate 
329*7c478bd9Sstevel@tonic-gate 	case SYSEVAL:
330*7c478bd9Sstevel@tonic-gate 		if (a1)
331*7c478bd9Sstevel@tonic-gate 			execexp(a1, &argv[2]);
332*7c478bd9Sstevel@tonic-gate 		break;
333*7c478bd9Sstevel@tonic-gate 
334*7c478bd9Sstevel@tonic-gate #ifndef RES
335*7c478bd9Sstevel@tonic-gate 	case SYSULIMIT:
336*7c478bd9Sstevel@tonic-gate 		sysulimit(argc, argv);
337*7c478bd9Sstevel@tonic-gate 		break;
338*7c478bd9Sstevel@tonic-gate 
339*7c478bd9Sstevel@tonic-gate 	case SYSUMASK:
340*7c478bd9Sstevel@tonic-gate 		if (a1)
341*7c478bd9Sstevel@tonic-gate 		{
342*7c478bd9Sstevel@tonic-gate 			int c;
343*7c478bd9Sstevel@tonic-gate 			mode_t i;
344*7c478bd9Sstevel@tonic-gate 
345*7c478bd9Sstevel@tonic-gate 			i = 0;
346*7c478bd9Sstevel@tonic-gate 			while ((c = *a1++) >= '0' && c <= '7')
347*7c478bd9Sstevel@tonic-gate 				i = (i << 3) + c - '0';
348*7c478bd9Sstevel@tonic-gate 			umask(i);
349*7c478bd9Sstevel@tonic-gate 		}
350*7c478bd9Sstevel@tonic-gate 		else
351*7c478bd9Sstevel@tonic-gate 		{
352*7c478bd9Sstevel@tonic-gate 			mode_t i;
353*7c478bd9Sstevel@tonic-gate 			int j;
354*7c478bd9Sstevel@tonic-gate 
355*7c478bd9Sstevel@tonic-gate 			umask(i = umask(0));
356*7c478bd9Sstevel@tonic-gate 			prc_buff('0');
357*7c478bd9Sstevel@tonic-gate 			for (j = 6; j >= 0; j -= 3)
358*7c478bd9Sstevel@tonic-gate 				prc_buff(((i >> j) & 07) +'0');
359*7c478bd9Sstevel@tonic-gate 			prc_buff(NL);
360*7c478bd9Sstevel@tonic-gate 		}
361*7c478bd9Sstevel@tonic-gate 		break;
362*7c478bd9Sstevel@tonic-gate 
363*7c478bd9Sstevel@tonic-gate #endif
364*7c478bd9Sstevel@tonic-gate 
365*7c478bd9Sstevel@tonic-gate 	case SYSTST:
366*7c478bd9Sstevel@tonic-gate 		exitval = test(argc, argv);
367*7c478bd9Sstevel@tonic-gate 		break;
368*7c478bd9Sstevel@tonic-gate 
369*7c478bd9Sstevel@tonic-gate 	case SYSECHO:
370*7c478bd9Sstevel@tonic-gate 		exitval = echo(argc, argv);
371*7c478bd9Sstevel@tonic-gate 		break;
372*7c478bd9Sstevel@tonic-gate 
373*7c478bd9Sstevel@tonic-gate 	case SYSHASH:
374*7c478bd9Sstevel@tonic-gate 		exitval = 0;
375*7c478bd9Sstevel@tonic-gate 
376*7c478bd9Sstevel@tonic-gate 		if (a1)
377*7c478bd9Sstevel@tonic-gate 		{
378*7c478bd9Sstevel@tonic-gate 			if (a1[0] == '-')
379*7c478bd9Sstevel@tonic-gate 			{
380*7c478bd9Sstevel@tonic-gate 				if (a1[1] == 'r')
381*7c478bd9Sstevel@tonic-gate 					zaphash();
382*7c478bd9Sstevel@tonic-gate 				else
383*7c478bd9Sstevel@tonic-gate 					error(badopt);
384*7c478bd9Sstevel@tonic-gate 			}
385*7c478bd9Sstevel@tonic-gate 			else
386*7c478bd9Sstevel@tonic-gate 			{
387*7c478bd9Sstevel@tonic-gate 				while (*++argv)
388*7c478bd9Sstevel@tonic-gate 				{
389*7c478bd9Sstevel@tonic-gate 					if (hashtype(hash_cmd(*argv)) == NOTFOUND)
390*7c478bd9Sstevel@tonic-gate 						failed(*argv, notfound);
391*7c478bd9Sstevel@tonic-gate 				}
392*7c478bd9Sstevel@tonic-gate 			}
393*7c478bd9Sstevel@tonic-gate 		}
394*7c478bd9Sstevel@tonic-gate 		else
395*7c478bd9Sstevel@tonic-gate 			hashpr();
396*7c478bd9Sstevel@tonic-gate 
397*7c478bd9Sstevel@tonic-gate 		break;
398*7c478bd9Sstevel@tonic-gate 
399*7c478bd9Sstevel@tonic-gate 	case SYSPWD:
400*7c478bd9Sstevel@tonic-gate 		{
401*7c478bd9Sstevel@tonic-gate 			exitval = 0;
402*7c478bd9Sstevel@tonic-gate 			cwdprint();
403*7c478bd9Sstevel@tonic-gate 		}
404*7c478bd9Sstevel@tonic-gate 		break;
405*7c478bd9Sstevel@tonic-gate 
406*7c478bd9Sstevel@tonic-gate 	case SYSRETURN:
407*7c478bd9Sstevel@tonic-gate 		if (funcnt == 0)
408*7c478bd9Sstevel@tonic-gate 			error(badreturn);
409*7c478bd9Sstevel@tonic-gate 
410*7c478bd9Sstevel@tonic-gate 		execbrk = 1;
411*7c478bd9Sstevel@tonic-gate 		exitval = (a1 ? stoi(a1) : retval);
412*7c478bd9Sstevel@tonic-gate 		break;
413*7c478bd9Sstevel@tonic-gate 
414*7c478bd9Sstevel@tonic-gate 	case SYSTYPE:
415*7c478bd9Sstevel@tonic-gate 		exitval = 0;
416*7c478bd9Sstevel@tonic-gate 		if (a1)
417*7c478bd9Sstevel@tonic-gate 		{
418*7c478bd9Sstevel@tonic-gate 			/* return success only if all names are found */
419*7c478bd9Sstevel@tonic-gate 			while (*++argv)
420*7c478bd9Sstevel@tonic-gate 				exitval |= what_is_path(*argv);
421*7c478bd9Sstevel@tonic-gate 		}
422*7c478bd9Sstevel@tonic-gate 		break;
423*7c478bd9Sstevel@tonic-gate 
424*7c478bd9Sstevel@tonic-gate 	case SYSUNS:
425*7c478bd9Sstevel@tonic-gate 		exitval = 0;
426*7c478bd9Sstevel@tonic-gate 		if (a1)
427*7c478bd9Sstevel@tonic-gate 		{
428*7c478bd9Sstevel@tonic-gate 			while (*++argv)
429*7c478bd9Sstevel@tonic-gate 				unset_name(*argv);
430*7c478bd9Sstevel@tonic-gate 		}
431*7c478bd9Sstevel@tonic-gate 		break;
432*7c478bd9Sstevel@tonic-gate 
433*7c478bd9Sstevel@tonic-gate 	case SYSGETOPT: {
434*7c478bd9Sstevel@tonic-gate 		int getoptval;
435*7c478bd9Sstevel@tonic-gate 		struct namnod *n;
436*7c478bd9Sstevel@tonic-gate 		extern unsigned char numbuf[];
437*7c478bd9Sstevel@tonic-gate 		unsigned char *varnam = argv[2];
438*7c478bd9Sstevel@tonic-gate 		unsigned char c[2];
439*7c478bd9Sstevel@tonic-gate 		if(argc < 3) {
440*7c478bd9Sstevel@tonic-gate 			failure(argv[0],mssgargn);
441*7c478bd9Sstevel@tonic-gate 			break;
442*7c478bd9Sstevel@tonic-gate 		}
443*7c478bd9Sstevel@tonic-gate 		exitval = 0;
444*7c478bd9Sstevel@tonic-gate 		n = lookup("OPTIND");
445*7c478bd9Sstevel@tonic-gate 		optind = stoi(n->namval);
446*7c478bd9Sstevel@tonic-gate 		if(argc > 3) {
447*7c478bd9Sstevel@tonic-gate 			argv[2] = dolv[0];
448*7c478bd9Sstevel@tonic-gate 			getoptval = getopt(argc-2, (char **)&argv[2], (char *)argv[1]);
449*7c478bd9Sstevel@tonic-gate 		}
450*7c478bd9Sstevel@tonic-gate 		else
451*7c478bd9Sstevel@tonic-gate 			getoptval = getopt(dolc+1, (char **)dolv, (char *)argv[1]);
452*7c478bd9Sstevel@tonic-gate 		if(getoptval == -1) {
453*7c478bd9Sstevel@tonic-gate 			itos(optind);
454*7c478bd9Sstevel@tonic-gate 			assign(n, numbuf);
455*7c478bd9Sstevel@tonic-gate 			n = lookup(varnam);
456*7c478bd9Sstevel@tonic-gate 			assign(n, nullstr);
457*7c478bd9Sstevel@tonic-gate 			exitval = 1;
458*7c478bd9Sstevel@tonic-gate 			break;
459*7c478bd9Sstevel@tonic-gate 		}
460*7c478bd9Sstevel@tonic-gate 		argv[2] = varnam;
461*7c478bd9Sstevel@tonic-gate 		itos(optind);
462*7c478bd9Sstevel@tonic-gate 		assign(n, numbuf);
463*7c478bd9Sstevel@tonic-gate 		c[0] = getoptval;
464*7c478bd9Sstevel@tonic-gate 		c[1] = 0;
465*7c478bd9Sstevel@tonic-gate 		n = lookup(varnam);
466*7c478bd9Sstevel@tonic-gate 		assign(n, c);
467*7c478bd9Sstevel@tonic-gate 		n = lookup("OPTARG");
468*7c478bd9Sstevel@tonic-gate 		assign(n, optarg);
469*7c478bd9Sstevel@tonic-gate 		}
470*7c478bd9Sstevel@tonic-gate 		break;
471*7c478bd9Sstevel@tonic-gate 
472*7c478bd9Sstevel@tonic-gate 	default:
473*7c478bd9Sstevel@tonic-gate 		prs_buff("unknown builtin\n");
474*7c478bd9Sstevel@tonic-gate 	}
475*7c478bd9Sstevel@tonic-gate 
476*7c478bd9Sstevel@tonic-gate 
477*7c478bd9Sstevel@tonic-gate 	flushb();
478*7c478bd9Sstevel@tonic-gate 	restore(index);
479*7c478bd9Sstevel@tonic-gate 	chktrap();
480*7c478bd9Sstevel@tonic-gate }
481