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
37 static char sccsid[] = "@(#)exec.c 8.1 (Berkeley) 5/31/93";
40 #include <sys/types.h>
41 #include <sys/param.h>
59 * System level search and execute of a command. We look in each directory
60 * for the specified command name. If the name contains a '/' then we
61 * execute only the full path name. If there is no search path then we
62 * execute only full path names.
64 extern char **environ;
67 * As we search for the command we note the first non-trivial error
68 * message for presentation to the user. This allows us often
69 * to show that a file has the wrong mode/no access when the file
70 * is not in the last component of the search path, so we must
71 * go on after first detecting the error.
73 static char *exerr; /* Execution error message */
74 static Char *expath; /* Path for exerr */
77 * Xhash is an array of HSHSIZ bits (HSHSIZ / 8 chars), which are used
78 * to hash execs. If it is allocated (havhash true), then to tell
79 * whether ``name'' is (possibly) present in the i'th component
80 * of the variable path, you look at the bit in xhash indexed by
81 * hash(hashname("name"), i). This is setup automatically
82 * after .login is executed, and recomputed whenever ``path'' is
84 * The two part hash function is designed to let texec() call the
85 * more expensive hashname() only once and the simple hash() several
86 * times (once for each path component checked).
87 * Byte size is assumed to be 8.
89 #define HSHSIZ 8192 /* 1k bytes */
90 #define HSHMASK (HSHSIZ - 1)
92 static char xhash[HSHSIZ / 8];
94 #define hash(a, b) (((a) * HSHMUL + (b)) & HSHMASK)
95 #define bit(h, b) ((h)[(b) >> 3] & 1 << ((b) & 7)) /* bit test */
96 #define bis(h, b) ((h)[(b) >> 3] |= 1 << ((b) & 7)) /* bit set */
97 static int hits, misses;
99 /* Dummy search path for just absolute search when no path */
100 static Char *justabs[] = {STRNULL, 0};
102 static void pexerr __P((void));
103 static void texec __P((Char *, Char **));
104 static int hashname __P((Char *));
105 static void tellmewhat __P((struct wordent *));
106 static int executable __P((Char *, Char *, bool));
107 static int iscommand __P((Char *));
116 register Char *dp, **pv, **av, *sav;
117 register struct varent *pathv;
119 register int hashval = 0, hashval1, i;
123 * Glob the command name. We will search $path even if this does something,
124 * as in sh but not in csh. One special case: if there is no PATH, then we
125 * execute only commands which start with '/'.
127 blk[0] = t->t_dcom[0];
129 gflag = 0, tglob(blk);
133 setname(vis_str(blk[0]));
134 stderror(ERR_NAME | ERR_NOMATCH);
144 expath = Strsave(pv[0]);
147 pathv = adrof(STRpath);
148 if (pathv == 0 && expath[0] != '/') {
152 slash = any(short2str(expath), '/');
155 * Glob the argument list, if necessary. Otherwise trim off the quote bits.
164 setname(vis_str(expath));
165 stderror(ERR_NAME | ERR_NOMATCH);
173 t->t_dcom = blkspl(pv, av);
179 if (*av == NULL || **av == '\0')
182 xechoit(av); /* Echo command if -x */
184 * Since all internal file descriptors are set to close on exec, we don't
185 * need to close them explicitly here. Just reorient ourselves for error
193 * We must do this AFTER any possible forking (like `foo` in glob) so that
194 * this shell can still do subprocesses.
196 (void) sigsetmask((sigset_t) 0);
198 * If no path, no words in path, or a / in the filename then restrict the
201 if (pathv == 0 || pathv->vec[0] == 0 || slash)
205 sav = Strspl(STRslash, *av);/* / command name for postpending */
208 hashval = hashname(*av);
213 * Try to save time by looking at the hash table for where this command
214 * could be. If we are doing delayed hashing, then we put the names in
215 * one at a time, as the user enters them. This is kinda like Korn
216 * Shell's "tracked aliases".
218 if (!slash && pv[0][0] == '/' && havhash) {
219 hashval1 = hash(hashval, i);
220 if (!bit(xhash, hashval1))
223 if (pv[0][0] == 0 || eq(pv[0], STRdot)) /* don't make ./xxx */
226 dp = Strspl(*pv, sav);
246 /* Couldn't find the damn thing */
248 setname(vis_str(expath));
250 xfree((ptr_t) expath);
256 stderror(ERR_NAME | ERR_STRING, exerr);
257 stderror(ERR_NAME | ERR_COMMAND);
261 * Execute command f, arg list t.
262 * Record error message if not found.
263 * Also do shell scripts here.
272 register struct varent *v;
279 /* The order for the conversions is significant */
283 errno = 0; /* don't use a previous error */
284 (void) execve(f, t, environ);
286 blkfree((Char **) t);
291 * From: casper@fwi.uva.nl (Casper H.S. Dik) If we could not execute
292 * it, don't feed it to the shell if it looks like a binary!
294 if ((fd = open(f, O_RDONLY)) != -1) {
295 if (read(fd, (char *) &c, 1) == 1) {
296 if (!Isprint(c) && (c != '\n' && c != '\t')) {
299 * We *know* what ENOEXEC means.
301 stderror(ERR_ARCH, f, strerror(errno));
311 * If there is an alias for shell, then put the words of the alias in
312 * front of the argument list replacing the command name. Note no
313 * interpretation of the words at this point.
315 v = adrof1(STRshell, &aliases);
318 vp[0] = adrof(STRshell) ? value(STRshell) : STR_SHELLPATH;
321 if (fd != -1 && c != '#')
330 st = blkspl(vp, st); /* Splice up the new arglst */
333 /* The order for the conversions is significant */
338 (void) execve(f, t, environ);
340 blkfree((Char **) t);
341 /* The sky is falling, the sky is falling! */
344 stderror(ERR_SYSTEM, f, strerror(errno));
351 exerr = strerror(errno);
353 xfree((ptr_t) expath);
354 expath = Strsave(sf);
364 register struct command *kp;
366 int saveIN, saveOUT, saveDIAG, saveSTD;
374 sig_t osigint, osigquit, osigterm;
376 if (chkstop == 0 && setintr)
379 * Hmm, we don't really want to do that now because we might
380 * fail, but what is the choice
384 osigint = signal(SIGINT, parintr);
385 osigquit = signal(SIGQUIT, parintr);
386 osigterm = signal(SIGTERM, parterm);
394 saveIN = dcopy(SHIN, -1);
395 saveOUT = dcopy(SHOUT, -1);
396 saveDIAG = dcopy(SHERR, -1);
397 saveSTD = dcopy(OLDSTD, -1);
399 lshift(kp->t_dcom, 1);
403 if ((my_reenter = setexit()) == 0) {
405 SHOUT = dcopy(1, -1);
406 SHERR = dcopy(2, -1);
411 (void) signal(SIGINT, osigint);
412 (void) signal(SIGQUIT, osigquit);
413 (void) signal(SIGTERM, osigterm);
420 (void) close(OLDSTD);
421 SHIN = dmove(saveIN, oSHIN);
422 SHOUT = dmove(saveOUT, oSHOUT);
423 SHERR = dmove(saveDIAG, oSHERR);
424 OLDSTD = dmove(saveSTD, oOLDSTD);
428 stderror(ERR_SILENT);
435 if (adrof(STRecho)) {
436 (void) fflush(csherr);
438 (void) fputc('\n', csherr);
449 register struct dirent *dp;
452 struct varent *pathv = adrof(STRpath);
457 for (cnt = 0; cnt < sizeof xhash; cnt++)
461 for (pv = pathv->vec; *pv; pv++, i++) {
464 dirp = opendir(short2str(*pv));
467 while ((dp = readdir(dirp)) != NULL) {
470 if (dp->d_name[0] == '.' &&
471 (dp->d_name[1] == '\0' ||
472 (dp->d_name[1] == '.' && dp->d_name[2] == '\0')))
474 hashval = hash(hashname(str2short(dp->d_name)), i);
476 /* tw_add_comm_name (dp->d_name); */
478 (void) closedir(dirp);
498 (void) fprintf(cshout, "%d hits, %d misses, %d%%\n",
499 hits, misses, 100 * hits / (hits + misses));
503 * Hash a command name.
522 register struct varent *v;
523 register bool slash = any(short2str(name), '/');
524 register int hashval = 0, hashval1, i;
527 if (v == 0 || v->vec[0] == 0 || slash)
531 sav = Strspl(STRslash, name); /* / command name for postpending */
533 hashval = hashname(name);
536 if (!slash && pv[0][0] == '/' && havhash) {
537 hashval1 = hash(hashval, i);
538 if (!bit(xhash, hashval1))
541 if (pv[0][0] == 0 || eq(pv[0], STRdot)) { /* don't make ./xxx */
542 if (executable(NULL, name, 0)) {
548 if (executable(*pv, sav, 0)) {
562 * Andreas Luik <luik@isaak.isa.de>
563 * I S A GmbH - Informationssysteme fuer computerintegrierte Automatisierung
567 * is the executable() routine below and changes to iscommand().
572 * executable() examines the pathname obtained by concatenating dir and name
573 * (dir may be NULL), and returns 1 either if it is executable by us, or
574 * if dir_ok is set and the pathname refers to a directory.
575 * This is a bit kludgy, but in the name of optimization...
578 executable(dir, name, dir_ok)
583 Char path[MAXPATHLEN + 1], *dp, *sp;
587 for (dp = path, sp = dir; *sp; *dp++ = *sp++)
588 if (dp == &path[MAXPATHLEN + 1]) {
592 for (sp = name; *sp; *dp++ = *sp++)
593 if (dp == &path[MAXPATHLEN + 1]) {
598 strname = short2str(path);
601 strname = short2str(name);
602 return (stat(strname, &stbuf) != -1 &&
603 ((S_ISREG(stbuf.st_mode) &&
604 /* save time by not calling access() in the hopeless case */
605 (stbuf.st_mode & (S_IXOTH | S_IXGRP | S_IXUSR)) &&
606 access(strname, X_OK) == 0) ||
607 (dir_ok && S_ISDIR(stbuf.st_mode))));
610 /* The dowhich() is by:
611 * Andreas Luik <luik@isaak.isa.de>
612 * I S A GmbH - Informationssysteme fuer computerintegrierte Automatisierung
624 struct wordent lex[3];
627 lex[0].next = &lex[1];
628 lex[1].next = &lex[2];
629 lex[2].next = &lex[0];
631 lex[0].prev = &lex[2];
632 lex[1].prev = &lex[0];
633 lex[2].prev = &lex[1];
635 lex[0].word = STRNULL;
636 lex[2].word = STRret;
639 if ((vp = adrof1(*v, &aliases)) != NULL) {
640 (void) fprintf(cshout, "%s: \t aliased to ", vis_str(*v));
641 blkpr(cshout, vp->vec);
642 (void) fputc('\n', cshout);
656 register struct biltins *bptr;
657 register struct wordent *sp = lex->next;
659 Char *s0, *s1, *s2, *cmd;
662 if (adrof1(sp->word, &aliases)) {
668 s0 = sp->word; /* to get the memory freeing right... */
670 /* handle quoted alias hack */
671 if ((*(sp->word) & (QUOTE | TRIM)) == QUOTE)
674 /* do quoting, if it hasn't been done */
681 while (*s2 && *s2 != qc)
682 *s1++ = *s2++ | QUOTE;
688 *s1++ = *s2++ | QUOTE;
695 for (bptr = bfunc; bptr < &bfunc[nbfunc]; bptr++) {
696 if (eq(sp->word, str2short(bptr->bname))) {
699 (void) fprintf(cshout, "%s: shell built-in command.\n",
701 sp->word = s0; /* we save and then restore this */
706 sp->word = cmd = globone(sp->word, G_IGNORE);
708 if ((i = iscommand(strip(sp->word))) != 0) {
710 register struct varent *v;
711 bool slash = any(short2str(sp->word), '/');
714 if (v == 0 || v->vec[0] == 0 || slash)
721 if (pv[0][0] == 0 || eq(pv[0], STRdot)) {
723 sp->word = Strspl(STRdotsl, sp->word);
725 xfree((ptr_t) sp->word);
728 sp->word = s0; /* we save and then restore this */
732 s1 = Strspl(*pv, STRslash);
733 sp->word = Strspl(s1, sp->word);
736 xfree((ptr_t) sp->word);
741 (void) fprintf(csherr, "%s: Command not found.\n", vis_str(sp->word));
744 sp->word = s0; /* we save and then restore this */