BSD 4_4_Lite2 release
[unix-history] / usr / src / bin / csh / exec.c
index ba3f3aa..49c04ae 100644 (file)
-static char *sccsid = "@(#)exec.c 4.1 %G%";
+/*-
+ * Copyright (c) 1980, 1991, 1993
+ *     The Regents of the University of California.  All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ *    must display the following acknowledgement:
+ *     This product includes software developed by the University of
+ *     California, Berkeley and its contributors.
+ * 4. Neither the name of the University nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
 
 
-#include "sh.h"
+#ifndef lint
+static char sccsid[] = "@(#)exec.c     8.3 (Berkeley) 5/23/95";
+#endif /* not lint */
+
+#include <sys/types.h>
+#include <sys/param.h>
+#include <dirent.h>
+#include <fcntl.h>
+#include <sys/stat.h>
+#include <errno.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#if __STDC__
+# include <stdarg.h>
+#else
+# include <varargs.h>
+#endif 
+
+#include "csh.h"
+#include "extern.h"
 
 /*
 
 /*
- * C shell
+ * System level search and execute of a command.  We look in each directory
+ * for the specified command name.  If the name contains a '/' then we
+ * execute only the full path name.  If there is no search path then we
+ * execute only full path names.
  */
  */
+extern char **environ;
 
 /*
 
 /*
- * System level search and execute of a command.
- * We look in each directory for the specified command name.
- * If the name contains a '/' then we execute only the full path name.
- * If there is no search path then we execute only full path names.
- */
-
-/* 
  * As we search for the command we note the first non-trivial error
  * message for presentation to the user.  This allows us often
  * to show that a file has the wrong mode/no access when the file
  * is not in the last component of the search path, so we must
  * go on after first detecting the error.
  */
  * As we search for the command we note the first non-trivial error
  * message for presentation to the user.  This allows us often
  * to show that a file has the wrong mode/no access when the file
  * is not in the last component of the search path, so we must
  * go on after first detecting the error.
  */
-char   *exerr;                 /* Execution error message */
-char   *expath;                /* Path for exerr */
+static char *exerr;            /* Execution error message */
+static Char *expath;           /* Path for exerr */
 
 /*
 
 /*
- * Xhash is an array of HSHSIZ chars, which are used to hash execs.
- * If it is allocated, then to tell whether ``name'' is (possibly)
- * present in the i'th component of the variable path, you look at
- * the i'th bit of xhash[hash("name")].  This is setup automatically
+ * Xhash is an array of HSHSIZ bits (HSHSIZ / 8 chars), which are used
+ * to hash execs.  If it is allocated (havhash true), then to tell
+ * whether ``name'' is (possibly) present in the i'th component
+ * of the variable path, you look at the bit in xhash indexed by
+ * hash(hashname("name"), i).  This is setup automatically
  * after .login is executed, and recomputed whenever ``path'' is
  * changed.
  * after .login is executed, and recomputed whenever ``path'' is
  * changed.
+ * The two part hash function is designed to let texec() call the
+ * more expensive hashname() only once and the simple hash() several
+ * times (once for each path component checked).
+ * Byte size is assumed to be 8.
  */
  */
-int    havhash;
-#define        HSHSIZ  511
-char   xhash[HSHSIZ];
-#ifdef VFORK
-int    hits, misses;
-#endif
+#define        HSHSIZ          8192    /* 1k bytes */
+#define HSHMASK                (HSHSIZ - 1)
+#define HSHMUL         243
+static char xhash[HSHSIZ / 8];
+
+#define hash(a, b)     (((a) * HSHMUL + (b)) & HSHMASK)
+#define bit(h, b)      ((h)[(b) >> 3] & 1 << ((b) & 7))        /* bit test */
+#define bis(h, b)      ((h)[(b) >> 3] |= 1 << ((b) & 7))       /* bit set */
+static int hits, misses;
 
 /* Dummy search path for just absolute search when no path */
 
 /* Dummy search path for just absolute search when no path */
