Home | History | Annotate | Line # | Download | only in csh
exec.c revision 1.31.14.1
      1  1.31.14.1  pgoyette /* $NetBSD: exec.c,v 1.31.14.1 2019/01/18 08:48:24 pgoyette Exp $ */
      2        1.6       cgd 
      3        1.1       cgd /*-
      4        1.5   mycroft  * Copyright (c) 1980, 1991, 1993
      5        1.5   mycroft  *	The Regents of the University of California.  All rights reserved.
      6        1.1       cgd  *
      7        1.1       cgd  * Redistribution and use in source and binary forms, with or without
      8        1.1       cgd  * modification, are permitted provided that the following conditions
      9        1.1       cgd  * are met:
     10        1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     11        1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     12        1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     13        1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     14        1.1       cgd  *    documentation and/or other materials provided with the distribution.
     15       1.21       agc  * 3. Neither the name of the University nor the names of its contributors
     16        1.1       cgd  *    may be used to endorse or promote products derived from this software
     17        1.1       cgd  *    without specific prior written permission.
     18        1.1       cgd  *
     19        1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20        1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21        1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22        1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23        1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24        1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25        1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26        1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27        1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28        1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29        1.1       cgd  * SUCH DAMAGE.
     30        1.1       cgd  */
     31        1.1       cgd 
     32       1.11  christos #include <sys/cdefs.h>
     33        1.1       cgd #ifndef lint
     34        1.6       cgd #if 0
     35        1.8  christos static char sccsid[] = "@(#)exec.c	8.3 (Berkeley) 5/23/95";
     36        1.6       cgd #else
     37  1.31.14.1  pgoyette __RCSID("$NetBSD: exec.c,v 1.31.14.1 2019/01/18 08:48:24 pgoyette Exp $");
     38        1.6       cgd #endif
     39        1.1       cgd #endif /* not lint */
     40        1.1       cgd 
     41       1.16       wiz #include <sys/param.h>
     42       1.16       wiz #include <sys/stat.h>
     43        1.1       cgd #include <sys/types.h>
     44       1.16       wiz 
     45        1.1       cgd #include <dirent.h>
     46       1.16       wiz #include <errno.h>
     47        1.1       cgd #include <fcntl.h>
     48       1.19       wiz #include <stdarg.h>
     49        1.1       cgd #include <stdlib.h>
     50        1.1       cgd #include <string.h>
     51        1.1       cgd #include <unistd.h>
     52        1.1       cgd 
     53        1.1       cgd #include "csh.h"
     54        1.1       cgd #include "extern.h"
     55        1.1       cgd 
     56        1.1       cgd /*
     57        1.1       cgd  * System level search and execute of a command.  We look in each directory
     58        1.1       cgd  * for the specified command name.  If the name contains a '/' then we
     59        1.1       cgd  * execute only the full path name.  If there is no search path then we
     60        1.1       cgd  * execute only full path names.
     61        1.1       cgd  */
     62        1.1       cgd extern char **environ;
     63        1.1       cgd 
     64        1.1       cgd /*
     65        1.1       cgd  * As we search for the command we note the first non-trivial error
     66        1.1       cgd  * message for presentation to the user.  This allows us often
     67        1.1       cgd  * to show that a file has the wrong mode/no access when the file
     68        1.1       cgd  * is not in the last component of the search path, so we must
     69        1.1       cgd  * go on after first detecting the error.
     70        1.1       cgd  */
     71       1.12   mycroft static const char *exerr;	/* Execution error message */
     72        1.1       cgd static Char *expath;		/* Path for exerr */
     73        1.1       cgd 
     74        1.1       cgd /*
     75        1.1       cgd  * Xhash is an array of HSHSIZ bits (HSHSIZ / 8 chars), which are used
     76        1.1       cgd  * to hash execs.  If it is allocated (havhash true), then to tell
     77        1.1       cgd  * whether ``name'' is (possibly) present in the i'th component
     78        1.1       cgd  * of the variable path, you look at the bit in xhash indexed by
     79        1.1       cgd  * hash(hashname("name"), i).  This is setup automatically
     80        1.1       cgd  * after .login is executed, and recomputed whenever ``path'' is
     81        1.1       cgd  * changed.
     82        1.1       cgd  * The two part hash function is designed to let texec() call the
     83        1.1       cgd  * more expensive hashname() only once and the simple hash() several
     84        1.1       cgd  * times (once for each path component checked).
     85        1.1       cgd  * Byte size is assumed to be 8.
     86        1.1       cgd  */
     87       1.16       wiz #define	HSHSIZ 8192	/* 1k bytes */
     88       1.16       wiz #define HSHMASK	(HSHSIZ - 1)
     89       1.16       wiz #define HSHMUL 243
     90       1.29  christos static unsigned char xhash[HSHSIZ / 8];
     91        1.1       cgd 
     92       1.16       wiz #define hash(a, b) (((a) * HSHMUL + (b)) & HSHMASK)
     93       1.31  christos /* these macros eval their arguments multiple times, so be careful */
     94       1.16       wiz #define bit(h, b) ((h)[(b) >> 3] & 1 << ((b) & 7))	/* bit test */
     95       1.30  christos #define bis(h, b) ((h)[(b) >> 3] = \
     96       1.30  christos     (unsigned char)((1 << ((b) & 7)) | (h)[(b) >> 3]))/* bit set */
     97        1.1       cgd static int hits, misses;
     98        1.1       cgd 
     99        1.1       cgd /* Dummy search path for just absolute search when no path */
    100        1.1       cgd static Char *justabs[] = {STRNULL, 0};
    101        1.1       cgd 
    102       1.27     perry static void pexerr(void) __dead;
    103       1.16       wiz static void texec(Char *, Char **);
    104       1.16       wiz static int hashname(Char *);
    105       1.16       wiz static int tellmewhat(struct wordent *, Char *);
    106       1.26  christos static int executable(Char *, Char *, int);
    107       1.16       wiz static int iscommand(Char *);
    108        1.1       cgd 
    109        1.1       cgd void
    110        1.5   mycroft /*ARGSUSED*/
    111       1.16       wiz doexec(Char **v, struct command *t)
    112        1.1       cgd {
    113       1.10       tls     struct varent *pathv;
    114       1.16       wiz     Char *blk[2], **av, *dp, **pv, *sav;
    115       1.16       wiz     int i, hashval, hashval1;
    116       1.20    kleink     sigset_t nsigset;
    117       1.26  christos     int slash;
    118        1.1       cgd 
    119       1.16       wiz     hashval = 0;
    120        1.1       cgd     /*
    121        1.1       cgd      * Glob the command name. We will search $path even if this does something,
    122        1.1       cgd      * as in sh but not in csh.  One special case: if there is no PATH, then we
    123        1.1       cgd      * execute only commands which start with '/'.
    124        1.1       cgd      */
    125        1.1       cgd     blk[0] = t->t_dcom[0];
    126        1.1       cgd     blk[1] = 0;
    127        1.1       cgd     gflag = 0, tglob(blk);
    128        1.1       cgd     if (gflag) {
    129        1.1       cgd 	pv = globall(blk);
    130        1.1       cgd 	if (pv == 0) {
    131        1.5   mycroft 	    setname(vis_str(blk[0]));
    132        1.1       cgd 	    stderror(ERR_NAME | ERR_NOMATCH);
    133        1.1       cgd 	}
    134        1.1       cgd 	gargv = 0;
    135        1.1       cgd     }
    136        1.1       cgd     else
    137        1.1       cgd 	pv = saveblk(blk);
    138        1.1       cgd 
    139        1.1       cgd     trim(pv);
    140        1.1       cgd 
    141        1.1       cgd     exerr = 0;
    142        1.1       cgd     expath = Strsave(pv[0]);
    143        1.1       cgd     Vexpath = expath;
    144        1.1       cgd 
    145        1.5   mycroft     pathv = adrof(STRpath);
    146        1.5   mycroft     if (pathv == 0 && expath[0] != '/') {
    147        1.1       cgd 	blkfree(pv);
    148        1.1       cgd 	pexerr();
    149        1.1       cgd     }
    150        1.1       cgd     slash = any(short2str(expath), '/');
    151        1.1       cgd 
    152        1.1       cgd     /*
    153        1.1       cgd      * Glob the argument list, if necessary. Otherwise trim off the quote bits.
    154        1.1       cgd      */
    155        1.1       cgd     gflag = 0;
    156        1.1       cgd     av = &t->t_dcom[1];
    157        1.1       cgd     tglob(av);
    158        1.1       cgd     if (gflag) {
    159        1.1       cgd 	av = globall(av);
    160        1.1       cgd 	if (av == 0) {
    161        1.1       cgd 	    blkfree(pv);
    162        1.5   mycroft 	    setname(vis_str(expath));
    163        1.1       cgd 	    stderror(ERR_NAME | ERR_NOMATCH);
    164        1.1       cgd 	}
    165        1.1       cgd 	gargv = 0;
    166        1.1       cgd     }
    167        1.1       cgd     else
    168        1.1       cgd 	av = saveblk(av);
    169        1.1       cgd 
    170        1.1       cgd     blkfree(t->t_dcom);
    171        1.1       cgd     t->t_dcom = blkspl(pv, av);
    172  1.31.14.1  pgoyette     free(pv);
    173  1.31.14.1  pgoyette     free(av);
    174        1.1       cgd     av = t->t_dcom;
    175        1.1       cgd     trim(av);
    176        1.1       cgd 
    177        1.1       cgd     if (*av == NULL || **av == '\0')
    178        1.1       cgd 	pexerr();
    179        1.1       cgd 
    180        1.1       cgd     xechoit(av);		/* Echo command if -x */
    181        1.1       cgd     /*
    182        1.1       cgd      * Since all internal file descriptors are set to close on exec, we don't
    183        1.1       cgd      * need to close them explicitly here.  Just reorient ourselves for error
    184        1.1       cgd      * messages.
    185        1.1       cgd      */
    186        1.1       cgd     SHIN = 0;
    187        1.1       cgd     SHOUT = 1;
    188        1.5   mycroft     SHERR = 2;
    189        1.1       cgd     OLDSTD = 0;
    190        1.1       cgd     /*
    191        1.1       cgd      * We must do this AFTER any possible forking (like `foo` in glob) so that
    192        1.1       cgd      * this shell can still do subprocesses.
    193        1.1       cgd      */
    194       1.20    kleink     sigemptyset(&nsigset);
    195       1.20    kleink     (void)sigprocmask(SIG_SETMASK, &nsigset, NULL);
    196        1.1       cgd     /*
    197        1.1       cgd      * If no path, no words in path, or a / in the filename then restrict the
    198        1.1       cgd      * command search.
    199        1.1       cgd      */
    200        1.5   mycroft     if (pathv == 0 || pathv->vec[0] == 0 || slash)
    201        1.1       cgd 	pv = justabs;
    202        1.1       cgd     else
    203        1.5   mycroft 	pv = pathv->vec;
    204       1.16       wiz     sav = Strspl(STRslash, *av); 	/* / command name for postpending */
    205        1.1       cgd     Vsav = sav;
    206        1.1       cgd     if (havhash)
    207        1.1       cgd 	hashval = hashname(*av);
    208        1.1       cgd     i = 0;
    209        1.1       cgd     hits++;
    210        1.1       cgd     do {
    211        1.1       cgd 	/*
    212        1.1       cgd 	 * Try to save time by looking at the hash table for where this command
    213        1.1       cgd 	 * could be.  If we are doing delayed hashing, then we put the names in
    214        1.1       cgd 	 * one at a time, as the user enters them.  This is kinda like Korn
    215        1.1       cgd 	 * Shell's "tracked aliases".
    216        1.1       cgd 	 */
    217        1.1       cgd 	if (!slash && pv[0][0] == '/' && havhash) {
    218        1.1       cgd 	    hashval1 = hash(hashval, i);
    219        1.1       cgd 	    if (!bit(xhash, hashval1))
    220        1.1       cgd 		goto cont;
    221        1.1       cgd 	}
    222        1.1       cgd 	if (pv[0][0] == 0 || eq(pv[0], STRdot))	/* don't make ./xxx */
    223        1.1       cgd 	    texec(*av, av);
    224        1.1       cgd 	else {
    225        1.1       cgd 	    dp = Strspl(*pv, sav);
    226        1.1       cgd 	    Vdp = dp;
    227        1.1       cgd 	    texec(dp, av);
    228        1.1       cgd 	    Vdp = 0;
    229  1.31.14.1  pgoyette 	    free(dp);
    230        1.1       cgd 	}
    231        1.1       cgd 	misses++;
    232        1.1       cgd cont:
    233        1.1       cgd 	pv++;
    234        1.1       cgd 	i++;
    235        1.1       cgd     } while (*pv);
    236        1.1       cgd     hits--;
    237        1.1       cgd     Vsav = 0;
    238  1.31.14.1  pgoyette     free(sav);
    239        1.1       cgd     pexerr();
    240       1.15   mycroft     /* NOTREACHED */
    241        1.1       cgd }
    242        1.1       cgd 
    243        1.1       cgd static void
    244       1.16       wiz pexerr(void)
    245        1.1       cgd {
    246        1.1       cgd     /* Couldn't find the damn thing */
    247        1.1       cgd     if (expath) {
    248        1.5   mycroft 	setname(vis_str(expath));
    249        1.1       cgd 	Vexpath = 0;
    250  1.31.14.1  pgoyette 	free(expath);
    251        1.1       cgd 	expath = 0;
    252        1.1       cgd     }
    253        1.1       cgd     else
    254        1.1       cgd 	setname("");
    255        1.1       cgd     if (exerr)
    256        1.1       cgd 	stderror(ERR_NAME | ERR_STRING, exerr);
    257       1.13   mycroft     else
    258       1.13   mycroft 	stderror(ERR_NAME | ERR_COMMAND);
    259       1.13   mycroft     /* NOTREACHED */
    260        1.1       cgd }
    261        1.1       cgd 
    262        1.1       cgd /*
    263        1.1       cgd  * Execute command f, arg list t.
    264        1.1       cgd  * Record error message if not found.
    265        1.1       cgd  * Also do shell scripts here.
    266        1.1       cgd  */
    267        1.1       cgd static void
    268       1.16       wiz texec(Char *sf, Char **st)
    269        1.1       cgd {
    270       1.10       tls     struct varent *v;
    271       1.16       wiz     Char *lastsh[2], **vp, *st0, **ost;
    272       1.16       wiz     char *f, **t;
    273       1.16       wiz     int fd;
    274       1.23  christos     unsigned char c = '\0';
    275        1.1       cgd 
    276        1.1       cgd     /* The order for the conversions is significant */
    277        1.1       cgd     t = short2blk(st);
    278        1.1       cgd     f = short2str(sf);
    279        1.1       cgd     Vt = t;
    280        1.1       cgd     errno = 0;			/* don't use a previous error */
    281       1.16       wiz     (void)execve(f, t, environ);
    282        1.1       cgd     Vt = 0;
    283       1.16       wiz     blkfree((Char **)t);
    284        1.1       cgd     switch (errno) {
    285        1.1       cgd 
    286        1.1       cgd     case ENOEXEC:
    287        1.1       cgd 	/*
    288        1.1       cgd 	 * From: casper (at) fwi.uva.nl (Casper H.S. Dik) If we could not execute
    289        1.1       cgd 	 * it, don't feed it to the shell if it looks like a binary!
    290        1.1       cgd 	 */
    291        1.1       cgd 	if ((fd = open(f, O_RDONLY)) != -1) {
    292       1.16       wiz 	    if (read(fd, (char *)&c, 1) == 1) {
    293        1.1       cgd 		if (!Isprint(c) && (c != '\n' && c != '\t')) {
    294       1.16       wiz 		    (void)close(fd);
    295        1.1       cgd 		    /*
    296        1.1       cgd 		     * We *know* what ENOEXEC means.
    297        1.1       cgd 		     */
    298        1.1       cgd 		    stderror(ERR_ARCH, f, strerror(errno));
    299        1.1       cgd 		}
    300        1.1       cgd 	    }
    301        1.1       cgd #ifdef _PATH_BSHELL
    302        1.1       cgd 	    else
    303        1.1       cgd 		c = '#';
    304        1.1       cgd #endif
    305       1.16       wiz 	    (void)close(fd);
    306        1.1       cgd 	}
    307        1.1       cgd 	/*
    308        1.1       cgd 	 * If there is an alias for shell, then put the words of the alias in
    309        1.1       cgd 	 * front of the argument list replacing the command name. Note no
    310        1.1       cgd 	 * interpretation of the words at this point.
    311        1.1       cgd 	 */
    312        1.1       cgd 	v = adrof1(STRshell, &aliases);
    313        1.1       cgd 	if (v == 0) {
    314        1.1       cgd 	    vp = lastsh;
    315        1.1       cgd 	    vp[0] = adrof(STRshell) ? value(STRshell) : STR_SHELLPATH;
    316        1.1       cgd 	    vp[1] = NULL;
    317        1.1       cgd #ifdef _PATH_BSHELL
    318        1.1       cgd 	    if (fd != -1 && c != '#')
    319        1.1       cgd 		vp[0] = STR_BSHELL;
    320        1.1       cgd #endif
    321        1.1       cgd 	}
    322        1.1       cgd 	else
    323        1.1       cgd 	    vp = v->vec;
    324        1.1       cgd 	st0 = st[0];
    325        1.1       cgd 	st[0] = sf;
    326        1.1       cgd 	ost = st;
    327        1.1       cgd 	st = blkspl(vp, st);	/* Splice up the new arglst */
    328        1.1       cgd 	ost[0] = st0;
    329        1.1       cgd 	sf = *st;
    330        1.1       cgd 	/* The order for the conversions is significant */
    331        1.1       cgd 	t = short2blk(st);
    332        1.1       cgd 	f = short2str(sf);
    333  1.31.14.1  pgoyette 	free(st);
    334        1.1       cgd 	Vt = t;
    335       1.16       wiz 	(void)execve(f, t, environ);
    336        1.1       cgd 	Vt = 0;
    337  1.31.14.1  pgoyette 	blkfree((Char **)t);
    338       1.13   mycroft 	/* FALLTHROUGH */
    339        1.1       cgd 
    340        1.1       cgd     case ENOMEM:
    341        1.1       cgd 	stderror(ERR_SYSTEM, f, strerror(errno));
    342       1.13   mycroft 	/* NOTREACHED */
    343        1.1       cgd 
    344        1.1       cgd     case ENOENT:
    345        1.1       cgd 	break;
    346        1.1       cgd 
    347        1.1       cgd     default:
    348        1.1       cgd 	if (exerr == 0) {
    349        1.1       cgd 	    exerr = strerror(errno);
    350        1.1       cgd 	    if (expath)
    351  1.31.14.1  pgoyette 		free(expath);
    352        1.1       cgd 	    expath = Strsave(sf);
    353        1.1       cgd 	    Vexpath = expath;
    354        1.1       cgd 	}
    355        1.1       cgd     }
    356        1.1       cgd }
    357        1.1       cgd 
    358        1.1       cgd /*ARGSUSED*/
    359        1.1       cgd void
    360       1.16       wiz execash(Char **t, struct command *kp)
    361       1.16       wiz {
    362        1.5   mycroft     jmp_buf osetexit;
    363       1.16       wiz     sig_t osigint, osigquit, osigterm;
    364       1.16       wiz     int my_reenter, odidfds, oOLDSTD, oSHERR, oSHIN, oSHOUT;
    365       1.16       wiz     int saveDIAG, saveIN, saveOUT, saveSTD;
    366        1.5   mycroft 
    367        1.1       cgd     if (chkstop == 0 && setintr)
    368        1.1       cgd 	panystop(0);
    369        1.5   mycroft     /*
    370        1.5   mycroft      * Hmm, we don't really want to do that now because we might
    371        1.5   mycroft      * fail, but what is the choice
    372        1.5   mycroft      */
    373        1.1       cgd     rechist();
    374        1.5   mycroft 
    375        1.5   mycroft     osigint  = signal(SIGINT, parintr);
    376        1.5   mycroft     osigquit = signal(SIGQUIT, parintr);
    377        1.5   mycroft     osigterm = signal(SIGTERM, parterm);
    378        1.5   mycroft 
    379        1.5   mycroft     odidfds = didfds;
    380        1.5   mycroft     oSHIN = SHIN;
    381        1.5   mycroft     oSHOUT = SHOUT;
    382        1.5   mycroft     oSHERR = SHERR;
    383        1.5   mycroft     oOLDSTD = OLDSTD;
    384        1.5   mycroft 
    385        1.5   mycroft     saveIN = dcopy(SHIN, -1);
    386        1.5   mycroft     saveOUT = dcopy(SHOUT, -1);
    387        1.5   mycroft     saveDIAG = dcopy(SHERR, -1);
    388        1.5   mycroft     saveSTD = dcopy(OLDSTD, -1);
    389        1.5   mycroft 
    390        1.1       cgd     lshift(kp->t_dcom, 1);
    391        1.5   mycroft 
    392        1.5   mycroft     getexit(osetexit);
    393        1.5   mycroft 
    394        1.5   mycroft     if ((my_reenter = setexit()) == 0) {
    395        1.5   mycroft 	SHIN = dcopy(0, -1);
    396        1.5   mycroft 	SHOUT = dcopy(1, -1);
    397        1.5   mycroft 	SHERR = dcopy(2, -1);
    398        1.5   mycroft 	didfds = 0;
    399        1.5   mycroft 	doexec(t, kp);
    400        1.5   mycroft     }
    401        1.5   mycroft 
    402       1.16       wiz     (void)signal(SIGINT, osigint);
    403       1.16       wiz     (void)signal(SIGQUIT, osigquit);
    404       1.16       wiz     (void)signal(SIGTERM, osigterm);
    405        1.5   mycroft 
    406        1.5   mycroft     doneinp = 0;
    407        1.5   mycroft     didfds = odidfds;
    408       1.16       wiz     (void)close(SHIN);
    409       1.16       wiz     (void)close(SHOUT);
    410       1.16       wiz     (void)close(SHERR);
    411       1.16       wiz     (void)close(OLDSTD);
    412        1.5   mycroft     SHIN = dmove(saveIN, oSHIN);
    413        1.5   mycroft     SHOUT = dmove(saveOUT, oSHOUT);
    414        1.5   mycroft     SHERR = dmove(saveDIAG, oSHERR);
    415        1.5   mycroft     OLDSTD = dmove(saveSTD, oOLDSTD);
    416        1.5   mycroft 
    417        1.5   mycroft     resexit(osetexit);
    418       1.15   mycroft     if (my_reenter)
    419        1.5   mycroft 	stderror(ERR_SILENT);
    420        1.1       cgd }
    421        1.1       cgd 
    422        1.1       cgd void
    423       1.16       wiz xechoit(Char **t)
    424        1.1       cgd {
    425        1.1       cgd     if (adrof(STRecho)) {
    426       1.18  christos 	int odidfds = didfds;
    427       1.16       wiz 	(void)fflush(csherr);
    428       1.18  christos 	odidfds = didfds;
    429       1.18  christos 	didfds = 0;
    430        1.5   mycroft 	blkpr(csherr, t);
    431       1.16       wiz 	(void)fputc('\n', csherr);
    432       1.18  christos 	(void)fflush(csherr);
    433       1.18  christos 	didfds = odidfds;
    434        1.1       cgd     }
    435        1.1       cgd }
    436        1.1       cgd 
    437        1.1       cgd void
    438        1.5   mycroft /*ARGSUSED*/
    439       1.16       wiz dohash(Char **v, struct command *t)
    440        1.1       cgd {
    441       1.10       tls     struct dirent *dp;
    442       1.16       wiz     struct varent *pathv;
    443       1.16       wiz     DIR *dirp;
    444       1.16       wiz     Char **pv;
    445       1.28     lukem     size_t cnt;
    446       1.28     lukem     int hashval, i;
    447        1.1       cgd 
    448       1.16       wiz     i = 0;
    449        1.1       cgd     havhash = 1;
    450       1.16       wiz     pathv = adrof(STRpath);
    451       1.16       wiz 
    452        1.1       cgd     for (cnt = 0; cnt < sizeof xhash; cnt++)
    453        1.1       cgd 	xhash[cnt] = 0;
    454        1.5   mycroft     if (pathv == 0)
    455        1.1       cgd 	return;
    456        1.5   mycroft     for (pv = pathv->vec; *pv; pv++, i++) {
    457        1.1       cgd 	if (pv[0][0] != '/')
    458        1.1       cgd 	    continue;
    459        1.1       cgd 	dirp = opendir(short2str(*pv));
    460        1.1       cgd 	if (dirp == NULL)
    461        1.1       cgd 	    continue;
    462        1.1       cgd 	while ((dp = readdir(dirp)) != NULL) {
    463        1.1       cgd 	    if (dp->d_ino == 0)
    464        1.1       cgd 		continue;
    465        1.1       cgd 	    if (dp->d_name[0] == '.' &&
    466        1.1       cgd 		(dp->d_name[1] == '\0' ||
    467        1.5   mycroft 		 (dp->d_name[1] == '.' && dp->d_name[2] == '\0')))
    468        1.1       cgd 		continue;
    469        1.1       cgd 	    hashval = hash(hashname(str2short(dp->d_name)), i);
    470        1.1       cgd 	    bis(xhash, hashval);
    471        1.1       cgd 	    /* tw_add_comm_name (dp->d_name); */
    472        1.1       cgd 	}
    473        1.1       cgd 	(void) closedir(dirp);
    474        1.1       cgd     }
    475        1.1       cgd }
    476        1.1       cgd 
    477        1.1       cgd void
    478        1.5   mycroft /*ARGSUSED*/
    479       1.16       wiz dounhash(Char **v, struct command *t)
    480        1.1       cgd {
    481        1.1       cgd     havhash = 0;
    482        1.1       cgd }
    483        1.1       cgd 
    484        1.1       cgd void
    485        1.5   mycroft /*ARGSUSED*/
    486       1.16       wiz hashstat(Char **v, struct command *t)
    487        1.1       cgd {
    488        1.1       cgd     if (hits + misses)
    489       1.16       wiz 	(void)fprintf(cshout, "%d hits, %d misses, %d%%\n",
    490       1.16       wiz 	    hits, misses, 100 * hits / (hits + misses));
    491        1.1       cgd }
    492        1.1       cgd 
    493        1.1       cgd /*
    494        1.1       cgd  * Hash a command name.
    495        1.1       cgd  */
    496        1.1       cgd static int
    497       1.16       wiz hashname(Char *cp)
    498        1.1       cgd {
    499       1.10       tls     long h = 0;
    500        1.1       cgd 
    501        1.1       cgd     while (*cp)
    502        1.1       cgd 	h = hash(h, *cp++);
    503        1.1       cgd     return ((int) h);
    504        1.5   mycroft }
    505        1.5   mycroft 
    506        1.5   mycroft static int
    507       1.16       wiz iscommand(Char *name)
    508        1.5   mycroft {
    509       1.10       tls     struct varent *v;
    510       1.16       wiz     Char **pv, *sav;
    511       1.16       wiz     int hashval, hashval1, i;
    512       1.26  christos     int slash;
    513        1.5   mycroft 
    514       1.16       wiz     hashval = 0;
    515       1.16       wiz     slash = any(short2str(name), '/');
    516        1.5   mycroft     v = adrof(STRpath);
    517       1.16       wiz 
    518        1.5   mycroft     if (v == 0 || v->vec[0] == 0 || slash)
    519        1.5   mycroft 	pv = justabs;
    520        1.5   mycroft     else
    521        1.5   mycroft 	pv = v->vec;
    522        1.5   mycroft     sav = Strspl(STRslash, name);	/* / command name for postpending */
    523        1.5   mycroft     if (havhash)
    524        1.5   mycroft 	hashval = hashname(name);
    525        1.5   mycroft     i = 0;
    526        1.5   mycroft     do {
    527        1.5   mycroft 	if (!slash && pv[0][0] == '/' && havhash) {
    528        1.5   mycroft 	    hashval1 = hash(hashval, i);
    529        1.5   mycroft 	    if (!bit(xhash, hashval1))
    530        1.5   mycroft 		goto cont;
    531        1.5   mycroft 	}
    532        1.5   mycroft 	if (pv[0][0] == 0 || eq(pv[0], STRdot)) {	/* don't make ./xxx */
    533        1.5   mycroft 	    if (executable(NULL, name, 0)) {
    534  1.31.14.1  pgoyette 		free(sav);
    535        1.5   mycroft 		return i + 1;
    536        1.5   mycroft 	    }
    537        1.5   mycroft 	}
    538        1.5   mycroft 	else {
    539        1.5   mycroft 	    if (executable(*pv, sav, 0)) {
    540  1.31.14.1  pgoyette 		free(sav);
    541        1.5   mycroft 		return i + 1;
    542        1.5   mycroft 	    }
    543        1.5   mycroft 	}
    544        1.5   mycroft cont:
    545        1.5   mycroft 	pv++;
    546        1.5   mycroft 	i++;
    547        1.5   mycroft     } while (*pv);
    548  1.31.14.1  pgoyette     free(sav);
    549        1.5   mycroft     return 0;
    550        1.5   mycroft }
    551        1.5   mycroft 
    552        1.5   mycroft /* Also by:
    553        1.5   mycroft  *  Andreas Luik <luik (at) isaak.isa.de>
    554        1.5   mycroft  *  I S A  GmbH - Informationssysteme fuer computerintegrierte Automatisierung
    555        1.5   mycroft  *  Azenberstr. 35
    556        1.5   mycroft  *  D-7000 Stuttgart 1
    557        1.5   mycroft  *  West-Germany
    558        1.5   mycroft  * is the executable() routine below and changes to iscommand().
    559        1.5   mycroft  * Thanks again!!
    560        1.5   mycroft  */
    561        1.5   mycroft 
    562        1.5   mycroft /*
    563        1.5   mycroft  * executable() examines the pathname obtained by concatenating dir and name
    564        1.5   mycroft  * (dir may be NULL), and returns 1 either if it is executable by us, or
    565        1.5   mycroft  * if dir_ok is set and the pathname refers to a directory.
    566        1.5   mycroft  * This is a bit kludgy, but in the name of optimization...
    567        1.5   mycroft  */
    568        1.5   mycroft static int
    569       1.26  christos executable(Char *dir, Char *name, int dir_ok)
    570        1.5   mycroft {
    571        1.5   mycroft     struct stat stbuf;
    572       1.16       wiz     Char path[MAXPATHLEN + 1], *dp, *sp;
    573       1.16       wiz     char *strname;
    574        1.5   mycroft 
    575        1.5   mycroft     if (dir && *dir) {
    576        1.5   mycroft 	for (dp = path, sp = dir; *sp; *dp++ = *sp++)
    577        1.5   mycroft 	    if (dp == &path[MAXPATHLEN + 1]) {
    578        1.5   mycroft 		*--dp = '\0';
    579        1.5   mycroft 		break;
    580        1.5   mycroft 	    }
    581        1.5   mycroft 	for (sp = name; *sp; *dp++ = *sp++)
    582        1.5   mycroft 	    if (dp == &path[MAXPATHLEN + 1]) {
    583        1.5   mycroft 		*--dp = '\0';
    584        1.5   mycroft 		break;
    585        1.5   mycroft 	    }
    586        1.5   mycroft 	*dp = '\0';
    587        1.5   mycroft 	strname = short2str(path);
    588        1.5   mycroft     }
    589        1.5   mycroft     else
    590        1.5   mycroft 	strname = short2str(name);
    591       1.16       wiz     return (stat(strname, &stbuf) != -1 && ((S_ISREG(stbuf.st_mode) &&
    592       1.16       wiz         /* save time by not calling access() in the hopeless case */
    593       1.16       wiz 	(stbuf.st_mode & (S_IXOTH | S_IXGRP | S_IXUSR)) &&
    594       1.16       wiz 	access(strname, X_OK) == 0) || (dir_ok && S_ISDIR(stbuf.st_mode))));
    595        1.5   mycroft }
    596        1.5   mycroft 
    597        1.5   mycroft /* The dowhich() is by:
    598        1.5   mycroft  *  Andreas Luik <luik (at) isaak.isa.de>
    599        1.5   mycroft  *  I S A  GmbH - Informationssysteme fuer computerintegrierte Automatisierung
    600        1.5   mycroft  *  Azenberstr. 35
    601        1.5   mycroft  *  D-7000 Stuttgart 1
    602        1.5   mycroft  *  West-Germany
    603        1.5   mycroft  * Thanks!!
    604        1.5   mycroft  */
    605        1.5   mycroft /*ARGSUSED*/
    606        1.5   mycroft void
    607       1.16       wiz dowhich(Char **v, struct command *c)
    608        1.5   mycroft {
    609       1.17     lukem     struct wordent lexw[3];
    610        1.5   mycroft     struct varent *vp;
    611        1.5   mycroft 
    612       1.17     lukem     lexw[0].next = &lexw[1];
    613       1.17     lukem     lexw[1].next = &lexw[2];
    614       1.17     lukem     lexw[2].next = &lexw[0];
    615       1.17     lukem 
    616       1.17     lukem     lexw[0].prev = &lexw[2];
    617       1.17     lukem     lexw[1].prev = &lexw[0];
    618       1.17     lukem     lexw[2].prev = &lexw[1];
    619        1.5   mycroft 
    620       1.17     lukem     lexw[0].word = STRNULL;
    621       1.17     lukem     lexw[2].word = STRret;
    622        1.5   mycroft 
    623        1.5   mycroft     while (*++v) {
    624        1.5   mycroft 	if ((vp = adrof1(*v, &aliases)) != NULL) {
    625       1.16       wiz 	    (void)fprintf(cshout, "%s: \t aliased to ", vis_str(*v));
    626        1.5   mycroft 	    blkpr(cshout, vp->vec);
    627       1.16       wiz 	    (void)fputc('\n', cshout);
    628        1.9  christos 	    set(STRstatus, Strsave(STR0));
    629        1.5   mycroft 	}
    630        1.5   mycroft 	else {
    631       1.17     lukem 	    lexw[1].word = *v;
    632       1.17     lukem 	    set(STRstatus, Strsave(tellmewhat(lexw, NULL) ? STR0 : STR1));
    633        1.5   mycroft 	}
    634        1.5   mycroft     }
    635        1.5   mycroft }
    636        1.5   mycroft 
    637        1.9  christos static int
    638       1.16       wiz tellmewhat(struct wordent *lexp, Char *str)
    639        1.5   mycroft {
    640       1.16       wiz     struct biltins *bptr;
    641       1.16       wiz     struct wordent *sp;
    642       1.16       wiz     Char *cmd, *s0, *s1, *s2;
    643       1.10       tls     int i;
    644       1.26  christos     int aliased, found;
    645       1.16       wiz     Char qc;
    646       1.16       wiz 
    647       1.16       wiz     aliased = 0;
    648       1.16       wiz     sp = lexp->next;
    649        1.5   mycroft 
    650        1.5   mycroft     if (adrof1(sp->word, &aliases)) {
    651        1.9  christos 	alias(lexp);
    652        1.9  christos 	sp = lexp->next;
    653        1.5   mycroft 	aliased = 1;
    654        1.5   mycroft     }
    655        1.5   mycroft 
    656        1.5   mycroft     s0 = sp->word;		/* to get the memory freeing right... */
    657        1.5   mycroft 
    658        1.5   mycroft     /* handle quoted alias hack */
    659        1.5   mycroft     if ((*(sp->word) & (QUOTE | TRIM)) == QUOTE)
    660        1.5   mycroft 	(sp->word)++;
    661        1.5   mycroft 
    662        1.5   mycroft     /* do quoting, if it hasn't been done */
    663        1.5   mycroft     s1 = s2 = sp->word;
    664        1.5   mycroft     while (*s2)
    665        1.5   mycroft 	switch (*s2) {
    666        1.5   mycroft 	case '\'':
    667        1.5   mycroft 	case '"':
    668        1.5   mycroft 	    qc = *s2++;
    669        1.5   mycroft 	    while (*s2 && *s2 != qc)
    670       1.29  christos 		*s1++ = (Char)(*s2++ | QUOTE);
    671        1.5   mycroft 	    if (*s2)
    672        1.5   mycroft 		s2++;
    673        1.5   mycroft 	    break;
    674        1.5   mycroft 	case '\\':
    675        1.5   mycroft 	    if (*++s2)
    676       1.29  christos 		*s1++ = (Char)(*s2++ | QUOTE);
    677        1.5   mycroft 	    break;
    678        1.5   mycroft 	default:
    679        1.5   mycroft 	    *s1++ = *s2++;
    680        1.5   mycroft 	}
    681        1.5   mycroft     *s1 = '\0';
    682        1.5   mycroft 
    683        1.5   mycroft     for (bptr = bfunc; bptr < &bfunc[nbfunc]; bptr++) {
    684        1.5   mycroft 	if (eq(sp->word, str2short(bptr->bname))) {
    685        1.9  christos 	    if (str == NULL) {
    686        1.9  christos 		if (aliased)
    687        1.9  christos 		    prlex(cshout, lexp);
    688       1.16       wiz 		(void)fprintf(cshout, "%s: shell built-in command.\n",
    689        1.9  christos 			       vis_str(sp->word));
    690        1.9  christos 	    }
    691        1.9  christos 	    else
    692       1.16       wiz 		(void)Strcpy(str, sp->word);
    693        1.5   mycroft 	    sp->word = s0;	/* we save and then restore this */
    694        1.9  christos 	    return 1;
    695        1.5   mycroft 	}
    696        1.5   mycroft     }
    697        1.5   mycroft 
    698        1.8  christos     sp->word = cmd = globone(sp->word, G_IGNORE);
    699        1.8  christos 
    700        1.9  christos     if ((i = iscommand(sp->word)) != 0) {
    701       1.10       tls 	Char **pv;
    702       1.10       tls 	struct varent *v;
    703       1.26  christos 	int    slash = any(short2str(sp->word), '/');
    704        1.5   mycroft 
    705        1.5   mycroft 	v = adrof(STRpath);
    706        1.5   mycroft 	if (v == 0 || v->vec[0] == 0 || slash)
    707        1.5   mycroft 	    pv = justabs;
    708        1.5   mycroft 	else
    709        1.5   mycroft 	    pv = v->vec;
    710        1.5   mycroft 
    711        1.5   mycroft 	while (--i)
    712        1.5   mycroft 	    pv++;
    713        1.5   mycroft 	if (pv[0][0] == 0 || eq(pv[0], STRdot)) {
    714        1.8  christos 	    if (!slash) {
    715        1.8  christos 		sp->word = Strspl(STRdotsl, sp->word);
    716        1.9  christos 		prlex(cshout, lexp);
    717  1.31.14.1  pgoyette 		free(sp->word);
    718        1.8  christos 	    }
    719        1.8  christos 	    else
    720        1.9  christos 		prlex(cshout, lexp);
    721        1.9  christos 	}
    722        1.9  christos 	else {
    723        1.9  christos 	    s1 = Strspl(*pv, STRslash);
    724        1.9  christos 	    sp->word = Strspl(s1, sp->word);
    725  1.31.14.1  pgoyette 	    free(s1);
    726        1.9  christos 	    if (str == NULL)
    727        1.9  christos 		prlex(cshout, lexp);
    728        1.9  christos 	    else
    729       1.16       wiz 		(void)Strcpy(str, sp->word);
    730  1.31.14.1  pgoyette 	    free(sp->word);
    731        1.5   mycroft 	}
    732        1.9  christos 	found = 1;
    733        1.5   mycroft     }
    734        1.5   mycroft     else {
    735        1.9  christos  	if (str == NULL) {
    736        1.9  christos 	    if (aliased)
    737        1.9  christos 		prlex(cshout, lexp);
    738       1.16       wiz 	    (void)fprintf(csherr,
    739        1.9  christos 			   "%s: Command not found.\n", vis_str(sp->word));
    740        1.9  christos 	}
    741        1.9  christos 	else
    742       1.16       wiz 	    (void)Strcpy(str, sp->word);
    743        1.9  christos 	found = 0;
    744        1.5   mycroft     }
    745        1.5   mycroft     sp->word = s0;		/* we save and then restore this */
    746  1.31.14.1  pgoyette     free(cmd);
    747        1.9  christos     return found;
    748        1.1       cgd }
    749