1 /* $Header: /p/tcsh/cvsroot/tcsh/sh.exec.c,v 3.73 2006/08/24 20:56:31 christos Exp $ */
3 * sh.exec.c: Search, find, and execute a command!
6 * Copyright (c) 1980, 1991 The Regents of the University of California.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. 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
35 RCSID("$tcsh: sh.exec.c,v 3.73 2006/08/24 20:56:31 christos Exp $")
41 #endif /*WINNT_NATIVE*/
48 # define FASTHASH /* Fast hashing is the default */
52 * System level search and execute of a command.
53 * We look in each directory for the specified command name.
54 * If the name contains a '/' then we execute only the full path name.
55 * If there is no search path then we execute only full path names.
59 * As we search for the command we note the first non-trivial error
60 * message for presentation to the user. This allows us often
61 * to show that a file has the wrong mode/no access when the file
62 * is not in the last component of the search path, so we must
63 * go on after first detecting the error.
65 static char *exerr
; /* Execution error message */
66 static Char
*expath
; /* Path for exerr */
69 * The two part hash function is designed to let texec() call the
70 * more expensive hashname() only once and the simple hash() several
71 * times (once for each path component checked).
72 * Byte size is assumed to be 8.
74 #define BITS_PER_BYTE 8
78 * xhash is an array of hash buckets which are used to hash execs. If
79 * it is allocated (havhash true), then to tell if ``name'' is
80 * (possibly) presend in the i'th component of the variable path, look
81 * at the [hashname(name)] bucket of size [hashwidth] bytes, in the [i
82 * mod size*8]'th bit. The cache size is defaults to a length of 1024
83 * buckets, each 1 byte wide. This implementation guarantees that
84 * objects n bytes wide will be aligned on n byte boundaries.
88 static unsigned long *xhash
= NULL
;
89 static unsigned int hashlength
= 0, uhashlength
= 0;
90 static unsigned int hashwidth
= 0, uhashwidth
= 0;
91 static int hashdebug
= 0;
93 # define hash(a, b) (((a) * HSHMUL + (b)) % (hashlength))
94 # define widthof(t) (sizeof(t) * BITS_PER_BYTE)
95 # define tbit(f, i, t) (((t *) xhash)[(f)] & \
96 (1UL << (i & (widthof(t) - 1))))
97 # define tbis(f, i, t) (((t *) xhash)[(f)] |= \
98 (1UL << (i & (widthof(t) - 1))))
99 # define cbit(f, i) tbit(f, i, unsigned char)
100 # define cbis(f, i) tbis(f, i, unsigned char)
101 # define sbit(f, i) tbit(f, i, unsigned short)
102 # define sbis(f, i) tbis(f, i, unsigned short)
103 # define ibit(f, i) tbit(f, i, unsigned int)
104 # define ibis(f, i) tbis(f, i, unsigned int)
105 # define lbit(f, i) tbit(f, i, unsigned long)
106 # define lbis(f, i) tbis(f, i, unsigned long)
108 # define bit(f, i) (hashwidth==sizeof(unsigned char) ? cbit(f,i) : \
109 ((hashwidth==sizeof(unsigned short) ? sbit(f,i) : \
110 ((hashwidth==sizeof(unsigned int) ? ibit(f,i) : \
112 # define bis(f, i) (hashwidth==sizeof(unsigned char) ? cbis(f,i) : \
113 ((hashwidth==sizeof(unsigned short) ? sbis(f,i) : \
114 ((hashwidth==sizeof(unsigned int) ? ibis(f,i) : \
118 * Xhash is an array of HSHSIZ bits (HSHSIZ / 8 chars), which are used
119 * to hash execs. If it is allocated (havhash true), then to tell
120 * whether ``name'' is (possibly) present in the i'th component
121 * of the variable path, you look at the bit in xhash indexed by
122 * hash(hashname("name"), i). This is setup automatically
123 * after .login is executed, and recomputed whenever ``path'' is
126 # define HSHSIZ 8192 /* 1k bytes */
127 # define HSHMASK (HSHSIZ - 1)
129 static char xhash
[HSHSIZ
/ BITS_PER_BYTE
];
131 # define hash(a, b) (((a) * HSHMUL + (b)) & HSHMASK)
132 # define bit(h, b) ((h)[(b) >> 3] & 1 << ((b) & 7)) /* bit test */
133 # define bis(h, b) ((h)[(b) >> 3] |= 1 << ((b) & 7)) /* bit set */
135 #endif /* FASTHASH */
138 static int hits
, misses
;
141 /* Dummy search path for just absolute search when no path */
142 static Char
*justabs
[] = {STRNULL
, 0};
144 static void pexerr (void);
145 static void texec (Char
*, Char
**);
146 int hashname (Char
*);
147 static int iscommand (Char
*);
150 doexec(struct command
*t
, int do_glob
)
152 Char
*dp
, **pv
, **av
, *sav
;
159 * Glob the command name. We will search $path even if this does something,
160 * as in sh but not in csh. One special case: if there is no PATH, then we
161 * execute only commands which start with '/'.
163 blk
[0] = t
->t_dcom
[0];
169 pv
= globall(blk
, gflag
);
171 setname(short2str(blk
[0]));
172 stderror(ERR_NAME
| ERR_NOMATCH
);
177 cleanup_push(pv
, blk_cleanup
);
182 expath
= Strsave(pv
[0]);
188 if (v
== 0 && expath
[0] != '/' && expath
[0] != '.')
190 slash
= any(short2str(expath
), '/');
193 * Glob the argument list, if necessary. Otherwise trim off the quote bits.
200 av
= globall(av
, gflag
);
202 setname(short2str(expath
));
203 stderror(ERR_NAME
| ERR_NOMATCH
);
212 t
->t_dcom
= blkspl(pv
, av
);
218 if (*av
== NULL
|| **av
== '\0')
221 xechoit(av
); /* Echo command if -x */
224 * Since all internal file descriptors are set to close on exec, we don't
225 * need to close them explicitly here. Just reorient ourselves for error
232 isoutatty
= isatty(SHOUT
);
233 isdiagatty
= isatty(SHDIAG
);
235 closech(); /* Close random fd's */
238 * We must do this AFTER any possible forking (like `foo` in glob) so that
239 * this shell can still do subprocesses.
244 sigaddset(&set
, SIGINT
);
245 sigaddset(&set
, SIGCHLD
);
246 sigprocmask(SIG_UNBLOCK
, &set
, NULL
);
252 * If no path, no words in path, or a / in the filename then restrict the
255 if (v
== NULL
|| v
->vec
== NULL
|| v
->vec
[0] == NULL
|| slash
)
259 sav
= Strspl(STRslash
, *av
);/* / command name for postpending */
261 cleanup_push(sav
, xfree
);
265 hashval
= havhash
? hashname(*av
) : 0;
273 * Try to save time by looking at the hash table for where this command
274 * could be. If we are doing delayed hashing, then we put the names in
275 * one at a time, as the user enters them. This is kinda like Korn
276 * Shell's "tracked aliases".
278 if (!slash
&& ABSOLUTEP(pv
[0]) && havhash
) {
280 if (!bit(hashval
, i
))
283 int hashval1
= hash(hashval
, i
);
284 if (!bit(xhash
, hashval1
))
286 #endif /* FASTHASH */
288 if (pv
[0][0] == 0 || eq(pv
[0], STRdot
)) /* don't make ./xxx */
291 dp
= Strspl(*pv
, sav
);
293 cleanup_push(dp
, xfree
);
328 /* Couldn't find the damn thing */
330 setname(short2str(expath
));
340 stderror(ERR_NAME
| ERR_STRING
, exerr
);
341 stderror(ERR_NAME
| ERR_COMMAND
);
345 * Execute command f, arg list t.
346 * Record error message if not found.
347 * Also do shell scripts here.
350 texec(Char
*sf
, Char
**st
)
361 /* The order for the conversions is significant */
367 errno
= 0; /* don't use a previous error */
370 * If we try to execute an nfs mounted directory on the apollo, we
371 * hang forever. So until apollo fixes that..
375 if (stat(f
, &stb
) == 0 && S_ISDIR(stb
.st_mode
))
381 #ifdef ISC_POSIX_EXEC_BUG
382 __setostype(0); /* "0" is "__OS_SYSV" in <sys/user.h> */
383 #endif /* ISC_POSIX_EXEC_BUG */
385 #ifdef ISC_POSIX_EXEC_BUG
386 __setostype(1); /* "1" is "__OS_POSIX" in <sys/user.h> */
387 #endif /* ISC_POSIX_EXEC_BUG */
392 blkfree((Char
**) t
);
398 #endif /* WINNT_NATIVE */
400 * From: casper@fwi.uva.nl (Casper H.S. Dik) If we could not execute
401 * it, don't feed it to the shell if it looks like a binary!
403 if ((fd
= xopen(f
, O_RDONLY
|O_LARGEFILE
)) != -1) {
408 if ((nread
= xread(fd
, pref
, 2)) == 2) {
409 if (!isprint((unsigned char)pref
[0]) &&
410 (pref
[0] != '\n' && pref
[0] != '\t')) {
416 * We *know* what ENOEXEC means.
418 stderror(ERR_ARCH
, f
, strerror(err
));
421 else if (nread
< 0) {
427 /* need to print error incase the file is migrated */
428 stderror(ERR_SYSTEM
, f
, strerror(err
));
440 pref
[0] != '#' || pref
[1] != '!' || hashbang(fd
, &vp
) == -1) {
441 #endif /* HASHBANG */
443 * If there is an alias for shell, then put the words of the alias in
444 * front of the argument list replacing the command name. Note no
445 * interpretation of the words at this point.
447 v
= adrof1(STRshell
, &aliases
);
448 if (v
== NULL
|| v
->vec
== NULL
) {
450 vp
[0] = adrof(STRshell
) ? varval(STRshell
) : STR_SHELLPATH
;
454 # ifndef ISC /* Compatible with ISC's /bin/csh */
463 vp
= saveblk(v
->vec
);
466 #endif /* HASHBANG */
473 st
= blkspl(vp
, st
); /* Splice up the new arglst */
476 /* The order for the conversions is significant */
480 blkfree((Char
**) vp
);
484 #ifdef ISC_POSIX_EXEC_BUG
485 __setostype(0); /* "0" is "__OS_SYSV" in <sys/user.h> */
486 #endif /* ISC_POSIX_EXEC_BUG */
488 #ifdef ISC_POSIX_EXEC_BUG
489 __setostype(1); /* "1" is "__OS_POSIX" in <sys/user.h> */
490 #endif /* ISC_POSIX_EXEC_BUG */
494 blkfree((Char
**) t
);
495 /* The sky is falling, the sky is falling! */
496 stderror(ERR_SYSTEM
, f
, strerror(errno
));
500 stderror(ERR_SYSTEM
, f
, strerror(errno
));
504 case 0: /* execv fails and returns 0! */
511 exerr
= strerror(errno
);
513 expath
= Strsave(sf
);
524 int saveIN
, saveOUT
, saveDIAG
, saveSTD
;
525 int SHIN
, SHOUT
, SHDIAG
, OLDSTD
;
527 #ifndef CLOSE_ON_EXEC
530 struct sigaction sigint
, sigquit
, sigterm
;
534 execash_cleanup(void *xstate
)
536 struct execash_state
*state
;
539 sigaction(SIGINT
, &state
->sigint
, NULL
);
540 sigaction(SIGQUIT
, &state
->sigquit
, NULL
);
541 sigaction(SIGTERM
, &state
->sigterm
, NULL
);
544 #ifndef CLOSE_ON_EXEC
545 didcch
= state
->didcch
;
546 #endif /* CLOSE_ON_EXEC */
547 didfds
= state
->didfds
;
552 close_on_exec(SHIN
= dmove(state
->saveIN
, state
->SHIN
), 1);
553 close_on_exec(SHOUT
= dmove(state
->saveOUT
, state
->SHOUT
), 1);
554 close_on_exec(SHDIAG
= dmove(state
->saveDIAG
, state
->SHDIAG
), 1);
555 close_on_exec(OLDSTD
= dmove(state
->saveSTD
, state
->OLDSTD
), 1);
560 execash(Char
**t
, struct command
*kp
)
562 struct execash_state state
;
565 if (chkstop
== 0 && setintr
)
568 * Hmm, we don't really want to do that now because we might
569 * fail, but what is the choice
571 rechist(NULL
, adrof(STRsavehist
) != NULL
);
574 sigaction(SIGINT
, &parintr
, &state
.sigint
);
575 sigaction(SIGQUIT
, &parintr
, &state
.sigquit
);
576 sigaction(SIGTERM
, &parterm
, &state
.sigterm
);
578 state
.didfds
= didfds
;
579 #ifndef CLOSE_ON_EXEC
580 state
.didcch
= didcch
;
581 #endif /* CLOSE_ON_EXEC */
584 state
.SHDIAG
= SHDIAG
;
585 state
.OLDSTD
= OLDSTD
;
587 (void)close_on_exec (state
.saveIN
= dcopy(SHIN
, -1), 1);
588 (void)close_on_exec (state
.saveOUT
= dcopy(SHOUT
, -1), 1);
589 (void)close_on_exec (state
.saveDIAG
= dcopy(SHDIAG
, -1), 1);
590 (void)close_on_exec (state
.saveSTD
= dcopy(OLDSTD
, -1), 1);
592 lshift(kp
->t_dcom
, 1);
594 (void)close_on_exec (SHIN
= dcopy(0, -1), 1);
595 (void)close_on_exec (SHOUT
= dcopy(1, -1), 1);
596 (void)close_on_exec (SHDIAG
= dcopy(2, -1), 1);
597 #ifndef CLOSE_ON_EXEC
599 #endif /* CLOSE_ON_EXEC */
601 cleanup_push(&state
, execash_cleanup
);
604 * Decrement the shell level
609 #endif /* WINNT_NATIVE */
612 cleanup_until(&state
);
618 if (adrof(STRecho
)) {
619 int odidfds
= didfds
;
623 blkpr(t
), xputchar('\n');
632 dohash(Char
**vv
, struct command
*c
)
640 struct varent
*v
= adrof(STRpath
);
644 int is_windir
; /* check if it is the windows directory */
646 #endif /* WINNT_NATIVE */
651 uhashlength
= atoi(short2str(vv
[1]));
653 uhashwidth
= atoi(short2str(vv
[2]));
654 if ((uhashwidth
!= sizeof(unsigned char)) &&
655 (uhashwidth
!= sizeof(unsigned short)) &&
656 (uhashwidth
!= sizeof(unsigned long)))
659 hashdebug
= atoi(short2str(vv
[3]));
664 hashwidth
= uhashwidth
;
669 for (pv
= v
->vec
; pv
&& *pv
; pv
++, hashwidth
++)
671 if (hashwidth
<= widthof(unsigned char))
672 hashwidth
= sizeof(unsigned char);
673 else if (hashwidth
<= widthof(unsigned short))
674 hashwidth
= sizeof(unsigned short);
675 else if (hashwidth
<= widthof(unsigned int))
676 hashwidth
= sizeof(unsigned int);
678 hashwidth
= sizeof(unsigned long);
682 hashlength
= uhashlength
;
684 hashlength
= hashwidth
* (8*64);/* "average" files per dir in path */
687 xhash
= xcalloc(hashlength
* hashwidth
, 1);
688 #endif /* FASTHASH */
690 (void) getusername(NULL
); /* flush the tilde cashe */
695 for (pv
= v
->vec
; pv
&& *pv
; pv
++, i
++) {
696 if (!ABSOLUTEP(pv
[0]))
698 dirp
= opendir(short2str(*pv
));
701 cleanup_push(dirp
, opendir_cleanup
);
702 #ifdef COMMENT /* this isn't needed. opendir won't open
704 if (fstat(dirp
->dd_fd
, &stb
) < 0 || !S_ISDIR(stb
.st_mode
)) {
710 is_windir
= nt_check_if_windir(short2str(*pv
));
711 #endif /* WINNT_NATIVE */
712 while ((dp
= readdir(dirp
)) != NULL
) {
715 if (dp
->d_name
[0] == '.' &&
716 (dp
->d_name
[1] == '\0' ||
717 (dp
->d_name
[1] == '.' && dp
->d_name
[2] == '\0')))
720 nt_check_name_and_hash(is_windir
, dp
->d_name
, i
);
721 #else /* !WINNT_NATIVE*/
722 #if defined(_UWIN) || defined(__CYGWIN__)
723 /* Turn foo.{exe,com,bat} into foo since UWIN's readdir returns
724 * the file with the .exe, .com, .bat extension
727 ssize_t ext
= strlen(dp
->d_name
) - 4;
728 if ((ext
> 0) && (strcasecmp(&dp
->d_name
[ext
], ".exe") == 0 ||
729 strcasecmp(&dp
->d_name
[ext
], ".bat") == 0 ||
730 strcasecmp(&dp
->d_name
[ext
], ".com") == 0)) {
732 /* Also store the variation with extension. */
733 hashval
= hashname(str2short(dp
->d_name
));
735 #endif /* __CYGWIN__ */
736 dp
->d_name
[ext
] = '\0';
739 #endif /* _UWIN || __CYGWIN__ */
741 hashval
= hashname(str2short(dp
->d_name
));
744 xprintf(CGETS(13, 1, "hash=%-4d dir=%-2d prog=%s\n"),
745 hashname(str2short(dp
->d_name
)), i
, dp
->d_name
);
746 # else /* OLD HASH */
747 hashval
= hash(hashname(str2short(dp
->d_name
)), i
);
749 # endif /* FASTHASH */
750 /* tw_add_comm_name (dp->d_name); */
751 #endif /* WINNT_NATIVE */
759 dounhash(Char
**v
, struct command
*c
)
767 #endif /* FASTHASH */
772 hashstat(Char
**v
, struct command
*c
)
777 if (havhash
&& hashlength
&& hashwidth
)
778 xprintf(CGETS(13, 2, "%d hash buckets of %d bits each\n"),
779 hashlength
, hashwidth
*8);
781 xprintf(CGETS(13, 3, "debug mask = 0x%08x\n"), hashdebug
);
782 #endif /* FASTHASH */
785 xprintf(CGETS(13, 4, "%d hits, %d misses, %d%%\n"),
786 hits
, misses
, 100 * hits
/ (hits
+ misses
));
792 * Hash a command name.
799 for (h
= 0; *cp
; cp
++)
805 iscommand(Char
*name
)
810 int slash
= any(short2str(name
), '/');
814 if (v
== NULL
|| v
->vec
== NULL
|| v
->vec
[0] == NULL
|| slash
)
818 sav
= Strspl(STRslash
, name
); /* / command name for postpending */
819 hashval
= havhash
? hashname(name
) : 0;
822 if (!slash
&& ABSOLUTEP(pv
[0]) && havhash
) {
824 if (!bit(hashval
, i
))
827 int hashval1
= hash(hashval
, i
);
828 if (!bit(xhash
, hashval1
))
830 #endif /* FASTHASH */
832 if (pv
[0][0] == 0 || eq(pv
[0], STRdot
)) { /* don't make ./xxx */
833 if (executable(NULL
, name
, 0)) {
839 if (executable(*pv
, sav
, 0)) {
853 * Andreas Luik <luik@isaak.isa.de>
854 * I S A GmbH - Informationssysteme fuer computerintegrierte Automatisierung
858 * is the executable() routine below and changes to iscommand().
864 * executable() examines the pathname obtained by concatenating dir and name
865 * (dir may be NULL), and returns 1 either if it is executable by us, or
866 * if dir_ok is set and the pathname refers to a directory.
867 * This is a bit kludgy, but in the name of optimization...
870 executable(const Char
*dir
, const Char
*name
, int dir_ok
)
878 path
= Strspl(dir
, name
);
879 strname
= short2str(path
);
883 strname
= short2str(name
);
885 return (stat(strname
, &stbuf
) != -1 &&
886 ((dir_ok
&& S_ISDIR(stbuf
.st_mode
)) ||
887 (S_ISREG(stbuf
.st_mode
) &&
888 /* save time by not calling access() in the hopeless case */
889 (stbuf
.st_mode
& (S_IXOTH
| S_IXGRP
| S_IXUSR
)) &&
890 access(strname
, X_OK
) == 0
893 #endif /*!WINNT_NATIVE*/
895 struct tellmewhat_s0_cleanup
901 tellmewhat_s0_cleanup(void *xstate
)
903 struct tellmewhat_s0_cleanup
*state
;
906 *state
->dest
= state
->val
;
910 tellmewhat(struct wordent
*lexp
, Char
**str
)
912 struct tellmewhat_s0_cleanup s0
;
914 const struct biltins
*bptr
;
915 struct wordent
*sp
= lexp
->next
;
916 int aliased
= 0, found
;
920 if (adrof1(sp
->word
, &aliases
)) {
926 s0
.dest
= &sp
->word
; /* to get the memory freeing right... */
928 cleanup_push(&s0
, tellmewhat_s0_cleanup
);
930 /* handle quoted alias hack */
931 if ((*(sp
->word
) & (QUOTE
| TRIM
)) == QUOTE
)
934 /* do quoting, if it hasn't been done */
941 while (*s2
&& *s2
!= qc
)
942 *s1
++ = *s2
++ | QUOTE
;
948 *s1
++ = *s2
++ | QUOTE
;
955 for (bptr
= bfunc
; bptr
< &bfunc
[nbfunc
]; bptr
++) {
956 if (eq(sp
->word
, str2short(bptr
->bname
))) {
960 xprintf(CGETS(13, 5, "%S: shell built-in command.\n"),
965 *str
= Strsave(sp
->word
);
971 for (bptr
= nt_bfunc
; bptr
< &nt_bfunc
[nt_nbfunc
]; bptr
++) {
972 if (eq(sp
->word
, str2short(bptr
->bname
))) {
976 xprintf(CGETS(13, 5, "%S: shell built-in command.\n"),
981 *str
= Strsave(sp
->word
);
986 #endif /* WINNT_NATIVE*/
988 sp
->word
= cmd
= globone(sp
->word
, G_IGNORE
);
989 cleanup_push(cmd
, xfree
);
991 if ((i
= iscommand(sp
->word
)) != 0) {
994 int slash
= any(short2str(sp
->word
), '/');
997 if (v
== NULL
|| v
->vec
== NULL
|| v
->vec
[0] == NULL
|| slash
)
1003 if (pv
[0][0] == 0 || eq(pv
[0], STRdot
)) {
1005 sp
->word
= Strspl(STRdotsl
, sp
->word
);
1006 cleanup_push(sp
->word
, xfree
);
1008 cleanup_until(sp
->word
);
1014 s1
= Strspl(*pv
, STRslash
);
1015 sp
->word
= Strspl(s1
, sp
->word
);
1017 cleanup_push(sp
->word
, xfree
);
1021 *str
= Strsave(sp
->word
);
1022 cleanup_until(sp
->word
);
1030 xprintf(CGETS(13, 6, "%S: Command not found.\n"), sp
->word
);
1034 *str
= Strsave(sp
->word
);
1042 * Builtin to look at and list all places a command may be defined:
1043 * aliases, shell builtins, and the path.
1045 * Marc Horowitz <marc@mit.edu>
1046 * MIT Student Information Processing Board
1051 dowhere(Char
**v
, struct command
*c
)
1056 found
&= find_cmd(*v
, 1);
1057 /* Make status nonzero if any command is not found. */
1059 setcopy(STRstatus
, STR1
, VAR_READWRITE
);
1063 find_cmd(Char
*cmd
, int prt
)
1066 const struct biltins
*bptr
;
1069 int hashval
, i
, ex
, rval
= 0;
1071 if (prt
&& any(short2str(cmd
), '/')) {
1072 xprintf(CGETS(13, 7, "where: / in command makes no sense\n"));
1076 /* first, look for an alias */
1078 if (prt
&& adrof1(cmd
, &aliases
)) {
1079 if ((var
= adrof1(cmd
, &aliases
)) != NULL
) {
1080 xprintf(CGETS(13, 8, "%S is aliased to "), cmd
);
1081 if (var
->vec
!= NULL
)
1088 /* next, look for a shell builtin */
1090 for (bptr
= bfunc
; bptr
< &bfunc
[nbfunc
]; bptr
++) {
1091 if (eq(cmd
, str2short(bptr
->bname
))) {
1094 xprintf(CGETS(13, 9, "%S is a shell built-in\n"), cmd
);
1100 for (bptr
= nt_bfunc
; bptr
< &nt_bfunc
[nt_nbfunc
]; bptr
++) {
1101 if (eq(cmd
, str2short(bptr
->bname
))) {
1104 xprintf(CGETS(13, 9, "%S is a shell built-in\n"), cmd
);
1109 #endif /* WINNT_NATIVE*/
1111 /* last, look through the path for the command */
1113 if ((var
= adrof(STRpath
)) == NULL
)
1116 hashval
= havhash
? hashname(cmd
) : 0;
1118 sv
= Strspl(STRslash
, cmd
);
1119 cleanup_push(sv
, xfree
);
1121 for (pv
= var
->vec
, i
= 0; pv
&& *pv
; pv
++, i
++) {
1122 if (havhash
&& !eq(*pv
, STRdot
)) {
1124 if (!bit(hashval
, i
))
1127 int hashval1
= hash(hashval
, i
);
1128 if (!bit(xhash
, hashval1
))
1130 #endif /* FASTHASH */
1132 ex
= executable(*pv
, sv
, 0);
1134 if (!ex
&& (hashdebug
& 2)) {
1135 xprintf(CGETS(13, 10, "hash miss: "));
1136 ex
= 1; /* Force printing */
1138 #endif /* FASTHASH */
1142 xprintf("%S/", *pv
);
1143 xprintf("%S\n", cmd
);
1153 int hashval_extern(cp
)
1156 return havhash
?hashname(cp
):0;
1158 int bit_extern(val
,i
)
1164 void bis_extern(val
,i
)
1170 #endif /* WINNT_NATIVE */