xref: /freebsd/bin/sh/exec.c (revision 941e286383714ef25f1ffe9ba6ae5040afdd7060)
1 /*-
2  * Copyright (c) 1991, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Kenneth Almquist.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 4. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 
33 #ifndef lint
34 #if 0
35 static char sccsid[] = "@(#)exec.c	8.4 (Berkeley) 6/8/95";
36 #endif
37 #endif /* not lint */
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
40 
41 #include <sys/types.h>
42 #include <sys/stat.h>
43 #include <unistd.h>
44 #include <fcntl.h>
45 #include <errno.h>
46 #include <stdlib.h>
47 
48 /*
49  * When commands are first encountered, they are entered in a hash table.
50  * This ensures that a full path search will not have to be done for them
51  * on each invocation.
52  *
53  * We should investigate converting to a linear search, even though that
54  * would make the command name "hash" a misnomer.
55  */
56 
57 #include "shell.h"
58 #include "main.h"
59 #include "nodes.h"
60 #include "parser.h"
61 #include "redir.h"
62 #include "eval.h"
63 #include "exec.h"
64 #include "builtins.h"
65 #include "var.h"
66 #include "options.h"
67 #include "input.h"
68 #include "output.h"
69 #include "syntax.h"
70 #include "memalloc.h"
71 #include "error.h"
72 #include "init.h"
73 #include "mystring.h"
74 #include "show.h"
75 #include "jobs.h"
76 #include "alias.h"
77 
78 
79 #define CMDTABLESIZE 31		/* should be prime */
80 #define ARB 1			/* actual size determined at run time */
81 
82 
83 
84 struct tblentry {
85 	struct tblentry *next;	/* next entry in hash chain */
86 	union param param;	/* definition of builtin function */
87 	int special;		/* flag for special builtin commands */
88 	short cmdtype;		/* index identifying command */
89 	char rehash;		/* if set, cd done since entry created */
90 	char cmdname[ARB];	/* name of command */
91 };
92 
93 
94 STATIC struct tblentry *cmdtable[CMDTABLESIZE];
95 STATIC int builtinloc = -1;		/* index in path of %builtin, or -1 */
96 int exerrno = 0;			/* Last exec error */
97 
98 
99 STATIC void tryexec(char *, char **, char **);
100 STATIC void printentry(struct tblentry *, int);
101 STATIC struct tblentry *cmdlookup(const char *, int);
102 STATIC void delete_cmd_entry(void);
103 
104 
105 
106 /*
107  * Exec a program.  Never returns.  If you change this routine, you may
108  * have to change the find_command routine as well.
109  */
110 
111 void
112 shellexec(char **argv, char **envp, const char *path, int idx)
113 {
114 	char *cmdname;
115 	int e;
116 
117 	if (strchr(argv[0], '/') != NULL) {
118 		tryexec(argv[0], argv, envp);
119 		e = errno;
120 	} else {
121 		e = ENOENT;
122 		while ((cmdname = padvance(&path, argv[0])) != NULL) {
123 			if (--idx < 0 && pathopt == NULL) {
124 				tryexec(cmdname, argv, envp);
125 				if (errno != ENOENT && errno != ENOTDIR)
126 					e = errno;
127 			}
128 			stunalloc(cmdname);
129 		}
130 	}
131 
132 	/* Map to POSIX errors */
133 	switch (e) {
134 	case EACCES:
135 		exerrno = 126;
136 		break;
137 	case ENOENT:
138 		exerrno = 127;
139 		break;
140 	default:
141 		exerrno = 2;
142 		break;
143 	}
144 	if (e == ENOENT || e == ENOTDIR)
145 		exerror(EXEXEC, "%s: not found", argv[0]);
146 	exerror(EXEXEC, "%s: %s", argv[0], strerror(e));
147 }
148 
149 
150 STATIC void
151 tryexec(char *cmd, char **argv, char **envp)
152 {
153 	int e;
154 
155 	execve(cmd, argv, envp);
156 	e = errno;
157 	if (e == ENOEXEC) {
158 		initshellproc();
159 		setinputfile(cmd, 0);
160 		commandname = arg0 = savestr(argv[0]);
161 		setparam(argv + 1);
162 		exraise(EXSHELLPROC);
163 		/*NOTREACHED*/
164 	}
165 	errno = e;
166 }
167 
168 /*
169  * Do a path search.  The variable path (passed by reference) should be
170  * set to the start of the path before the first call; padvance will update
171  * this value as it proceeds.  Successive calls to padvance will return
172  * the possible path expansions in sequence.  If an option (indicated by
173  * a percent sign) appears in the path entry then the global variable
174  * pathopt will be set to point to it; otherwise pathopt will be set to
175  * NULL.
176  */
177 
178 const char *pathopt;
179 
180 char *
181 padvance(const char **path, const char *name)
182 {
183 	const char *p, *start;
184 	char *q;
185 	int len;
186 
187 	if (*path == NULL)
188 		return NULL;
189 	start = *path;
190 	for (p = start; *p && *p != ':' && *p != '%'; p++)
191 		; /* nothing */
192 	len = p - start + strlen(name) + 2;	/* "2" is for '/' and '\0' */
193 	while (stackblocksize() < len)
194 		growstackblock();
195 	q = stackblock();
196 	if (p != start) {
197 		memcpy(q, start, p - start);
198 		q += p - start;
199 		*q++ = '/';
200 	}
201 	strcpy(q, name);
202 	pathopt = NULL;
203 	if (*p == '%') {
204 		pathopt = ++p;
205 		while (*p && *p != ':')  p++;
206 	}
207 	if (*p == ':')
208 		*path = p + 1;
209 	else
210 		*path = NULL;
211 	return stalloc(len);
212 }
213 
214 
215 
216 /*** Command hashing code ***/
217 
218 
219 int
220 hashcmd(int argc __unused, char **argv __unused)
221 {
222 	struct tblentry **pp;
223 	struct tblentry *cmdp;
224 	int c;
225 	int verbose;
226 	struct cmdentry entry;
227 	char *name;
228 
229 	verbose = 0;
230 	while ((c = nextopt("rv")) != '\0') {
231 		if (c == 'r') {
232 			clearcmdentry(0);
233 		} else if (c == 'v') {
234 			verbose++;
235 		}
236 	}
237 	if (*argptr == NULL) {
238 		for (pp = cmdtable ; pp < &cmdtable[CMDTABLESIZE] ; pp++) {
239 			for (cmdp = *pp ; cmdp ; cmdp = cmdp->next) {
240 				if (cmdp->cmdtype == CMDNORMAL)
241 					printentry(cmdp, verbose);
242 			}
243 		}
244 		return 0;
245 	}
246 	while ((name = *argptr) != NULL) {
247 		if ((cmdp = cmdlookup(name, 0)) != NULL
248 		 && (cmdp->cmdtype == CMDNORMAL
249 		     || (cmdp->cmdtype == CMDBUILTIN && builtinloc >= 0)))
250 			delete_cmd_entry();
251 		find_command(name, &entry, 1, pathval());
252 		if (verbose) {
253 			if (entry.cmdtype != CMDUNKNOWN) {	/* if no error msg */
254 				cmdp = cmdlookup(name, 0);
255 				if (cmdp != NULL)
256 					printentry(cmdp, verbose);
257 				else
258 					outfmt(out2, "%s: not found\n", name);
259 			}
260 			flushall();
261 		}
262 		argptr++;
263 	}
264 	return 0;
265 }
266 
267 
268 STATIC void
269 printentry(struct tblentry *cmdp, int verbose)
270 {
271 	int idx;
272 	const char *path;
273 	char *name;
274 
275 	if (cmdp->cmdtype == CMDNORMAL) {
276 		idx = cmdp->param.index;
277 		path = pathval();
278 		do {
279 			name = padvance(&path, cmdp->cmdname);
280 			stunalloc(name);
281 		} while (--idx >= 0);
282 		out1str(name);
283 	} else if (cmdp->cmdtype == CMDBUILTIN) {
284 		out1fmt("builtin %s", cmdp->cmdname);
285 	} else if (cmdp->cmdtype == CMDFUNCTION) {
286 		out1fmt("function %s", cmdp->cmdname);
287 		if (verbose) {
288 			INTOFF;
289 			name = commandtext(getfuncnode(cmdp->param.func));
290 			out1c(' ');
291 			out1str(name);
292 			ckfree(name);
293 			INTON;
294 		}
295 #ifdef DEBUG
296 	} else {
297 		error("internal error: cmdtype %d", cmdp->cmdtype);
298 #endif
299 	}
300 	if (cmdp->rehash)
301 		out1c('*');
302 	out1c('\n');
303 }
304 
305 
306 
307 /*
308  * Resolve a command name.  If you change this routine, you may have to
309  * change the shellexec routine as well.
310  */
311 
312 void
313 find_command(const char *name, struct cmdentry *entry, int printerr,
314     const char *path)
315 {
316 	struct tblentry *cmdp;
317 	int idx;
318 	int prev;
319 	char *fullname;
320 	struct stat statb;
321 	int e;
322 	int i;
323 	int spec;
324 
325 	/* If name contains a slash, don't use the hash table */
326 	if (strchr(name, '/') != NULL) {
327 		entry->cmdtype = CMDNORMAL;
328 		entry->u.index = 0;
329 		return;
330 	}
331 
332 	/* If name is in the table, and not invalidated by cd, we're done */
333 	if ((cmdp = cmdlookup(name, 0)) != NULL && cmdp->rehash == 0)
334 		goto success;
335 
336 	/* If %builtin not in path, check for builtin next */
337 	if (builtinloc < 0 && (i = find_builtin(name, &spec)) >= 0) {
338 		INTOFF;
339 		cmdp = cmdlookup(name, 1);
340 		cmdp->cmdtype = CMDBUILTIN;
341 		cmdp->param.index = i;
342 		cmdp->special = spec;
343 		INTON;
344 		goto success;
345 	}
346 
347 	/* We have to search path. */
348 	prev = -1;		/* where to start */
349 	if (cmdp) {		/* doing a rehash */
350 		if (cmdp->cmdtype == CMDBUILTIN)
351 			prev = builtinloc;
352 		else
353 			prev = cmdp->param.index;
354 	}
355 
356 	e = ENOENT;
357 	idx = -1;
358 loop:
359 	while ((fullname = padvance(&path, name)) != NULL) {
360 		stunalloc(fullname);
361 		idx++;
362 		if (pathopt) {
363 			if (prefix("builtin", pathopt)) {
364 				if ((i = find_builtin(name, &spec)) < 0)
365 					goto loop;
366 				INTOFF;
367 				cmdp = cmdlookup(name, 1);
368 				cmdp->cmdtype = CMDBUILTIN;
369 				cmdp->param.index = i;
370 				cmdp->special = spec;
371 				INTON;
372 				goto success;
373 			} else if (prefix("func", pathopt)) {
374 				/* handled below */
375 			} else {
376 				goto loop;	/* ignore unimplemented options */
377 			}
378 		}
379 		/* if rehash, don't redo absolute path names */
380 		if (fullname[0] == '/' && idx <= prev) {
381 			if (idx < prev)
382 				goto loop;
383 			TRACE(("searchexec \"%s\": no change\n", name));
384 			goto success;
385 		}
386 		if (stat(fullname, &statb) < 0) {
387 			if (errno != ENOENT && errno != ENOTDIR)
388 				e = errno;
389 			goto loop;
390 		}
391 		e = EACCES;	/* if we fail, this will be the error */
392 		if (!S_ISREG(statb.st_mode))
393 			goto loop;
394 		if (pathopt) {		/* this is a %func directory */
395 			stalloc(strlen(fullname) + 1);
396 			readcmdfile(fullname);
397 			if ((cmdp = cmdlookup(name, 0)) == NULL || cmdp->cmdtype != CMDFUNCTION)
398 				error("%s not defined in %s", name, fullname);
399 			stunalloc(fullname);
400 			goto success;
401 		}
402 #ifdef notdef
403 		if (statb.st_uid == geteuid()) {
404 			if ((statb.st_mode & 0100) == 0)
405 				goto loop;
406 		} else if (statb.st_gid == getegid()) {
407 			if ((statb.st_mode & 010) == 0)
408 				goto loop;
409 		} else {
410 			if ((statb.st_mode & 01) == 0)
411 				goto loop;
412 		}
413 #endif
414 		TRACE(("searchexec \"%s\" returns \"%s\"\n", name, fullname));
415 		INTOFF;
416 		cmdp = cmdlookup(name, 1);
417 		cmdp->cmdtype = CMDNORMAL;
418 		cmdp->param.index = idx;
419 		INTON;
420 		goto success;
421 	}
422 
423 	/* We failed.  If there was an entry for this command, delete it */
424 	if (cmdp)
425 		delete_cmd_entry();
426 	if (printerr) {
427 		if (e == ENOENT || e == ENOTDIR)
428 			outfmt(out2, "%s: not found\n", name);
429 		else
430 			outfmt(out2, "%s: %s\n", name, strerror(e));
431 	}
432 	entry->cmdtype = CMDUNKNOWN;
433 	entry->u.index = 0;
434 	return;
435 
436 success:
437 	cmdp->rehash = 0;
438 	entry->cmdtype = cmdp->cmdtype;
439 	entry->u = cmdp->param;
440 	entry->special = cmdp->special;
441 }
442 
443 
444 
445 /*
446  * Search the table of builtin commands.
447  */
448 
449 int
450 find_builtin(const char *name, int *special)
451 {
452 	const struct builtincmd *bp;
453 
454 	for (bp = builtincmd ; bp->name ; bp++) {
455 		if (*bp->name == *name && equal(bp->name, name)) {
456 			*special = bp->special;
457 			return bp->code;
458 		}
459 	}
460 	return -1;
461 }
462 
463 
464 
465 /*
466  * Called when a cd is done.  Marks all commands so the next time they
467  * are executed they will be rehashed.
468  */
469 
470 void
471 hashcd(void)
472 {
473 	struct tblentry **pp;
474 	struct tblentry *cmdp;
475 
476 	for (pp = cmdtable ; pp < &cmdtable[CMDTABLESIZE] ; pp++) {
477 		for (cmdp = *pp ; cmdp ; cmdp = cmdp->next) {
478 			if (cmdp->cmdtype == CMDNORMAL
479 			 || (cmdp->cmdtype == CMDBUILTIN && builtinloc >= 0))
480 				cmdp->rehash = 1;
481 		}
482 	}
483 }
484 
485 
486 
487 /*
488  * Called before PATH is changed.  The argument is the new value of PATH;
489  * pathval() still returns the old value at this point.  Called with
490  * interrupts off.
491  */
492 
493 void
494 changepath(const char *newval)
495 {
496 	const char *old, *new;
497 	int idx;
498 	int firstchange;
499 	int bltin;
500 
501 	old = pathval();
502 	new = newval;
503 	firstchange = 9999;	/* assume no change */
504 	idx = 0;
505 	bltin = -1;
506 	for (;;) {
507 		if (*old != *new) {
508 			firstchange = idx;
509 			if ((*old == '\0' && *new == ':')
510 			 || (*old == ':' && *new == '\0'))
511 				firstchange++;
512 			old = new;	/* ignore subsequent differences */
513 		}
514 		if (*new == '\0')
515 			break;
516 		if (*new == '%' && bltin < 0 && prefix("builtin", new + 1))
517 			bltin = idx;
518 		if (*new == ':') {
519 			idx++;
520 		}
521 		new++, old++;
522 	}
523 	if (builtinloc < 0 && bltin >= 0)
524 		builtinloc = bltin;		/* zap builtins */
525 	if (builtinloc >= 0 && bltin < 0)
526 		firstchange = 0;
527 	clearcmdentry(firstchange);
528 	builtinloc = bltin;
529 }
530 
531 
532 /*
533  * Clear out command entries.  The argument specifies the first entry in
534  * PATH which has changed.
535  */
536 
537 void
538 clearcmdentry(int firstchange)
539 {
540 	struct tblentry **tblp;
541 	struct tblentry **pp;
542 	struct tblentry *cmdp;
543 
544 	INTOFF;
545 	for (tblp = cmdtable ; tblp < &cmdtable[CMDTABLESIZE] ; tblp++) {
546 		pp = tblp;
547 		while ((cmdp = *pp) != NULL) {
548 			if ((cmdp->cmdtype == CMDNORMAL &&
549 			     cmdp->param.index >= firstchange)
550 			 || (cmdp->cmdtype == CMDBUILTIN &&
551 			     builtinloc >= firstchange)) {
552 				*pp = cmdp->next;
553 				ckfree(cmdp);
554 			} else {
555 				pp = &cmdp->next;
556 			}
557 		}
558 	}
559 	INTON;
560 }
561 
562 
563 /*
564  * Delete all functions.
565  */
566 
567 #ifdef mkinit
568 MKINIT void deletefuncs(void);
569 
570 SHELLPROC {
571 	deletefuncs();
572 }
573 #endif
574 
575 void
576 deletefuncs(void)
577 {
578 	struct tblentry **tblp;
579 	struct tblentry **pp;
580 	struct tblentry *cmdp;
581 
582 	INTOFF;
583 	for (tblp = cmdtable ; tblp < &cmdtable[CMDTABLESIZE] ; tblp++) {
584 		pp = tblp;
585 		while ((cmdp = *pp) != NULL) {
586 			if (cmdp->cmdtype == CMDFUNCTION) {
587 				*pp = cmdp->next;
588 				unreffunc(cmdp->param.func);
589 				ckfree(cmdp);
590 			} else {
591 				pp = &cmdp->next;
592 			}
593 		}
594 	}
595 	INTON;
596 }
597 
598 
599 
600 /*
601  * Locate a command in the command hash table.  If "add" is nonzero,
602  * add the command to the table if it is not already present.  The
603  * variable "lastcmdentry" is set to point to the address of the link
604  * pointing to the entry, so that delete_cmd_entry can delete the
605  * entry.
606  */
607 
608 STATIC struct tblentry **lastcmdentry;
609 
610 
611 STATIC struct tblentry *
612 cmdlookup(const char *name, int add)
613 {
614 	int hashval;
615 	const char *p;
616 	struct tblentry *cmdp;
617 	struct tblentry **pp;
618 
619 	p = name;
620 	hashval = *p << 4;
621 	while (*p)
622 		hashval += *p++;
623 	hashval &= 0x7FFF;
624 	pp = &cmdtable[hashval % CMDTABLESIZE];
625 	for (cmdp = *pp ; cmdp ; cmdp = cmdp->next) {
626 		if (equal(cmdp->cmdname, name))
627 			break;
628 		pp = &cmdp->next;
629 	}
630 	if (add && cmdp == NULL) {
631 		INTOFF;
632 		cmdp = *pp = ckmalloc(sizeof (struct tblentry) - ARB
633 					+ strlen(name) + 1);
634 		cmdp->next = NULL;
635 		cmdp->cmdtype = CMDUNKNOWN;
636 		cmdp->rehash = 0;
637 		strcpy(cmdp->cmdname, name);
638 		INTON;
639 	}
640 	lastcmdentry = pp;
641 	return cmdp;
642 }
643 
644 /*
645  * Delete the command entry returned on the last lookup.
646  */
647 
648 STATIC void
649 delete_cmd_entry(void)
650 {
651 	struct tblentry *cmdp;
652 
653 	INTOFF;
654 	cmdp = *lastcmdentry;
655 	*lastcmdentry = cmdp->next;
656 	ckfree(cmdp);
657 	INTON;
658 }
659 
660 
661 
662 /*
663  * Add a new command entry, replacing any existing command entry for
664  * the same name.
665  */
666 
667 void
668 addcmdentry(const char *name, struct cmdentry *entry)
669 {
670 	struct tblentry *cmdp;
671 
672 	INTOFF;
673 	cmdp = cmdlookup(name, 1);
674 	if (cmdp->cmdtype == CMDFUNCTION) {
675 		unreffunc(cmdp->param.func);
676 	}
677 	cmdp->cmdtype = entry->cmdtype;
678 	cmdp->param = entry->u;
679 	INTON;
680 }
681 
682 
683 /*
684  * Define a shell function.
685  */
686 
687 void
688 defun(const char *name, union node *func)
689 {
690 	struct cmdentry entry;
691 
692 	INTOFF;
693 	entry.cmdtype = CMDFUNCTION;
694 	entry.u.func = copyfunc(func);
695 	addcmdentry(name, &entry);
696 	INTON;
697 }
698 
699 
700 /*
701  * Delete a function if it exists.
702  */
703 
704 int
705 unsetfunc(const char *name)
706 {
707 	struct tblentry *cmdp;
708 
709 	if ((cmdp = cmdlookup(name, 0)) != NULL && cmdp->cmdtype == CMDFUNCTION) {
710 		unreffunc(cmdp->param.func);
711 		delete_cmd_entry();
712 		return (0);
713 	}
714 	return (0);
715 }
716 
717 /*
718  * Shared code for the following builtin commands:
719  *    type, command -v, command -V
720  */
721 
722 int
723 typecmd_impl(int argc, char **argv, int cmd, const char *path)
724 {
725 	struct cmdentry entry;
726 	struct tblentry *cmdp;
727 	const char *const *pp;
728 	struct alias *ap;
729 	int i;
730 	int error1 = 0;
731 
732 	if (path != pathval())
733 		clearcmdentry(0);
734 
735 	for (i = 1; i < argc; i++) {
736 		/* First look at the keywords */
737 		for (pp = parsekwd; *pp; pp++)
738 			if (**pp == *argv[i] && equal(*pp, argv[i]))
739 				break;
740 
741 		if (*pp) {
742 			if (cmd == TYPECMD_SMALLV)
743 				out1fmt("%s\n", argv[i]);
744 			else
745 				out1fmt("%s is a shell keyword\n", argv[i]);
746 			continue;
747 		}
748 
749 		/* Then look at the aliases */
750 		if ((ap = lookupalias(argv[i], 1)) != NULL) {
751 			if (cmd == TYPECMD_SMALLV)
752 				out1fmt("alias %s='%s'\n", argv[i], ap->val);
753 			else
754 				out1fmt("%s is an alias for %s\n", argv[i],
755 				    ap->val);
756 			continue;
757 		}
758 
759 		/* Then check if it is a tracked alias */
760 		if ((cmdp = cmdlookup(argv[i], 0)) != NULL) {
761 			entry.cmdtype = cmdp->cmdtype;
762 			entry.u = cmdp->param;
763 			entry.special = cmdp->special;
764 		}
765 		else {
766 			/* Finally use brute force */
767 			find_command(argv[i], &entry, 0, path);
768 		}
769 
770 		switch (entry.cmdtype) {
771 		case CMDNORMAL: {
772 			if (strchr(argv[i], '/') == NULL) {
773 				const char *path2 = path;
774 				char *name;
775 				int j = entry.u.index;
776 				do {
777 					name = padvance(&path2, argv[i]);
778 					stunalloc(name);
779 				} while (--j >= 0);
780 				if (cmd == TYPECMD_SMALLV)
781 					out1fmt("%s\n", name);
782 				else
783 					out1fmt("%s is%s %s\n", argv[i],
784 					    (cmdp && cmd == TYPECMD_TYPE) ?
785 						" a tracked alias for" : "",
786 					    name);
787 			} else {
788 				if (eaccess(argv[i], X_OK) == 0) {
789 					if (cmd == TYPECMD_SMALLV)
790 						out1fmt("%s\n", argv[i]);
791 					else
792 						out1fmt("%s is %s\n", argv[i],
793 						    argv[i]);
794 				} else {
795 					if (cmd != TYPECMD_SMALLV)
796 						outfmt(out2, "%s: %s\n",
797 						    argv[i], strerror(errno));
798 					error1 |= 127;
799 				}
800 			}
801 			break;
802 		}
803 		case CMDFUNCTION:
804 			if (cmd == TYPECMD_SMALLV)
805 				out1fmt("%s\n", argv[i]);
806 			else
807 				out1fmt("%s is a shell function\n", argv[i]);
808 			break;
809 
810 		case CMDBUILTIN:
811 			if (cmd == TYPECMD_SMALLV)
812 				out1fmt("%s\n", argv[i]);
813 			else if (entry.special)
814 				out1fmt("%s is a special shell builtin\n",
815 				    argv[i]);
816 			else
817 				out1fmt("%s is a shell builtin\n", argv[i]);
818 			break;
819 
820 		default:
821 			if (cmd != TYPECMD_SMALLV)
822 				outfmt(out2, "%s: not found\n", argv[i]);
823 			error1 |= 127;
824 			break;
825 		}
826 	}
827 
828 	if (path != pathval())
829 		clearcmdentry(0);
830 
831 	return error1;
832 }
833 
834 /*
835  * Locate and print what a word is...
836  */
837 
838 int
839 typecmd(int argc, char **argv)
840 {
841 	return typecmd_impl(argc, argv, TYPECMD_TYPE, bltinlookup("PATH", 1));
842 }
843