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[] =
43 #include <sys/param.h>
60 * System level search and execute of a command. We look in each directory
61 * for the specified command name. If the name contains a '/' then we
62 * execute only the full path name. If there is no search path then we
63 * execute only full path names.
65 extern char **environ;
68 * As we search for the command we note the first non-trivial error
69 * message for presentation to the user. This allows us often
70 * to show that a file has the wrong mode/no access when the file
71 * is not in the last component of the search path, so we must
72 * go on after first detecting the error.
74 static char *exerr; /* Execution error message */
75 static Char *expath; /* Path for exerr */
78 * Xhash is an array of HSHSIZ bits (HSHSIZ / 8 chars), which are used
79 * to hash execs. If it is allocated (havhash true), then to tell
80 * whether ``name'' is (possibly) present in the i'th component
81 * of the variable path, you look at the bit in xhash indexed by
82 * hash(hashname("name"), i). This is setup automatically
83 * after .login is executed, and recomputed whenever ``path'' is
85 * The two part hash function is designed to let texec() call the
86 * more expensive hashname() only once and the simple hash() several
87 * times (once for each path component checked).
88 * Byte size is assumed to be 8.
90 #define HSHSIZ 8192 /* 1k bytes */
91 #define HSHMASK (HSHSIZ - 1)
93 static char xhash[HSHSIZ / 8];
95 #define hash(a, b) (((a) * HSHMUL + (b)) & HSHMASK)
96 #define bit(h, b) ((h)[(b) >> 3] & 1 << ((b) & 7)) /* bit test */
97 #define bis(h, b) ((h)[(b) >> 3] |= 1 << ((b) & 7)) /* bit set */
98 static int hits, misses;
100 /* Dummy search path for just absolute search when no path */
101 static Char *justabs[] = {STRNULL, 0};
103 static void pexerr __P((void));
104 static void texec __P((Char *, Char **));
105 static int hashname __P((Char *));
106 static void tellmewhat __P((struct wordent *));
107 static int executable __P((Char *, Char *, bool));
108 static int iscommand __P((Char *));
117 Char *dp, **pv, **av, *sav;
118 struct varent *pathv;
120 int hashval = 0, hashval1, i;
124 * Glob the command name. We will search $path even if this does something,
125 * as in sh but not in csh. One special case: if there is no PATH, then we
126 * execute only commands which start with '/'.
128 blk[0] = t->t_dcom[0];
130 gflag = 0, tglob(blk);
134 setname(vis_str(blk[0]));
135 stderror(ERR_NAME | ERR_NOMATCH);
145 expath = Strsave(pv[0]);
148 pathv = adrof(STRpath);
149 if (pathv == 0 && expath[0] != '/') {
153 slash = any(short2str(expath), '/');
156 * Glob the argument list, if necessary. Otherwise trim off the quote bits.
165 setname(vis_str(expath));
166 stderror(ERR_NAME | ERR_NOMATCH);
174 t->t_dcom = blkspl(pv, av);
180 if (*av == NULL || **av == '\0')
183 xechoit(av); /* Echo command if -x */
185 * Since all internal file descriptors are set to close on exec, we don't
186 * need to close them explicitly here. Just reorient ourselves for error
194 * We must do this AFTER any possible forking (like `foo` in glob) so that
195 * this shell can still do subprocesses.
197 (void) sigsetmask(0);
199 * If no path, no words in path, or a / in the filename then restrict the
202 if (pathv == 0 || pathv->vec[0] == 0 || slash)
206 sav = Strspl(STRslash, *av);/* / command name for postpending */
209 hashval = hashname(*av);
214 * Try to save time by looking at the hash table for where this command
215 * could be. If we are doing delayed hashing, then we put the names in
216 * one at a time, as the user enters them. This is kinda like Korn
217 * Shell's "tracked aliases".
219 if (!slash && pv[0][0] == '/' && havhash) {
220 hashval1 = hash(hashval, i);
221 if (!bit(xhash, hashval1))
224 if (pv[0][0] == 0 || eq(pv[0], STRdot)) /* don't make ./xxx */
227 dp = Strspl(*pv, sav);
247 /* Couldn't find the damn thing */
249 setname(vis_str(expath));
251 xfree((ptr_t) expath);
257 stderror(ERR_NAME | ERR_STRING, exerr);
258 stderror(ERR_NAME | ERR_COMMAND);
262 * Execute command f, arg list t.
263 * Record error message if not found.
264 * Also do shell scripts here.
280 /* The order for the conversions is significant */
284 errno = 0; /* don't use a previous error */
285 (void) execve(f, t, environ);
287 blkfree((Char **) t);
292 * From: casper@fwi.uva.nl (Casper H.S. Dik) If we could not execute
293 * it, don't feed it to the shell if it looks like a binary!
295 if ((fd = open(f, O_RDONLY)) != -1) {
296 if (read(fd, (char *) &c, 1) == 1) {
297 if (!Isprint(c) && (c != '\n' && c != '\t')) {
300 * We *know* what ENOEXEC means.
302 stderror(ERR_ARCH, f, strerror(errno));
312 * If there is an alias for shell, then put the words of the alias in
313 * front of the argument list replacing the command name. Note no
314 * interpretation of the words at this point.
316 v = adrof1(STRshell, &aliases);
319 vp[0] = adrof(STRshell) ? value(STRshell) : STR_SHELLPATH;
322 if (fd != -1 && c != '#')
331 st = blkspl(vp, st); /* Splice up the new arglst */
334 /* The order for the conversions is significant */
339 (void) execve(f, t, environ);
341 blkfree((Char **) t);
342 /* The sky is falling, the sky is falling! */
345 stderror(ERR_SYSTEM, f, strerror(errno));
352 exerr = strerror(errno);
354 xfree((ptr_t) expath);
355 expath = Strsave(sf);
367 int saveIN, saveOUT, saveDIAG, saveSTD;
375 sig_t osigint, osigquit, osigterm;
377 if (chkstop == 0 && setintr)
380 * Hmm, we don't really want to do that now because we might
381 * fail, but what is the choice
385 osigint = signal(SIGINT, parintr);
386 osigquit = signal(SIGQUIT, parintr);
387 osigterm = signal(SIGTERM, parterm);
395 saveIN = dcopy(SHIN, -1);
396 saveOUT = dcopy(SHOUT, -1);
397 saveDIAG = dcopy(SHERR, -1);
398 saveSTD = dcopy(OLDSTD, -1);
400 lshift(kp->t_dcom, 1);
404 if ((my_reenter = setexit()) == 0) {
406 SHOUT = dcopy(1, -1);
407 SHERR = dcopy(2, -1);
412 (void) signal(SIGINT, osigint);
413 (void) signal(SIGQUIT, osigquit);
414 (void) signal(SIGTERM, osigterm);
421 (void) close(OLDSTD);
422 SHIN = dmove(saveIN, oSHIN);
423 SHOUT = dmove(saveOUT, oSHOUT);
424 SHERR = dmove(saveDIAG, oSHERR);
425 OLDSTD = dmove(saveSTD, oOLDSTD);
429 stderror(ERR_SILENT);
436 if (adrof(STRecho)) {
437 (void) fflush(csherr);
439 (void) fputc('\n', csherr);
453 struct varent *pathv = adrof(STRpath);
458 for (cnt = 0; cnt < sizeof xhash; cnt++)
462 for (pv = pathv->vec; *pv; pv++, i++) {
465 dirp = opendir(short2str(*pv));
468 while ((dp = readdir(dirp)) != NULL) {
471 if (dp->d_name[0] == '.' &&
472 (dp->d_name[1] == '\0' ||
473 (dp->d_name[1] == '.' && dp->d_name[2] == '\0')))
475 hashval = hash(hashname(str2short(dp->d_name)), i);
477 /* tw_add_comm_name (dp->d_name); */
479 (void) closedir(dirp);
499 (void) fprintf(cshout, "%d hits, %d misses, %d%%\n",
500 hits, misses, 100 * hits / (hits + misses));
504 * Hash a command name.
524 bool slash = any(short2str(name), '/');
525 int hashval = 0, hashval1, i;
528 if (v == 0 || v->vec[0] == 0 || slash)
532 sav = Strspl(STRslash, name); /* / command name for postpending */
534 hashval = hashname(name);
537 if (!slash && pv[0][0] == '/' && havhash) {
538 hashval1 = hash(hashval, i);
539 if (!bit(xhash, hashval1))
542 if (pv[0][0] == 0 || eq(pv[0], STRdot)) { /* don't make ./xxx */
543 if (executable(NULL, name, 0)) {
549 if (executable(*pv, sav, 0)) {
563 * Andreas Luik <luik@isaak.isa.de>
564 * I S A GmbH - Informationssysteme fuer computerintegrierte Automatisierung
568 * is the executable() routine below and changes to iscommand().
573 * executable() examines the pathname obtained by concatenating dir and name
574 * (dir may be NULL), and returns 1 either if it is executable by us, or
575 * if dir_ok is set and the pathname refers to a directory.
576 * This is a bit kludgy, but in the name of optimization...
579 executable(dir, name, dir_ok)
584 Char path[MAXPATHLEN + 1], *dp, *sp;
588 for (dp = path, sp = dir; *sp; *dp++ = *sp++)
589 if (dp == &path[MAXPATHLEN + 1]) {
593 for (sp = name; *sp; *dp++ = *sp++)
594 if (dp == &path[MAXPATHLEN + 1]) {
599 strname = short2str(path);
602 strname = short2str(name);
603 return (stat(strname, &stbuf) != -1 &&
604 ((S_ISREG(stbuf.st_mode) &&
605 /* save time by not calling access() in the hopeless case */
606 (stbuf.st_mode & (S_IXOTH | S_IXGRP | S_IXUSR)) &&
607 access(strname, X_OK) == 0) ||
608 (dir_ok && S_ISDIR(stbuf.st_mode))));
611 /* The dowhich() is by:
612 * Andreas Luik <luik@isaak.isa.de>
613 * I S A GmbH - Informationssysteme fuer computerintegrierte Automatisierung
625 struct wordent lex[3];
628 lex[0].next = &lex[1];
629 lex[1].next = &lex[2];
630 lex[2].next = &lex[0];
632 lex[0].prev = &lex[2];
633 lex[1].prev = &lex[0];
634 lex[2].prev = &lex[1];
636 lex[0].word = STRNULL;
637 lex[2].word = STRret;
640 if ((vp = adrof1(*v, &aliases)) != NULL) {
641 (void) fprintf(cshout, "%s: \t aliased to ", vis_str(*v));
642 blkpr(cshout, vp->vec);
643 (void) fputc('\n', cshout);
657 struct biltins *bptr;
658 struct wordent *sp = lex->next;
660 Char *s0, *s1, *s2, *cmd;
663 if (adrof1(sp->word, &aliases)) {
669 s0 = sp->word; /* to get the memory freeing right... */
671 /* handle quoted alias hack */
672 if ((*(sp->word) & (QUOTE | TRIM)) == QUOTE)
675 /* do quoting, if it hasn't been done */
682 while (*s2 && *s2 != qc)
683 *s1++ = *s2++ | QUOTE;
689 *s1++ = *s2++ | QUOTE;
696 for (bptr = bfunc; bptr < &bfunc[nbfunc]; bptr++) {
697 if (eq(sp->word, str2short(bptr->bname))) {
700 (void) fprintf(cshout, "%s: shell built-in command.\n",
702 sp->word = s0; /* we save and then restore this */
707 sp->word = cmd = globone(sp->word, G_IGNORE);
709 if ((i = iscommand(strip(sp->word))) != 0) {
712 bool slash = any(short2str(sp->word), '/');
715 if (v == 0 || v->vec[0] == 0 || slash)
722 if (pv[0][0] == 0 || eq(pv[0], STRdot)) {
724 sp->word = Strspl(STRdotsl, sp->word);
726 xfree((ptr_t) sp->word);
729 sp->word = s0; /* we save and then restore this */
733 s1 = Strspl(*pv, STRslash);
734 sp->word = Strspl(s1, sp->word);
737 xfree((ptr_t) sp->word);
742 (void) fprintf(csherr, "%s: Command not found.\n", vis_str(sp->word));
745 sp->word = s0; /* we save and then restore this */