-char   *justabs[] =    { "", 0 };
+static Char *justabs[] = {STRNULL, 0};
 
 
-doexec(t)
-       register struct command *t;
-{
-       char *sav;
-       register char *dp, **pv, **av;
-       register struct varent *v;
-       bool slash = any('/', t->t_dcom[0]);
-       int hashval, i;
-       char *blk[2];
+static void    pexerr __P((void));
+static void    texec __P((Char *, Char **));
+static int     hashname __P((Char *));
+static void    tellmewhat __P((struct wordent *));
+static int     executable __P((Char *, Char *, bool));
+static int     iscommand __P((Char *));
 
 
-       /*
-        * Glob the command name.  If this does anything, then we
-        * will execute the command only relative to ".".  One special
-        * case: if there is no PATH, then we execute only commands
-        * which start with '/'.
-        */
-       dp = globone(t->t_dcom[0]);
-       sav = t->t_dcom[0];
-       exerr = 0; expath = t->t_dcom[0] = dp;
-       xfree(sav);
-       v = adrof("path");
-       if (v == 0 && expath[0] != '/')
-               pexerr();
-       slash |= gflag;
 
 
-       /*
-        * Glob the argument list, if necessary.
-        * Otherwise trim off the quote bits.
-        */
-       gflag = 0; av = &t->t_dcom[1];
-       rscan(av, tglob);
-       if (gflag) {
-               av = glob(av);
-               if (av == 0)
-                       error("No match");
+void
+/*ARGSUSED*/
+doexec(v, t)
+    Char **v;
+    struct command *t;
+{
+    register Char *dp, **pv, **av, *sav;
+    register struct varent *pathv;
+    register bool slash;
+    register int hashval = 0, hashval1, i;
+    Char   *blk[2];
+    sigset_t sigset;
+
+    /*
+     * Glob the command name. We will search $path even if this does something,
+     * as in sh but not in csh.  One special case: if there is no PATH, then we
+     * execute only commands which start with '/'.
+     */
+    blk[0] = t->t_dcom[0];
+    blk[1] = 0;
+    gflag = 0, tglob(blk);
+    if (gflag) {
+       pv = globall(blk);
+       if (pv == 0) {
+           setname(vis_str(blk[0]));
+           stderror(ERR_NAME | ERR_NOMATCH);
        }
        }
-       blk[0] = t->t_dcom[0];
-       blk[1] = 0;
-       av = blkspl(blk, av);
-#ifdef VFORK
-       Vav = av;
-#endif
-       scan(av, trim);
+       gargv = 0;
+    }
+    else
+       pv = saveblk(blk);
 
 
-       xechoit(av);            /* Echo command if -x */
-       closech();              /* Close random fd's */
+    trim(pv);
 
 
-       /*
-        * We must do this after any possible forking (like `foo`
-        * in glob) so that this shell can still do subprocesses.
-        */
-       sigsys(SIGCHLD, SIG_IGN);       /* sigsys for vforks sake */
+    exerr = 0;
+    expath = Strsave(pv[0]);
+    Vexpath = expath;
 
 
+    pathv = adrof(STRpath);
+    if (pathv == 0 && expath[0] != '/') {
+       blkfree(pv);
+       pexerr();
+    }
+    slash = any(short2str(expath), '/');
+
+    /*
+     * Glob the argument list, if necessary. Otherwise trim off the quote bits.
+     */
+    gflag = 0;
+    av = &t->t_dcom[1];
+    tglob(av);
+    if (gflag) {
+       av = globall(av);
+       if (av == 0) {
+           blkfree(pv);
+           setname(vis_str(expath));
+           stderror(ERR_NAME | ERR_NOMATCH);
+       }
+       gargv = 0;
+    }
+    else
+       av = saveblk(av);
+
+    blkfree(t->t_dcom);
+    t->t_dcom = blkspl(pv, av);
+    xfree((ptr_t) pv);
+    xfree((ptr_t) av);
+    av = t->t_dcom;
+    trim(av);
+
+    if (*av == NULL || **av == '\0')
+       pexerr();
+
+    xechoit(av);               /* Echo command if -x */
+    /*
+     * Since all internal file descriptors are set to close on exec, we don't
+     * need to close them explicitly here.  Just reorient ourselves for error
+     * messages.
+     */
+    SHIN = 0;
+    SHOUT = 1;
+    SHERR = 2;
+    OLDSTD = 0;
+    /*
+     * We must do this AFTER any possible forking (like `foo` in glob) so that
+     * this shell can still do subprocesses.
+     */
+    sigemptyset(&sigset);
+    sigprocmask(SIG_SETMASK, &sigset, NULL);
+    /*
+     * If no path, no words in path, or a / in the filename then restrict the
+     * command search.
+     */
+    if (pathv == 0 || pathv->vec[0] == 0 || slash)
+       pv = justabs;
+    else
+       pv = pathv->vec;
+    sav = Strspl(STRslash, *av);/* / command name for postpending */
+    Vsav = sav;
+    if (havhash)
+       hashval = hashname(*av);
+    i = 0;
+    hits++;
+    do {
        /*
        /*
-        * If no path, no words in path, or a / in the filename
-        * then restrict the command search.
+        * Try to save time by looking at the hash table for where this command
+        * could be.  If we are doing delayed hashing, then we put the names in
+        * one at a time, as the user enters them.  This is kinda like Korn
+        * Shell's "tracked aliases".
         */
         */
-       if (v == 0 || v->vec[0] == 0 || slash)
-               pv = justabs;
-       else
-               pv = v->vec;
-       sav = strspl("/", *av);         /* / command name for postpending */
-#ifdef VFORK
-       Vsav = sav;
-#endif
-       if (havhash)
-               hashval = xhash[hash(*av)];
-       i = 0;
-#ifdef VFORK
-       hits++;
-#endif
-       do {
-               if (!slash && pv[0][0] == '/' && havhash && (hashval & (1 << (i % 8))) == 0)
-                       goto cont;
-               if (pv[0][0] == 0 || eq(pv[0], "."))    /* don't make ./xxx */
-                       texec(*av, av);
-               else {
-                       dp = strspl(*pv, sav);
-#ifdef VFORK
-                       Vdp = dp;
-#endif
-                       texec(dp, av);
-#ifdef VFORK
-                       Vdp = 0;
-#endif
-                       xfree(dp);
-               }
-#ifdef VFORK
-               misses++;
-#endif
+       if (!slash && pv[0][0] == '/' && havhash) {
+           hashval1 = hash(hashval, i);
+           if (!bit(xhash, hashval1))
+               goto cont;
+       }
+       if (pv[0][0] == 0 || eq(pv[0], STRdot)) /* don't make ./xxx */
+           texec(*av, av);
+       else {
+           dp = Strspl(*pv, sav);
+           Vdp = dp;
+           texec(dp, av);
+           Vdp = 0;
+           xfree((ptr_t) dp);
+       }
+       misses++;
 cont:
 cont:
-               pv++;
-               i++;
-       } while (*pv);
-#ifdef VFORK
-       hits--;
-#endif
-#ifdef VFORK
-       Vsav = 0;
-       Vav = 0;
-#endif
-       xfree(sav);
-       xfree(av);
-       pexerr();
+       pv++;
+       i++;
+    } while (*pv);
+    hits--;
+    Vsav = 0;
+    xfree((ptr_t) sav);
+    pexerr();
 }
 
 }
 
