#include <sys/cdefs.h>
#ifndef lint
#if 0
static char sccsid[] = "@(#)exec.c 8.4 (Berkeley) 6/8/95";
#else
__RCSID("$NetBSD: exec.c,v 1.61 2026/05/28 10:07:58 kre Exp $");
#endif
#endif
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include "shell.h"
#include "main.h"
#include "nodes.h"
#include "parser.h"
#include "redir.h"
#include "eval.h"
#include "exec.h"
#include "builtins.h"
#include "var.h"
#include "options.h"
#include "input.h"
#include "output.h"
#include "syntax.h"
#include "memalloc.h"
#include "error.h"
#include "init.h"
#include "mystring.h"
#include "show.h"
#include "jobs.h"
#include "alias.h"
#define CMDTABLESIZE 31
#define ARB 1
struct tblentry {
struct tblentry *next;
union param param;
short cmdtype;
char rehash;
char fn_ln1;
int lineno;
char cmdname[ARB];
};
STATIC struct tblentry *cmdtable[CMDTABLESIZE];
STATIC int builtinloc = -1;
int exerrno = 0;
STATIC void tryexec(char *, char **, char **, int);
STATIC void printentry(struct tblentry *, int);
STATIC void addcmdentry(char *, struct cmdentry *);
STATIC void clearcmdentry(int);
STATIC struct tblentry *cmdlookup(const char *, int);
STATIC void delete_cmd_entry(void);
#ifndef BSD
STATIC void execinterp(char **, char **);
#endif
extern const char *const parsekwd[];
void
shellexec(char **argv, char **envp, const char *path, int idx, int vforked)
{
char *cmdname;
int e, action;
struct stat statb;
action = E_EXEC;
if (strchr(argv[0], '/') != NULL) {
tryexec(argv[0], argv, envp, vforked);
e = errno;
if (e == EACCES && stat(argv[0], &statb) == -1)
action = E_OPEN;
} else {
e = ENOENT;
while ((cmdname = padvance(&path, argv[0], 1)) != NULL) {
if (--idx < 0 && pathopt == NULL) {
tryexec(cmdname, argv, envp, vforked);
if (e == ENOENT &&
errno != ENOENT && errno != ENOTDIR) {
if (errno == EACCES) {
if (stat(cmdname, &statb) != -1)
e = EACCES;
} else {
e = errno;
}
}
}
stunalloc(cmdname);
}
}
switch (e) {
case EACCES:
if (action == E_OPEN) {
exerrno = 127;
break;
}
case EINVAL:
case ENOEXEC:
default:
exerrno = 126;
break;
case ENOENT:
case ELOOP:
case ENOTDIR:
case ENAMETOOLONG:
exerrno = 127;
break;
}
CTRACE(DBG_ERRS|DBG_CMDS|DBG_EVAL,
("shellexec failed for %s, errno %d, vforked %d, suppressint %d\n",
argv[0], e, vforked, suppressint));
exerror(EXEXEC, "%s: %s", argv[0], errmsg(e, action));
}
STATIC void
tryexec(char *cmd, char **argv, char **envp, int vforked)
{
int e;
#ifndef BSD
char *p;
#endif
#ifdef SYSV
do {
execve(cmd, argv, envp);
} while (errno == EINTR);
#else
execve(cmd, argv, envp);
#endif
e = errno;
if (e == ENOEXEC) {
if (vforked) {
exraise(EXSHELLPROC);
}
#ifdef DEBUG
VTRACE(DBG_CMDS, ("execve(cmd=%s) returned ENOEXEC\n", cmd));
#endif
initshellproc();
setinputfile(cmd, 0);
commandname = arg0 = savestr(argv[0]);
#ifndef BSD
pgetc(); pungetc();
p = parsenextc;
if (parsenleft > 2 && p[0] == '#' && p[1] == '!') {
argv[0] = cmd;
execinterp(argv, envp);
}
#endif
setparam(argv + 1);
exraise(EXSHELLPROC);
}
errno = e;
}
#ifndef BSD
#define NEWARGS 5
STATIC void
execinterp(char **argv, char **envp)
{
int n;
char *inp;
char *outp;
char c;
char *p;
char **ap;
char *newargs[NEWARGS];
int i;
char **ap2;
char **new;
n = parsenleft - 2;
inp = parsenextc + 2;
ap = newargs;
for (;;) {
while (--n >= 0 && (*inp == ' ' || *inp == '\t'))
inp++;
if (n < 0)
goto bad;
if ((c = *inp++) == '\n')
break;
if (ap == &newargs[NEWARGS])
bad: error("Bad #! line");
STARTSTACKSTR(outp);
do {
STPUTC(c, outp);
} while (--n >= 0 && (c = *inp++) != ' ' && c != '\t' && c != '\n');
STPUTC('\0', outp);
n++, inp--;
*ap++ = grabstackstr(outp);
}
if (ap == newargs + 1) {
p = newargs[0];
for (;;) {
if (equal(p, "sh") || equal(p, "ash")) {
return;
}
while (*p != '/') {
if (*p == '\0')
goto break2;
p++;
}
p++;
}
break2:;
}
i = (char *)ap - (char *)newargs;
if (i == 0)
error("Bad #! line");
for (ap2 = argv ; *ap2++ != NULL ; );
new = ckmalloc(i + ((char *)ap2 - (char *)argv));
ap = newargs, ap2 = new;
while ((i -= sizeof (char **)) >= 0)
*ap2++ = *ap++;
ap = argv;
while (*ap2++ = *ap++);
shellexec(new, envp, pathval(), 0);
}
#endif
const char *pathopt;
char *
padvance(const char **path, const char *name, int magic_percent)
{
const char *p;
char *q;
const char *start;
int len;
if (*path == NULL)
return NULL;
if (magic_percent)
magic_percent = '%';
start = *path;
for (p = start ; *p && *p != ':' && *p != magic_percent ; p++)
;
len = p - start + strlen(name) + 2;
while (stackblocksize() < len)
growstackblock();
q = stackblock();
if (p != start) {
memcpy(q, start, p - start);
q += p - start;
if (q[-1] != '/')
*q++ = '/';
}
strcpy(q, name);
pathopt = NULL;
if (*p == magic_percent) {
pathopt = ++p;
while (*p && *p != ':')
p++;
}
if (*p == ':')
*path = p + 1;
else
*path = NULL;
return grabstackstr(q + strlen(name) + 1);
}
int
hashcmd(int argc, char **argv)
{
struct tblentry **pp;
struct tblentry *cmdp;
int c;
struct cmdentry entry;
char *name;
int allopt=0, bopt=0, fopt=0, ropt=0, sopt=0, uopt=0, verbose=0;
int errs=1, emsg=DO_ERR;
int status = 0;
while ((c = nextopt("bcefqrsuv")) != '\0')
switch (c) {
case 'b': bopt = 1; break;
case 'c': uopt = 1; break;
case 'e': errs = 0; break;
case 'f': fopt = 1; break;
case 'q': emsg = 0; break;
case 'r': ropt = 1; break;
case 's': sopt = 1; break;
case 'u': uopt = 1; break;
case 'v': verbose = 1; break;
}
if (!errs)
emsg ^= DO_ERR;
if (ropt)
clearcmdentry(0);
if (bopt == 0 && fopt == 0 && sopt == 0 && uopt == 0)
allopt = bopt = fopt = sopt = uopt = 1;
if (*argptr == NULL) {
for (pp = cmdtable ; pp < &cmdtable[CMDTABLESIZE] ; pp++) {
for (cmdp = *pp ; cmdp ; cmdp = cmdp->next) {
switch (cmdp->cmdtype) {
case CMDNORMAL:
if (!uopt)
continue;
break;
case CMDBUILTIN:
if (!bopt)
continue;
break;
case CMDSPLBLTIN:
if (!sopt)
continue;
break;
case CMDFUNCTION:
if (!fopt)
continue;
break;
default:
continue;
}
if (!allopt || verbose ||
cmdp->cmdtype == CMDNORMAL)
printentry(cmdp, verbose);
}
}
flushout(out1);
if (io_err(out1)) {
out2str("hash: I/O error writing to standard output\n");
return 1;
}
return 0;
}
while ((name = *argptr++) != NULL) {
if ((cmdp = cmdlookup(name, 0)) != NULL) {
switch (cmdp->cmdtype) {
case CMDNORMAL:
if (!uopt)
continue;
delete_cmd_entry();
break;
case CMDBUILTIN:
if (!bopt)
continue;
if (builtinloc >= 0)
delete_cmd_entry();
break;
case CMDSPLBLTIN:
if (!sopt)
continue;
break;
case CMDFUNCTION:
if (!fopt)
continue;
break;
}
}
find_command(name, &entry, emsg, pathval());
if (errs && entry.cmdtype == CMDUNKNOWN)
status = 1;
if (verbose) {
if (entry.cmdtype != CMDUNKNOWN) {
cmdp = cmdlookup(name, 0);
if (cmdp != NULL)
printentry(cmdp, verbose);
}
flushall();
}
}
return status;
}
STATIC void
printentry(struct tblentry *cmdp, int verbose)
{
int idx;
const char *path;
char *name;
switch (cmdp->cmdtype) {
case CMDNORMAL:
idx = cmdp->param.index;
path = pathval();
do {
name = padvance(&path, cmdp->cmdname, 1);
stunalloc(name);
} while (--idx >= 0);
if (verbose)
out1fmt("Command from PATH[%d]: ",
cmdp->param.index);
out1str(name);
break;
case CMDSPLBLTIN:
if (verbose)
out1str("special ");
case CMDBUILTIN:
if (verbose)
out1str("builtin ");
out1fmt("%s", cmdp->cmdname);
break;
case CMDFUNCTION:
if (verbose)
out1str("function ");
out1fmt("%s", cmdp->cmdname);
if (verbose) {
struct procstat ps;
INTOFF;
commandtext(&ps, getfuncnode(cmdp->param.func));
INTON;
out1str("() { ");
out1str(ps.cmd);
out1str("; }");
}
break;
default:
error("internal error: %s cmdtype %d",
cmdp->cmdname, cmdp->cmdtype);
}
if (cmdp->rehash)
out1c('*');
out1c('\n');
}
void
find_command(char *name, struct cmdentry *entry, int act, const char *path)
{
struct tblentry *cmdp, loc_cmd;
int idx;
int prev;
char *fullname;
struct stat statb;
int e;
int (*bltin)(int,char **);
if (strchr(name, '/') != NULL) {
if (act & DO_ABS) {
while (stat(name, &statb) < 0) {
#ifdef SYSV
if (errno == EINTR)
continue;
#endif
if (errno != ENOENT && errno != ENOTDIR)
e = errno;
entry->cmdtype = CMDUNKNOWN;
entry->u.index = -1;
return;
}
entry->cmdtype = CMDNORMAL;
entry->u.index = -1;
return;
}
entry->cmdtype = CMDNORMAL;
entry->u.index = 0;
return;
}
if (path != pathval())
act |= DO_ALTPATH;
if (act & DO_ALTPATH && strstr(path, "%builtin") != NULL)
act |= DO_ALTBLTIN;
if ((cmdp = cmdlookup(name, 0)) != NULL) {
switch (cmdp->cmdtype) {
case CMDNORMAL:
if (act & DO_ALTPATH)
cmdp = NULL;
break;
case CMDFUNCTION:
if (act & DO_NOFUNC)
cmdp = NULL;
break;
case CMDBUILTIN:
if ((act & DO_ALTBLTIN) || builtinloc >= 0)
cmdp = NULL;
break;
}
if (cmdp != NULL && cmdp->rehash == 0)
goto success;
}
if ((act & DO_ALTPATH ? !(act & DO_ALTBLTIN) : builtinloc < 0) &&
(bltin = find_builtin(name)) != 0)
goto builtin_success;
prev = -1;
if (cmdp) {
if (cmdp->cmdtype == CMDBUILTIN)
prev = builtinloc;
else
prev = cmdp->param.index;
}
e = ENOENT;
idx = -1;
loop:
while ((fullname = padvance(&path, name, 1)) != NULL) {
stunalloc(fullname);
idx++;
if (pathopt) {
if (prefix("builtin", pathopt)) {
if ((bltin = find_builtin(name)) == 0)
goto loop;
goto builtin_success;
} else if (prefix("func", pathopt)) {
} else {
goto loop;
}
}
if (fullname[0] == '/' && idx <= prev) {
if (idx < prev)
goto loop;
VTRACE(DBG_CMDS, ("searchexec \"%s\": no change\n",
name));
goto success;
}
while (stat(fullname, &statb) < 0) {
#ifdef SYSV
if (errno == EINTR)
continue;
#endif
if (errno != ENOENT && errno != ENOTDIR)
e = errno;
goto loop;
}
e = EACCES;
if (!S_ISREG(statb.st_mode))
goto loop;
if (pathopt) {
char *endname;
if (act & DO_NOFUNC)
goto loop;
endname = fullname + strlen(fullname) + 1;
grabstackstr(endname);
readcmdfile(fullname);
if ((cmdp = cmdlookup(name, 0)) == NULL ||
cmdp->cmdtype != CMDFUNCTION)
error("%s not defined in %s", name, fullname);
ungrabstackstr(fullname, endname);
goto success;
}
#ifdef notdef
if (statb.st_uid == geteuid()) {
if ((statb.st_mode & 0100) == 0)
goto loop;
} else if (statb.st_gid == getegid()) {
if ((statb.st_mode & 010) == 0)
goto loop;
} else {
if ((statb.st_mode & 01) == 0)
goto loop;
}
#endif
VTRACE(DBG_CMDS, ("searchexec \"%s\" returns \"%s\"\n", name,
fullname));
INTOFF;
if (act & DO_ALTPATH) {
cmdp = &loc_cmd;
} else
cmdp = cmdlookup(name, 1);
if (cmdp->cmdtype == CMDFUNCTION)
cmdp = &loc_cmd;
cmdp->cmdtype = CMDNORMAL;
cmdp->param.index = idx;
INTON;
goto success;
}
if (cmdp)
delete_cmd_entry();
if (act & DO_ERR)
outfmt(out2, "%s: %s\n", name, errmsg(e, E_EXEC));
entry->cmdtype = CMDUNKNOWN;
entry->u.index = idx + 1;
return;
builtin_success:
INTOFF;
if (act & DO_ALTPATH)
cmdp = &loc_cmd;
else
cmdp = cmdlookup(name, 1);
if (cmdp->cmdtype == CMDFUNCTION)
cmdp = &loc_cmd;
cmdp->cmdtype = CMDBUILTIN;
cmdp->param.bltin = bltin;
INTON;
success:
if (cmdp) {
cmdp->rehash = 0;
entry->cmdtype = cmdp->cmdtype;
entry->lineno = cmdp->lineno;
entry->lno_frel = cmdp->fn_ln1;
entry->u = cmdp->param;
} else {
entry->cmdtype = CMDUNKNOWN;
entry->u.index = -1;
}
}
int
(*find_builtin(char *name))(int, char **)
{
const struct builtincmd *bp;
for (bp = builtincmd ; bp->name ; bp++) {
if (*bp->name == *name
&& (*name == '%' || equal(bp->name, name)))
return bp->builtin;
}
return 0;
}
int
(*find_splbltin(char *name))(int, char **)
{
const struct builtincmd *bp;
for (bp = splbltincmd ; bp->name ; bp++) {
if (*bp->name == *name && equal(bp->name, name))
return bp->builtin;
}
return 0;
}
void
hash_special_builtins(void)
{
const struct builtincmd *bp;
struct tblentry *cmdp;
for (bp = splbltincmd ; bp->name ; bp++) {
cmdp = cmdlookup(bp->name, 1);
cmdp->cmdtype = CMDSPLBLTIN;
cmdp->param.bltin = bp->builtin;
}
}
void
hashcd(void)
{
struct tblentry **pp;
struct tblentry *cmdp;
for (pp = cmdtable ; pp < &cmdtable[CMDTABLESIZE] ; pp++) {
for (cmdp = *pp ; cmdp ; cmdp = cmdp->next) {
if (cmdp->cmdtype == CMDNORMAL
|| (cmdp->cmdtype == CMDBUILTIN && builtinloc >= 0))
cmdp->rehash = 1;
}
}
}
void
changepath(const char *new, int flags __unused, struct var *vp __unused)
{
const char *old;
int idx;
int firstchange;
int bltin;
old = pathval();
firstchange = 9999;
idx = 0;
bltin = -1;
for (;;) {
if (*old != *new) {
firstchange = idx;
if ((*old == '\0' && *new == ':')
|| (*old == ':' && *new == '\0'))
firstchange++;
old = new;
}
if (*new == '\0')
break;
if (*new == '%' && bltin < 0 && prefix("builtin", new + 1))
bltin = idx;
if (*new == ':') {
idx++;
}
new++, old++;
}
if (builtinloc < 0 && bltin >= 0)
builtinloc = bltin;
if (builtinloc >= 0 && bltin < 0)
firstchange = 0;
clearcmdentry(firstchange);
builtinloc = bltin;
}
STATIC void
clearcmdentry(int firstchange)
{
struct tblentry **tblp;
struct tblentry **pp;
struct tblentry *cmdp;
INTOFF;
for (tblp = cmdtable ; tblp < &cmdtable[CMDTABLESIZE] ; tblp++) {
pp = tblp;
while ((cmdp = *pp) != NULL) {
if ((cmdp->cmdtype == CMDNORMAL &&
cmdp->param.index >= firstchange)
|| (cmdp->cmdtype == CMDBUILTIN &&
builtinloc >= firstchange)) {
*pp = cmdp->next;
ckfree(cmdp);
} else {
pp = &cmdp->next;
}
}
}
INTON;
}
#ifdef mkinit
MKINIT void deletefuncs(void);
MKINIT void hash_special_builtins(void);
INIT {
hash_special_builtins();
}
SHELLPROC {
deletefuncs();
}
#endif
void
deletefuncs(void)
{
struct tblentry **tblp;
struct tblentry **pp;
struct tblentry *cmdp;
INTOFF;
for (tblp = cmdtable ; tblp < &cmdtable[CMDTABLESIZE] ; tblp++) {
pp = tblp;
while ((cmdp = *pp) != NULL) {
if (cmdp->cmdtype == CMDFUNCTION) {
*pp = cmdp->next;
freefunc(cmdp->param.func);
ckfree(cmdp);
} else {
pp = &cmdp->next;
}
}
}
INTON;
}
struct tblentry **lastcmdentry;
STATIC struct tblentry *
cmdlookup(const char *name, int add)
{
int hashval;
const char *p;
struct tblentry *cmdp;
struct tblentry **pp;
p = name;
hashval = *p << 4;
while (*p)
hashval += *p++;
hashval &= 0x7FFF;
pp = &cmdtable[hashval % CMDTABLESIZE];
for (cmdp = *pp ; cmdp ; cmdp = cmdp->next) {
if (equal(cmdp->cmdname, name))
break;
pp = &cmdp->next;
}
if (add && cmdp == NULL) {
INTOFF;
cmdp = *pp = ckmalloc(sizeof (struct tblentry) - ARB
+ strlen(name) + 1);
cmdp->next = NULL;
cmdp->cmdtype = CMDUNKNOWN;
cmdp->rehash = 0;
strcpy(cmdp->cmdname, name);
INTON;
}
lastcmdentry = pp;
return cmdp;
}
STATIC void
delete_cmd_entry(void)
{
struct tblentry *cmdp;
INTOFF;
cmdp = *lastcmdentry;
*lastcmdentry = cmdp->next;
ckfree(cmdp);
INTON;
}
#ifdef notdef
void
getcmdentry(char *name, struct cmdentry *entry)
{
struct tblentry *cmdp = cmdlookup(name, 0);
if (cmdp) {
entry->u = cmdp->param;
entry->cmdtype = cmdp->cmdtype;
} else {
entry->cmdtype = CMDUNKNOWN;
entry->u.index = 0;
}
}
#endif
STATIC void
addcmdentry(char *name, struct cmdentry *entry)
{
struct tblentry *cmdp;
INTOFF;
cmdp = cmdlookup(name, 1);
if (cmdp->cmdtype != CMDSPLBLTIN) {
if (cmdp->cmdtype == CMDFUNCTION)
unreffunc(cmdp->param.func);
cmdp->cmdtype = entry->cmdtype;
cmdp->lineno = entry->lineno;
cmdp->fn_ln1 = entry->lno_frel;
cmdp->param = entry->u;
}
INTON;
}
void
defun(char *name, union node *func, int lineno)
{
struct cmdentry entry;
INTOFF;
entry.cmdtype = CMDFUNCTION;
entry.lineno = lineno;
entry.lno_frel = fnline1;
entry.u.func = copyfunc(func);
addcmdentry(name, &entry);
INTON;
}
int
unsetfunc(char *name)
{
struct tblentry *cmdp;
if ((cmdp = cmdlookup(name, 0)) != NULL &&
cmdp->cmdtype == CMDFUNCTION) {
unreffunc(cmdp->param.func);
delete_cmd_entry();
}
return 0;
}
int
typecmd(int argc, char **argv)
{
struct cmdentry entry;
struct tblentry *cmdp;
const char * const *pp;
struct alias *ap;
int err = 0;
char *arg;
int c;
int V_flag = 0;
int v_flag = 0;
int p_flag = 0;
while ((c = nextopt("vVp")) != 0) {
switch (c) {
case 'v': v_flag = 1; break;
case 'V': V_flag = 1; break;
case 'p': p_flag = 1; break;
}
}
if (argv[0][0] != 'c' && v_flag | V_flag | p_flag)
error("usage: %s name...", argv[0]);
if (v_flag && V_flag)
error("-v and -V cannot both be specified");
if (*argptr == NULL)
error("usage: %s%s name ...", argv[0],
argv[0][0] == 'c' ? " [-p] [-v|-V]" : "");
while ((arg = *argptr++)) {
if (arg[0] == '\0' && !v_flag) {
out1str("'' is an empty string\n");
continue;
}
if (!v_flag)
out1str(arg);
for (pp = parsekwd; *pp; pp++)
if (**pp == *arg && equal(*pp, arg))
break;
if (*pp) {
if (v_flag)
out1fmt("%s\n", arg);
else
out1str(" is a shell keyword\n");
continue;
}
if ((ap = lookupalias(arg, 1)) != NULL) {
int ml = 0;
if (!v_flag) {
out1str(" is an alias ");
if (strchr(ap->val, '\n')) {
out1str("(multiline)...\n");
ml = 1;
} else
out1str("for: ");
}
out1fmt("%s\n", ap->val);
if (ml && *argptr != NULL)
out1c('\n');
continue;
}
if (!p_flag && (cmdp = cmdlookup(arg, 0)) != NULL) {
entry.cmdtype = cmdp->cmdtype;
entry.u = cmdp->param;
} else {
cmdp = NULL;
find_command(arg, &entry, DO_ABS,
p_flag ? syspath() + 5 : pathval());
}
switch (entry.cmdtype) {
case CMDNORMAL: {
if (strchr(arg, '/') == NULL) {
const char *path;
char *name;
int j = entry.u.index;
path = p_flag ? syspath() + 5 : pathval();
do {
name = padvance(&path, arg, 1);
stunalloc(name);
} while (--j >= 0);
if (!v_flag)
out1str(" is ");
out1fmt("%s\n", name);
} else {
if (access(arg, X_OK) == 0) {
if (!v_flag)
out1str(" is ");
out1fmt("%s\n", arg);
} else {
if (!v_flag)
out1fmt(": %s\n",
strerror(errno));
else
err = 126;
}
}
break;
}
case CMDFUNCTION:
if (!v_flag)
out1str(" is a shell function\n");
else
out1fmt("%s\n", arg);
break;
case CMDBUILTIN:
if (!v_flag)
out1str(" is a shell builtin\n");
else
out1fmt("%s\n", arg);
break;
case CMDSPLBLTIN:
if (!v_flag)
out1str(" is a special shell builtin\n");
else
out1fmt("%s\n", arg);
break;
default:
if (!v_flag)
out1str(": not found\n");
err = 127;
break;
}
}
return err;
}