xref: /freebsd/contrib/tcsh/sh.exec.c (revision 41466b50c1d5bfd1cf6adaae547a579a75d7c04e)
1 /* $Header: /src/pub/tcsh/sh.exec.c,v 3.51 2000/11/11 23:03:36 christos Exp $ */
2 /*
3  * sh.exec.c: Search, find, and execute a command!
4  */
5 /*-
6  * Copyright (c) 1980, 1991 The Regents of the University of California.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. All advertising materials mentioning features or use of this software
18  *    must display the following acknowledgement:
19  *	This product includes software developed by the University of
20  *	California, Berkeley and its contributors.
21  * 4. Neither the name of the University nor the names of its contributors
22  *    may be used to endorse or promote products derived from this software
23  *    without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35  * SUCH DAMAGE.
36  */
37 #include "sh.h"
38 
39 RCSID("$Id: sh.exec.c,v 3.51 2000/11/11 23:03:36 christos Exp $")
40 
41 #include "tc.h"
42 #include "tw.h"
43 #ifdef WINNT_NATIVE
44 #include <nt.const.h>
45 #endif /*WINNT_NATIVE*/
46 
47 /*
48  * C shell
49  */
50 
51 #ifndef OLDHASH
52 # define FASTHASH	/* Fast hashing is the default */
53 #endif /* OLDHASH */
54 
55 /*
56  * System level search and execute of a command.
57  * We look in each directory for the specified command name.
58  * If the name contains a '/' then we execute only the full path name.
59  * If there is no search path then we execute only full path names.
60  */
61 
62 /*
63  * As we search for the command we note the first non-trivial error
64  * message for presentation to the user.  This allows us often
65  * to show that a file has the wrong mode/no access when the file
66  * is not in the last component of the search path, so we must
67  * go on after first detecting the error.
68  */
69 static char *exerr;		/* Execution error message */
70 static Char *expath;		/* Path for exerr */
71 
72 /*
73  * The two part hash function is designed to let texec() call the
74  * more expensive hashname() only once and the simple hash() several
75  * times (once for each path component checked).
76  * Byte size is assumed to be 8.
77  */
78 #define BITS_PER_BYTE	8
79 
80 #ifdef FASTHASH
81 /*
82  * xhash is an array of hash buckets which are used to hash execs.  If
83  * it is allocated (havhash true), then to tell if ``name'' is
84  * (possibly) presend in the i'th component of the variable path, look
85  * at the [hashname(name)] bucket of size [hashwidth] bytes, in the [i
86  * mod size*8]'th bit.  The cache size is defaults to a length of 1024
87  * buckets, each 1 byte wide.  This implementation guarantees that
88  * objects n bytes wide will be aligned on n byte boundaries.
89  */
90 # define HSHMUL		241
91 
92 static unsigned long *xhash = NULL;
93 static unsigned int hashlength = 0, uhashlength = 0;
94 static unsigned int hashwidth = 0, uhashwidth = 0;
95 static int hashdebug = 0;
96 
97 # define hash(a, b)	(((a) * HSHMUL + (b)) % (hashlength))
98 # define widthof(t)	(sizeof(t) * BITS_PER_BYTE)
99 # define tbit(f, i, t)	(((t *) xhash)[(f)] &  \
100 			 (1L << (i & (widthof(t) - 1))))
101 # define tbis(f, i, t)	(((t *) xhash)[(f)] |= \
102 			 (1L << (i & (widthof(t) - 1))))
103 # define cbit(f, i)	tbit(f, i, unsigned char)
104 # define cbis(f, i)	tbis(f, i, unsigned char)
105 # define sbit(f, i)	tbit(f, i, unsigned short)
106 # define sbis(f, i)	tbis(f, i, unsigned short)
107 # define ibit(f, i)	tbit(f, i, unsigned int)
108 # define ibis(f, i)	tbis(f, i, unsigned int)
109 # define lbit(f, i)	tbit(f, i, unsigned long)
110 # define lbis(f, i)	tbis(f, i, unsigned long)
111 
112 # define bit(f, i) (hashwidth==sizeof(unsigned char)  ? cbit(f,i) : \
113  		    ((hashwidth==sizeof(unsigned short) ? sbit(f,i) : \
114 		     ((hashwidth==sizeof(unsigned int)   ? ibit(f,i) : \
115 		     lbit(f,i))))))
116 # define bis(f, i) (hashwidth==sizeof(unsigned char)  ? cbis(f,i) : \
117  		    ((hashwidth==sizeof(unsigned short) ? sbis(f,i) : \
118 		     ((hashwidth==sizeof(unsigned int)   ? ibis(f,i) : \
119 		     lbis(f,i))))))
120 #else /* OLDHASH */
121 /*
122  * Xhash is an array of HSHSIZ bits (HSHSIZ / 8 chars), which are used
123  * to hash execs.  If it is allocated (havhash true), then to tell
124  * whether ``name'' is (possibly) present in the i'th component
125  * of the variable path, you look at the bit in xhash indexed by
126  * hash(hashname("name"), i).  This is setup automatically
127  * after .login is executed, and recomputed whenever ``path'' is
128  * changed.
129  */
130 # define HSHSIZ		8192	/* 1k bytes */
131 # define HSHMASK		(HSHSIZ - 1)
132 # define HSHMUL		243
133 static char xhash[HSHSIZ / BITS_PER_BYTE];
134 
135 # define hash(a, b)	(((a) * HSHMUL + (b)) & HSHMASK)
136 # define bit(h, b)	((h)[(b) >> 3] & 1 << ((b) & 7))	/* bit test */
137 # define bis(h, b)	((h)[(b) >> 3] |= 1 << ((b) & 7))	/* bit set */
138 
139 #endif /* FASTHASH */
140 
141 #ifdef VFORK
142 static int hits, misses;
143 #endif /* VFORK */
144 
145 /* Dummy search path for just absolute search when no path */
146 static Char *justabs[] = {STRNULL, 0};
147 
148 static	void	pexerr		__P((void));
149 static	void	texec		__P((Char *, Char **));
150 int	hashname	__P((Char *));
151 static	int 	iscommand	__P((Char *));
152 
153 void
154 doexec(t)
155     register struct command *t;
156 {
157     register Char *dp, **pv, **av, *sav;
158     register struct varent *v;
159     register bool slash;
160     register int hashval, i;
161     Char   *blk[2];
162 
163     /*
164      * Glob the command name. We will search $path even if this does something,
165      * as in sh but not in csh.  One special case: if there is no PATH, then we
166      * execute only commands which start with '/'.
167      */
168     blk[0] = t->t_dcom[0];
169     blk[1] = 0;
170     gflag = 0, tglob(blk);
171     if (gflag) {
172 	pv = globall(blk);
173 	if (pv == 0) {
174 	    setname(short2str(blk[0]));
175 	    stderror(ERR_NAME | ERR_NOMATCH);
176 	}
177 	gargv = 0;
178     }
179     else
180 	pv = saveblk(blk);
181 
182     trim(pv);
183 
184     exerr = 0;
185     expath = Strsave(pv[0]);
186 #ifdef VFORK
187     Vexpath = expath;
188 #endif /* VFORK */
189 
190     v = adrof(STRpath);
191     if (v == 0 && expath[0] != '/' && expath[0] != '.') {
192 	blkfree(pv);
193 	pexerr();
194     }
195     slash = any(short2str(expath), '/');
196 
197     /*
198      * Glob the argument list, if necessary. Otherwise trim off the quote bits.
199      */
200     gflag = 0;
201     av = &t->t_dcom[1];
202     tglob(av);
203     if (gflag) {
204 	av = globall(av);
205 	if (av == 0) {
206 	    blkfree(pv);
207 	    setname(short2str(expath));
208 	    stderror(ERR_NAME | ERR_NOMATCH);
209 	}
210 	gargv = 0;
211     }
212     else
213 	av = saveblk(av);
214 
215     blkfree(t->t_dcom);
216     t->t_dcom = blkspl(pv, av);
217     xfree((ptr_t) pv);
218     xfree((ptr_t) av);
219     av = t->t_dcom;
220     trim(av);
221 
222     if (*av == NULL || **av == '\0')
223 	pexerr();
224 
225     xechoit(av);		/* Echo command if -x */
226 #ifdef CLOSE_ON_EXEC
227     /*
228      * Since all internal file descriptors are set to close on exec, we don't
229      * need to close them explicitly here.  Just reorient ourselves for error
230      * messages.
231      */
232     SHIN = 0;
233     SHOUT = 1;
234     SHDIAG = 2;
235     OLDSTD = 0;
236     isoutatty = isatty(SHOUT);
237     isdiagatty = isatty(SHDIAG);
238 #else
239     closech();			/* Close random fd's */
240 #endif
241     /*
242      * We must do this AFTER any possible forking (like `foo` in glob) so that
243      * this shell can still do subprocesses.
244      */
245 #ifdef BSDSIGS
246     (void) sigsetmask((sigmask_t) 0);
247 #else /* BSDSIGS */
248     (void) sigrelse(SIGINT);
249     (void) sigrelse(SIGCHLD);
250 #endif /* BSDSIGS */
251 
252     /*
253      * If no path, no words in path, or a / in the filename then restrict the
254      * command search.
255      */
256     if (v == 0 || v->vec[0] == 0 || slash)
257 	pv = justabs;
258     else
259 	pv = v->vec;
260     sav = Strspl(STRslash, *av);/* / command name for postpending */
261 #ifdef VFORK
262     Vsav = sav;
263 #endif /* VFORK */
264     hashval = havhash ? hashname(*av) : 0;
265 
266     i = 0;
267 #ifdef VFORK
268     hits++;
269 #endif /* VFORK */
270     do {
271 	/*
272 	 * Try to save time by looking at the hash table for where this command
273 	 * could be.  If we are doing delayed hashing, then we put the names in
274 	 * one at a time, as the user enters them.  This is kinda like Korn
275 	 * Shell's "tracked aliases".
276 	 */
277 	if (!slash && ABSOLUTEP(pv[0]) && havhash) {
278 #ifdef FASTHASH
279 	    if (!bit(hashval, i))
280 		goto cont;
281 #else /* OLDHASH */
282 	    int hashval1 = hash(hashval, i);
283 	    if (!bit(xhash, hashval1))
284 		goto cont;
285 #endif /* FASTHASH */
286 	}
287 	if (pv[0][0] == 0 || eq(pv[0], STRdot))	/* don't make ./xxx */
288 	{
289 
290 #ifdef COHERENT
291 	    if (t->t_dflg & F_AMPERSAND) {
292 # ifdef JOBDEBUG
293     	        xprintf("set SIGINT to SIG_IGN\n");
294     	        xprintf("set SIGQUIT to SIG_DFL\n");
295 # endif /* JOBDEBUG */
296     	        (void) signal(SIGINT,SIG_IGN); /* may not be necessary */
297 	        (void) signal(SIGQUIT,SIG_DFL);
298 	    }
299 
300 	    if (gointr && eq(gointr, STRminus)) {
301 # ifdef JOBDEBUG
302     	        xprintf("set SIGINT to SIG_IGN\n");
303     	        xprintf("set SIGQUIT to SIG_IGN\n");
304 # endif /* JOBDEBUG */
305     	        (void) signal(SIGINT,SIG_IGN); /* may not be necessary */
306 	        (void) signal(SIGQUIT,SIG_IGN);
307 	    }
308 #endif /* COHERENT */
309 
310 	    texec(*av, av);
311 }
312 	else {
313 	    dp = Strspl(*pv, sav);
314 #ifdef VFORK
315 	    Vdp = dp;
316 #endif /* VFORK */
317 
318 #ifdef COHERENT
319 	    if ((t->t_dflg & F_AMPERSAND)) {
320 # ifdef JOBDEBUG
321     	        xprintf("set SIGINT to SIG_IGN\n");
322 # endif /* JOBDEBUG */
323 		/*
324 		 * this is necessary on Coherent or all background
325 		 * jobs are killed by CTRL-C
326 		 * (there must be a better fix for this)
327 		 */
328     	        (void) signal(SIGINT,SIG_IGN);
329 	    }
330 	    if (gointr && eq(gointr,STRminus)) {
331 # ifdef JOBDEBUG
332     	        xprintf("set SIGINT to SIG_IGN\n");
333     	        xprintf("set SIGQUIT to SIG_IGN\n");
334 # endif /* JOBDEBUG */
335     	        (void) signal(SIGINT,SIG_IGN); /* may not be necessary */
336 	        (void) signal(SIGQUIT,SIG_IGN);
337 	    }
338 #endif /* COHERENT */
339 
340 	    texec(dp, av);
341 #ifdef VFORK
342 	    Vdp = 0;
343 #endif /* VFORK */
344 	    xfree((ptr_t) dp);
345 	}
346 #ifdef VFORK
347 	misses++;
348 #endif /* VFORK */
349 cont:
350 	pv++;
351 	i++;
352     } while (*pv);
353 #ifdef VFORK
354     hits--;
355     Vsav = 0;
356 #endif /* VFORK */
357     xfree((ptr_t) sav);
358     pexerr();
359 }
360 
361 static void
362 pexerr()
363 {
364     /* Couldn't find the damn thing */
365     if (expath) {
366 	setname(short2str(expath));
367 #ifdef VFORK
368 	Vexpath = 0;
369 #endif /* VFORK */
370 	xfree((ptr_t) expath);
371 	expath = 0;
372     }
373     else
374 	setname("");
375     if (exerr)
376 	stderror(ERR_NAME | ERR_STRING, exerr);
377     stderror(ERR_NAME | ERR_COMMAND);
378 }
379 
380 /*
381  * Execute command f, arg list t.
382  * Record error message if not found.
383  * Also do shell scripts here.
384  */
385 static void
386 texec(sf, st)
387     Char   *sf;
388     register Char **st;
389 {
390     register char **t;
391     register char *f;
392     register struct varent *v;
393     Char  **vp;
394     Char   *lastsh[2];
395     char    pref[2];
396     int     fd;
397     Char   *st0, **ost;
398 
399     /* The order for the conversions is significant */
400     t = short2blk(st);
401     f = short2str(sf);
402 #ifdef VFORK
403     Vt = t;
404 #endif /* VFORK */
405     errno = 0;			/* don't use a previous error */
406 #ifdef apollo
407     /*
408      * If we try to execute an nfs mounted directory on the apollo, we
409      * hang forever. So until apollo fixes that..
410      */
411     {
412 	struct stat stb;
413 	if (stat(f, &stb) == 0 && S_ISDIR(stb.st_mode))
414 	    errno = EISDIR;
415     }
416     if (errno == 0)
417 #endif /* apollo */
418     {
419 #ifdef ISC_POSIX_EXEC_BUG
420 	__setostype(0);		/* "0" is "__OS_SYSV" in <sys/user.h> */
421 #endif /* ISC_POSIX_EXEC_BUG */
422 	(void) execv(f, t);
423 #ifdef ISC_POSIX_EXEC_BUG
424 	__setostype(1);		/* "1" is "__OS_POSIX" in <sys/user.h> */
425 #endif /* ISC_POSIX_EXEC_BUG */
426     }
427 #ifdef VFORK
428     Vt = 0;
429 #endif /* VFORK */
430     blkfree((Char **) t);
431     switch (errno) {
432 
433     case ENOEXEC:
434 #ifdef WINNT_NATIVE
435 		nt_feed_to_cmd(f,t);
436 #endif /* WINNT_NATIVE */
437 	/*
438 	 * From: casper@fwi.uva.nl (Casper H.S. Dik) If we could not execute
439 	 * it, don't feed it to the shell if it looks like a binary!
440 	 */
441 	if ((fd = open(f, O_RDONLY)) != -1) {
442 	    int nread;
443 	    if ((nread = read(fd, (char *) pref, 2)) == 2) {
444 		if (!Isprint(pref[0]) && (pref[0] != '\n' && pref[0] != '\t')) {
445 		    (void) close(fd);
446 		    /*
447 		     * We *know* what ENOEXEC means.
448 		     */
449 		    stderror(ERR_ARCH, f, strerror(errno));
450 		}
451 	    }
452 	    else if (nread < 0 && errno != EINTR) {
453 #ifdef convex
454 		/* need to print error incase the file is migrated */
455 		stderror(ERR_SYSTEM, f, strerror(errno));
456 #endif
457 	    }
458 #ifdef _PATH_BSHELL
459 	    else {
460 		pref[0] = '#';
461 		pref[1] = '\0';
462 	    }
463 #endif
464 	}
465 #ifdef HASHBANG
466 	if (fd == -1 ||
467 	    pref[0] != '#' || pref[1] != '!' || hashbang(fd, &vp) == -1) {
468 #endif /* HASHBANG */
469 	/*
470 	 * If there is an alias for shell, then put the words of the alias in
471 	 * front of the argument list replacing the command name. Note no
472 	 * interpretation of the words at this point.
473 	 */
474 	    v = adrof1(STRshell, &aliases);
475 	    if (v == 0) {
476 		vp = lastsh;
477 		vp[0] = adrof(STRshell) ? varval(STRshell) : STR_SHELLPATH;
478 		vp[1] = NULL;
479 #ifdef _PATH_BSHELL
480 		if (fd != -1
481 # ifndef ISC	/* Compatible with ISC's /bin/csh */
482 		    && pref[0] != '#'
483 # endif /* ISC */
484 		    )
485 		    vp[0] = STR_BSHELL;
486 #endif
487 		vp = saveblk(vp);
488 	    }
489 	    else
490 		vp = saveblk(v->vec);
491 #ifdef HASHBANG
492 	}
493 #endif /* HASHBANG */
494 	if (fd != -1)
495 	    (void) close(fd);
496 
497 	st0 = st[0];
498 	st[0] = sf;
499 	ost = st;
500 	st = blkspl(vp, st);	/* Splice up the new arglst */
501 	ost[0] = st0;
502 	sf = *st;
503 	/* The order for the conversions is significant */
504 	t = short2blk(st);
505 	f = short2str(sf);
506 	xfree((ptr_t) st);
507 	blkfree((Char **) vp);
508 #ifdef VFORK
509 	Vt = t;
510 #endif /* VFORK */
511 #ifdef ISC_POSIX_EXEC_BUG
512 	__setostype(0);		/* "0" is "__OS_SYSV" in <sys/user.h> */
513 #endif /* ISC_POSIX_EXEC_BUG */
514 	(void) execv(f, t);
515 #ifdef ISC_POSIX_EXEC_BUG
516 	__setostype(1);		/* "1" is "__OS_POSIX" in <sys/user.h> */
517 #endif /* ISC_POSIX_EXEC_BUG */
518 #ifdef VFORK
519 	Vt = 0;
520 #endif /* VFORK */
521 	blkfree((Char **) t);
522 	/* The sky is falling, the sky is falling! */
523 	stderror(ERR_SYSTEM, f, strerror(errno));
524 	break;
525 
526     case ENOMEM:
527 	stderror(ERR_SYSTEM, f, strerror(errno));
528 	break;
529 
530 #ifdef _IBMR2
531     case 0:			/* execv fails and returns 0! */
532 #endif /* _IBMR2 */
533     case ENOENT:
534 	break;
535 
536     default:
537 	if (exerr == 0) {
538 	    exerr = strerror(errno);
539 	    if (expath)
540 		xfree((ptr_t) expath);
541 	    expath = Strsave(sf);
542 #ifdef VFORK
543 	    Vexpath = expath;
544 #endif /* VFORK */
545 	}
546 	break;
547     }
548 }
549 
550 /*ARGSUSED*/
551 void
552 execash(t, kp)
553     Char  **t;
554     register struct command *kp;
555 {
556     int     saveIN, saveOUT, saveDIAG, saveSTD;
557     int     oSHIN;
558     int     oSHOUT;
559     int     oSHDIAG;
560     int     oOLDSTD;
561     jmp_buf_t osetexit;
562     int	    my_reenter;
563     int     odidfds;
564 #ifndef CLOSE_ON_EXEC
565     int	    odidcch;
566 #endif /* CLOSE_ON_EXEC */
567     signalfun_t osigint, osigquit, osigterm;
568 
569     USE(t);
570     if (chkstop == 0 && setintr)
571 	panystop(0);
572     /*
573      * Hmm, we don't really want to do that now because we might
574      * fail, but what is the choice
575      */
576     rechist(NULL, adrof(STRsavehist) != NULL);
577 
578 
579     osigint  = signal(SIGINT, parintr);
580     osigquit = signal(SIGQUIT, parintr);
581     osigterm = signal(SIGTERM, parterm);
582 
583     odidfds = didfds;
584 #ifndef CLOSE_ON_EXEC
585     odidcch = didcch;
586 #endif /* CLOSE_ON_EXEC */
587     oSHIN = SHIN;
588     oSHOUT = SHOUT;
589     oSHDIAG = SHDIAG;
590     oOLDSTD = OLDSTD;
591 
592     saveIN = dcopy(SHIN, -1);
593     saveOUT = dcopy(SHOUT, -1);
594     saveDIAG = dcopy(SHDIAG, -1);
595     saveSTD = dcopy(OLDSTD, -1);
596 
597     lshift(kp->t_dcom, 1);
598 
599     getexit(osetexit);
600 
601     /* PWP: setjmp/longjmp bugfix for optimizing compilers */
602 #ifdef cray
603     my_reenter = 1;             /* assume non-zero return val */
604     if (setexit() == 0) {
605         my_reenter = 0;         /* Oh well, we were wrong */
606 #else /* !cray */
607     if ((my_reenter = setexit()) == 0) {
608 #endif /* cray */
609 	SHIN = dcopy(0, -1);
610 	SHOUT = dcopy(1, -1);
611 	SHDIAG = dcopy(2, -1);
612 #ifndef CLOSE_ON_EXEC
613 	didcch = 0;
614 #endif /* CLOSE_ON_EXEC */
615 	didfds = 0;
616 	/*
617 	 * Decrement the shell level
618 	 */
619 	shlvl(-1);
620 #ifdef WINNT_NATIVE
621 	__nt_really_exec=1;
622 #endif /* WINNT_NATIVE */
623 	doexec(kp);
624     }
625 
626     (void) sigset(SIGINT, osigint);
627     (void) sigset(SIGQUIT, osigquit);
628     (void) sigset(SIGTERM, osigterm);
629 
630     doneinp = 0;
631 #ifndef CLOSE_ON_EXEC
632     didcch = odidcch;
633 #endif /* CLOSE_ON_EXEC */
634     didfds = odidfds;
635     (void) close(SHIN);
636     (void) close(SHOUT);
637     (void) close(SHDIAG);
638     (void) close(OLDSTD);
639     SHIN = dmove(saveIN, oSHIN);
640     SHOUT = dmove(saveOUT, oSHOUT);
641     SHDIAG = dmove(saveDIAG, oSHDIAG);
642     OLDSTD = dmove(saveSTD, oOLDSTD);
643 
644     resexit(osetexit);
645     if (my_reenter)
646 	stderror(ERR_SILENT);
647 }
648 
649 void
650 xechoit(t)
651     Char  **t;
652 {
653     if (adrof(STRecho)) {
654 	flush();
655 	haderr = 1;
656 	blkpr(t), xputchar('\n');
657 	haderr = 0;
658     }
659 }
660 
661 /*ARGSUSED*/
662 void
663 dohash(vv, c)
664     Char **vv;
665     struct command *c;
666 {
667 #ifdef COMMENT
668     struct stat stb;
669 #endif
670     DIR    *dirp;
671     register struct dirent *dp;
672     int     i = 0;
673     struct varent *v = adrof(STRpath);
674     Char  **pv;
675     int hashval;
676 #ifdef WINNT_NATIVE
677     int is_windir; /* check if it is the windows directory */
678     USE(hashval);
679 #endif /* WINNT_NATIVE */
680 
681     USE(c);
682 #ifdef FASTHASH
683     if (vv && vv[1]) {
684         uhashlength = atoi(short2str(vv[1]));
685         if (vv[2]) {
686 	    uhashwidth = atoi(short2str(vv[2]));
687 	    if ((uhashwidth != sizeof(unsigned char)) &&
688 	        (uhashwidth != sizeof(unsigned short)) &&
689 	        (uhashwidth != sizeof(unsigned long)))
690 	        uhashwidth = 0;
691 	    if (vv[3])
692 		hashdebug = atoi(short2str(vv[3]));
693         }
694     }
695 
696     if (uhashwidth)
697 	hashwidth = uhashwidth;
698     else {
699 	hashwidth = 0;
700 	if (v == NULL)
701 	    return;
702 	for (pv = v->vec; *pv; pv++, hashwidth++)
703 	    continue;
704 	if (hashwidth <= widthof(unsigned char))
705 	    hashwidth = sizeof(unsigned char);
706 	else if (hashwidth <= widthof(unsigned short))
707 	    hashwidth = sizeof(unsigned short);
708 	else if (hashwidth <= widthof(unsigned int))
709 	    hashwidth = sizeof(unsigned int);
710 	else
711 	    hashwidth = sizeof(unsigned long);
712     }
713 
714     if (uhashlength)
715 	hashlength = uhashlength;
716     else
717         hashlength = hashwidth * (8*64);/* "average" files per dir in path */
718 
719     if (xhash)
720         xfree((ptr_t) xhash);
721     xhash = (unsigned long *) xcalloc((size_t) (hashlength * hashwidth),
722 				      (size_t) 1);
723 #endif /* FASTHASH */
724 
725     (void) getusername(NULL);	/* flush the tilde cashe */
726     tw_cmd_free();
727     havhash = 1;
728     if (v == NULL)
729 	return;
730     for (pv = v->vec; *pv; pv++, i++) {
731 	if (!ABSOLUTEP(pv[0]))
732 	    continue;
733 	dirp = opendir(short2str(*pv));
734 	if (dirp == NULL)
735 	    continue;
736 #ifdef COMMENT			/* this isn't needed.  opendir won't open
737 				 * non-dirs */
738 	if (fstat(dirp->dd_fd, &stb) < 0 || !S_ISDIR(stb.st_mode)) {
739 	    (void) closedir(dirp);
740 	    continue;
741 	}
742 #endif
743 #ifdef WINNT_NATIVE
744 	is_windir = nt_check_if_windir(short2str(*pv));
745 #endif /* WINNT_NATIVE */
746 	while ((dp = readdir(dirp)) != NULL) {
747 	    if (dp->d_ino == 0)
748 		continue;
749 	    if (dp->d_name[0] == '.' &&
750 		(dp->d_name[1] == '\0' ||
751 		 (dp->d_name[1] == '.' && dp->d_name[2] == '\0')))
752 		continue;
753 #ifdef WINNT_NATIVE
754 	    nt_check_name_and_hash(is_windir, dp->d_name, i);
755 #else /* !WINNT_NATIVE*/
756 #if defined(_UWIN) || defined(__CYGWIN__)
757 	    /* Turn foo.{exe,com,bat} into foo since UWIN's readdir returns
758 	     * the file with the .exe, .com, .bat extension
759 	     */
760 	    {
761 		size_t	ext = strlen(dp->d_name) - 4;
762 		if ((ext > 0) && (strcmp(&dp->d_name[ext], ".exe") == 0 ||
763 				  strcmp(&dp->d_name[ext], ".bat") == 0 ||
764 				  strcmp(&dp->d_name[ext], ".com") == 0))
765 		    dp->d_name[ext] = '\0';
766 	    }
767 #endif /* _UWIN || __CYGWIN__ */
768 # ifdef FASTHASH
769 	    hashval = hashname(str2short(dp->d_name));
770 	    bis(hashval, i);
771 	    if (hashdebug & 1)
772 	        xprintf(CGETS(13, 1, "hash=%-4d dir=%-2d prog=%s\n"),
773 		        hashname(str2short(dp->d_name)), i, dp->d_name);
774 # else /* OLD HASH */
775 	    hashval = hash(hashname(str2short(dp->d_name)), i);
776 	    bis(xhash, hashval);
777 # endif /* FASTHASH */
778 	    /* tw_add_comm_name (dp->d_name); */
779 #endif /* WINNT_NATIVE */
780 	}
781 	(void) closedir(dirp);
782     }
783 }
784 
785 /*ARGSUSED*/
786 void
787 dounhash(v, c)
788     Char **v;
789     struct command *c;
790 {
791     USE(c);
792     USE(v);
793     havhash = 0;
794 #ifdef FASTHASH
795     if (xhash) {
796        xfree((ptr_t) xhash);
797        xhash = NULL;
798     }
799 #endif /* FASTHASH */
800 }
801 
802 /*ARGSUSED*/
803 void
804 hashstat(v, c)
805     Char **v;
806     struct command *c;
807 {
808     USE(c);
809     USE(v);
810 #ifdef FASTHASH
811    if (havhash && hashlength && hashwidth)
812       xprintf(CGETS(13, 2, "%d hash buckets of %d bits each\n"),
813 	      hashlength, hashwidth*8);
814    if (hashdebug)
815       xprintf(CGETS(13, 3, "debug mask = 0x%08x\n"), hashdebug);
816 #endif /* FASTHASH */
817 #ifdef VFORK
818    if (hits + misses)
819       xprintf(CGETS(13, 4, "%d hits, %d misses, %d%%\n"),
820 	      hits, misses, 100 * hits / (hits + misses));
821 #endif
822 }
823 
824 
825 /*
826  * Hash a command name.
827  */
828 int
829 hashname(cp)
830     register Char *cp;
831 {
832     register unsigned long h;
833 
834     for (h = 0; *cp; cp++)
835 	h = hash(h, *cp);
836     return ((int) h);
837 }
838 
839 static int
840 iscommand(name)
841     Char   *name;
842 {
843     register Char **pv;
844     register Char *sav;
845     register struct varent *v;
846     register bool slash = any(short2str(name), '/');
847     register int hashval, i;
848 
849     v = adrof(STRpath);
850     if (v == 0 || v->vec[0] == 0 || slash)
851 	pv = justabs;
852     else
853 	pv = v->vec;
854     sav = Strspl(STRslash, name);	/* / command name for postpending */
855     hashval = havhash ? hashname(name) : 0;
856     i = 0;
857     do {
858 	if (!slash && ABSOLUTEP(pv[0]) && havhash) {
859 #ifdef FASTHASH
860 	    if (!bit(hashval, i))
861 		goto cont;
862 #else /* OLDHASH */
863 	    int hashval1 = hash(hashval, i);
864 	    if (!bit(xhash, hashval1))
865 		goto cont;
866 #endif /* FASTHASH */
867 	}
868 	if (pv[0][0] == 0 || eq(pv[0], STRdot)) {	/* don't make ./xxx */
869 	    if (executable(NULL, name, 0)) {
870 		xfree((ptr_t) sav);
871 		return i + 1;
872 	    }
873 	}
874 	else {
875 	    if (executable(*pv, sav, 0)) {
876 		xfree((ptr_t) sav);
877 		return i + 1;
878 	    }
879 	}
880 cont:
881 	pv++;
882 	i++;
883     } while (*pv);
884     xfree((ptr_t) sav);
885     return 0;
886 }
887 
888 /* Also by:
889  *  Andreas Luik <luik@isaak.isa.de>
890  *  I S A  GmbH - Informationssysteme fuer computerintegrierte Automatisierung
891  *  Azenberstr. 35
892  *  D-7000 Stuttgart 1
893  *  West-Germany
894  * is the executable() routine below and changes to iscommand().
895  * Thanks again!!
896  */
897 
898 #ifndef WINNT_NATIVE
899 /*
900  * executable() examines the pathname obtained by concatenating dir and name
901  * (dir may be NULL), and returns 1 either if it is executable by us, or
902  * if dir_ok is set and the pathname refers to a directory.
903  * This is a bit kludgy, but in the name of optimization...
904  */
905 int
906 executable(dir, name, dir_ok)
907     Char   *dir, *name;
908     bool    dir_ok;
909 {
910     struct stat stbuf;
911     Char    path[MAXPATHLEN + 1];
912     char   *strname;
913     (void) memset(path, 0, sizeof(path));
914 
915     if (dir && *dir) {
916 	copyn(path, dir, MAXPATHLEN);
917 	catn(path, name, MAXPATHLEN);
918 	strname = short2str(path);
919     }
920     else
921 	strname = short2str(name);
922 
923     return (stat(strname, &stbuf) != -1 &&
924 	    ((dir_ok && S_ISDIR(stbuf.st_mode)) ||
925 	     (S_ISREG(stbuf.st_mode) &&
926     /* save time by not calling access() in the hopeless case */
927 	      (stbuf.st_mode & (S_IXOTH | S_IXGRP | S_IXUSR)) &&
928 	      access(strname, X_OK) == 0
929 	)));
930 }
931 #endif /*!WINNT_NATIVE*/
932 
933 int
934 tellmewhat(lexp, str)
935     struct wordent *lexp;
936     Char *str;
937 {
938     register int i;
939     register struct biltins *bptr;
940     register struct wordent *sp = lexp->next;
941     bool    aliased = 0, found;
942     Char   *s0, *s1, *s2, *cmd;
943     Char    qc;
944 
945     if (adrof1(sp->word, &aliases)) {
946 	alias(lexp);
947 	sp = lexp->next;
948 	aliased = 1;
949     }
950 
951     s0 = sp->word;		/* to get the memory freeing right... */
952 
953     /* handle quoted alias hack */
954     if ((*(sp->word) & (QUOTE | TRIM)) == QUOTE)
955 	(sp->word)++;
956 
957     /* do quoting, if it hasn't been done */
958     s1 = s2 = sp->word;
959     while (*s2)
960 	switch (*s2) {
961 	case '\'':
962 	case '"':
963 	    qc = *s2++;
964 	    while (*s2 && *s2 != qc)
965 		*s1++ = *s2++ | QUOTE;
966 	    if (*s2)
967 		s2++;
968 	    break;
969 	case '\\':
970 	    if (*++s2)
971 		*s1++ = *s2++ | QUOTE;
972 	    break;
973 	default:
974 	    *s1++ = *s2++;
975 	}
976     *s1 = '\0';
977 
978     for (bptr = bfunc; bptr < &bfunc[nbfunc]; bptr++) {
979 	if (eq(sp->word, str2short(bptr->bname))) {
980 	    if (str == NULL) {
981 		if (aliased)
982 		    prlex(lexp);
983 		xprintf(CGETS(13, 5, "%S: shell built-in command.\n"),
984 			      sp->word);
985 		flush();
986 	    }
987 	    else
988 		(void) Strcpy(str, sp->word);
989 	    sp->word = s0;	/* we save and then restore this */
990 	    return TRUE;
991 	}
992     }
993 #ifdef WINNT_NATIVE
994     for (bptr = nt_bfunc; bptr < &nt_bfunc[nt_nbfunc]; bptr++) {
995 	if (eq(sp->word, str2short(bptr->bname))) {
996 	    if (str == NULL) {
997 		if (aliased)
998 		    prlex(lexp);
999 		xprintf(CGETS(13, 5, "%S: shell built-in command.\n"),
1000 			      sp->word);
1001 		flush();
1002 	    }
1003 	    else
1004 		(void) Strcpy(str, sp->word);
1005 	    sp->word = s0;	/* we save and then restore this */
1006 	    return TRUE;
1007 	}
1008     }
1009 #endif /* WINNT_NATIVE*/
1010 
1011     sp->word = cmd = globone(sp->word, G_IGNORE);
1012 
1013     if ((i = iscommand(sp->word)) != 0) {
1014 	register Char **pv;
1015 	register struct varent *v;
1016 	bool    slash = any(short2str(sp->word), '/');
1017 
1018 	v = adrof(STRpath);
1019 	if (v == 0 || v->vec[0] == 0 || slash)
1020 	    pv = justabs;
1021 	else
1022 	    pv = v->vec;
1023 
1024 	while (--i)
1025 	    pv++;
1026 	if (pv[0][0] == 0 || eq(pv[0], STRdot)) {
1027 	    if (!slash) {
1028 		sp->word = Strspl(STRdotsl, sp->word);
1029 		prlex(lexp);
1030 		xfree((ptr_t) sp->word);
1031 	    }
1032 	    else
1033 		prlex(lexp);
1034 	}
1035 	else {
1036 	    s1 = Strspl(*pv, STRslash);
1037 	    sp->word = Strspl(s1, sp->word);
1038 	    xfree((ptr_t) s1);
1039 	    if (str == NULL)
1040 		prlex(lexp);
1041 	    else
1042 		(void) Strcpy(str, sp->word);
1043 	    xfree((ptr_t) sp->word);
1044 	}
1045 	found = 1;
1046     }
1047     else {
1048 	if (str == NULL) {
1049 	    if (aliased)
1050 		prlex(lexp);
1051 	    xprintf(CGETS(13, 6, "%S: Command not found.\n"), sp->word);
1052 	    flush();
1053 	}
1054 	else
1055 	    (void) Strcpy(str, sp->word);
1056 	found = 0;
1057     }
1058     sp->word = s0;		/* we save and then restore this */
1059     xfree((ptr_t) cmd);
1060     return found;
1061 }
1062 
1063 /*
1064  * Builtin to look at and list all places a command may be defined:
1065  * aliases, shell builtins, and the path.
1066  *
1067  * Marc Horowitz <marc@mit.edu>
1068  * MIT Student Information Processing Board
1069  */
1070 
1071 /*ARGSUSED*/
1072 void
1073 dowhere(v, c)
1074     register Char **v;
1075     struct command *c;
1076 {
1077     int found = 1;
1078     USE(c);
1079     for (v++; *v; v++)
1080 	found &= find_cmd(*v, 1);
1081     /* Make status nonzero if any command is not found. */
1082     if (!found)
1083       set(STRstatus, Strsave(STR1), VAR_READWRITE);
1084 }
1085 
1086 int
1087 find_cmd(cmd, prt)
1088     Char *cmd;
1089     int prt;
1090 {
1091     struct varent *var;
1092     struct biltins *bptr;
1093     Char **pv;
1094     Char *sv;
1095     int hashval, i, ex, rval = 0;
1096 
1097     if (prt && any(short2str(cmd), '/')) {
1098 	xprintf(CGETS(13, 7, "where: / in command makes no sense\n"));
1099 	return rval;
1100     }
1101 
1102     /* first, look for an alias */
1103 
1104     if (prt && adrof1(cmd, &aliases)) {
1105 	if ((var = adrof1(cmd, &aliases)) != NULL) {
1106 	    xprintf(CGETS(13, 8, "%S is aliased to "), cmd);
1107 	    blkpr(var->vec);
1108 	    xputchar('\n');
1109 	    rval = 1;
1110 	}
1111     }
1112 
1113     /* next, look for a shell builtin */
1114 
1115     for (bptr = bfunc; bptr < &bfunc[nbfunc]; bptr++) {
1116 	if (eq(cmd, str2short(bptr->bname))) {
1117 	    rval = 1;
1118 	    if (prt)
1119 		xprintf(CGETS(13, 9, "%S is a shell built-in\n"), cmd);
1120 	    else
1121 		return rval;
1122 	}
1123     }
1124 #ifdef WINNT_NATIVE
1125     for (bptr = nt_bfunc; bptr < &nt_bfunc[nt_nbfunc]; bptr++) {
1126 	if (eq(cmd, str2short(bptr->bname))) {
1127 	    rval = 1;
1128 	    if (prt)
1129 		xprintf(CGETS(13, 9, "%S is a shell built-in\n"), cmd);
1130 	    else
1131 		return rval;
1132 	}
1133     }
1134 #endif /* WINNT_NATIVE*/
1135 
1136     /* last, look through the path for the command */
1137 
1138     if ((var = adrof(STRpath)) == NULL)
1139 	return rval;
1140 
1141     hashval = havhash ? hashname(cmd) : 0;
1142 
1143     sv = Strspl(STRslash, cmd);
1144 
1145     for (pv = var->vec, i = 0; *pv; pv++, i++) {
1146 	if (havhash && !eq(*pv, STRdot)) {
1147 #ifdef FASTHASH
1148 	    if (!bit(hashval, i))
1149 		continue;
1150 #else /* OLDHASH */
1151 	    int hashval1 = hash(hashval, i);
1152 	    if (!bit(xhash, hashval1))
1153 		continue;
1154 #endif /* FASTHASH */
1155 	}
1156 	ex = executable(*pv, sv, 0);
1157 #ifdef FASTHASH
1158 	if (!ex && (hashdebug & 2)) {
1159 	    xprintf(CGETS(13, 10, "hash miss: "));
1160 	    ex = 1;	/* Force printing */
1161 	}
1162 #endif /* FASTHASH */
1163 	if (ex) {
1164 	    rval = 1;
1165 	    if (prt) {
1166 		xprintf("%S/", *pv);
1167 		xprintf("%S\n", cmd);
1168 	    }
1169 	    else
1170 		return rval;
1171 	}
1172     }
1173     xfree((ptr_t) sv);
1174     return rval;
1175 }
1176 #ifdef WINNT_NATIVE
1177 int hashval_extern(cp)
1178 	Char *cp;
1179 {
1180 	return havhash?hashname(cp):0;
1181 }
1182 int bit_extern(val,i)
1183 	int val;
1184 	int i;
1185 {
1186 	return bit(val,i);
1187 }
1188 void bis_extern(val,i)
1189 	int val;
1190 	int i;
1191 {
1192 	bis(val,i);
1193 }
1194 #endif /* WINNT_NATIVE */
1195 
1196