+static void
 pexerr()
 {
 pexerr()
 {
-
-       /* Couldn't find the damn thing */
-       setname(expath);
-       /* xfree(expath); */
-       if (exerr)
-               bferr(exerr);
-       bferr("Command not found");
+    /* Couldn't find the damn thing */
+    if (expath) {
+       setname(vis_str(expath));
+       Vexpath = 0;
+       xfree((ptr_t) expath);
+       expath = 0;
+    }
+    else
+       setname("");
+    if (exerr)
+       stderror(ERR_NAME | ERR_STRING, exerr);
+    stderror(ERR_NAME | ERR_COMMAND);
 }
 
 }
 
-/* Last resort shell */
-char   *lastsh[] =     { SHELLPATH, 0 };
-
 /*
  * Execute command f, arg list t.
  * Record error message if not found.
  * Also do shell scripts here.
  */
 /*
  * Execute command f, arg list t.
  * Record error message if not found.
  * Also do shell scripts here.
  */
-texec(f, t)
-       char *f;
-       register char **t;
+static void
+texec(sf, st)
+    Char   *sf;
+    register Char **st;
 {
 {
-       register struct varent *v;
-       register char **vp;
-       extern char *sys_errlist[];
-
-       execv(f, t);
-       switch (errno) {
-
-       case ENOEXEC:
-               /*
-                * If there is an alias for shell, then
-                * put the words of the alias in front of the
-                * argument list replacing the command name.
-                * Note no interpretation of the words at this point.
-                */
-               v = adrof1("shell", &aliases);
-               if (v == 0) {
-#ifdef OTHERSH
-                       register int ff = open(f, 0);
-                       char ch;
+    register char **t;
+    register char *f;
+    register struct varent *v;
+    register Char **vp;
+    Char   *lastsh[2];
+    int     fd;
+    unsigned char c;
+    Char   *st0, **ost;
+
+    /* The order for the conversions is significant */
+    t = short2blk(st);
+    f = short2str(sf);
+    Vt = t;
+    errno = 0;                 /* don't use a previous error */
+    (void) execve(f, t, environ);
+    Vt = 0;
+    blkfree((Char **) t);
+    switch (errno) {
+
+    case ENOEXEC:
+       /*
+        * From: casper@fwi.uva.nl (Casper H.S. Dik) If we could not execute
+        * it, don't feed it to the shell if it looks like a binary!
+        */
+       if ((fd = open(f, O_RDONLY)) != -1) {
+           if (read(fd, (char *) &c, 1) == 1) {
+               if (!Isprint(c) && (c != '\n' && c != '\t')) {
+                   (void) close(fd);
+                   /*
+                    * We *know* what ENOEXEC means.
+                    */
+                   stderror(ERR_ARCH, f, strerror(errno));
+               }
+           }
+#ifdef _PATH_BSHELL
+           else
+               c = '#';
 #endif
 #endif
-
-                       vp = lastsh;
-                       vp[0] = adrof("shell") ? value("shell") : SHELLPATH;
-#ifdef OTHERSH
-                       if (ff != -1 && read(ff, &ch, 1) == 1 && ch != '#')
-                               vp[0] = OTHERSH;
-                       close(ff);
+           (void) close(fd);
+       }
+       /*
+        * If there is an alias for shell, then put the words of the alias in
+        * front of the argument list replacing the command name. Note no
+        * interpretation of the words at this point.
+        */
+       v = adrof1(STRshell, &aliases);
+       if (v == 0) {
+           vp = lastsh;
+           vp[0] = adrof(STRshell) ? value(STRshell) : STR_SHELLPATH;
+           vp[1] = NULL;
+#ifdef _PATH_BSHELL
+           if (fd != -1 && c != '#')
+               vp[0] = STR_BSHELL;
 #endif
 #endif
-               } else
-                       vp = v->vec;
-               t[0] = f;
-               t = blkspl(vp, t);              /* Splice up the new arglst */
-               f = *t;
-               execv(f, t);
-               xfree((char *)t);
-               /* The sky is falling, the sky is falling! */
-
-       case ENOMEM:
-               Perror(f);
-
-       case ENOENT:
-               break;
-
-       default:
-               if (exerr == 0) {
-                       exerr = sys_errlist[errno];
-                       expath = savestr(f);
-               }
        }
        }
+       else
+           vp = v->vec;
+       st0 = st[0];
+       st[0] = sf;
+       ost = st;
+       st = blkspl(vp, st);    /* Splice up the new arglst */
+       ost[0] = st0;
+       sf = *st;
+       /* The order for the conversions is significant */
+       t = short2blk(st);
+       f = short2str(sf);
+       xfree((ptr_t) st);
+       Vt = t;
+       (void) execve(f, t, environ);
+       Vt = 0;
+       blkfree((Char **) t);
+       /* The sky is falling, the sky is falling! */
+
+    case ENOMEM:
+       stderror(ERR_SYSTEM, f, strerror(errno));
+
+    case ENOENT:
+       break;
+
+    default:
+       if (exerr == 0) {
+           exerr = strerror(errno);
+           if (expath)
+               xfree((ptr_t) expath);
+           expath = Strsave(sf);
+           Vexpath = expath;
+       }
+    }
 }
 
 }
 
