1 /* $Header: /p/tcsh/cvsroot/tcsh/sh.exec.c,v 3.75 2009/06/25 21:15:37 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.75 2009/06/25 21:15:37 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) {
405 if ((nread
= xread(fd
, pref
, 2)) == 2) {
406 if (!isprint((unsigned char)pref
[0]) &&
407 (pref
[0] != '\n' && pref
[0] != '\t')) {
413 * We *know* what ENOEXEC means.
415 stderror(ERR_ARCH
, f
, strerror(err
));
418 else if (nread
< 0) {
424 /* need to print error incase the file is migrated */
425 stderror(ERR_SYSTEM
, f
, strerror(err
));
437 pref
[0] != '#' || pref
[1] != '!' || hashbang(fd
, &vp
) == -1) {
438 #endif /* HASHBANG */
440 * If there is an alias for shell, then put the words of the alias in
441 * front of the argument list replacing the command name. Note no
442 * interpretation of the words at this point.
444 v
= adrof1(STRshell
, &aliases
);
445 if (v
== NULL
|| v
->vec
== NULL
) {
447 vp
[0] = adrof(STRshell
) ? varval(STRshell
) : STR_SHELLPATH
;
451 # ifndef ISC /* Compatible with ISC's /bin/csh */
460 vp
= saveblk(v
->vec
);
463 #endif /* HASHBANG */
470 st
= blkspl(vp
, st
); /* Splice up the new arglst */
473 /* The order for the conversions is significant */
477 blkfree((Char
**) vp
);
481 #ifdef ISC_POSIX_EXEC_BUG
482 __setostype(0); /* "0" is "__OS_SYSV" in <sys/user.h> */
483 #endif /* ISC_POSIX_EXEC_BUG */
485 #ifdef ISC_POSIX_EXEC_BUG
486 __setostype(1); /* "1" is "__OS_POSIX" in <sys/user.h> */
487 #endif /* ISC_POSIX_EXEC_BUG */
491 blkfree((Char
**) t
);
492 /* The sky is falling, the sky is falling! */
493 stderror(ERR_SYSTEM
, f
, strerror(errno
));
497 stderror(ERR_SYSTEM
, f
, strerror(errno
));
501 case 0: /* execv fails and returns 0! */
508 exerr
= strerror(errno
);
510 expath
= Strsave(sf
);
521 int saveIN
, saveOUT
, saveDIAG
, saveSTD
;
522 int SHIN
, SHOUT
, SHDIAG
, OLDSTD
;
524 #ifndef CLOSE_ON_EXEC
527 struct sigaction sigint
, sigquit
, sigterm
;
531 execash_cleanup(void *xstate
)
533 struct execash_state
*state
;
536 sigaction(SIGINT
, &state
->sigint
, NULL
);
537 sigaction(SIGQUIT
, &state
->sigquit
, NULL
);
538 sigaction(SIGTERM
, &state
->sigterm
, NULL
);
541 #ifndef CLOSE_ON_EXEC
542 didcch
= state
->didcch
;
543 #endif /* CLOSE_ON_EXEC */
544 didfds
= state
->didfds
;
549 close_on_exec(SHIN
= dmove(state
->saveIN
, state
->SHIN
), 1);
550 close_on_exec(SHOUT
= dmove(state
->saveOUT
, state
->SHOUT
), 1);
551 close_on_exec(SHDIAG
= dmove(state
->saveDIAG
, state
->SHDIAG
), 1);
552 close_on_exec(OLDSTD
= dmove(state
->saveSTD
, state
->OLDSTD
), 1);
557 execash(Char
**t
, struct command
*kp
)
559 struct execash_state state
;
562 if (chkstop
== 0 && setintr
)
565 * Hmm, we don't really want to do that now because we might
566 * fail, but what is the choice
568 rechist(NULL
, adrof(STRsavehist
) != NULL
);
571 sigaction(SIGINT
, &parintr
, &state
.sigint
);
572 sigaction(SIGQUIT
, &parintr
, &state
.sigquit
);
573 sigaction(SIGTERM
, &parterm
, &state
.sigterm
);
575 state
.didfds
= didfds
;
576 #ifndef CLOSE_ON_EXEC
577 state
.didcch
= didcch
;
578 #endif /* CLOSE_ON_EXEC */
581 state
.SHDIAG
= SHDIAG
;
582 state
.OLDSTD
= OLDSTD
;
584 (void)close_on_exec (state
.saveIN
= dcopy(SHIN
, -1), 1);
585 (void)close_on_exec (state
.saveOUT
= dcopy(SHOUT
, -1), 1);
586 (void)close_on_exec (state
.saveDIAG
= dcopy(SHDIAG
, -1), 1);
587 (void)close_on_exec (state
.saveSTD
= dcopy(OLDSTD
, -1), 1);
589 lshift(kp
->t_dcom
, 1);
591 (void)close_on_exec (SHIN
= dcopy(0, -1), 1);
592 (void)close_on_exec (SHOUT
= dcopy(1, -1), 1);
593 (void)close_on_exec (SHDIAG
= dcopy(2, -1), 1);
594 #ifndef CLOSE_ON_EXEC
596 #endif /* CLOSE_ON_EXEC */
598 cleanup_push(&state
, execash_cleanup
);
601 * Decrement the shell level
606 #endif /* WINNT_NATIVE */
609 cleanup_until(&state
);
615 if (adrof(STRecho
)) {
616 int odidfds
= didfds
;
620 blkpr(t
), xputchar('\n');
629 dohash(Char
**vv
, struct command
*c
)
637 struct varent
*v
= adrof(STRpath
);
641 int is_windir
; /* check if it is the windows directory */
643 #endif /* WINNT_NATIVE */
648 uhashlength
= atoi(short2str(vv
[1]));
650 uhashwidth
= atoi(short2str(vv
[2]));
651 if ((uhashwidth
!= sizeof(unsigned char)) &&
652 (uhashwidth
!= sizeof(unsigned short)) &&
653 (uhashwidth
!= sizeof(unsigned long)))
656 hashdebug
= atoi(short2str(vv
[3]));
661 hashwidth
= uhashwidth
;
666 for (pv
= v
->vec
; pv
&& *pv
; pv
++, hashwidth
++)
668 if (hashwidth
<= widthof(unsigned char))
669 hashwidth
= sizeof(unsigned char);
670 else if (hashwidth
<= widthof(unsigned short))
671 hashwidth
= sizeof(unsigned short);
672 else if (hashwidth
<= widthof(unsigned int))
673 hashwidth
= sizeof(unsigned int);
675 hashwidth
= sizeof(unsigned long);
679 hashlength
= uhashlength
;
681 hashlength
= hashwidth
* (8*64);/* "average" files per dir in path */
684 xhash
= xcalloc(hashlength
* hashwidth
, 1);
685 #endif /* FASTHASH */
687 (void) getusername(NULL
); /* flush the tilde cashe */
692 for (pv
= v
->vec
; pv
&& *pv
; pv
++, i
++) {
693 if (!ABSOLUTEP(pv
[0]))
695 dirp
= opendir(short2str(*pv
));
698 cleanup_push(dirp
, opendir_cleanup
);
699 #ifdef COMMENT /* this isn't needed. opendir won't open
701 if (fstat(dirp
->dd_fd
, &stb
) < 0 || !S_ISDIR(stb
.st_mode
)) {
707 is_windir
= nt_check_if_windir(short2str(*pv
));
708 #endif /* WINNT_NATIVE */
709 while ((dp
= readdir(dirp
)) != NULL
) {
712 if (dp
->d_name
[0] == '.' &&
713 (dp
->d_name
[1] == '\0' ||
714 (dp
->d_name
[1] == '.' && dp
->d_name
[2] == '\0')))
717 nt_check_name_and_hash(is_windir
, dp
->d_name
, i
);
718 #else /* !WINNT_NATIVE*/
719 #if defined(_UWIN) || defined(__CYGWIN__)
720 /* Turn foo.{exe,com,bat} into foo since UWIN's readdir returns
721 * the file with the .exe, .com, .bat extension
724 ssize_t ext
= strlen(dp
->d_name
) - 4;
725 if ((ext
> 0) && (strcasecmp(&dp
->d_name
[ext
], ".exe") == 0 ||
726 strcasecmp(&dp
->d_name
[ext
], ".bat") == 0 ||
727 strcasecmp(&dp
->d_name
[ext
], ".com") == 0)) {
729 /* Also store the variation with extension. */
730 hashval
= hashname(str2short(dp
->d_name
));
732 #endif /* __CYGWIN__ */
733 dp
->d_name
[ext
] = '\0';
736 #endif /* _UWIN || __CYGWIN__ */
738 hashval
= hashname(str2short(dp
->d_name
));
741 xprintf(CGETS(13, 1, "hash=%-4d dir=%-2d prog=%s\n"),
742 hashname(str2short(dp
->d_name
)), i
, dp
->d_name
);
743 # else /* OLD HASH */
744 hashval
= hash(hashname(str2short(dp
->d_name
)), i
);
746 # endif /* FASTHASH */
747 /* tw_add_comm_name (dp->d_name); */
748 #endif /* WINNT_NATIVE */
756 dounhash(Char
**v
, struct command
*c
)
764 #endif /* FASTHASH */
769 hashstat(Char
**v
, struct command
*c
)
774 if (havhash
&& hashlength
&& hashwidth
)
775 xprintf(CGETS(13, 2, "%d hash buckets of %d bits each\n"),
776 hashlength
, hashwidth
*8);
778 xprintf(CGETS(13, 3, "debug mask = 0x%08x\n"), hashdebug
);
779 #endif /* FASTHASH */
782 xprintf(CGETS(13, 4, "%d hits, %d misses, %d%%\n"),
783 hits
, misses
, 100 * hits
/ (hits
+ misses
));
789 * Hash a command name.
796 for (h
= 0; *cp
; cp
++)
802 iscommand(Char
*name
)
807 int slash
= any(short2str(name
), '/');
811 if (v
== NULL
|| v
->vec
== NULL
|| v
->vec
[0] == NULL
|| slash
)
815 sav
= Strspl(STRslash
, name
); /* / command name for postpending */
816 hashval
= havhash
? hashname(name
) : 0;
819 if (!slash
&& ABSOLUTEP(pv
[0]) && havhash
) {
821 if (!bit(hashval
, i
))
824 int hashval1
= hash(hashval
, i
);
825 if (!bit(xhash
, hashval1
))
827 #endif /* FASTHASH */
829 if (pv
[0][0] == 0 || eq(pv
[0], STRdot
)) { /* don't make ./xxx */
830 if (executable(NULL
, name
, 0)) {
836 if (executable(*pv
, sav
, 0)) {
850 * Andreas Luik <luik@isaak.isa.de>
851 * I S A GmbH - Informationssysteme fuer computerintegrierte Automatisierung
855 * is the executable() routine below and changes to iscommand().
861 * executable() examines the pathname obtained by concatenating dir and name
862 * (dir may be NULL), and returns 1 either if it is executable by us, or
863 * if dir_ok is set and the pathname refers to a directory.
864 * This is a bit kludgy, but in the name of optimization...
867 executable(const Char
*dir
, const Char
*name
, int dir_ok
)
875 path
= Strspl(dir
, name
);
876 strname
= short2str(path
);
880 strname
= short2str(name
);
882 return (stat(strname
, &stbuf
) != -1 &&
883 ((dir_ok
&& S_ISDIR(stbuf
.st_mode
)) ||
884 (S_ISREG(stbuf
.st_mode
) &&
885 /* save time by not calling access() in the hopeless case */
886 (stbuf
.st_mode
& (S_IXOTH
| S_IXGRP
| S_IXUSR
)) &&
887 access(strname
, X_OK
) == 0
890 #endif /*!WINNT_NATIVE*/
892 struct tellmewhat_s0_cleanup
898 tellmewhat_s0_cleanup(void *xstate
)
900 struct tellmewhat_s0_cleanup
*state
;
903 *state
->dest
= state
->val
;
907 tellmewhat(struct wordent
*lexp
, Char
**str
)
909 struct tellmewhat_s0_cleanup s0
;
911 const struct biltins
*bptr
;
912 struct wordent
*sp
= lexp
->next
;
913 int aliased
= 0, found
;
917 if (adrof1(sp
->word
, &aliases
)) {
923 s0
.dest
= &sp
->word
; /* to get the memory freeing right... */
925 cleanup_push(&s0
, tellmewhat_s0_cleanup
);
927 /* handle quoted alias hack */
928 if ((*(sp
->word
) & (QUOTE
| TRIM
)) == QUOTE
)
931 /* do quoting, if it hasn't been done */
938 while (*s2
&& *s2
!= qc
)
939 *s1
++ = *s2
++ | QUOTE
;
945 *s1
++ = *s2
++ | QUOTE
;
952 for (bptr
= bfunc
; bptr
< &bfunc
[nbfunc
]; bptr
++) {
953 if (eq(sp
->word
, str2short(bptr
->bname
))) {
957 xprintf(CGETS(13, 5, "%S: shell built-in command.\n"),
962 *str
= Strsave(sp
->word
);
968 for (bptr
= nt_bfunc
; bptr
< &nt_bfunc
[nt_nbfunc
]; bptr
++) {
969 if (eq(sp
->word
, str2short(bptr
->bname
))) {
973 xprintf(CGETS(13, 5, "%S: shell built-in command.\n"),
978 *str
= Strsave(sp
->word
);
983 #endif /* WINNT_NATIVE*/
985 sp
->word
= cmd
= globone(sp
->word
, G_IGNORE
);
986 cleanup_push(cmd
, xfree
);
988 if ((i
= iscommand(sp
->word
)) != 0) {
991 int slash
= any(short2str(sp
->word
), '/');
994 if (v
== NULL
|| v
->vec
== NULL
|| v
->vec
[0] == NULL
|| slash
)
1000 if (pv
[0][0] == 0 || eq(pv
[0], STRdot
)) {
1002 sp
->word
= Strspl(STRdotsl
, sp
->word
);
1003 cleanup_push(sp
->word
, xfree
);
1005 cleanup_until(sp
->word
);
1011 s1
= Strspl(*pv
, STRslash
);
1012 sp
->word
= Strspl(s1
, sp
->word
);
1014 cleanup_push(sp
->word
, xfree
);
1018 *str
= Strsave(sp
->word
);
1019 cleanup_until(sp
->word
);
1027 xprintf(CGETS(13, 6, "%S: Command not found.\n"), sp
->word
);
1031 *str
= Strsave(sp
->word
);
1039 * Builtin to look at and list all places a command may be defined:
1040 * aliases, shell builtins, and the path.
1042 * Marc Horowitz <marc@mit.edu>
1043 * MIT Student Information Processing Board
1048 dowhere(Char
**v
, struct command
*c
)
1053 found
&= find_cmd(*v
, 1);
1054 /* Make status nonzero if any command is not found. */
1056 setcopy(STRstatus
, STR1
, VAR_READWRITE
);
1060 find_cmd(Char
*cmd
, int prt
)
1063 const struct biltins
*bptr
;
1066 int hashval
, i
, ex
, rval
= 0;
1068 if (prt
&& any(short2str(cmd
), '/')) {
1069 xprintf("%s", CGETS(13, 7, "where: / in command makes no sense\n"));
1073 /* first, look for an alias */
1075 if (prt
&& adrof1(cmd
, &aliases
)) {
1076 if ((var
= adrof1(cmd
, &aliases
)) != NULL
) {
1077 xprintf(CGETS(13, 8, "%S is aliased to "), cmd
);
1078 if (var
->vec
!= NULL
)
1085 /* next, look for a shell builtin */
1087 for (bptr
= bfunc
; bptr
< &bfunc
[nbfunc
]; bptr
++) {
1088 if (eq(cmd
, str2short(bptr
->bname
))) {
1091 xprintf(CGETS(13, 9, "%S is a shell built-in\n"), cmd
);
1097 for (bptr
= nt_bfunc
; bptr
< &nt_bfunc
[nt_nbfunc
]; bptr
++) {
1098 if (eq(cmd
, str2short(bptr
->bname
))) {
1101 xprintf(CGETS(13, 9, "%S is a shell built-in\n"), cmd
);
1106 #endif /* WINNT_NATIVE*/
1108 /* last, look through the path for the command */
1110 if ((var
= adrof(STRpath
)) == NULL
)
1113 hashval
= havhash
? hashname(cmd
) : 0;
1115 sv
= Strspl(STRslash
, cmd
);
1116 cleanup_push(sv
, xfree
);
1118 for (pv
= var
->vec
, i
= 0; pv
&& *pv
; pv
++, i
++) {
1119 if (havhash
&& !eq(*pv
, STRdot
)) {
1121 if (!bit(hashval
, i
))
1124 int hashval1
= hash(hashval
, i
);
1125 if (!bit(xhash
, hashval1
))
1127 #endif /* FASTHASH */
1129 ex
= executable(*pv
, sv
, 0);
1131 if (!ex
&& (hashdebug
& 2)) {
1132 xprintf("%s", CGETS(13, 10, "hash miss: "));
1133 ex
= 1; /* Force printing */
1135 #endif /* FASTHASH */
1139 xprintf("%S/", *pv
);
1140 xprintf("%S\n", cmd
);
1150 int hashval_extern(cp
)
1153 return havhash
?hashname(cp
):0;
1155 int bit_extern(val
,i
)
1161 void bis_extern(val
,i
)
1167 #endif /* WINNT_NATIVE */