2 * Copyright (c) 1991, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
32 * @(#)exec.c 8.4 (Berkeley) 6/8/95
33 * $FreeBSD: head/bin/sh/exec.c 245426 2013-01-14 16:40:50Z jilles $
36 #include <sys/types.h>
45 * When commands are first encountered, they are entered in a hash table.
46 * This ensures that a full path search will not have to be done for them
49 * We should investigate converting to a linear search, even though that
50 * would make the command name "hash" a misnomer.
75 #define CMDTABLESIZE 31 /* should be prime */
80 struct tblentry *next; /* next entry in hash chain */
81 union param param; /* definition of builtin function */
82 int special; /* flag for special builtin commands */
83 signed char cmdtype; /* index identifying command */
84 char cmdname[]; /* name of command */
88 static struct tblentry *cmdtable[CMDTABLESIZE];
89 static int cmdtable_cd = 0; /* cmdtable contains cd-dependent entries */
90 int exerrno = 0; /* Last exec error */
93 static void tryexec(char *, char **, char **);
94 static void printentry(struct tblentry *, int);
95 static struct tblentry *cmdlookup(const char *, int);
96 static void delete_cmd_entry(void);
97 static void addcmdentry(const char *, struct cmdentry *);
100 * Exec a program. Never returns. If you change this routine, you may
101 * have to change the find_command routine as well.
103 * The argv array may be changed and element argv[-1] should be writable.
107 shellexec(char **argv, char **envp, const char *path, int idx)
112 if (strchr(argv[0], '/') != NULL) {
113 tryexec(argv[0], argv, envp);
117 while ((cmdname = padvance(&path, argv[0])) != NULL) {
118 if (--idx < 0 && pathopt == NULL) {
119 tryexec(cmdname, argv, envp);
120 if (errno != ENOENT && errno != ENOTDIR)
129 /* Map to POSIX errors */
130 if (e == ENOENT || e == ENOTDIR) {
132 exerror(EXEXEC, "%s: not found", argv[0]);
135 exerror(EXEXEC, "%s: %s", argv[0], strerror(e));
141 tryexec(char *cmd, char **argv, char **envp)
147 execve(cmd, argv, envp);
151 in = open(cmd, O_RDONLY | O_NONBLOCK);
153 n = pread(in, buf, sizeof buf, 0);
155 if (n > 0 && memchr(buf, '\0', n) != NULL) {
161 *--argv = __DECONST(char *, _PATH_BSHELL);
162 execve(_PATH_BSHELL, argv, envp);
168 * Do a path search. The variable path (passed by reference) should be
169 * set to the start of the path before the first call; padvance will update
170 * this value as it proceeds. Successive calls to padvance will return
171 * the possible path expansions in sequence. If an option (indicated by
172 * a percent sign) appears in the path entry then the global variable
173 * pathopt will be set to point to it; otherwise pathopt will be set to
180 padvance(const char **path, const char *name)
182 const char *p, *start;
189 for (p = start; *p && *p != ':' && *p != '%'; p++)
191 len = p - start + strlen(name) + 2; /* "2" is for '/' and '\0' */
193 CHECKSTRSPACE(len, q);
195 memcpy(q, start, p - start);
203 while (*p && *p != ':') p++;
214 /*** Command hashing code ***/
218 hashcmd(int argc __unused, char **argv __unused)
220 struct tblentry **pp;
221 struct tblentry *cmdp;
224 struct cmdentry entry;
230 while ((c = nextopt("rv")) != '\0') {
233 } else if (c == 'v') {
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);
246 while ((name = *argptr) != NULL) {
247 if ((cmdp = cmdlookup(name, 0)) != NULL
248 && cmdp->cmdtype == CMDNORMAL)
250 find_command(name, &entry, DO_ERR, pathval());
251 if (entry.cmdtype == CMDUNKNOWN)
254 cmdp = cmdlookup(name, 0);
256 printentry(cmdp, verbose);
258 outfmt(out2, "%s: not found\n", name);
270 printentry(struct tblentry *cmdp, int verbose)
276 if (cmdp->cmdtype == CMDNORMAL) {
277 idx = cmdp->param.index;
280 name = padvance(&path, cmdp->cmdname);
282 } while (--idx >= 0);
284 } else if (cmdp->cmdtype == CMDBUILTIN) {
285 out1fmt("builtin %s", cmdp->cmdname);
286 } else if (cmdp->cmdtype == CMDFUNCTION) {
287 out1fmt("function %s", cmdp->cmdname);
290 name = commandtext(getfuncnode(cmdp->param.func));
298 error("internal error: cmdtype %d", cmdp->cmdtype);
307 * Resolve a command name. If you change this routine, you may have to
308 * change the shellexec routine as well.
312 find_command(const char *name, struct cmdentry *entry, int act,
315 struct tblentry *cmdp, loc_cmd;
324 /* If name contains a slash, don't use the hash table */
325 if (strchr(name, '/') != NULL) {
326 entry->cmdtype = CMDNORMAL;
333 /* If name is in the table, and not invalidated by cd, we're done */
334 if ((cmdp = cmdlookup(name, 0)) != NULL) {
335 if (cmdp->cmdtype == CMDFUNCTION && act & DO_NOFUNC)
341 /* Check for builtin next */
342 if ((i = find_builtin(name, &spec)) >= 0) {
344 cmdp = cmdlookup(name, 1);
345 if (cmdp->cmdtype == CMDFUNCTION)
347 cmdp->cmdtype = CMDBUILTIN;
348 cmdp->param.index = i;
349 cmdp->special = spec;
354 /* We have to search path. */
359 while ((fullname = padvance(&path, name)) != NULL) {
363 if (prefix("func", pathopt)) {
366 goto loop; /* ignore unimplemented options */
369 if (fullname[0] != '/')
371 if (stat(fullname, &statb) < 0) {
372 if (errno != ENOENT && errno != ENOTDIR)
376 e = EACCES; /* if we fail, this will be the error */
377 if (!S_ISREG(statb.st_mode))
379 if (pathopt) { /* this is a %func directory */
380 stalloc(strlen(fullname) + 1);
381 readcmdfile(fullname);
382 if ((cmdp = cmdlookup(name, 0)) == NULL || cmdp->cmdtype != CMDFUNCTION)
383 error("%s not defined in %s", name, fullname);
388 if (statb.st_uid == geteuid()) {
389 if ((statb.st_mode & 0100) == 0)
391 } else if (statb.st_gid == getegid()) {
392 if ((statb.st_mode & 010) == 0)
395 if ((statb.st_mode & 01) == 0)
399 TRACE(("searchexec \"%s\" returns \"%s\"\n", name, fullname));
401 cmdp = cmdlookup(name, 1);
402 if (cmdp->cmdtype == CMDFUNCTION)
404 cmdp->cmdtype = CMDNORMAL;
405 cmdp->param.index = idx;
411 if (e == ENOENT || e == ENOTDIR)
412 outfmt(out2, "%s: not found\n", name);
414 outfmt(out2, "%s: %s\n", name, strerror(e));
416 entry->cmdtype = CMDUNKNOWN;
424 entry->cmdtype = cmdp->cmdtype;
425 entry->u = cmdp->param;
426 entry->special = cmdp->special;
428 entry->cmdtype = CMDUNKNOWN;
434 * Search the table of builtin commands.
438 find_builtin(const char *name, int *special)
440 const struct builtincmd *bp;
442 for (bp = builtincmd ; bp->name ; bp++) {
443 if (*bp->name == *name && equal(bp->name, name)) {
444 *special = bp->special;
454 * Called when a cd is done. If any entry in cmdtable depends on the current
455 * directory, simply clear cmdtable completely.
468 * Called before PATH is changed. The argument is the new value of PATH;
469 * pathval() still returns the old value at this point. Called with
474 changepath(const char *newval __unused)
481 * Clear out command entries. The argument specifies the first entry in
482 * PATH which has changed.
488 struct tblentry **tblp;
489 struct tblentry **pp;
490 struct tblentry *cmdp;
493 for (tblp = cmdtable ; tblp < &cmdtable[CMDTABLESIZE] ; tblp++) {
495 while ((cmdp = *pp) != NULL) {
496 if (cmdp->cmdtype == CMDNORMAL) {
510 * Locate a command in the command hash table. If "add" is nonzero,
511 * add the command to the table if it is not already present. The
512 * variable "lastcmdentry" is set to point to the address of the link
513 * pointing to the entry, so that delete_cmd_entry can delete the
517 static struct tblentry **lastcmdentry;
520 static struct tblentry *
521 cmdlookup(const char *name, int add)
525 struct tblentry *cmdp;
526 struct tblentry **pp;
533 pp = &cmdtable[hashval % CMDTABLESIZE];
534 for (cmdp = *pp ; cmdp ; cmdp = cmdp->next) {
535 if (equal(cmdp->cmdname, name))
539 if (add && cmdp == NULL) {
541 cmdp = *pp = ckmalloc(sizeof (struct tblentry)
544 cmdp->cmdtype = CMDUNKNOWN;
545 strcpy(cmdp->cmdname, name);
553 * Delete the command entry returned on the last lookup.
557 delete_cmd_entry(void)
559 struct tblentry *cmdp;
562 cmdp = *lastcmdentry;
563 *lastcmdentry = cmdp->next;
571 * Add a new command entry, replacing any existing command entry for
576 addcmdentry(const char *name, struct cmdentry *entry)
578 struct tblentry *cmdp;
581 cmdp = cmdlookup(name, 1);
582 if (cmdp->cmdtype == CMDFUNCTION) {
583 unreffunc(cmdp->param.func);
585 cmdp->cmdtype = entry->cmdtype;
586 cmdp->param = entry->u;
592 * Define a shell function.
596 defun(const char *name, union node *func)
598 struct cmdentry entry;
601 entry.cmdtype = CMDFUNCTION;
602 entry.u.func = copyfunc(func);
603 addcmdentry(name, &entry);
609 * Delete a function if it exists.
613 unsetfunc(const char *name)
615 struct tblentry *cmdp;
617 if ((cmdp = cmdlookup(name, 0)) != NULL && cmdp->cmdtype == CMDFUNCTION) {
618 unreffunc(cmdp->param.func);
626 * Check if a function by a certain name exists.
629 isfunc(const char *name)
631 struct tblentry *cmdp;
632 cmdp = cmdlookup(name, 0);
633 return (cmdp != NULL && cmdp->cmdtype == CMDFUNCTION);
638 * Shared code for the following builtin commands:
639 * type, command -v, command -V
643 typecmd_impl(int argc, char **argv, int cmd, const char *path)
645 struct cmdentry entry;
646 struct tblentry *cmdp;
647 const char * const *pp;
652 if (path != pathval())
655 for (i = 1; i < argc; i++) {
656 /* First look at the keywords */
657 for (pp = parsekwd; *pp; pp++)
658 if (**pp == *argv[i] && equal(*pp, argv[i]))
662 if (cmd == TYPECMD_SMALLV)
663 out1fmt("%s\n", argv[i]);
665 out1fmt("%s is a shell keyword\n", argv[i]);
669 /* Then look at the aliases */
670 if ((ap = lookupalias(argv[i], 1)) != NULL) {
671 if (cmd == TYPECMD_SMALLV)
672 out1fmt("alias %s='%s'\n", argv[i], ap->val);
674 out1fmt("%s is an alias for %s\n", argv[i],
679 /* Then check if it is a tracked alias */
680 if ((cmdp = cmdlookup(argv[i], 0)) != NULL) {
681 entry.cmdtype = cmdp->cmdtype;
682 entry.u = cmdp->param;
683 entry.special = cmdp->special;
686 /* Finally use brute force */
687 find_command(argv[i], &entry, 0, path);
690 switch (entry.cmdtype) {
692 if (strchr(argv[i], '/') == NULL) {
693 const char *path2 = path;
695 int j = entry.u.index;
697 name = padvance(&path2, argv[i]);
700 if (cmd == TYPECMD_SMALLV)
701 out1fmt("%s\n", name);
703 out1fmt("%s is%s %s\n", argv[i],
704 (cmdp && cmd == TYPECMD_TYPE) ?
705 " a tracked alias for" : "",
708 if (access(argv[i], X_OK) == 0) {
709 if (cmd == TYPECMD_SMALLV)
710 out1fmt("%s\n", argv[i]);
712 out1fmt("%s is %s\n", argv[i],
715 if (cmd != TYPECMD_SMALLV)
716 outfmt(out2, "%s: %s\n",
717 argv[i], strerror(errno));
724 if (cmd == TYPECMD_SMALLV)
725 out1fmt("%s\n", argv[i]);
727 out1fmt("%s is a shell function\n", argv[i]);
731 if (cmd == TYPECMD_SMALLV)
732 out1fmt("%s\n", argv[i]);
733 else if (entry.special)
734 out1fmt("%s is a special shell builtin\n",
737 out1fmt("%s is a shell builtin\n", argv[i]);
741 if (cmd != TYPECMD_SMALLV)
742 outfmt(out2, "%s: not found\n", argv[i]);
748 if (path != pathval())
755 * Locate and print what a word is...
759 typecmd(int argc, char **argv)
761 return typecmd_impl(argc, argv, TYPECMD_TYPE, bltinlookup("PATH", 1));