+/*ARGSUSED*/
+void
 execash(t, kp)
 execash(t, kp)
-       register struct command *kp;
+    Char  **t;
+    register struct command *kp;
 {
 {
-
-       didcch++;
-       signal(SIGINT, parintr);
-       signal(SIGQUIT, parintr);
-       signal(SIGTERM, parterm);               /* if doexec loses, screw */
-       lshift(kp->t_dcom, 1);
-       exiterr++;
-       doexec(kp);
-       /*NOTREACHED*/
+    int     saveIN, saveOUT, saveDIAG, saveSTD;
+    int     oSHIN;
+    int     oSHOUT;
+    int     oSHERR;
+    int     oOLDSTD;
+    jmp_buf osetexit;
+    int            my_reenter;
+    int     odidfds;
+    sig_t   osigint, osigquit, osigterm;
+
+    if (chkstop == 0 && setintr)
+       panystop(0);
+    /*
+     * Hmm, we don't really want to do that now because we might
+     * fail, but what is the choice
+     */
+    rechist();
+
+    osigint  = signal(SIGINT, parintr);
+    osigquit = signal(SIGQUIT, parintr);
+    osigterm = signal(SIGTERM, parterm);
+
+    odidfds = didfds;
+    oSHIN = SHIN;
+    oSHOUT = SHOUT;
+    oSHERR = SHERR;
+    oOLDSTD = OLDSTD;
+
+    saveIN = dcopy(SHIN, -1);
+    saveOUT = dcopy(SHOUT, -1);
+    saveDIAG = dcopy(SHERR, -1);
+    saveSTD = dcopy(OLDSTD, -1);
+
+    lshift(kp->t_dcom, 1);
+
+    getexit(osetexit);
+
+    if ((my_reenter = setexit()) == 0) {
+       SHIN = dcopy(0, -1);
+       SHOUT = dcopy(1, -1);
+       SHERR = dcopy(2, -1);
+       didfds = 0;
+       doexec(t, kp);
+    }
+
+    (void) signal(SIGINT, osigint);
+    (void) signal(SIGQUIT, osigquit);
+    (void) signal(SIGTERM, osigterm);
+
+    doneinp = 0;
+    didfds = odidfds;
+    (void) close(SHIN);
+    (void) close(SHOUT);
+    (void) close(SHERR);
+    (void) close(OLDSTD);
+    SHIN = dmove(saveIN, oSHIN);
+    SHOUT = dmove(saveOUT, oSHOUT);
+    SHERR = dmove(saveDIAG, oSHERR);
+    OLDSTD = dmove(saveSTD, oOLDSTD);
+
+    resexit(osetexit);
+    if (my_reenter)
+       stderror(ERR_SILENT);
 }
 
 }
 
