xref: /freebsd/bin/sh/exec.c (revision 3835f47c7e144fc73123f5d5706c6ec666cf3ae4)
14b88c807SRodney W. Grimes /*-
24b88c807SRodney W. Grimes  * Copyright (c) 1991, 1993
34b88c807SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
44b88c807SRodney W. Grimes  *
54b88c807SRodney W. Grimes  * This code is derived from software contributed to Berkeley by
64b88c807SRodney W. Grimes  * Kenneth Almquist.
74b88c807SRodney W. Grimes  *
84b88c807SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
94b88c807SRodney W. Grimes  * modification, are permitted provided that the following conditions
104b88c807SRodney W. Grimes  * are met:
114b88c807SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
124b88c807SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
134b88c807SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
144b88c807SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
154b88c807SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
164b88c807SRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
174b88c807SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
184b88c807SRodney W. Grimes  *    without specific prior written permission.
194b88c807SRodney W. Grimes  *
204b88c807SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
214b88c807SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
224b88c807SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
234b88c807SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
244b88c807SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
254b88c807SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
264b88c807SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
274b88c807SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
284b88c807SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
294b88c807SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
304b88c807SRodney W. Grimes  * SUCH DAMAGE.
314b88c807SRodney W. Grimes  */
324b88c807SRodney W. Grimes 
334b88c807SRodney W. Grimes #ifndef lint
343d7b5b93SPhilippe Charnier #if 0
353d7b5b93SPhilippe Charnier static char sccsid[] = "@(#)exec.c	8.4 (Berkeley) 6/8/95";
363d7b5b93SPhilippe Charnier #endif
374b88c807SRodney W. Grimes #endif /* not lint */
382749b141SDavid E. O'Brien #include <sys/cdefs.h>
392749b141SDavid E. O'Brien __FBSDID("$FreeBSD$");
404b88c807SRodney W. Grimes 
41aa9caaf6SPeter Wemm #include <sys/types.h>
42aa9caaf6SPeter Wemm #include <sys/stat.h>
43aa9caaf6SPeter Wemm #include <unistd.h>
44aa9caaf6SPeter Wemm #include <fcntl.h>
45aa9caaf6SPeter Wemm #include <errno.h>
46*3835f47cSJilles Tjoelker #include <paths.h>
47aa9caaf6SPeter Wemm #include <stdlib.h>
48aa9caaf6SPeter Wemm 
494b88c807SRodney W. Grimes /*
504b88c807SRodney W. Grimes  * When commands are first encountered, they are entered in a hash table.
514b88c807SRodney W. Grimes  * This ensures that a full path search will not have to be done for them
524b88c807SRodney W. Grimes  * on each invocation.
534b88c807SRodney W. Grimes  *
544b88c807SRodney W. Grimes  * We should investigate converting to a linear search, even though that
554b88c807SRodney W. Grimes  * would make the command name "hash" a misnomer.
564b88c807SRodney W. Grimes  */
574b88c807SRodney W. Grimes 
584b88c807SRodney W. Grimes #include "shell.h"
594b88c807SRodney W. Grimes #include "main.h"
604b88c807SRodney W. Grimes #include "nodes.h"
614b88c807SRodney W. Grimes #include "parser.h"
624b88c807SRodney W. Grimes #include "redir.h"
634b88c807SRodney W. Grimes #include "eval.h"
644b88c807SRodney W. Grimes #include "exec.h"
654b88c807SRodney W. Grimes #include "builtins.h"
664b88c807SRodney W. Grimes #include "var.h"
674b88c807SRodney W. Grimes #include "options.h"
684b88c807SRodney W. Grimes #include "input.h"
694b88c807SRodney W. Grimes #include "output.h"
704b88c807SRodney W. Grimes #include "syntax.h"
714b88c807SRodney W. Grimes #include "memalloc.h"
724b88c807SRodney W. Grimes #include "error.h"
734b88c807SRodney W. Grimes #include "init.h"
744b88c807SRodney W. Grimes #include "mystring.h"
75aa9caaf6SPeter Wemm #include "show.h"
764b88c807SRodney W. Grimes #include "jobs.h"
7776ad65f7SSteve Price #include "alias.h"
784b88c807SRodney W. Grimes 
794b88c807SRodney W. Grimes 
804b88c807SRodney W. Grimes #define CMDTABLESIZE 31		/* should be prime */
814b88c807SRodney W. Grimes #define ARB 1			/* actual size determined at run time */
824b88c807SRodney W. Grimes 
834b88c807SRodney W. Grimes 
844b88c807SRodney W. Grimes 
854b88c807SRodney W. Grimes struct tblentry {
864b88c807SRodney W. Grimes 	struct tblentry *next;	/* next entry in hash chain */
874b88c807SRodney W. Grimes 	union param param;	/* definition of builtin function */
8885170a4aSStefan Farfeleder 	int special;		/* flag for special builtin commands */
894b88c807SRodney W. Grimes 	short cmdtype;		/* index identifying command */
904b88c807SRodney W. Grimes 	char rehash;		/* if set, cd done since entry created */
914b88c807SRodney W. Grimes 	char cmdname[ARB];	/* name of command */
924b88c807SRodney W. Grimes };
934b88c807SRodney W. Grimes 
944b88c807SRodney W. Grimes 
95aa7b6f82SDavid E. O'Brien static struct tblentry *cmdtable[CMDTABLESIZE];
96ab0a2172SSteve Price int exerrno = 0;			/* Last exec error */
974b88c807SRodney W. Grimes 
984b88c807SRodney W. Grimes 
9988328642SDavid E. O'Brien static void tryexec(char *, char **, char **);
10088328642SDavid E. O'Brien static void printentry(struct tblentry *, int);
10188328642SDavid E. O'Brien static struct tblentry *cmdlookup(const char *, int);
10288328642SDavid E. O'Brien static void delete_cmd_entry(void);
1034b88c807SRodney W. Grimes 
1044b88c807SRodney W. Grimes 
1054b88c807SRodney W. Grimes 
1064b88c807SRodney W. Grimes /*
1074b88c807SRodney W. Grimes  * Exec a program.  Never returns.  If you change this routine, you may
1084b88c807SRodney W. Grimes  * have to change the find_command routine as well.
109*3835f47cSJilles Tjoelker  *
110*3835f47cSJilles Tjoelker  * The argv array may be changed and element argv[-1] should be writable.
1114b88c807SRodney W. Grimes  */
1124b88c807SRodney W. Grimes 
1134b88c807SRodney W. Grimes void
114384aedabSJilles Tjoelker shellexec(char **argv, char **envp, const char *path, int idx)
1154b88c807SRodney W. Grimes {
1164b88c807SRodney W. Grimes 	char *cmdname;
1174b88c807SRodney W. Grimes 	int e;
1184b88c807SRodney W. Grimes 
1194b88c807SRodney W. Grimes 	if (strchr(argv[0], '/') != NULL) {
1204b88c807SRodney W. Grimes 		tryexec(argv[0], argv, envp);
1214b88c807SRodney W. Grimes 		e = errno;
1224b88c807SRodney W. Grimes 	} else {
1234b88c807SRodney W. Grimes 		e = ENOENT;
1244b88c807SRodney W. Grimes 		while ((cmdname = padvance(&path, argv[0])) != NULL) {
125384aedabSJilles Tjoelker 			if (--idx < 0 && pathopt == NULL) {
1264b88c807SRodney W. Grimes 				tryexec(cmdname, argv, envp);
1274b88c807SRodney W. Grimes 				if (errno != ENOENT && errno != ENOTDIR)
1284b88c807SRodney W. Grimes 					e = errno;
1294b88c807SRodney W. Grimes 			}
1304b88c807SRodney W. Grimes 			stunalloc(cmdname);
1314b88c807SRodney W. Grimes 		}
1324b88c807SRodney W. Grimes 	}
133ab0a2172SSteve Price 
134ab0a2172SSteve Price 	/* Map to POSIX errors */
135834d160bSJilles Tjoelker 	if (e == ENOENT || e == ENOTDIR) {
136ab0a2172SSteve Price 		exerrno = 127;
1378c395729STim J. Robbins 		exerror(EXEXEC, "%s: not found", argv[0]);
138834d160bSJilles Tjoelker 	} else {
139834d160bSJilles Tjoelker 		exerrno = 126;
1401c59560dSTim J. Robbins 		exerror(EXEXEC, "%s: %s", argv[0], strerror(e));
1414b88c807SRodney W. Grimes 	}
142834d160bSJilles Tjoelker }
1434b88c807SRodney W. Grimes 
1444b88c807SRodney W. Grimes 
14588328642SDavid E. O'Brien static void
1465134c3f7SWarner Losh tryexec(char *cmd, char **argv, char **envp)
1474b88c807SRodney W. Grimes {
1484b88c807SRodney W. Grimes 	int e;
1494b88c807SRodney W. Grimes 
1504b88c807SRodney W. Grimes 	execve(cmd, argv, envp);
1514b88c807SRodney W. Grimes 	e = errno;
1524b88c807SRodney W. Grimes 	if (e == ENOEXEC) {
153*3835f47cSJilles Tjoelker 		*argv = cmd;
154*3835f47cSJilles Tjoelker 		*--argv = _PATH_BSHELL;
155*3835f47cSJilles Tjoelker 		execve(_PATH_BSHELL, argv, envp);
1564b88c807SRodney W. Grimes 	}
1574b88c807SRodney W. Grimes 	errno = e;
1584b88c807SRodney W. Grimes }
1594b88c807SRodney W. Grimes 
1604b88c807SRodney W. Grimes /*
1614b88c807SRodney W. Grimes  * Do a path search.  The variable path (passed by reference) should be
1624b88c807SRodney W. Grimes  * set to the start of the path before the first call; padvance will update
1634b88c807SRodney W. Grimes  * this value as it proceeds.  Successive calls to padvance will return
1644b88c807SRodney W. Grimes  * the possible path expansions in sequence.  If an option (indicated by
1654b88c807SRodney W. Grimes  * a percent sign) appears in the path entry then the global variable
1664b88c807SRodney W. Grimes  * pathopt will be set to point to it; otherwise pathopt will be set to
1674b88c807SRodney W. Grimes  * NULL.
1684b88c807SRodney W. Grimes  */
1694b88c807SRodney W. Grimes 
1702cac6e36SJilles Tjoelker const char *pathopt;
1714b88c807SRodney W. Grimes 
1724b88c807SRodney W. Grimes char *
1732cac6e36SJilles Tjoelker padvance(const char **path, const char *name)
1744b88c807SRodney W. Grimes {
1752cac6e36SJilles Tjoelker 	const char *p, *start;
1762cac6e36SJilles Tjoelker 	char *q;
1774b88c807SRodney W. Grimes 	int len;
1784b88c807SRodney W. Grimes 
1794b88c807SRodney W. Grimes 	if (*path == NULL)
1804b88c807SRodney W. Grimes 		return NULL;
1814b88c807SRodney W. Grimes 	start = *path;
18235f2d3b6SRalf S. Engelschall 	for (p = start; *p && *p != ':' && *p != '%'; p++)
18335f2d3b6SRalf S. Engelschall 		; /* nothing */
1844b88c807SRodney W. Grimes 	len = p - start + strlen(name) + 2;	/* "2" is for '/' and '\0' */
185d8f32e72SJilles Tjoelker 	STARTSTACKSTR(q);
186d8f32e72SJilles Tjoelker 	CHECKSTRSPACE(len, q);
1874b88c807SRodney W. Grimes 	if (p != start) {
188aa9caaf6SPeter Wemm 		memcpy(q, start, p - start);
1894b88c807SRodney W. Grimes 		q += p - start;
1904b88c807SRodney W. Grimes 		*q++ = '/';
1914b88c807SRodney W. Grimes 	}
1924b88c807SRodney W. Grimes 	strcpy(q, name);
1934b88c807SRodney W. Grimes 	pathopt = NULL;
1944b88c807SRodney W. Grimes 	if (*p == '%') {
1954b88c807SRodney W. Grimes 		pathopt = ++p;
1964b88c807SRodney W. Grimes 		while (*p && *p != ':')  p++;
1974b88c807SRodney W. Grimes 	}
1984b88c807SRodney W. Grimes 	if (*p == ':')
1994b88c807SRodney W. Grimes 		*path = p + 1;
2004b88c807SRodney W. Grimes 	else
2014b88c807SRodney W. Grimes 		*path = NULL;
2024b88c807SRodney W. Grimes 	return stalloc(len);
2034b88c807SRodney W. Grimes }
2044b88c807SRodney W. Grimes 
2054b88c807SRodney W. Grimes 
2064b88c807SRodney W. Grimes 
2074b88c807SRodney W. Grimes /*** Command hashing code ***/
2084b88c807SRodney W. Grimes 
2094b88c807SRodney W. Grimes 
210aa9caaf6SPeter Wemm int
2115134c3f7SWarner Losh hashcmd(int argc __unused, char **argv __unused)
212aa9caaf6SPeter Wemm {
2134b88c807SRodney W. Grimes 	struct tblentry **pp;
2144b88c807SRodney W. Grimes 	struct tblentry *cmdp;
2154b88c807SRodney W. Grimes 	int c;
2164b88c807SRodney W. Grimes 	int verbose;
2174b88c807SRodney W. Grimes 	struct cmdentry entry;
2184b88c807SRodney W. Grimes 	char *name;
2194b88c807SRodney W. Grimes 
2204b88c807SRodney W. Grimes 	verbose = 0;
2214b88c807SRodney W. Grimes 	while ((c = nextopt("rv")) != '\0') {
2224b88c807SRodney W. Grimes 		if (c == 'r') {
2234b88c807SRodney W. Grimes 			clearcmdentry(0);
2244b88c807SRodney W. Grimes 		} else if (c == 'v') {
2254b88c807SRodney W. Grimes 			verbose++;
2264b88c807SRodney W. Grimes 		}
2274b88c807SRodney W. Grimes 	}
2284b88c807SRodney W. Grimes 	if (*argptr == NULL) {
2294b88c807SRodney W. Grimes 		for (pp = cmdtable ; pp < &cmdtable[CMDTABLESIZE] ; pp++) {
2304b88c807SRodney W. Grimes 			for (cmdp = *pp ; cmdp ; cmdp = cmdp->next) {
23183952a11STim J. Robbins 				if (cmdp->cmdtype == CMDNORMAL)
2324b88c807SRodney W. Grimes 					printentry(cmdp, verbose);
2334b88c807SRodney W. Grimes 			}
2344b88c807SRodney W. Grimes 		}
2354b88c807SRodney W. Grimes 		return 0;
2364b88c807SRodney W. Grimes 	}
2374b88c807SRodney W. Grimes 	while ((name = *argptr) != NULL) {
2384b88c807SRodney W. Grimes 		if ((cmdp = cmdlookup(name, 0)) != NULL
2394b45b49aSJilles Tjoelker 		 && cmdp->cmdtype == CMDNORMAL)
2404b88c807SRodney W. Grimes 			delete_cmd_entry();
241c848bc18SJilles Tjoelker 		find_command(name, &entry, DO_ERR, pathval());
2424b88c807SRodney W. Grimes 		if (verbose) {
2434b88c807SRodney W. Grimes 			if (entry.cmdtype != CMDUNKNOWN) {	/* if no error msg */
2444b88c807SRodney W. Grimes 				cmdp = cmdlookup(name, 0);
2454c7e4a54SGeorge C A Reid 				if (cmdp != NULL)
2464b88c807SRodney W. Grimes 					printentry(cmdp, verbose);
2474c7e4a54SGeorge C A Reid 				else
248f7cc73afSJilles Tjoelker 					outfmt(out2, "%s: not found\n", name);
2494b88c807SRodney W. Grimes 			}
2504b88c807SRodney W. Grimes 			flushall();
2514b88c807SRodney W. Grimes 		}
2524b88c807SRodney W. Grimes 		argptr++;
2534b88c807SRodney W. Grimes 	}
2544b88c807SRodney W. Grimes 	return 0;
2554b88c807SRodney W. Grimes }
2564b88c807SRodney W. Grimes 
2574b88c807SRodney W. Grimes 
25888328642SDavid E. O'Brien static void
2595134c3f7SWarner Losh printentry(struct tblentry *cmdp, int verbose)
2604b88c807SRodney W. Grimes {
261384aedabSJilles Tjoelker 	int idx;
2622cac6e36SJilles Tjoelker 	const char *path;
2634b88c807SRodney W. Grimes 	char *name;
2644b88c807SRodney W. Grimes 
2654b88c807SRodney W. Grimes 	if (cmdp->cmdtype == CMDNORMAL) {
266384aedabSJilles Tjoelker 		idx = cmdp->param.index;
2674b88c807SRodney W. Grimes 		path = pathval();
2684b88c807SRodney W. Grimes 		do {
2694b88c807SRodney W. Grimes 			name = padvance(&path, cmdp->cmdname);
2704b88c807SRodney W. Grimes 			stunalloc(name);
271384aedabSJilles Tjoelker 		} while (--idx >= 0);
2724b88c807SRodney W. Grimes 		out1str(name);
2734b88c807SRodney W. Grimes 	} else if (cmdp->cmdtype == CMDBUILTIN) {
2744b88c807SRodney W. Grimes 		out1fmt("builtin %s", cmdp->cmdname);
2754b88c807SRodney W. Grimes 	} else if (cmdp->cmdtype == CMDFUNCTION) {
2764b88c807SRodney W. Grimes 		out1fmt("function %s", cmdp->cmdname);
2774b88c807SRodney W. Grimes 		if (verbose) {
2784b88c807SRodney W. Grimes 			INTOFF;
279e16947f8SJilles Tjoelker 			name = commandtext(getfuncnode(cmdp->param.func));
2804b88c807SRodney W. Grimes 			out1c(' ');
2814b88c807SRodney W. Grimes 			out1str(name);
2824b88c807SRodney W. Grimes 			ckfree(name);
2834b88c807SRodney W. Grimes 			INTON;
2844b88c807SRodney W. Grimes 		}
2854b88c807SRodney W. Grimes #ifdef DEBUG
2864b88c807SRodney W. Grimes 	} else {
2874b88c807SRodney W. Grimes 		error("internal error: cmdtype %d", cmdp->cmdtype);
2884b88c807SRodney W. Grimes #endif
2894b88c807SRodney W. Grimes 	}
2904b88c807SRodney W. Grimes 	if (cmdp->rehash)
2914b88c807SRodney W. Grimes 		out1c('*');
2924b88c807SRodney W. Grimes 	out1c('\n');
2934b88c807SRodney W. Grimes }
2944b88c807SRodney W. Grimes 
2954b88c807SRodney W. Grimes 
2964b88c807SRodney W. Grimes 
2974b88c807SRodney W. Grimes /*
2984b88c807SRodney W. Grimes  * Resolve a command name.  If you change this routine, you may have to
2994b88c807SRodney W. Grimes  * change the shellexec routine as well.
3004b88c807SRodney W. Grimes  */
3014b88c807SRodney W. Grimes 
3024b88c807SRodney W. Grimes void
303c848bc18SJilles Tjoelker find_command(const char *name, struct cmdentry *entry, int act,
3042cac6e36SJilles Tjoelker     const char *path)
3054b88c807SRodney W. Grimes {
306c848bc18SJilles Tjoelker 	struct tblentry *cmdp, loc_cmd;
307384aedabSJilles Tjoelker 	int idx;
3084b88c807SRodney W. Grimes 	int prev;
3094b88c807SRodney W. Grimes 	char *fullname;
3104b88c807SRodney W. Grimes 	struct stat statb;
3114b88c807SRodney W. Grimes 	int e;
3124b88c807SRodney W. Grimes 	int i;
31385170a4aSStefan Farfeleder 	int spec;
3144b88c807SRodney W. Grimes 
3154b88c807SRodney W. Grimes 	/* If name contains a slash, don't use the hash table */
3164b88c807SRodney W. Grimes 	if (strchr(name, '/') != NULL) {
3174b88c807SRodney W. Grimes 		entry->cmdtype = CMDNORMAL;
3184b88c807SRodney W. Grimes 		entry->u.index = 0;
3194b88c807SRodney W. Grimes 		return;
3204b88c807SRodney W. Grimes 	}
3214b88c807SRodney W. Grimes 
3224b88c807SRodney W. Grimes 	/* If name is in the table, and not invalidated by cd, we're done */
323c848bc18SJilles Tjoelker 	if ((cmdp = cmdlookup(name, 0)) != NULL && cmdp->rehash == 0) {
324c848bc18SJilles Tjoelker 		if (cmdp->cmdtype == CMDFUNCTION && act & DO_NOFUNC)
325c848bc18SJilles Tjoelker 			cmdp = NULL;
326c848bc18SJilles Tjoelker 		else
3274b88c807SRodney W. Grimes 			goto success;
328c848bc18SJilles Tjoelker 	}
3294b88c807SRodney W. Grimes 
3304b45b49aSJilles Tjoelker 	/* Check for builtin next */
3314b45b49aSJilles Tjoelker 	if ((i = find_builtin(name, &spec)) >= 0) {
3324b88c807SRodney W. Grimes 		INTOFF;
3334b88c807SRodney W. Grimes 		cmdp = cmdlookup(name, 1);
334c848bc18SJilles Tjoelker 		if (cmdp->cmdtype == CMDFUNCTION)
335c848bc18SJilles Tjoelker 			cmdp = &loc_cmd;
3364b88c807SRodney W. Grimes 		cmdp->cmdtype = CMDBUILTIN;
3374b88c807SRodney W. Grimes 		cmdp->param.index = i;
33885170a4aSStefan Farfeleder 		cmdp->special = spec;
3394b88c807SRodney W. Grimes 		INTON;
3404b88c807SRodney W. Grimes 		goto success;
3414b88c807SRodney W. Grimes 	}
3424b88c807SRodney W. Grimes 
3434b88c807SRodney W. Grimes 	/* We have to search path. */
3444b88c807SRodney W. Grimes 	prev = -1;		/* where to start */
3454b88c807SRodney W. Grimes 	if (cmdp) {		/* doing a rehash */
3464b88c807SRodney W. Grimes 		if (cmdp->cmdtype == CMDBUILTIN)
3474b45b49aSJilles Tjoelker 			prev = -1;
3484b88c807SRodney W. Grimes 		else
3494b88c807SRodney W. Grimes 			prev = cmdp->param.index;
3504b88c807SRodney W. Grimes 	}
3514b88c807SRodney W. Grimes 
3524b88c807SRodney W. Grimes 	e = ENOENT;
353384aedabSJilles Tjoelker 	idx = -1;
3544b88c807SRodney W. Grimes loop:
3554b88c807SRodney W. Grimes 	while ((fullname = padvance(&path, name)) != NULL) {
3564b88c807SRodney W. Grimes 		stunalloc(fullname);
357384aedabSJilles Tjoelker 		idx++;
3584b88c807SRodney W. Grimes 		if (pathopt) {
3594b45b49aSJilles Tjoelker 			if (prefix("func", pathopt)) {
3604b88c807SRodney W. Grimes 				/* handled below */
3614b88c807SRodney W. Grimes 			} else {
3624b88c807SRodney W. Grimes 				goto loop;	/* ignore unimplemented options */
3634b88c807SRodney W. Grimes 			}
3644b88c807SRodney W. Grimes 		}
3654b88c807SRodney W. Grimes 		/* if rehash, don't redo absolute path names */
366384aedabSJilles Tjoelker 		if (fullname[0] == '/' && idx <= prev) {
367384aedabSJilles Tjoelker 			if (idx < prev)
3684b88c807SRodney W. Grimes 				goto loop;
3694b88c807SRodney W. Grimes 			TRACE(("searchexec \"%s\": no change\n", name));
3704b88c807SRodney W. Grimes 			goto success;
3714b88c807SRodney W. Grimes 		}
3722628ebdbSTim J. Robbins 		if (stat(fullname, &statb) < 0) {
3734b88c807SRodney W. Grimes 			if (errno != ENOENT && errno != ENOTDIR)
3744b88c807SRodney W. Grimes 				e = errno;
3754b88c807SRodney W. Grimes 			goto loop;
3764b88c807SRodney W. Grimes 		}
3774b88c807SRodney W. Grimes 		e = EACCES;	/* if we fail, this will be the error */
378aa9caaf6SPeter Wemm 		if (!S_ISREG(statb.st_mode))
3794b88c807SRodney W. Grimes 			goto loop;
3804b88c807SRodney W. Grimes 		if (pathopt) {		/* this is a %func directory */
3814b88c807SRodney W. Grimes 			stalloc(strlen(fullname) + 1);
3824b88c807SRodney W. Grimes 			readcmdfile(fullname);
3834b88c807SRodney W. Grimes 			if ((cmdp = cmdlookup(name, 0)) == NULL || cmdp->cmdtype != CMDFUNCTION)
3844b88c807SRodney W. Grimes 				error("%s not defined in %s", name, fullname);
3854b88c807SRodney W. Grimes 			stunalloc(fullname);
3864b88c807SRodney W. Grimes 			goto success;
3874b88c807SRodney W. Grimes 		}
3884b88c807SRodney W. Grimes #ifdef notdef
3894b88c807SRodney W. Grimes 		if (statb.st_uid == geteuid()) {
3904b88c807SRodney W. Grimes 			if ((statb.st_mode & 0100) == 0)
3914b88c807SRodney W. Grimes 				goto loop;
3924b88c807SRodney W. Grimes 		} else if (statb.st_gid == getegid()) {
3934b88c807SRodney W. Grimes 			if ((statb.st_mode & 010) == 0)
3944b88c807SRodney W. Grimes 				goto loop;
3954b88c807SRodney W. Grimes 		} else {
3964b88c807SRodney W. Grimes 			if ((statb.st_mode & 01) == 0)
3974b88c807SRodney W. Grimes 				goto loop;
3984b88c807SRodney W. Grimes 		}
3994b88c807SRodney W. Grimes #endif
4004b88c807SRodney W. Grimes 		TRACE(("searchexec \"%s\" returns \"%s\"\n", name, fullname));
4014b88c807SRodney W. Grimes 		INTOFF;
4024b88c807SRodney W. Grimes 		cmdp = cmdlookup(name, 1);
403c848bc18SJilles Tjoelker 		if (cmdp->cmdtype == CMDFUNCTION)
404c848bc18SJilles Tjoelker 			cmdp = &loc_cmd;
4054b88c807SRodney W. Grimes 		cmdp->cmdtype = CMDNORMAL;
406384aedabSJilles Tjoelker 		cmdp->param.index = idx;
4074b88c807SRodney W. Grimes 		INTON;
4084b88c807SRodney W. Grimes 		goto success;
4094b88c807SRodney W. Grimes 	}
4104b88c807SRodney W. Grimes 
4114b88c807SRodney W. Grimes 	/* We failed.  If there was an entry for this command, delete it */
412c848bc18SJilles Tjoelker 	if (cmdp && cmdp->cmdtype != CMDFUNCTION)
4134b88c807SRodney W. Grimes 		delete_cmd_entry();
414c848bc18SJilles Tjoelker 	if (act & DO_ERR) {
4158c395729STim J. Robbins 		if (e == ENOENT || e == ENOTDIR)
4168c395729STim J. Robbins 			outfmt(out2, "%s: not found\n", name);
4178c395729STim J. Robbins 		else
4181c59560dSTim J. Robbins 			outfmt(out2, "%s: %s\n", name, strerror(e));
4198c395729STim J. Robbins 	}
4204b88c807SRodney W. Grimes 	entry->cmdtype = CMDUNKNOWN;
421640b70e4SJilles Tjoelker 	entry->u.index = 0;
4224b88c807SRodney W. Grimes 	return;
4234b88c807SRodney W. Grimes 
4244b88c807SRodney W. Grimes success:
4254b88c807SRodney W. Grimes 	cmdp->rehash = 0;
4264b88c807SRodney W. Grimes 	entry->cmdtype = cmdp->cmdtype;
4274b88c807SRodney W. Grimes 	entry->u = cmdp->param;
42885170a4aSStefan Farfeleder 	entry->special = cmdp->special;
4294b88c807SRodney W. Grimes }
4304b88c807SRodney W. Grimes 
4314b88c807SRodney W. Grimes 
4324b88c807SRodney W. Grimes 
4334b88c807SRodney W. Grimes /*
4344b88c807SRodney W. Grimes  * Search the table of builtin commands.
4354b88c807SRodney W. Grimes  */
4364b88c807SRodney W. Grimes 
4374b88c807SRodney W. Grimes int
4382cac6e36SJilles Tjoelker find_builtin(const char *name, int *special)
4394b88c807SRodney W. Grimes {
44076ad65f7SSteve Price 	const struct builtincmd *bp;
4414b88c807SRodney W. Grimes 
4424b88c807SRodney W. Grimes 	for (bp = builtincmd ; bp->name ; bp++) {
44385170a4aSStefan Farfeleder 		if (*bp->name == *name && equal(bp->name, name)) {
44485170a4aSStefan Farfeleder 			*special = bp->special;
4454b88c807SRodney W. Grimes 			return bp->code;
4464b88c807SRodney W. Grimes 		}
44785170a4aSStefan Farfeleder 	}
4484b88c807SRodney W. Grimes 	return -1;
4494b88c807SRodney W. Grimes }
4504b88c807SRodney W. Grimes 
4514b88c807SRodney W. Grimes 
4524b88c807SRodney W. Grimes 
4534b88c807SRodney W. Grimes /*
4544b88c807SRodney W. Grimes  * Called when a cd is done.  Marks all commands so the next time they
4554b88c807SRodney W. Grimes  * are executed they will be rehashed.
4564b88c807SRodney W. Grimes  */
4574b88c807SRodney W. Grimes 
4584b88c807SRodney W. Grimes void
4595134c3f7SWarner Losh hashcd(void)
4605134c3f7SWarner Losh {
4614b88c807SRodney W. Grimes 	struct tblentry **pp;
4624b88c807SRodney W. Grimes 	struct tblentry *cmdp;
4634b88c807SRodney W. Grimes 
4644b88c807SRodney W. Grimes 	for (pp = cmdtable ; pp < &cmdtable[CMDTABLESIZE] ; pp++) {
4654b88c807SRodney W. Grimes 		for (cmdp = *pp ; cmdp ; cmdp = cmdp->next) {
4664b45b49aSJilles Tjoelker 			if (cmdp->cmdtype == CMDNORMAL)
4674b88c807SRodney W. Grimes 				cmdp->rehash = 1;
4684b88c807SRodney W. Grimes 		}
4694b88c807SRodney W. Grimes 	}
4704b88c807SRodney W. Grimes }
4714b88c807SRodney W. Grimes 
4724b88c807SRodney W. Grimes 
4734b88c807SRodney W. Grimes 
4744b88c807SRodney W. Grimes /*
4754b88c807SRodney W. Grimes  * Called before PATH is changed.  The argument is the new value of PATH;
4764b88c807SRodney W. Grimes  * pathval() still returns the old value at this point.  Called with
4774b88c807SRodney W. Grimes  * interrupts off.
4784b88c807SRodney W. Grimes  */
4794b88c807SRodney W. Grimes 
4804b88c807SRodney W. Grimes void
4815134c3f7SWarner Losh changepath(const char *newval)
4824b88c807SRodney W. Grimes {
483ab0a2172SSteve Price 	const char *old, *new;
484384aedabSJilles Tjoelker 	int idx;
4854b88c807SRodney W. Grimes 	int firstchange;
4864b88c807SRodney W. Grimes 
4874b88c807SRodney W. Grimes 	old = pathval();
4884b88c807SRodney W. Grimes 	new = newval;
4894b88c807SRodney W. Grimes 	firstchange = 9999;	/* assume no change */
490384aedabSJilles Tjoelker 	idx = 0;
4914b88c807SRodney W. Grimes 	for (;;) {
4924b88c807SRodney W. Grimes 		if (*old != *new) {
493384aedabSJilles Tjoelker 			firstchange = idx;
494aa9caaf6SPeter Wemm 			if ((*old == '\0' && *new == ':')
495aa9caaf6SPeter Wemm 			 || (*old == ':' && *new == '\0'))
4964b88c807SRodney W. Grimes 				firstchange++;
4974b88c807SRodney W. Grimes 			old = new;	/* ignore subsequent differences */
4984b88c807SRodney W. Grimes 		}
4994b88c807SRodney W. Grimes 		if (*new == '\0')
5004b88c807SRodney W. Grimes 			break;
5014b88c807SRodney W. Grimes 		if (*new == ':') {
502384aedabSJilles Tjoelker 			idx++;
5034b88c807SRodney W. Grimes 		}
5044b88c807SRodney W. Grimes 		new++, old++;
5054b88c807SRodney W. Grimes 	}
5064b88c807SRodney W. Grimes 	clearcmdentry(firstchange);
5074b88c807SRodney W. Grimes }
5084b88c807SRodney W. Grimes 
5094b88c807SRodney W. Grimes 
5104b88c807SRodney W. Grimes /*
5114b88c807SRodney W. Grimes  * Clear out command entries.  The argument specifies the first entry in
5124b88c807SRodney W. Grimes  * PATH which has changed.
5134b88c807SRodney W. Grimes  */
5144b88c807SRodney W. Grimes 
515a436dc79SMartin Cracauer void
5165134c3f7SWarner Losh clearcmdentry(int firstchange)
517aa9caaf6SPeter Wemm {
5184b88c807SRodney W. Grimes 	struct tblentry **tblp;
5194b88c807SRodney W. Grimes 	struct tblentry **pp;
5204b88c807SRodney W. Grimes 	struct tblentry *cmdp;
5214b88c807SRodney W. Grimes 
5224b88c807SRodney W. Grimes 	INTOFF;
5234b88c807SRodney W. Grimes 	for (tblp = cmdtable ; tblp < &cmdtable[CMDTABLESIZE] ; tblp++) {
5244b88c807SRodney W. Grimes 		pp = tblp;
5254b88c807SRodney W. Grimes 		while ((cmdp = *pp) != NULL) {
526aa9caaf6SPeter Wemm 			if ((cmdp->cmdtype == CMDNORMAL &&
5274b45b49aSJilles Tjoelker 			     cmdp->param.index >= firstchange)) {
5284b88c807SRodney W. Grimes 				*pp = cmdp->next;
5294b88c807SRodney W. Grimes 				ckfree(cmdp);
5304b88c807SRodney W. Grimes 			} else {
5314b88c807SRodney W. Grimes 				pp = &cmdp->next;
5324b88c807SRodney W. Grimes 			}
5334b88c807SRodney W. Grimes 		}
5344b88c807SRodney W. Grimes 	}
5354b88c807SRodney W. Grimes 	INTON;
5364b88c807SRodney W. Grimes }
5374b88c807SRodney W. Grimes 
5384b88c807SRodney W. Grimes 
5394b88c807SRodney W. Grimes /*
5404b88c807SRodney W. Grimes  * Locate a command in the command hash table.  If "add" is nonzero,
5414b88c807SRodney W. Grimes  * add the command to the table if it is not already present.  The
5424b88c807SRodney W. Grimes  * variable "lastcmdentry" is set to point to the address of the link
5434b88c807SRodney W. Grimes  * pointing to the entry, so that delete_cmd_entry can delete the
5444b88c807SRodney W. Grimes  * entry.
5454b88c807SRodney W. Grimes  */
5464b88c807SRodney W. Grimes 
547aa7b6f82SDavid E. O'Brien static struct tblentry **lastcmdentry;
5484b88c807SRodney W. Grimes 
5494b88c807SRodney W. Grimes 
55088328642SDavid E. O'Brien static struct tblentry *
5512cac6e36SJilles Tjoelker cmdlookup(const char *name, int add)
5524b88c807SRodney W. Grimes {
5534b88c807SRodney W. Grimes 	int hashval;
5542cac6e36SJilles Tjoelker 	const char *p;
5554b88c807SRodney W. Grimes 	struct tblentry *cmdp;
5564b88c807SRodney W. Grimes 	struct tblentry **pp;
5574b88c807SRodney W. Grimes 
5584b88c807SRodney W. Grimes 	p = name;
5594b88c807SRodney W. Grimes 	hashval = *p << 4;
5604b88c807SRodney W. Grimes 	while (*p)
5614b88c807SRodney W. Grimes 		hashval += *p++;
5624b88c807SRodney W. Grimes 	hashval &= 0x7FFF;
5634b88c807SRodney W. Grimes 	pp = &cmdtable[hashval % CMDTABLESIZE];
5644b88c807SRodney W. Grimes 	for (cmdp = *pp ; cmdp ; cmdp = cmdp->next) {
5654b88c807SRodney W. Grimes 		if (equal(cmdp->cmdname, name))
5664b88c807SRodney W. Grimes 			break;
5674b88c807SRodney W. Grimes 		pp = &cmdp->next;
5684b88c807SRodney W. Grimes 	}
5694b88c807SRodney W. Grimes 	if (add && cmdp == NULL) {
5704b88c807SRodney W. Grimes 		INTOFF;
5714b88c807SRodney W. Grimes 		cmdp = *pp = ckmalloc(sizeof (struct tblentry) - ARB
5724b88c807SRodney W. Grimes 					+ strlen(name) + 1);
5734b88c807SRodney W. Grimes 		cmdp->next = NULL;
5744b88c807SRodney W. Grimes 		cmdp->cmdtype = CMDUNKNOWN;
5754b88c807SRodney W. Grimes 		cmdp->rehash = 0;
5764b88c807SRodney W. Grimes 		strcpy(cmdp->cmdname, name);
5774b88c807SRodney W. Grimes 		INTON;
5784b88c807SRodney W. Grimes 	}
5794b88c807SRodney W. Grimes 	lastcmdentry = pp;
5804b88c807SRodney W. Grimes 	return cmdp;
5814b88c807SRodney W. Grimes }
5824b88c807SRodney W. Grimes 
5834b88c807SRodney W. Grimes /*
5844b88c807SRodney W. Grimes  * Delete the command entry returned on the last lookup.
5854b88c807SRodney W. Grimes  */
5864b88c807SRodney W. Grimes 
58788328642SDavid E. O'Brien static void
5885134c3f7SWarner Losh delete_cmd_entry(void)
5895134c3f7SWarner Losh {
5904b88c807SRodney W. Grimes 	struct tblentry *cmdp;
5914b88c807SRodney W. Grimes 
5924b88c807SRodney W. Grimes 	INTOFF;
5934b88c807SRodney W. Grimes 	cmdp = *lastcmdentry;
5944b88c807SRodney W. Grimes 	*lastcmdentry = cmdp->next;
5954b88c807SRodney W. Grimes 	ckfree(cmdp);
5964b88c807SRodney W. Grimes 	INTON;
5974b88c807SRodney W. Grimes }
5984b88c807SRodney W. Grimes 
5994b88c807SRodney W. Grimes 
6004b88c807SRodney W. Grimes 
6014b88c807SRodney W. Grimes /*
6024b88c807SRodney W. Grimes  * Add a new command entry, replacing any existing command entry for
6034b88c807SRodney W. Grimes  * the same name.
6044b88c807SRodney W. Grimes  */
6054b88c807SRodney W. Grimes 
6064b88c807SRodney W. Grimes void
6072cac6e36SJilles Tjoelker addcmdentry(const char *name, struct cmdentry *entry)
6084b88c807SRodney W. Grimes {
6094b88c807SRodney W. Grimes 	struct tblentry *cmdp;
6104b88c807SRodney W. Grimes 
6114b88c807SRodney W. Grimes 	INTOFF;
6124b88c807SRodney W. Grimes 	cmdp = cmdlookup(name, 1);
6134b88c807SRodney W. Grimes 	if (cmdp->cmdtype == CMDFUNCTION) {
614eb33e843SJilles Tjoelker 		unreffunc(cmdp->param.func);
6154b88c807SRodney W. Grimes 	}
6164b88c807SRodney W. Grimes 	cmdp->cmdtype = entry->cmdtype;
6174b88c807SRodney W. Grimes 	cmdp->param = entry->u;
6184b88c807SRodney W. Grimes 	INTON;
6194b88c807SRodney W. Grimes }
6204b88c807SRodney W. Grimes 
6214b88c807SRodney W. Grimes 
6224b88c807SRodney W. Grimes /*
6234b88c807SRodney W. Grimes  * Define a shell function.
6244b88c807SRodney W. Grimes  */
6254b88c807SRodney W. Grimes 
6264b88c807SRodney W. Grimes void
6272cac6e36SJilles Tjoelker defun(const char *name, union node *func)
6284b88c807SRodney W. Grimes {
6294b88c807SRodney W. Grimes 	struct cmdentry entry;
6304b88c807SRodney W. Grimes 
6314b88c807SRodney W. Grimes 	INTOFF;
6324b88c807SRodney W. Grimes 	entry.cmdtype = CMDFUNCTION;
6334b88c807SRodney W. Grimes 	entry.u.func = copyfunc(func);
6344b88c807SRodney W. Grimes 	addcmdentry(name, &entry);
6354b88c807SRodney W. Grimes 	INTON;
6364b88c807SRodney W. Grimes }
6374b88c807SRodney W. Grimes 
6384b88c807SRodney W. Grimes 
6394b88c807SRodney W. Grimes /*
6404b88c807SRodney W. Grimes  * Delete a function if it exists.
6414b88c807SRodney W. Grimes  */
6424b88c807SRodney W. Grimes 
6434b88c807SRodney W. Grimes int
6442cac6e36SJilles Tjoelker unsetfunc(const char *name)
6454b88c807SRodney W. Grimes {
6464b88c807SRodney W. Grimes 	struct tblentry *cmdp;
6474b88c807SRodney W. Grimes 
6484b88c807SRodney W. Grimes 	if ((cmdp = cmdlookup(name, 0)) != NULL && cmdp->cmdtype == CMDFUNCTION) {
649eb33e843SJilles Tjoelker 		unreffunc(cmdp->param.func);
6504b88c807SRodney W. Grimes 		delete_cmd_entry();
6514b88c807SRodney W. Grimes 		return (0);
6524b88c807SRodney W. Grimes 	}
653bd766773SDag-Erling Smørgrav 	return (0);
6544b88c807SRodney W. Grimes }
65576ad65f7SSteve Price 
65676ad65f7SSteve Price /*
657b2f153feSStefan Farfeleder  * Shared code for the following builtin commands:
658b2f153feSStefan Farfeleder  *    type, command -v, command -V
65976ad65f7SSteve Price  */
66076ad65f7SSteve Price 
66176ad65f7SSteve Price int
66206a8a57fSJilles Tjoelker typecmd_impl(int argc, char **argv, int cmd, const char *path)
66376ad65f7SSteve Price {
66476ad65f7SSteve Price 	struct cmdentry entry;
66576ad65f7SSteve Price 	struct tblentry *cmdp;
666384aedabSJilles Tjoelker 	const char *const *pp;
66776ad65f7SSteve Price 	struct alias *ap;
66876ad65f7SSteve Price 	int i;
669384aedabSJilles Tjoelker 	int error1 = 0;
67076ad65f7SSteve Price 
67106a8a57fSJilles Tjoelker 	if (path != pathval())
67206a8a57fSJilles Tjoelker 		clearcmdentry(0);
67306a8a57fSJilles Tjoelker 
67476ad65f7SSteve Price 	for (i = 1; i < argc; i++) {
67576ad65f7SSteve Price 		/* First look at the keywords */
676384aedabSJilles Tjoelker 		for (pp = parsekwd; *pp; pp++)
67776ad65f7SSteve Price 			if (**pp == *argv[i] && equal(*pp, argv[i]))
67876ad65f7SSteve Price 				break;
67976ad65f7SSteve Price 
68076ad65f7SSteve Price 		if (*pp) {
681b2f153feSStefan Farfeleder 			if (cmd == TYPECMD_SMALLV)
682b2f153feSStefan Farfeleder 				out1fmt("%s\n", argv[i]);
683b2f153feSStefan Farfeleder 			else
684f7bbf3ffSStefan Farfeleder 				out1fmt("%s is a shell keyword\n", argv[i]);
68576ad65f7SSteve Price 			continue;
68676ad65f7SSteve Price 		}
68776ad65f7SSteve Price 
68876ad65f7SSteve Price 		/* Then look at the aliases */
68976ad65f7SSteve Price 		if ((ap = lookupalias(argv[i], 1)) != NULL) {
690b2f153feSStefan Farfeleder 			if (cmd == TYPECMD_SMALLV)
691b2f153feSStefan Farfeleder 				out1fmt("alias %s='%s'\n", argv[i], ap->val);
692b2f153feSStefan Farfeleder 			else
693f7bbf3ffSStefan Farfeleder 				out1fmt("%s is an alias for %s\n", argv[i],
694f7bbf3ffSStefan Farfeleder 				    ap->val);
69576ad65f7SSteve Price 			continue;
69676ad65f7SSteve Price 		}
69776ad65f7SSteve Price 
69876ad65f7SSteve Price 		/* Then check if it is a tracked alias */
69976ad65f7SSteve Price 		if ((cmdp = cmdlookup(argv[i], 0)) != NULL) {
70076ad65f7SSteve Price 			entry.cmdtype = cmdp->cmdtype;
70176ad65f7SSteve Price 			entry.u = cmdp->param;
70230268dfaSJilles Tjoelker 			entry.special = cmdp->special;
70376ad65f7SSteve Price 		}
70476ad65f7SSteve Price 		else {
70576ad65f7SSteve Price 			/* Finally use brute force */
70606a8a57fSJilles Tjoelker 			find_command(argv[i], &entry, 0, path);
70776ad65f7SSteve Price 		}
70876ad65f7SSteve Price 
70976ad65f7SSteve Price 		switch (entry.cmdtype) {
71076ad65f7SSteve Price 		case CMDNORMAL: {
711d753a425SMartin Cracauer 			if (strchr(argv[i], '/') == NULL) {
71206a8a57fSJilles Tjoelker 				const char *path2 = path;
7132cac6e36SJilles Tjoelker 				char *name;
714d753a425SMartin Cracauer 				int j = entry.u.index;
71576ad65f7SSteve Price 				do {
71606a8a57fSJilles Tjoelker 					name = padvance(&path2, argv[i]);
71776ad65f7SSteve Price 					stunalloc(name);
71876ad65f7SSteve Price 				} while (--j >= 0);
719b2f153feSStefan Farfeleder 				if (cmd == TYPECMD_SMALLV)
720b2f153feSStefan Farfeleder 					out1fmt("%s\n", name);
721b2f153feSStefan Farfeleder 				else
722f7bbf3ffSStefan Farfeleder 					out1fmt("%s is%s %s\n", argv[i],
723b2f153feSStefan Farfeleder 					    (cmdp && cmd == TYPECMD_TYPE) ?
724b2f153feSStefan Farfeleder 						" a tracked alias for" : "",
725b2f153feSStefan Farfeleder 					    name);
726d753a425SMartin Cracauer 			} else {
727d92e35fdSStefan Farfeleder 				if (eaccess(argv[i], X_OK) == 0) {
728b2f153feSStefan Farfeleder 					if (cmd == TYPECMD_SMALLV)
729b2f153feSStefan Farfeleder 						out1fmt("%s\n", argv[i]);
730b2f153feSStefan Farfeleder 					else
731f7bbf3ffSStefan Farfeleder 						out1fmt("%s is %s\n", argv[i],
732f7bbf3ffSStefan Farfeleder 						    argv[i]);
733f30842baSStefan Farfeleder 				} else {
734f30842baSStefan Farfeleder 					if (cmd != TYPECMD_SMALLV)
735f7bbf3ffSStefan Farfeleder 						outfmt(out2, "%s: %s\n",
736f7bbf3ffSStefan Farfeleder 						    argv[i], strerror(errno));
737384aedabSJilles Tjoelker 					error1 |= 127;
738b2f153feSStefan Farfeleder 				}
739d753a425SMartin Cracauer 			}
74076ad65f7SSteve Price 			break;
74176ad65f7SSteve Price 		}
74276ad65f7SSteve Price 		case CMDFUNCTION:
743b2f153feSStefan Farfeleder 			if (cmd == TYPECMD_SMALLV)
744b2f153feSStefan Farfeleder 				out1fmt("%s\n", argv[i]);
745b2f153feSStefan Farfeleder 			else
746f7bbf3ffSStefan Farfeleder 				out1fmt("%s is a shell function\n", argv[i]);
74776ad65f7SSteve Price 			break;
74876ad65f7SSteve Price 
74976ad65f7SSteve Price 		case CMDBUILTIN:
750b2f153feSStefan Farfeleder 			if (cmd == TYPECMD_SMALLV)
751b2f153feSStefan Farfeleder 				out1fmt("%s\n", argv[i]);
75230268dfaSJilles Tjoelker 			else if (entry.special)
75330268dfaSJilles Tjoelker 				out1fmt("%s is a special shell builtin\n",
75430268dfaSJilles Tjoelker 				    argv[i]);
755b2f153feSStefan Farfeleder 			else
756f7bbf3ffSStefan Farfeleder 				out1fmt("%s is a shell builtin\n", argv[i]);
75776ad65f7SSteve Price 			break;
75876ad65f7SSteve Price 
75976ad65f7SSteve Price 		default:
760b2f153feSStefan Farfeleder 			if (cmd != TYPECMD_SMALLV)
761f7bbf3ffSStefan Farfeleder 				outfmt(out2, "%s: not found\n", argv[i]);
762384aedabSJilles Tjoelker 			error1 |= 127;
76376ad65f7SSteve Price 			break;
76476ad65f7SSteve Price 		}
76576ad65f7SSteve Price 	}
76606a8a57fSJilles Tjoelker 
76706a8a57fSJilles Tjoelker 	if (path != pathval())
76806a8a57fSJilles Tjoelker 		clearcmdentry(0);
76906a8a57fSJilles Tjoelker 
770384aedabSJilles Tjoelker 	return error1;
77176ad65f7SSteve Price }
772b2f153feSStefan Farfeleder 
773b2f153feSStefan Farfeleder /*
774b2f153feSStefan Farfeleder  * Locate and print what a word is...
775b2f153feSStefan Farfeleder  */
776b2f153feSStefan Farfeleder 
777b2f153feSStefan Farfeleder int
778b2f153feSStefan Farfeleder typecmd(int argc, char **argv)
779b2f153feSStefan Farfeleder {
7800fb60646SJilles Tjoelker 	return typecmd_impl(argc, argv, TYPECMD_TYPE, bltinlookup("PATH", 1));
781b2f153feSStefan Farfeleder }
782