2 * Copyright (c) 1980, 1991, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 static char sccsid[] = "@(#)exec.c 8.1 (Berkeley) 5/31/93";
38 static const char rcsid[] =
39 "$Id: exec.c,v 1.6 1997/02/22 14:01:50 peter Exp $";
43 #include <sys/types.h>
44 #include <sys/param.h>
62 * System level search and execute of a command. We look in each directory
63 * for the specified command name. If the name contains a '/' then we
64 * execute only the full path name. If there is no search path then we
65 * execute only full path names.
67 extern char **environ;
70 * As we search for the command we note the first non-trivial error
71 * message for presentation to the user. This allows us often
72 * to show that a file has the wrong mode/no access when the file
73 * is not in the last component of the search path, so we must
74 * go on after first detecting the error.
76 static char *exerr; /* Execution error message */
77 static Char *expath; /* Path for exerr */
80 * Xhash is an array of HSHSIZ bits (HSHSIZ / 8 chars), which are used
81 * to hash execs. If it is allocated (havhash true), then to tell
82 * whether ``name'' is (possibly) present in the i'th component
83 * of the variable path, you look at the bit in xhash indexed by
84 * hash(hashname("name"), i). This is setup automatically
85 * after .login is executed, and recomputed whenever ``path'' is
87 * The two part hash function is designed to let texec() call the
88 * more expensive hashname() only once and the simple hash() several
89 * times (once for each path component checked).
90 * Byte size is assumed to be 8.
92 #define HSHSIZ 8192 /* 1k bytes */
93 #define HSHMASK (HSHSIZ - 1)
95 static char xhash[HSHSIZ / 8];
97 #define hash(a, b) (((a) * HSHMUL + (b)) & HSHMASK)
98 #define bit(h, b) ((h)[(b) >> 3] & 1 << ((b) & 7)) /* bit test */
99 #define bis(h, b) ((h)[(b) >> 3] |= 1 << ((b) & 7)) /* bit set */
100 static int hits, misses;
102 /* Dummy search path for just absolute search when no path */
103 static Char *justabs[] = {STRNULL, 0};
105 static void pexerr __P((void));
106 static void texec __P((Char *, Char **));
107 static int hashname __P((Char *));
108 static void tellmewhat __P((struct wordent *));
109 static int executable __P((Char *, Char *, bool));
110 static int iscommand __P((Char *));
119 Char *dp, **pv, **av, *sav;
120 struct varent *pathv;
122 int hashval = 0, hashval1, i;
126 * Glob the command name. We will search $path even if this does something,
127 * as in sh but not in csh. One special case: if there is no PATH, then we
128 * execute only commands which start with '/'.
130 blk[0] = t->t_dcom[0];
132 gflag = 0, tglob(blk);
136 setname(vis_str(blk[0]));
137 stderror(ERR_NAME | ERR_NOMATCH);
147 expath = Strsave(pv[0]);
150 pathv = adrof(STRpath);
151 if (pathv == 0 && expath[0] != '/') {
155 slash = any(short2str(expath), '/');
158 * Glob the argument list, if necessary. Otherwise trim off the quote bits.
167 setname(vis_str(expath));
168 stderror(ERR_NAME | ERR_NOMATCH);
176 t->t_dcom = blkspl(pv, av);
182 if (*av == NULL || **av == '\0')
185 xechoit(av); /* Echo command if -x */
187 * Since all internal file descriptors are set to close on exec, we don't
188 * need to close them explicitly here. Just reorient ourselves for error
196 * We must do this AFTER any possible forking (like `foo` in glob) so that
197 * this shell can still do subprocesses.
199 (void) sigsetmask((sigset_t) 0);
201 * If no path, no words in path, or a / in the filename then restrict the
204 if (pathv == 0 || pathv->vec[0] == 0 || slash)
208 sav = Strspl(STRslash, *av);/* / command name for postpending */
211 hashval = hashname(*av);
216 * Try to save time by looking at the hash table for where this command
217 * could be. If we are doing delayed hashing, then we put the names in
218 * one at a time, as the user enters them. This is kinda like Korn
219 * Shell's "tracked aliases".
221 if (!slash && pv[0][0] == '/' && havhash) {
222 hashval1 = hash(hashval, i);
223 if (!bit(xhash, hashval1))
226 if (pv[0][0] == 0 || eq(pv[0], STRdot)) /* don't make ./xxx */
229 dp = Strspl(*pv, sav);
249 /* Couldn't find the damn thing */
251 setname(vis_str(expath));
253 xfree((ptr_t) expath);
259 stderror(ERR_NAME | ERR_STRING, exerr);
260 stderror(ERR_NAME | ERR_COMMAND);
264 * Execute command f, arg list t.
265 * Record error message if not found.
266 * Also do shell scripts here.
282 /* The order for the conversions is significant */
286 errno = 0; /* don't use a previous error */
287 (void) execve(f, t, environ);
289 blkfree((Char **) t);
294 * From: casper@fwi.uva.nl (Casper H.S. Dik) If we could not execute
295 * it, don't feed it to the shell if it looks like a binary!
297 if ((fd = open(f, O_RDONLY)) != -1) {
298 if (read(fd, (char *) &c, 1) == 1) {
299 if (!Isprint(c) && (c != '\n' && c != '\t')) {
302 * We *know* what ENOEXEC means.
304 stderror(ERR_ARCH, f, strerror(errno));
314 * If there is an alias for shell, then put the words of the alias in
315 * front of the argument list replacing the command name. Note no
316 * interpretation of the words at this point.
318 v = adrof1(STRshell, &aliases);
321 vp[0] = adrof(STRshell) ? value(STRshell) : STR_SHELLPATH;
324 if (fd != -1 && c != '#')
333 st = blkspl(vp, st); /* Splice up the new arglst */
336 /* The order for the conversions is significant */
341 (void) execve(f, t, environ);
343 blkfree((Char **) t);
344 /* The sky is falling, the sky is falling! */
347 stderror(ERR_SYSTEM, f, strerror(errno));
354 exerr = strerror(errno);
356 xfree((ptr_t) expath);
357 expath = Strsave(sf);
369 int saveIN, saveOUT, saveDIAG, saveSTD;
377 sig_t osigint, osigquit, osigterm;
379 if (chkstop == 0 && setintr)
382 * Hmm, we don't really want to do that now because we might
383 * fail, but what is the choice
387 osigint = signal(SIGINT, parintr);
388 osigquit = signal(SIGQUIT, parintr);
389 osigterm = signal(SIGTERM, parterm);
397 saveIN = dcopy(SHIN, -1);
398 saveOUT = dcopy(SHOUT, -1);
399 saveDIAG = dcopy(SHERR, -1);
400 saveSTD = dcopy(OLDSTD, -1);
402 lshift(kp->t_dcom, 1);
406 if ((my_reenter = setexit()) == 0) {
408 SHOUT = dcopy(1, -1);
409 SHERR = dcopy(2, -1);
414 (void) signal(SIGINT, osigint);
415 (void) signal(SIGQUIT, osigquit);
416 (void) signal(SIGTERM, osigterm);
423 (void) close(OLDSTD);
424 SHIN = dmove(saveIN, oSHIN);
425 SHOUT = dmove(saveOUT, oSHOUT);
426 SHERR = dmove(saveDIAG, oSHERR);
427 OLDSTD = dmove(saveSTD, oOLDSTD);
431 stderror(ERR_SILENT);
438 if (adrof(STRecho)) {
439 (void) fflush(csherr);
441 (void) fputc('\n', csherr);
455 struct varent *pathv = adrof(STRpath);
460 for (cnt = 0; cnt < sizeof xhash; cnt++)
464 for (pv = pathv->vec; *pv; pv++, i++) {
467 dirp = opendir(short2str(*pv));
470 while ((dp = readdir(dirp)) != NULL) {
473 if (dp->d_name[0] == '.' &&
474 (dp->d_name[1] == '\0' ||
475 (dp->d_name[1] == '.' && dp->d_name[2] == '\0')))
477 hashval = hash(hashname(str2short(dp->d_name)), i);
479 /* tw_add_comm_name (dp->d_name); */
481 (void) closedir(dirp);
501 (void) fprintf(cshout, "%d hits, %d misses, %d%%\n",
502 hits, misses, 100 * hits / (hits + misses));
506 * Hash a command name.
526 bool slash = any(short2str(name), '/');
527 int hashval = 0, hashval1, i;
530 if (v == 0 || v->vec[0] == 0 || slash)
534 sav = Strspl(STRslash, name); /* / command name for postpending */
536 hashval = hashname(name);
539 if (!slash && pv[0][0] == '/' && havhash) {
540 hashval1 = hash(hashval, i);
541 if (!bit(xhash, hashval1))
544 if (pv[0][0] == 0 || eq(pv[0], STRdot)) { /* don't make ./xxx */
545 if (executable(NULL, name, 0)) {
551 if (executable(*pv, sav, 0)) {
565 * Andreas Luik <luik@isaak.isa.de>
566 * I S A GmbH - Informationssysteme fuer computerintegrierte Automatisierung
570 * is the executable() routine below and changes to iscommand().
575 * executable() examines the pathname obtained by concatenating dir and name
576 * (dir may be NULL), and returns 1 either if it is executable by us, or
577 * if dir_ok is set and the pathname refers to a directory.
578 * This is a bit kludgy, but in the name of optimization...
581 executable(dir, name, dir_ok)
586 Char path[MAXPATHLEN + 1], *dp, *sp;
590 for (dp = path, sp = dir; *sp; *dp++ = *sp++)
591 if (dp == &path[MAXPATHLEN + 1]) {
595 for (sp = name; *sp; *dp++ = *sp++)
596 if (dp == &path[MAXPATHLEN + 1]) {
601 strname = short2str(path);
604 strname = short2str(name);
605 return (stat(strname, &stbuf) != -1 &&
606 ((S_ISREG(stbuf.st_mode) &&
607 /* save time by not calling access() in the hopeless case */
608 (stbuf.st_mode & (S_IXOTH | S_IXGRP | S_IXUSR)) &&
609 access(strname, X_OK) == 0) ||
610 (dir_ok && S_ISDIR(stbuf.st_mode))));
613 /* The dowhich() is by:
614 * Andreas Luik <luik@isaak.isa.de>
615 * I S A GmbH - Informationssysteme fuer computerintegrierte Automatisierung
627 struct wordent lex[3];
630 lex[0].next = &lex[1];
631 lex[1].next = &lex[2];
632 lex[2].next = &lex[0];
634 lex[0].prev = &lex[2];
635 lex[1].prev = &lex[0];
636 lex[2].prev = &lex[1];
638 lex[0].word = STRNULL;
639 lex[2].word = STRret;
642 if ((vp = adrof1(*v, &aliases)) != NULL) {
643 (void) fprintf(cshout, "%s: \t aliased to ", vis_str(*v));
644 blkpr(cshout, vp->vec);
645 (void) fputc('\n', cshout);
659 struct biltins *bptr;
660 struct wordent *sp = lex->next;
662 Char *s0, *s1, *s2, *cmd;
665 if (adrof1(sp->word, &aliases)) {
671 s0 = sp->word; /* to get the memory freeing right... */
673 /* handle quoted alias hack */
674 if ((*(sp->word) & (QUOTE | TRIM)) == QUOTE)
677 /* do quoting, if it hasn't been done */
684 while (*s2 && *s2 != qc)
685 *s1++ = *s2++ | QUOTE;
691 *s1++ = *s2++ | QUOTE;
698 for (bptr = bfunc; bptr < &bfunc[nbfunc]; bptr++) {
699 if (eq(sp->word, str2short(bptr->bname))) {
702 (void) fprintf(cshout, "%s: shell built-in command.\n",
704 sp->word = s0; /* we save and then restore this */
709 sp->word = cmd = globone(sp->word, G_IGNORE);
711 if ((i = iscommand(strip(sp->word))) != 0) {
714 bool slash = any(short2str(sp->word), '/');
717 if (v == 0 || v->vec[0] == 0 || slash)
724 if (pv[0][0] == 0 || eq(pv[0], STRdot)) {
726 sp->word = Strspl(STRdotsl, sp->word);
728 xfree((ptr_t) sp->word);
731 sp->word = s0; /* we save and then restore this */
735 s1 = Strspl(*pv, STRslash);
736 sp->word = Strspl(s1, sp->word);
739 xfree((ptr_t) sp->word);
744 (void) fprintf(csherr, "%s: Command not found.\n", vis_str(sp->word));
747 sp->word = s0; /* we save and then restore this */