+void
 xechoit(t)
 xechoit(t)
-       char **t;
+    Char  **t;
 {
 {
+    if (adrof(STRecho)) {
+       (void) fflush(csherr);
+       blkpr(csherr, t);
+       (void) fputc('\n', csherr);
+    }
+}
 
 
-       if (adrof("echo")) {
-               flush();
-               haderr = 1;
-               blkpr(t), printf("\n");
-               haderr = 0;
+void
+/*ARGSUSED*/
+dohash(v, t)
+    Char **v;
+    struct command *t;
+{
+    DIR    *dirp;
+    register struct dirent *dp;
+    register int cnt;
+    int     i = 0;
+    struct varent *pathv = adrof(STRpath);
+    Char  **pv;
+    int     hashval;
+
+    havhash = 1;
+    for (cnt = 0; cnt < sizeof xhash; cnt++)
+       xhash[cnt] = 0;
+    if (pathv == 0)
+       return;
+    for (pv = pathv->vec; *pv; pv++, i++) {
+       if (pv[0][0] != '/')
+           continue;
+       dirp = opendir(short2str(*pv));
+       if (dirp == NULL)
+           continue;
+       while ((dp = readdir(dirp)) != NULL) {
+           if (dp->d_ino == 0)
+               continue;
+           if (dp->d_name[0] == '.' &&
+               (dp->d_name[1] == '\0' ||
+                (dp->d_name[1] == '.' && dp->d_name[2] == '\0')))
+               continue;
+           hashval = hash(hashname(str2short(dp->d_name)), i);
+           bis(xhash, hashval);
+           /* tw_add_comm_name (dp->d_name); */
        }
        }
+       (void) closedir(dirp);
+    }
 }
 
 }
 
