xref: /onnv-gate/usr/src/cmd/sh/hashserv.c (revision 0)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
23*0Sstevel@tonic-gate /*	  All Rights Reserved  	*/
24*0Sstevel@tonic-gate 
25*0Sstevel@tonic-gate 
26*0Sstevel@tonic-gate /*      Portions Copyright(c) 1988, Sun Microsystems, Inc.      */
27*0Sstevel@tonic-gate /*      All Rights Reserved.                                    */
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate #ident	"%Z%%M%	%I%	%E% SMI"	/* SVr4.0 1.10.5.1	*/
30*0Sstevel@tonic-gate /*
31*0Sstevel@tonic-gate  *	UNIX shell
32*0Sstevel@tonic-gate  */
33*0Sstevel@tonic-gate 
34*0Sstevel@tonic-gate #include	"hash.h"
35*0Sstevel@tonic-gate #include	"defs.h"
36*0Sstevel@tonic-gate #include	<sys/types.h>
37*0Sstevel@tonic-gate #include	<sys/stat.h>
38*0Sstevel@tonic-gate #include	<errno.h>
39*0Sstevel@tonic-gate 
40*0Sstevel@tonic-gate #define		EXECUTE		01
41*0Sstevel@tonic-gate 
42*0Sstevel@tonic-gate static unsigned char	cost;
43*0Sstevel@tonic-gate static int	dotpath;
44*0Sstevel@tonic-gate static int	multrel;
45*0Sstevel@tonic-gate static struct entry	relcmd;
46*0Sstevel@tonic-gate 
47*0Sstevel@tonic-gate static int	argpath();
48*0Sstevel@tonic-gate 
49*0Sstevel@tonic-gate short
50*0Sstevel@tonic-gate pathlook(com, flg, arg)
51*0Sstevel@tonic-gate 	unsigned char	*com;
52*0Sstevel@tonic-gate 	int		flg;
53*0Sstevel@tonic-gate 	register struct argnod	*arg;
54*0Sstevel@tonic-gate {
55*0Sstevel@tonic-gate 	register unsigned char	*name = com;
56*0Sstevel@tonic-gate 	register ENTRY	*h;
57*0Sstevel@tonic-gate 
58*0Sstevel@tonic-gate 	ENTRY		hentry;
59*0Sstevel@tonic-gate 	int		count = 0;
60*0Sstevel@tonic-gate 	int		i;
61*0Sstevel@tonic-gate 	int		pathset = 0;
62*0Sstevel@tonic-gate 	int		oldpath = 0;
63*0Sstevel@tonic-gate 	struct namnod	*n;
64*0Sstevel@tonic-gate 
65*0Sstevel@tonic-gate 
66*0Sstevel@tonic-gate 
67*0Sstevel@tonic-gate 	hentry.data = 0;
68*0Sstevel@tonic-gate 
69*0Sstevel@tonic-gate 	if (any('/', name))
70*0Sstevel@tonic-gate 		return(COMMAND);
71*0Sstevel@tonic-gate 
72*0Sstevel@tonic-gate 	h = hfind(name);
73*0Sstevel@tonic-gate 
74*0Sstevel@tonic-gate 
75*0Sstevel@tonic-gate 	if (h)
76*0Sstevel@tonic-gate 	{
77*0Sstevel@tonic-gate 		if (h->data & (BUILTIN | FUNCTION))
78*0Sstevel@tonic-gate 		{
79*0Sstevel@tonic-gate 			if (flg)
80*0Sstevel@tonic-gate 				h->hits++;
81*0Sstevel@tonic-gate 			return(h->data);
82*0Sstevel@tonic-gate 		}
83*0Sstevel@tonic-gate 
84*0Sstevel@tonic-gate 		if (arg && (pathset = argpath(arg)))
85*0Sstevel@tonic-gate 			return(PATH_COMMAND);
86*0Sstevel@tonic-gate 
87*0Sstevel@tonic-gate 		if ((h->data & DOT_COMMAND) == DOT_COMMAND)
88*0Sstevel@tonic-gate 		{
89*0Sstevel@tonic-gate 			if (multrel == 0 && hashdata(h->data) > dotpath)
90*0Sstevel@tonic-gate 				oldpath = hashdata(h->data);
91*0Sstevel@tonic-gate 			else
92*0Sstevel@tonic-gate 				oldpath = dotpath;
93*0Sstevel@tonic-gate 
94*0Sstevel@tonic-gate 			h->data = 0;
95*0Sstevel@tonic-gate 			goto pathsrch;
96*0Sstevel@tonic-gate 		}
97*0Sstevel@tonic-gate 
98*0Sstevel@tonic-gate 		if (h->data & (COMMAND | REL_COMMAND))
99*0Sstevel@tonic-gate 		{
100*0Sstevel@tonic-gate 			if (flg)
101*0Sstevel@tonic-gate 				h->hits++;
102*0Sstevel@tonic-gate 			return(h->data);
103*0Sstevel@tonic-gate 		}
104*0Sstevel@tonic-gate 
105*0Sstevel@tonic-gate 		h->data = 0;
106*0Sstevel@tonic-gate 		h->cost = 0;
107*0Sstevel@tonic-gate 	}
108*0Sstevel@tonic-gate 
109*0Sstevel@tonic-gate 	if (i = syslook(name, commands, no_commands))
110*0Sstevel@tonic-gate 	{
111*0Sstevel@tonic-gate 		hentry.data = (BUILTIN | i);
112*0Sstevel@tonic-gate 		count = 1;
113*0Sstevel@tonic-gate 	}
114*0Sstevel@tonic-gate 	else
115*0Sstevel@tonic-gate 	{
116*0Sstevel@tonic-gate 		if (arg && (pathset = argpath(arg)))
117*0Sstevel@tonic-gate 			return(PATH_COMMAND);
118*0Sstevel@tonic-gate pathsrch:
119*0Sstevel@tonic-gate 			count = findpath(name, oldpath);
120*0Sstevel@tonic-gate 	}
121*0Sstevel@tonic-gate 
122*0Sstevel@tonic-gate 	if (count > 0)
123*0Sstevel@tonic-gate 	{
124*0Sstevel@tonic-gate 		if (h == 0)
125*0Sstevel@tonic-gate 		{
126*0Sstevel@tonic-gate 			hentry.cost = 0;
127*0Sstevel@tonic-gate 			hentry.key = make(name);
128*0Sstevel@tonic-gate 			h = henter(hentry);
129*0Sstevel@tonic-gate 		}
130*0Sstevel@tonic-gate 
131*0Sstevel@tonic-gate 		if (h->data == 0)
132*0Sstevel@tonic-gate 		{
133*0Sstevel@tonic-gate 			if (count < dotpath)
134*0Sstevel@tonic-gate 				h->data = COMMAND | count;
135*0Sstevel@tonic-gate 			else
136*0Sstevel@tonic-gate 			{
137*0Sstevel@tonic-gate 				h->data = REL_COMMAND | count;
138*0Sstevel@tonic-gate 				h->next = relcmd.next;
139*0Sstevel@tonic-gate 				relcmd.next = h;
140*0Sstevel@tonic-gate 			}
141*0Sstevel@tonic-gate 		}
142*0Sstevel@tonic-gate 
143*0Sstevel@tonic-gate 
144*0Sstevel@tonic-gate 		h->hits = flg;
145*0Sstevel@tonic-gate 		h->cost += cost;
146*0Sstevel@tonic-gate 		return(h->data);
147*0Sstevel@tonic-gate 	}
148*0Sstevel@tonic-gate 	else
149*0Sstevel@tonic-gate 	{
150*0Sstevel@tonic-gate 		return(-count);
151*0Sstevel@tonic-gate 	}
152*0Sstevel@tonic-gate }
153*0Sstevel@tonic-gate 
154*0Sstevel@tonic-gate 
155*0Sstevel@tonic-gate static void
156*0Sstevel@tonic-gate zapentry(h)
157*0Sstevel@tonic-gate 	ENTRY *h;
158*0Sstevel@tonic-gate {
159*0Sstevel@tonic-gate 	h->data &= HASHZAP;
160*0Sstevel@tonic-gate }
161*0Sstevel@tonic-gate 
162*0Sstevel@tonic-gate void
163*0Sstevel@tonic-gate zaphash()
164*0Sstevel@tonic-gate {
165*0Sstevel@tonic-gate 	hscan(zapentry);
166*0Sstevel@tonic-gate 	relcmd.next = 0;
167*0Sstevel@tonic-gate }
168*0Sstevel@tonic-gate 
169*0Sstevel@tonic-gate void
170*0Sstevel@tonic-gate zapcd()
171*0Sstevel@tonic-gate {
172*0Sstevel@tonic-gate 	ENTRY *ptr = relcmd.next;
173*0Sstevel@tonic-gate 
174*0Sstevel@tonic-gate 	while (ptr)
175*0Sstevel@tonic-gate 	{
176*0Sstevel@tonic-gate 		ptr->data |= CDMARK;
177*0Sstevel@tonic-gate 		ptr = ptr->next;
178*0Sstevel@tonic-gate 	}
179*0Sstevel@tonic-gate 	relcmd.next = 0;
180*0Sstevel@tonic-gate }
181*0Sstevel@tonic-gate 
182*0Sstevel@tonic-gate 
183*0Sstevel@tonic-gate static void
184*0Sstevel@tonic-gate hashout(h)
185*0Sstevel@tonic-gate 	ENTRY *h;
186*0Sstevel@tonic-gate {
187*0Sstevel@tonic-gate 	sigchk();
188*0Sstevel@tonic-gate 
189*0Sstevel@tonic-gate 	if (hashtype(h->data) == NOTFOUND)
190*0Sstevel@tonic-gate 		return;
191*0Sstevel@tonic-gate 
192*0Sstevel@tonic-gate 	if (h->data & (BUILTIN | FUNCTION))
193*0Sstevel@tonic-gate 		return;
194*0Sstevel@tonic-gate 
195*0Sstevel@tonic-gate 	prn_buff(h->hits);
196*0Sstevel@tonic-gate 
197*0Sstevel@tonic-gate 	if (h->data & REL_COMMAND)
198*0Sstevel@tonic-gate 		prc_buff('*');
199*0Sstevel@tonic-gate 
200*0Sstevel@tonic-gate 
201*0Sstevel@tonic-gate 	prc_buff(TAB);
202*0Sstevel@tonic-gate 	prn_buff(h->cost);
203*0Sstevel@tonic-gate 	prc_buff(TAB);
204*0Sstevel@tonic-gate 
205*0Sstevel@tonic-gate 	pr_path(h->key, hashdata(h->data));
206*0Sstevel@tonic-gate 	prc_buff(NL);
207*0Sstevel@tonic-gate }
208*0Sstevel@tonic-gate 
209*0Sstevel@tonic-gate void
210*0Sstevel@tonic-gate hashpr()
211*0Sstevel@tonic-gate {
212*0Sstevel@tonic-gate 	prs_buff("hits	cost	command\n");
213*0Sstevel@tonic-gate 	hscan(hashout);
214*0Sstevel@tonic-gate }
215*0Sstevel@tonic-gate 
216*0Sstevel@tonic-gate 
217*0Sstevel@tonic-gate set_dotpath()
218*0Sstevel@tonic-gate {
219*0Sstevel@tonic-gate 	register unsigned char	*path;
220*0Sstevel@tonic-gate 	register int	cnt = 1;
221*0Sstevel@tonic-gate 
222*0Sstevel@tonic-gate 	dotpath = 10000;
223*0Sstevel@tonic-gate 	path = getpath("");
224*0Sstevel@tonic-gate 
225*0Sstevel@tonic-gate 	while (path && *path)
226*0Sstevel@tonic-gate 	{
227*0Sstevel@tonic-gate 		if (*path == '/')
228*0Sstevel@tonic-gate 			cnt++;
229*0Sstevel@tonic-gate 		else
230*0Sstevel@tonic-gate 		{
231*0Sstevel@tonic-gate 			if (dotpath == 10000)
232*0Sstevel@tonic-gate 				dotpath = cnt;
233*0Sstevel@tonic-gate 			else
234*0Sstevel@tonic-gate 			{
235*0Sstevel@tonic-gate 				multrel = 1;
236*0Sstevel@tonic-gate 				return;
237*0Sstevel@tonic-gate 			}
238*0Sstevel@tonic-gate 		}
239*0Sstevel@tonic-gate 
240*0Sstevel@tonic-gate 		path = nextpath(path);
241*0Sstevel@tonic-gate 	}
242*0Sstevel@tonic-gate 
243*0Sstevel@tonic-gate 	multrel = 0;
244*0Sstevel@tonic-gate }
245*0Sstevel@tonic-gate 
246*0Sstevel@tonic-gate 
247*0Sstevel@tonic-gate hash_func(name)
248*0Sstevel@tonic-gate 	unsigned char *name;
249*0Sstevel@tonic-gate {
250*0Sstevel@tonic-gate 	ENTRY	*h;
251*0Sstevel@tonic-gate 	ENTRY	hentry;
252*0Sstevel@tonic-gate 
253*0Sstevel@tonic-gate 	h = hfind(name);
254*0Sstevel@tonic-gate 
255*0Sstevel@tonic-gate 	if (h)
256*0Sstevel@tonic-gate 		h->data = FUNCTION;
257*0Sstevel@tonic-gate 	else
258*0Sstevel@tonic-gate 	{
259*0Sstevel@tonic-gate 		hentry.data = FUNCTION;
260*0Sstevel@tonic-gate 		hentry.key = make(name);
261*0Sstevel@tonic-gate 		hentry.cost = 0;
262*0Sstevel@tonic-gate 		hentry.hits = 0;
263*0Sstevel@tonic-gate 		henter(hentry);
264*0Sstevel@tonic-gate 	}
265*0Sstevel@tonic-gate }
266*0Sstevel@tonic-gate 
267*0Sstevel@tonic-gate func_unhash(name)
268*0Sstevel@tonic-gate 	unsigned char *name;
269*0Sstevel@tonic-gate {
270*0Sstevel@tonic-gate 	ENTRY 	*h;
271*0Sstevel@tonic-gate 	int i;
272*0Sstevel@tonic-gate 
273*0Sstevel@tonic-gate 	h = hfind(name);
274*0Sstevel@tonic-gate 
275*0Sstevel@tonic-gate 	if (h && (h->data & FUNCTION)) {
276*0Sstevel@tonic-gate 		if(i = syslook(name, commands, no_commands))
277*0Sstevel@tonic-gate 			h->data = (BUILTIN|i);
278*0Sstevel@tonic-gate 		else
279*0Sstevel@tonic-gate 			h->data = NOTFOUND;
280*0Sstevel@tonic-gate 	}
281*0Sstevel@tonic-gate }
282*0Sstevel@tonic-gate 
283*0Sstevel@tonic-gate 
284*0Sstevel@tonic-gate short
285*0Sstevel@tonic-gate hash_cmd(name)
286*0Sstevel@tonic-gate 	unsigned char *name;
287*0Sstevel@tonic-gate {
288*0Sstevel@tonic-gate 	ENTRY	*h;
289*0Sstevel@tonic-gate 
290*0Sstevel@tonic-gate 	if (any('/', name))
291*0Sstevel@tonic-gate 		return(COMMAND);
292*0Sstevel@tonic-gate 
293*0Sstevel@tonic-gate 	h = hfind(name);
294*0Sstevel@tonic-gate 
295*0Sstevel@tonic-gate 	if (h)
296*0Sstevel@tonic-gate 	{
297*0Sstevel@tonic-gate 		if (h->data & (BUILTIN | FUNCTION))
298*0Sstevel@tonic-gate 			return(h->data);
299*0Sstevel@tonic-gate 		else if ((h->data & REL_COMMAND) == REL_COMMAND)
300*0Sstevel@tonic-gate 		{ /* unlink h from relative command list */
301*0Sstevel@tonic-gate 			ENTRY *ptr = &relcmd;
302*0Sstevel@tonic-gate 			while(ptr-> next != h)
303*0Sstevel@tonic-gate 				ptr = ptr->next;
304*0Sstevel@tonic-gate 			ptr->next = h->next;
305*0Sstevel@tonic-gate 		}
306*0Sstevel@tonic-gate 		zapentry(h);
307*0Sstevel@tonic-gate 	}
308*0Sstevel@tonic-gate 
309*0Sstevel@tonic-gate 	return(pathlook(name, 0, 0));
310*0Sstevel@tonic-gate }
311*0Sstevel@tonic-gate 
312*0Sstevel@tonic-gate 
313*0Sstevel@tonic-gate /*
314*0Sstevel@tonic-gate  * Return 0 if found, 1 if not.
315*0Sstevel@tonic-gate  */
316*0Sstevel@tonic-gate what_is_path(name)
317*0Sstevel@tonic-gate 	register unsigned char *name;
318*0Sstevel@tonic-gate {
319*0Sstevel@tonic-gate 	register ENTRY	*h;
320*0Sstevel@tonic-gate 	int		cnt;
321*0Sstevel@tonic-gate 	short	hashval;
322*0Sstevel@tonic-gate 
323*0Sstevel@tonic-gate 	h = hfind(name);
324*0Sstevel@tonic-gate 
325*0Sstevel@tonic-gate 	prs_buff(name);
326*0Sstevel@tonic-gate 	if (h)
327*0Sstevel@tonic-gate 	{
328*0Sstevel@tonic-gate 		hashval = hashdata(h->data);
329*0Sstevel@tonic-gate 
330*0Sstevel@tonic-gate 		switch (hashtype(h->data))
331*0Sstevel@tonic-gate 		{
332*0Sstevel@tonic-gate 			case BUILTIN:
333*0Sstevel@tonic-gate 				prs_buff(" is a shell builtin\n");
334*0Sstevel@tonic-gate 				return (0);
335*0Sstevel@tonic-gate 
336*0Sstevel@tonic-gate 			case FUNCTION:
337*0Sstevel@tonic-gate 			{
338*0Sstevel@tonic-gate 				struct namnod *n = lookup(name);
339*0Sstevel@tonic-gate 
340*0Sstevel@tonic-gate 				prs_buff(" is a function\n");
341*0Sstevel@tonic-gate 				prs_buff(name);
342*0Sstevel@tonic-gate 				prs_buff("(){\n");
343*0Sstevel@tonic-gate 				prf(n->namenv);
344*0Sstevel@tonic-gate 				prs_buff("\n}\n");
345*0Sstevel@tonic-gate 				return (0);
346*0Sstevel@tonic-gate 			}
347*0Sstevel@tonic-gate 
348*0Sstevel@tonic-gate 			case REL_COMMAND:
349*0Sstevel@tonic-gate 			{
350*0Sstevel@tonic-gate 				short hash;
351*0Sstevel@tonic-gate 
352*0Sstevel@tonic-gate 				if ((h->data & DOT_COMMAND) == DOT_COMMAND)
353*0Sstevel@tonic-gate 				{
354*0Sstevel@tonic-gate 					hash = pathlook(name, 0, 0);
355*0Sstevel@tonic-gate 					if (hashtype(hash) == NOTFOUND)
356*0Sstevel@tonic-gate 					{
357*0Sstevel@tonic-gate 						prs_buff(" not found\n");
358*0Sstevel@tonic-gate 						return (1);
359*0Sstevel@tonic-gate 					}
360*0Sstevel@tonic-gate 					else
361*0Sstevel@tonic-gate 						hashval = hashdata(hash);
362*0Sstevel@tonic-gate 				}
363*0Sstevel@tonic-gate 			}
364*0Sstevel@tonic-gate 
365*0Sstevel@tonic-gate 			case COMMAND:
366*0Sstevel@tonic-gate 				prs_buff(" is hashed (");
367*0Sstevel@tonic-gate 				pr_path(name, hashval);
368*0Sstevel@tonic-gate 				prs_buff(")\n");
369*0Sstevel@tonic-gate 				return (0);
370*0Sstevel@tonic-gate 		}
371*0Sstevel@tonic-gate 	}
372*0Sstevel@tonic-gate 
373*0Sstevel@tonic-gate 	if (syslook(name, commands, no_commands))
374*0Sstevel@tonic-gate 	{
375*0Sstevel@tonic-gate 		prs_buff(" is a shell builtin\n");
376*0Sstevel@tonic-gate 		return (0);
377*0Sstevel@tonic-gate 	}
378*0Sstevel@tonic-gate 
379*0Sstevel@tonic-gate 	if ((cnt = findpath(name, 0)) > 0)
380*0Sstevel@tonic-gate 	{
381*0Sstevel@tonic-gate 		prs_buff(" is ");
382*0Sstevel@tonic-gate 		pr_path(name, cnt);
383*0Sstevel@tonic-gate 		prc_buff(NL);
384*0Sstevel@tonic-gate 		return (0);
385*0Sstevel@tonic-gate 	}
386*0Sstevel@tonic-gate 	else
387*0Sstevel@tonic-gate 	{
388*0Sstevel@tonic-gate 		prs_buff(" not found\n");
389*0Sstevel@tonic-gate 		return (1);
390*0Sstevel@tonic-gate 	}
391*0Sstevel@tonic-gate }
392*0Sstevel@tonic-gate 
393*0Sstevel@tonic-gate 
394*0Sstevel@tonic-gate findpath(name, oldpath)
395*0Sstevel@tonic-gate 	register unsigned char *name;
396*0Sstevel@tonic-gate 	int oldpath;
397*0Sstevel@tonic-gate {
398*0Sstevel@tonic-gate 	register unsigned char 	*path;
399*0Sstevel@tonic-gate 	register int	count = 1;
400*0Sstevel@tonic-gate 
401*0Sstevel@tonic-gate 	unsigned char	*p;
402*0Sstevel@tonic-gate 	int	ok = 1;
403*0Sstevel@tonic-gate 	int 	e_code = 1;
404*0Sstevel@tonic-gate 
405*0Sstevel@tonic-gate 	cost = 0;
406*0Sstevel@tonic-gate 	path = getpath(name);
407*0Sstevel@tonic-gate 
408*0Sstevel@tonic-gate 	if (oldpath)
409*0Sstevel@tonic-gate 	{
410*0Sstevel@tonic-gate 		count = dotpath;
411*0Sstevel@tonic-gate 		while (--count)
412*0Sstevel@tonic-gate 			path = nextpath(path);
413*0Sstevel@tonic-gate 
414*0Sstevel@tonic-gate 		if (oldpath > dotpath)
415*0Sstevel@tonic-gate 		{
416*0Sstevel@tonic-gate 			catpath(path, name);
417*0Sstevel@tonic-gate 			p = curstak();
418*0Sstevel@tonic-gate 			cost = 1;
419*0Sstevel@tonic-gate 
420*0Sstevel@tonic-gate 			if ((ok = chk_access(p, S_IEXEC, 1)) == 0)
421*0Sstevel@tonic-gate 				return(dotpath);
422*0Sstevel@tonic-gate 			else
423*0Sstevel@tonic-gate 				return(oldpath);
424*0Sstevel@tonic-gate 		}
425*0Sstevel@tonic-gate 		else
426*0Sstevel@tonic-gate 			count = dotpath;
427*0Sstevel@tonic-gate 	}
428*0Sstevel@tonic-gate 
429*0Sstevel@tonic-gate 	while (path)
430*0Sstevel@tonic-gate 	{
431*0Sstevel@tonic-gate 		path = catpath(path, name);
432*0Sstevel@tonic-gate 		cost++;
433*0Sstevel@tonic-gate 		p = curstak();
434*0Sstevel@tonic-gate 
435*0Sstevel@tonic-gate 		if ((ok = chk_access(p, S_IEXEC, 1)) == 0)
436*0Sstevel@tonic-gate 			break;
437*0Sstevel@tonic-gate 		else
438*0Sstevel@tonic-gate 			e_code = max(e_code, ok);
439*0Sstevel@tonic-gate 
440*0Sstevel@tonic-gate 		count++;
441*0Sstevel@tonic-gate 	}
442*0Sstevel@tonic-gate 
443*0Sstevel@tonic-gate 	return(ok ? -e_code : count);
444*0Sstevel@tonic-gate }
445*0Sstevel@tonic-gate 
446*0Sstevel@tonic-gate /*
447*0Sstevel@tonic-gate  * Determine if file given by name is accessible with permissions
448*0Sstevel@tonic-gate  * given by mode.
449*0Sstevel@tonic-gate  * Regflag argument non-zero means not to consider
450*0Sstevel@tonic-gate  * a non-regular file as executable.
451*0Sstevel@tonic-gate  */
452*0Sstevel@tonic-gate 
453*0Sstevel@tonic-gate chk_access(name, mode, regflag)
454*0Sstevel@tonic-gate register unsigned char	*name;
455*0Sstevel@tonic-gate mode_t mode;
456*0Sstevel@tonic-gate int regflag;
457*0Sstevel@tonic-gate {
458*0Sstevel@tonic-gate 	static int flag;
459*0Sstevel@tonic-gate 	static uid_t euid;
460*0Sstevel@tonic-gate 	struct stat statb;
461*0Sstevel@tonic-gate 	mode_t ftype;
462*0Sstevel@tonic-gate 
463*0Sstevel@tonic-gate 	if(flag == 0) {
464*0Sstevel@tonic-gate 		euid = geteuid();
465*0Sstevel@tonic-gate 		flag = 1;
466*0Sstevel@tonic-gate 	}
467*0Sstevel@tonic-gate 	ftype = statb.st_mode & S_IFMT;
468*0Sstevel@tonic-gate 	if (stat((char *)name, &statb) == 0) {
469*0Sstevel@tonic-gate 		ftype = statb.st_mode & S_IFMT;
470*0Sstevel@tonic-gate 		if(mode == S_IEXEC && regflag && ftype != S_IFREG)
471*0Sstevel@tonic-gate 			return(2);
472*0Sstevel@tonic-gate 		if(access((char *)name, 010|(mode>>6)) == 0) {
473*0Sstevel@tonic-gate 			if(euid == 0) {
474*0Sstevel@tonic-gate 				if (ftype != S_IFREG || mode != S_IEXEC)
475*0Sstevel@tonic-gate 					return(0);
476*0Sstevel@tonic-gate 		    		/* root can execute file as long as it has execute
477*0Sstevel@tonic-gate 			   	permission for someone */
478*0Sstevel@tonic-gate 				if (statb.st_mode & (S_IEXEC|(S_IEXEC>>3)|(S_IEXEC>>6)))
479*0Sstevel@tonic-gate 					return(0);
480*0Sstevel@tonic-gate 				return(3);
481*0Sstevel@tonic-gate 			}
482*0Sstevel@tonic-gate 			return(0);
483*0Sstevel@tonic-gate 		}
484*0Sstevel@tonic-gate 	}
485*0Sstevel@tonic-gate 	return(errno == EACCES ? 3 : 1);
486*0Sstevel@tonic-gate }
487*0Sstevel@tonic-gate 
488*0Sstevel@tonic-gate 
489*0Sstevel@tonic-gate pr_path(name, count)
490*0Sstevel@tonic-gate 	register unsigned char	*name;
491*0Sstevel@tonic-gate 	int count;
492*0Sstevel@tonic-gate {
493*0Sstevel@tonic-gate 	register unsigned char	*path;
494*0Sstevel@tonic-gate 
495*0Sstevel@tonic-gate 	path = getpath(name);
496*0Sstevel@tonic-gate 
497*0Sstevel@tonic-gate 	while (--count && path)
498*0Sstevel@tonic-gate 		path = nextpath(path, name);
499*0Sstevel@tonic-gate 
500*0Sstevel@tonic-gate 	catpath(path, name);
501*0Sstevel@tonic-gate 	prs_buff(curstak());
502*0Sstevel@tonic-gate }
503*0Sstevel@tonic-gate 
504*0Sstevel@tonic-gate 
505*0Sstevel@tonic-gate static
506*0Sstevel@tonic-gate argpath(arg)
507*0Sstevel@tonic-gate 	register struct argnod	*arg;
508*0Sstevel@tonic-gate {
509*0Sstevel@tonic-gate 	register unsigned char 	*s;
510*0Sstevel@tonic-gate 	register unsigned char	*start;
511*0Sstevel@tonic-gate 
512*0Sstevel@tonic-gate 	while (arg)
513*0Sstevel@tonic-gate 	{
514*0Sstevel@tonic-gate 		s = arg->argval;
515*0Sstevel@tonic-gate 		start = s;
516*0Sstevel@tonic-gate 
517*0Sstevel@tonic-gate 		if (letter(*s))
518*0Sstevel@tonic-gate 		{
519*0Sstevel@tonic-gate 			while (alphanum(*s))
520*0Sstevel@tonic-gate 				s++;
521*0Sstevel@tonic-gate 
522*0Sstevel@tonic-gate 			if (*s == '=')
523*0Sstevel@tonic-gate 			{
524*0Sstevel@tonic-gate 				*s = 0;
525*0Sstevel@tonic-gate 
526*0Sstevel@tonic-gate 				if (eq(start, pathname))
527*0Sstevel@tonic-gate 				{
528*0Sstevel@tonic-gate 					*s = '=';
529*0Sstevel@tonic-gate 					return(1);
530*0Sstevel@tonic-gate 				}
531*0Sstevel@tonic-gate 				else
532*0Sstevel@tonic-gate 					*s = '=';
533*0Sstevel@tonic-gate 			}
534*0Sstevel@tonic-gate 		}
535*0Sstevel@tonic-gate 		arg = arg->argnxt;
536*0Sstevel@tonic-gate 	}
537*0Sstevel@tonic-gate 
538*0Sstevel@tonic-gate 	return(0);
539*0Sstevel@tonic-gate }
540