-dohash()
+void
+/*ARGSUSED*/
+dounhash(v, t)
+    Char **v;
+    struct command *t;
 {
 {
-       struct stat stb;
-       struct direct dirbuf[BUFSIZ / sizeof (struct direct)];
-       char d_name[DIRSIZ + 1];
-       register int dirf, cnt;
-       int i = 0;
-       struct varent *v = adrof("path");
-       char **pv;
-
-       havhash = 1;
-       for (cnt = 0; cnt < HSHSIZ; cnt++)
-               xhash[cnt] = 0;
-       if (v == 0)
-               return;
-       for (pv = v->vec; *pv; pv++, i = (i + 1) % 8) {
-               if (pv[0][0] != '/')
-                       continue;
-               dirf = open(*pv, 0);
-               if (dirf < 0)
-                       continue;
-               if (fstat(dirf, &stb) < 0 || !isdir(stb)) {
-                       close(dirf);
-                       continue;
-               }
-               while ((cnt = read(dirf, (char *) dirbuf, sizeof dirbuf)) >= sizeof dirbuf[0]) {
-                       register struct direct *ep = dirbuf;
-
-                       for (cnt /= sizeof(struct direct); cnt > 0; cnt--, ep++) {
-                               if (ep->d_ino == 0)
-                                       continue;
-                               copdent(d_name, ep->d_name);
-                               xhash[hash(d_name)] |= (1 << i);
-                       }
-               }
-               close(dirf);
-       }
+    havhash = 0;
 }
 
 }
 
-dounhash()
+void
+/*ARGSUSED*/
+hashstat(v, t)
+    Char **v;
+    struct command *t;
 {
 {
+    if (hits + misses)
+       (void) fprintf(cshout, "%d hits, %d misses, %d%%\n",
+                      hits, misses, 100 * hits / (hits + misses));
+}
+
+/*
+ * Hash a command name.
+ */
+static int
+hashname(cp)
+    register Char *cp;
+{
+    register long h = 0;
+
+    while (*cp)
+       h = hash(h, *cp++);
+    return ((int) h);
+}
+
+static int
+iscommand(name)
+    Char   *name;
+{
+    register Char **pv;
+    register Char *sav;
+    register struct varent *v;
+    register bool slash = any(short2str(name), '/');
+    register int hashval = 0, hashval1, i;
+
+    v = adrof(STRpath);
+    if (v == 0 || v->vec[0] == 0 || slash)
+       pv = justabs;
+    else
+       pv = v->vec;
+    sav = Strspl(STRslash, name);      /* / command name for postpending */
+    if (havhash)
+       hashval = hashname(name);
+    i = 0;
+    do {
+       if (!slash && pv[0][0] == '/' && havhash) {
+           hashval1 = hash(hashval, i);
+           if (!bit(xhash, hashval1))
+               goto cont;
+       }
+       if (pv[0][0] == 0 || eq(pv[0], STRdot)) {       /* don't make ./xxx */
+           if (executable(NULL, name, 0)) {
+               xfree((ptr_t) sav);
+               return i + 1;
+           }
+       }
+       else {
+           if (executable(*pv, sav, 0)) {
+               xfree((ptr_t) sav);
+               return i + 1;
+           }
+       }
+cont:
+       pv++;
+       i++;
+    } while (*pv);
+    xfree((ptr_t) sav);
+    return 0;
+}
+
+/* Also by:
+ *  Andreas Luik <luik@isaak.isa.de>
+ *  I S A  GmbH - Informationssysteme fuer computerintegrierte Automatisierung
+ *  Azenberstr. 35
+ *  D-7000 Stuttgart 1
+ *  West-Germany
+ * is the executable() routine below and changes to iscommand().
+ * Thanks again!!
+ */
 
 
-       havhash = 0;
+/*
+ * executable() examines the pathname obtained by concatenating dir and name
+ * (dir may be NULL), and returns 1 either if it is executable by us, or
+ * if dir_ok is set and the pathname refers to a directory.
+ * This is a bit kludgy, but in the name of optimization...
+ */
+static int
+executable(dir, name, dir_ok)
+    Char   *dir, *name;
+    bool    dir_ok;
+{
+    struct stat stbuf;
+    Char    path[MAXPATHLEN + 1], *dp, *sp;
+    char   *strname;
+
+    if (dir && *dir) {
+       for (dp = path, sp = dir; *sp; *dp++ = *sp++)
+           if (dp == &path[MAXPATHLEN + 1]) {
+               *--dp = '\0';
+               break;
+           }
+       for (sp = name; *sp; *dp++ = *sp++)
+           if (dp == &path[MAXPATHLEN + 1]) {
+               *--dp = '\0';
+               break;
+           }
+       *dp = '\0';
+       strname = short2str(path);
+    }
+    else
+       strname = short2str(name);
+    return (stat(strname, &stbuf) != -1 &&
+           ((S_ISREG(stbuf.st_mode) &&
+    /* save time by not calling access() in the hopeless case */
+             (stbuf.st_mode & (S_IXOTH | S_IXGRP | S_IXUSR)) &&
+             access(strname, X_OK) == 0) ||
+            (dir_ok && S_ISDIR(stbuf.st_mode))));
 }
 
 }
 
-#ifdef VFORK
-hashstat()
+/* The dowhich() is by:
+ *  Andreas Luik <luik@isaak.isa.de>
+ *  I S A  GmbH - Informationssysteme fuer computerintegrierte Automatisierung
+ *  Azenberstr. 35
+ *  D-7000 Stuttgart 1
+ *  West-Germany
+ * Thanks!!
+ */
+/*ARGSUSED*/
+void
+dowhich(v, c)
+    register Char **v;
+    struct command *c;
 {
 {
+    struct wordent lex[3];
+    struct varent *vp;
+
+    lex[0].next = &lex[1];
+    lex[1].next = &lex[2];
+    lex[2].next = &lex[0];
+
+    lex[0].prev = &lex[2];
+    lex[1].prev = &lex[0];
+    lex[2].prev = &lex[1];
 
 
-       if (hits+misses)
-       printf("%d hits, %d misses, %2d%%\n", hits, misses, 100 * hits / (hits + misses));
+    lex[0].word = STRNULL;
+    lex[2].word = STRret;
+
+    while (*++v) {
+       if ((vp = adrof1(*v, &aliases)) != NULL) {
+           (void) fprintf(cshout, "%s: \t aliased to ", vis_str(*v));
+           blkpr(cshout, vp->vec);
+           (void) fputc('\n', cshout);
+       }
+       else {
+           lex[1].word = *v;
+           tellmewhat(lex);
+       }
+    }
 }
 }
-#endif
 
 
-hash(cp)
-       register char *cp;
+static void
+tellmewhat(lex)
+    struct wordent *lex;
 {
 {
-       register long hash = 0;
-       int retval;
-
-       while (*cp)
-               hash += hash + *cp++;
-       if (hash < 0)
-               hash = -hash;
-       retval = hash % HSHSIZ;
-       return (retval);
+    register int i;
+    register struct biltins *bptr;
+    register struct wordent *sp = lex->next;
+    bool    aliased = 0;
+    Char   *s0, *s1, *s2, *cmd;
+    Char    qc;
+
+    if (adrof1(sp->word, &aliases)) {
+       alias(lex);
+       sp = lex->next;
+       aliased = 1;
+    }
+
+    s0 = sp->word;             /* to get the memory freeing right... */
+
+    /* handle quoted alias hack */
+    if ((*(sp->word) & (QUOTE | TRIM)) == QUOTE)
+       (sp->word)++;
+
+    /* do quoting, if it hasn't been done */
+    s1 = s2 = sp->word;
+    while (*s2)
+       switch (*s2) {
+       case '\'':
+       case '"':
+           qc = *s2++;
+           while (*s2 && *s2 != qc)
+               *s1++ = *s2++ | QUOTE;
+           if (*s2)
+               s2++;
+           break;
+       case '\\':
+           if (*++s2)
+               *s1++ = *s2++ | QUOTE;
+           break;
+       default:
+           *s1++ = *s2++;
+       }
+    *s1 = '\0';
+
+    for (bptr = bfunc; bptr < &bfunc[nbfunc]; bptr++) {
+       if (eq(sp->word, str2short(bptr->bname))) {
+           if (aliased)
+               prlex(cshout, lex);
+           (void) fprintf(cshout, "%s: shell built-in command.\n", 
+                          vis_str(sp->word));
+           sp->word = s0;      /* we save and then restore this */
+           return;
+       }
+    }
+
+    sp->word = cmd = globone(sp->word, G_IGNORE);
+
+    if ((i = iscommand(strip(sp->word))) != 0) {
+       register Char **pv;
+       register struct varent *v;
+       bool    slash = any(short2str(sp->word), '/');
+
+       v = adrof(STRpath);
+       if (v == 0 || v->vec[0] == 0 || slash)
+           pv = justabs;
+       else
+           pv = v->vec;
+
+       while (--i)
+           pv++;
+       if (pv[0][0] == 0 || eq(pv[0], STRdot)) {
+           if (!slash) {
+               sp->word = Strspl(STRdotsl, sp->word);
+               prlex(cshout, lex);
+               xfree((ptr_t) sp->word);
+           }
+           else
+               prlex(cshout, lex);
+           sp->word = s0;      /* we save and then restore this */
+           xfree((ptr_t) cmd);
+           return;
+       }
+       s1 = Strspl(*pv, STRslash);
+       sp->word = Strspl(s1, sp->word);
+       xfree((ptr_t) s1);
+       prlex(cshout, lex);
+       xfree((ptr_t) sp->word);
+    }
+    else {
+       if (aliased)
+           prlex(cshout, lex);
+       (void) fprintf(csherr, "%s: Command not found.\n", vis_str(sp->word));
+    }
+    sp->word = s0;             /* we save and then restore this */
+    xfree((ptr_t) cmd);
 }
 }