1 /* $Header: /src/pub/tcsh/sh.exec.c,v 3.58 2003/03/12 19:14:51 christos Exp $ */ 2 /* 3 * sh.exec.c: Search, find, and execute a command! 4 */ 5 /*- 6 * Copyright (c) 1980, 1991 The Regents of the University of California. 7 * All rights reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 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. 20 * 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 31 * SUCH DAMAGE. 32 */ 33 #include "sh.h" 34 35 RCSID("$Id: sh.exec.c,v 3.58 2003/03/12 19:14:51 christos Exp $") 36 37 #include "tc.h" 38 #include "tw.h" 39 #ifdef WINNT_NATIVE 40 #include <nt.const.h> 41 #endif /*WINNT_NATIVE*/ 42 43 /* 44 * C shell 45 */ 46 47 #ifndef OLDHASH 48 # define FASTHASH /* Fast hashing is the default */ 49 #endif /* OLDHASH */ 50 51 /* 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. 56 */ 57 58 /* 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. 64 */ 65 static char *exerr; /* Execution error message */ 66 static Char *expath; /* Path for exerr */ 67 68 /* 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. 73 */ 74 #define BITS_PER_BYTE 8 75 76 #ifdef FASTHASH 77 /* 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. 85 */ 86 # define HSHMUL 241 87 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; 92 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 (1L << (i & (widthof(t) - 1)))) 97 # define tbis(f, i, t) (((t *) xhash)[(f)] |= \ 98 (1L << (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) 107 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) : \ 111 lbit(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) : \ 115 lbis(f,i)))))) 116 #else /* OLDHASH */ 117 /* 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 124 * changed. 125 */ 126 # define HSHSIZ 8192 /* 1k bytes */ 127 # define HSHMASK (HSHSIZ - 1) 128 # define HSHMUL 243 129 static char xhash[HSHSIZ / BITS_PER_BYTE]; 130 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 */ 134 135 #endif /* FASTHASH */ 136 137 #ifdef VFORK 138 static int hits, misses; 139 #endif /* VFORK */ 140 141 /* Dummy search path for just absolute search when no path */ 142 static Char *justabs[] = {STRNULL, 0}; 143 144 static void pexerr __P((void)); 145 static void texec __P((Char *, Char **)); 146 int hashname __P((Char *)); 147 static int iscommand __P((Char *)); 148 149 void 150 doexec(t, do_glob) 151 register struct command *t; 152 bool do_glob; 153 { 154 Char *dp, **pv, **av, *sav; 155 struct varent *v; 156 bool slash; 157 int hashval, i; 158 Char *blk[2]; 159 160 /* 161 * Glob the command name. We will search $path even if this does something, 162 * as in sh but not in csh. One special case: if there is no PATH, then we 163 * execute only commands which start with '/'. 164 */ 165 blk[0] = t->t_dcom[0]; 166 blk[1] = 0; 167 gflag = 0; 168 if (do_glob) 169 tglob(blk); 170 if (gflag) { 171 pv = globall(blk); 172 if (pv == 0) { 173 setname(short2str(blk[0])); 174 stderror(ERR_NAME | ERR_NOMATCH); 175 } 176 gargv = 0; 177 } 178 else 179 pv = saveblk(blk); 180 181 trim(pv); 182 183 exerr = 0; 184 expath = Strsave(pv[0]); 185 #ifdef VFORK 186 Vexpath = expath; 187 #endif /* VFORK */ 188 189 v = adrof(STRpath); 190 if (v == 0 && expath[0] != '/' && expath[0] != '.') { 191 blkfree(pv); 192 pexerr(); 193 } 194 slash = any(short2str(expath), '/'); 195 196 /* 197 * Glob the argument list, if necessary. Otherwise trim off the quote bits. 198 */ 199 gflag = 0; 200 av = &t->t_dcom[1]; 201 if (do_glob) 202 tglob(av); 203 if (gflag) { 204 av = globall(av); 205 if (av == 0) { 206 blkfree(pv); 207 setname(short2str(expath)); 208 stderror(ERR_NAME | ERR_NOMATCH); 209 } 210 gargv = 0; 211 } 212 else 213 av = saveblk(av); 214 215 blkfree(t->t_dcom); 216 t->t_dcom = blkspl(pv, av); 217 xfree((ptr_t) pv); 218 xfree((ptr_t) av); 219 av = t->t_dcom; 220 trim(av); 221 222 if (*av == NULL || **av == '\0') 223 pexerr(); 224 225 xechoit(av); /* Echo command if -x */ 226 #ifdef CLOSE_ON_EXEC 227 /* 228 * Since all internal file descriptors are set to close on exec, we don't 229 * need to close them explicitly here. Just reorient ourselves for error 230 * messages. 231 */ 232 SHIN = 0; 233 SHOUT = 1; 234 SHDIAG = 2; 235 OLDSTD = 0; 236 isoutatty = isatty(SHOUT); 237 isdiagatty = isatty(SHDIAG); 238 #else 239 closech(); /* Close random fd's */ 240 #endif 241 /* 242 * We must do this AFTER any possible forking (like `foo` in glob) so that 243 * this shell can still do subprocesses. 244 */ 245 #ifdef BSDSIGS 246 (void) sigsetmask((sigmask_t) 0); 247 #else /* BSDSIGS */ 248 (void) sigrelse(SIGINT); 249 (void) sigrelse(SIGCHLD); 250 #endif /* BSDSIGS */ 251 252 /* 253 * If no path, no words in path, or a / in the filename then restrict the 254 * command search. 255 */ 256 if (v == NULL || v->vec == NULL || v->vec[0] == NULL || slash) 257 pv = justabs; 258 else 259 pv = v->vec; 260 sav = Strspl(STRslash, *av);/* / command name for postpending */ 261 #ifdef VFORK 262 Vsav = sav; 263 #endif /* VFORK */ 264 hashval = havhash ? hashname(*av) : 0; 265 266 i = 0; 267 #ifdef VFORK 268 hits++; 269 #endif /* VFORK */ 270 do { 271 /* 272 * Try to save time by looking at the hash table for where this command 273 * could be. If we are doing delayed hashing, then we put the names in 274 * one at a time, as the user enters them. This is kinda like Korn 275 * Shell's "tracked aliases". 276 */ 277 if (!slash && ABSOLUTEP(pv[0]) && havhash) { 278 #ifdef FASTHASH 279 if (!bit(hashval, i)) 280 goto cont; 281 #else /* OLDHASH */ 282 int hashval1 = hash(hashval, i); 283 if (!bit(xhash, hashval1)) 284 goto cont; 285 #endif /* FASTHASH */ 286 } 287 if (pv[0][0] == 0 || eq(pv[0], STRdot)) /* don't make ./xxx */ 288 { 289 290 #ifdef COHERENT 291 if (t->t_dflg & F_AMPERSAND) { 292 # ifdef JOBDEBUG 293 xprintf("set SIGINT to SIG_IGN\n"); 294 xprintf("set SIGQUIT to SIG_DFL\n"); 295 # endif /* JOBDEBUG */ 296 (void) signal(SIGINT,SIG_IGN); /* may not be necessary */ 297 (void) signal(SIGQUIT,SIG_DFL); 298 } 299 300 if (gointr && eq(gointr, STRminus)) { 301 # ifdef JOBDEBUG 302 xprintf("set SIGINT to SIG_IGN\n"); 303 xprintf("set SIGQUIT to SIG_IGN\n"); 304 # endif /* JOBDEBUG */ 305 (void) signal(SIGINT,SIG_IGN); /* may not be necessary */ 306 (void) signal(SIGQUIT,SIG_IGN); 307 } 308 #endif /* COHERENT */ 309 310 texec(*av, av); 311 } 312 else { 313 dp = Strspl(*pv, sav); 314 #ifdef VFORK 315 Vdp = dp; 316 #endif /* VFORK */ 317 318 #ifdef COHERENT 319 if ((t->t_dflg & F_AMPERSAND)) { 320 # ifdef JOBDEBUG 321 xprintf("set SIGINT to SIG_IGN\n"); 322 # endif /* JOBDEBUG */ 323 /* 324 * this is necessary on Coherent or all background 325 * jobs are killed by CTRL-C 326 * (there must be a better fix for this) 327 */ 328 (void) signal(SIGINT,SIG_IGN); 329 } 330 if (gointr && eq(gointr,STRminus)) { 331 # ifdef JOBDEBUG 332 xprintf("set SIGINT to SIG_IGN\n"); 333 xprintf("set SIGQUIT to SIG_IGN\n"); 334 # endif /* JOBDEBUG */ 335 (void) signal(SIGINT,SIG_IGN); /* may not be necessary */ 336 (void) signal(SIGQUIT,SIG_IGN); 337 } 338 #endif /* COHERENT */ 339 340 texec(dp, av); 341 #ifdef VFORK 342 Vdp = 0; 343 #endif /* VFORK */ 344 xfree((ptr_t) dp); 345 } 346 #ifdef VFORK 347 misses++; 348 #endif /* VFORK */ 349 cont: 350 pv++; 351 i++; 352 } while (*pv); 353 #ifdef VFORK 354 hits--; 355 Vsav = 0; 356 #endif /* VFORK */ 357 xfree((ptr_t) sav); 358 pexerr(); 359 } 360 361 static void 362 pexerr() 363 { 364 /* Couldn't find the damn thing */ 365 if (expath) { 366 setname(short2str(expath)); 367 #ifdef VFORK 368 Vexpath = 0; 369 #endif /* VFORK */ 370 xfree((ptr_t) expath); 371 expath = 0; 372 } 373 else 374 setname(""); 375 if (exerr) 376 stderror(ERR_NAME | ERR_STRING, exerr); 377 stderror(ERR_NAME | ERR_COMMAND); 378 } 379 380 /* 381 * Execute command f, arg list t. 382 * Record error message if not found. 383 * Also do shell scripts here. 384 */ 385 static void 386 texec(sf, st) 387 Char *sf; 388 register Char **st; 389 { 390 register char **t; 391 register char *f; 392 register struct varent *v; 393 Char **vp; 394 Char *lastsh[2]; 395 char pref[2]; 396 int fd; 397 Char *st0, **ost; 398 399 /* The order for the conversions is significant */ 400 t = short2blk(st); 401 f = short2str(sf); 402 #ifdef VFORK 403 Vt = t; 404 #endif /* VFORK */ 405 errno = 0; /* don't use a previous error */ 406 #ifdef apollo 407 /* 408 * If we try to execute an nfs mounted directory on the apollo, we 409 * hang forever. So until apollo fixes that.. 410 */ 411 { 412 struct stat stb; 413 if (stat(f, &stb) == 0 && S_ISDIR(stb.st_mode)) 414 errno = EISDIR; 415 } 416 if (errno == 0) 417 #endif /* apollo */ 418 { 419 #ifdef ISC_POSIX_EXEC_BUG 420 __setostype(0); /* "0" is "__OS_SYSV" in <sys/user.h> */ 421 #endif /* ISC_POSIX_EXEC_BUG */ 422 (void) execv(f, t); 423 #ifdef ISC_POSIX_EXEC_BUG 424 __setostype(1); /* "1" is "__OS_POSIX" in <sys/user.h> */ 425 #endif /* ISC_POSIX_EXEC_BUG */ 426 } 427 #ifdef VFORK 428 Vt = 0; 429 #endif /* VFORK */ 430 blkfree((Char **) t); 431 switch (errno) { 432 433 case ENOEXEC: 434 #ifdef WINNT_NATIVE 435 nt_feed_to_cmd(f,t); 436 #endif /* WINNT_NATIVE */ 437 /* 438 * From: casper@fwi.uva.nl (Casper H.S. Dik) If we could not execute 439 * it, don't feed it to the shell if it looks like a binary! 440 */ 441 if ((fd = open(f, O_RDONLY|O_LARGEFILE)) != -1) { 442 int nread; 443 #ifdef O_TEXT 444 setmode(fd, O_TEXT); 445 #endif 446 if ((nread = read(fd, (char *) pref, 2)) == 2) { 447 if (!Isprint(pref[0]) && (pref[0] != '\n' && pref[0] != '\t')) { 448 (void) close(fd); 449 /* 450 * We *know* what ENOEXEC means. 451 */ 452 stderror(ERR_ARCH, f, strerror(errno)); 453 } 454 } 455 else if (nread < 0 && errno != EINTR) { 456 #ifdef convex 457 /* need to print error incase the file is migrated */ 458 stderror(ERR_SYSTEM, f, strerror(errno)); 459 #endif 460 } 461 #ifdef _PATH_BSHELL 462 else { 463 pref[0] = '#'; 464 pref[1] = '\0'; 465 } 466 #endif 467 } 468 #ifdef HASHBANG 469 if (fd == -1 || 470 pref[0] != '#' || pref[1] != '!' || hashbang(fd, &vp) == -1) { 471 #endif /* HASHBANG */ 472 /* 473 * If there is an alias for shell, then put the words of the alias in 474 * front of the argument list replacing the command name. Note no 475 * interpretation of the words at this point. 476 */ 477 v = adrof1(STRshell, &aliases); 478 if (v == NULL || v->vec == NULL) { 479 vp = lastsh; 480 vp[0] = adrof(STRshell) ? varval(STRshell) : STR_SHELLPATH; 481 vp[1] = NULL; 482 #ifdef _PATH_BSHELL 483 if (fd != -1 484 # ifndef ISC /* Compatible with ISC's /bin/csh */ 485 && pref[0] != '#' 486 # endif /* ISC */ 487 ) 488 vp[0] = STR_BSHELL; 489 #endif 490 vp = saveblk(vp); 491 } 492 else 493 vp = saveblk(v->vec); 494 #ifdef HASHBANG 495 } 496 #endif /* HASHBANG */ 497 if (fd != -1) 498 (void) close(fd); 499 500 st0 = st[0]; 501 st[0] = sf; 502 ost = st; 503 st = blkspl(vp, st); /* Splice up the new arglst */ 504 ost[0] = st0; 505 sf = *st; 506 /* The order for the conversions is significant */ 507 t = short2blk(st); 508 f = short2str(sf); 509 xfree((ptr_t) st); 510 blkfree((Char **) vp); 511 #ifdef VFORK 512 Vt = t; 513 #endif /* VFORK */ 514 #ifdef ISC_POSIX_EXEC_BUG 515 __setostype(0); /* "0" is "__OS_SYSV" in <sys/user.h> */ 516 #endif /* ISC_POSIX_EXEC_BUG */ 517 (void) execv(f, t); 518 #ifdef ISC_POSIX_EXEC_BUG 519 __setostype(1); /* "1" is "__OS_POSIX" in <sys/user.h> */ 520 #endif /* ISC_POSIX_EXEC_BUG */ 521 #ifdef VFORK 522 Vt = 0; 523 #endif /* VFORK */ 524 blkfree((Char **) t); 525 /* The sky is falling, the sky is falling! */ 526 stderror(ERR_SYSTEM, f, strerror(errno)); 527 break; 528 529 case ENOMEM: 530 stderror(ERR_SYSTEM, f, strerror(errno)); 531 break; 532 533 #ifdef _IBMR2 534 case 0: /* execv fails and returns 0! */ 535 #endif /* _IBMR2 */ 536 case ENOENT: 537 break; 538 539 default: 540 if (exerr == 0) { 541 exerr = strerror(errno); 542 if (expath) 543 xfree((ptr_t) expath); 544 expath = Strsave(sf); 545 #ifdef VFORK 546 Vexpath = expath; 547 #endif /* VFORK */ 548 } 549 break; 550 } 551 } 552 553 /*ARGSUSED*/ 554 void 555 execash(t, kp) 556 Char **t; 557 register struct command *kp; 558 { 559 int saveIN, saveOUT, saveDIAG, saveSTD; 560 int oSHIN; 561 int oSHOUT; 562 int oSHDIAG; 563 int oOLDSTD; 564 jmp_buf_t osetexit; 565 int my_reenter; 566 int odidfds; 567 #ifndef CLOSE_ON_EXEC 568 int odidcch; 569 #endif /* CLOSE_ON_EXEC */ 570 signalfun_t osigint, osigquit, osigterm; 571 572 USE(t); 573 if (chkstop == 0 && setintr) 574 panystop(0); 575 /* 576 * Hmm, we don't really want to do that now because we might 577 * fail, but what is the choice 578 */ 579 rechist(NULL, adrof(STRsavehist) != NULL); 580 581 582 osigint = signal(SIGINT, parintr); 583 osigquit = signal(SIGQUIT, parintr); 584 osigterm = signal(SIGTERM, parterm); 585 586 odidfds = didfds; 587 #ifndef CLOSE_ON_EXEC 588 odidcch = didcch; 589 #endif /* CLOSE_ON_EXEC */ 590 oSHIN = SHIN; 591 oSHOUT = SHOUT; 592 oSHDIAG = SHDIAG; 593 oOLDSTD = OLDSTD; 594 595 saveIN = dcopy(SHIN, -1); 596 saveOUT = dcopy(SHOUT, -1); 597 saveDIAG = dcopy(SHDIAG, -1); 598 saveSTD = dcopy(OLDSTD, -1); 599 600 lshift(kp->t_dcom, 1); 601 602 getexit(osetexit); 603 604 /* PWP: setjmp/longjmp bugfix for optimizing compilers */ 605 #ifdef cray 606 my_reenter = 1; /* assume non-zero return val */ 607 if (setexit() == 0) { 608 my_reenter = 0; /* Oh well, we were wrong */ 609 #else /* !cray */ 610 if ((my_reenter = setexit()) == 0) { 611 #endif /* cray */ 612 SHIN = dcopy(0, -1); 613 SHOUT = dcopy(1, -1); 614 SHDIAG = dcopy(2, -1); 615 #ifndef CLOSE_ON_EXEC 616 didcch = 0; 617 #endif /* CLOSE_ON_EXEC */ 618 didfds = 0; 619 /* 620 * Decrement the shell level 621 */ 622 shlvl(-1); 623 #ifdef WINNT_NATIVE 624 __nt_really_exec=1; 625 #endif /* WINNT_NATIVE */ 626 doexec(kp, 1); 627 } 628 629 (void) sigset(SIGINT, osigint); 630 (void) sigset(SIGQUIT, osigquit); 631 (void) sigset(SIGTERM, osigterm); 632 633 doneinp = 0; 634 #ifndef CLOSE_ON_EXEC 635 didcch = odidcch; 636 #endif /* CLOSE_ON_EXEC */ 637 didfds = odidfds; 638 (void) close(SHIN); 639 (void) close(SHOUT); 640 (void) close(SHDIAG); 641 (void) close(OLDSTD); 642 SHIN = dmove(saveIN, oSHIN); 643 SHOUT = dmove(saveOUT, oSHOUT); 644 SHDIAG = dmove(saveDIAG, oSHDIAG); 645 OLDSTD = dmove(saveSTD, oOLDSTD); 646 647 resexit(osetexit); 648 if (my_reenter) 649 stderror(ERR_SILENT); 650 } 651 652 void 653 xechoit(t) 654 Char **t; 655 { 656 if (adrof(STRecho)) { 657 int odidfds = didfds; 658 flush(); 659 haderr = 1; 660 didfds = 0; 661 blkpr(t), xputchar('\n'); 662 flush(); 663 didfds = odidfds; 664 haderr = 0; 665 } 666 } 667 668 /*ARGSUSED*/ 669 void 670 dohash(vv, c) 671 Char **vv; 672 struct command *c; 673 { 674 #ifdef COMMENT 675 struct stat stb; 676 #endif 677 DIR *dirp; 678 register struct dirent *dp; 679 int i = 0; 680 struct varent *v = adrof(STRpath); 681 Char **pv; 682 int hashval; 683 #ifdef WINNT_NATIVE 684 int is_windir; /* check if it is the windows directory */ 685 USE(hashval); 686 #endif /* WINNT_NATIVE */ 687 688 USE(c); 689 #ifdef FASTHASH 690 if (vv && vv[1]) { 691 uhashlength = atoi(short2str(vv[1])); 692 if (vv[2]) { 693 uhashwidth = atoi(short2str(vv[2])); 694 if ((uhashwidth != sizeof(unsigned char)) && 695 (uhashwidth != sizeof(unsigned short)) && 696 (uhashwidth != sizeof(unsigned long))) 697 uhashwidth = 0; 698 if (vv[3]) 699 hashdebug = atoi(short2str(vv[3])); 700 } 701 } 702 703 if (uhashwidth) 704 hashwidth = uhashwidth; 705 else { 706 hashwidth = 0; 707 if (v == NULL) 708 return; 709 for (pv = v->vec; pv && *pv; pv++, hashwidth++) 710 continue; 711 if (hashwidth <= widthof(unsigned char)) 712 hashwidth = sizeof(unsigned char); 713 else if (hashwidth <= widthof(unsigned short)) 714 hashwidth = sizeof(unsigned short); 715 else if (hashwidth <= widthof(unsigned int)) 716 hashwidth = sizeof(unsigned int); 717 else 718 hashwidth = sizeof(unsigned long); 719 } 720 721 if (uhashlength) 722 hashlength = uhashlength; 723 else 724 hashlength = hashwidth * (8*64);/* "average" files per dir in path */ 725 726 if (xhash) 727 xfree((ptr_t) xhash); 728 xhash = (unsigned long *) xcalloc((size_t) (hashlength * hashwidth), 729 (size_t) 1); 730 #endif /* FASTHASH */ 731 732 (void) getusername(NULL); /* flush the tilde cashe */ 733 tw_cmd_free(); 734 havhash = 1; 735 if (v == NULL) 736 return; 737 for (pv = v->vec; pv && *pv; pv++, i++) { 738 if (!ABSOLUTEP(pv[0])) 739 continue; 740 dirp = opendir(short2str(*pv)); 741 if (dirp == NULL) 742 continue; 743 #ifdef COMMENT /* this isn't needed. opendir won't open 744 * non-dirs */ 745 if (fstat(dirp->dd_fd, &stb) < 0 || !S_ISDIR(stb.st_mode)) { 746 (void) closedir(dirp); 747 continue; 748 } 749 #endif 750 #ifdef WINNT_NATIVE 751 is_windir = nt_check_if_windir(short2str(*pv)); 752 #endif /* WINNT_NATIVE */ 753 while ((dp = readdir(dirp)) != NULL) { 754 if (dp->d_ino == 0) 755 continue; 756 if (dp->d_name[0] == '.' && 757 (dp->d_name[1] == '\0' || 758 (dp->d_name[1] == '.' && dp->d_name[2] == '\0'))) 759 continue; 760 #ifdef WINNT_NATIVE 761 nt_check_name_and_hash(is_windir, dp->d_name, i); 762 #else /* !WINNT_NATIVE*/ 763 #if defined(_UWIN) || defined(__CYGWIN__) 764 /* Turn foo.{exe,com,bat} into foo since UWIN's readdir returns 765 * the file with the .exe, .com, .bat extension 766 */ 767 { 768 size_t ext = strlen(dp->d_name) - 4; 769 if ((ext > 0) && (strcasecmp(&dp->d_name[ext], ".exe") == 0 || 770 strcasecmp(&dp->d_name[ext], ".bat") == 0 || 771 strcasecmp(&dp->d_name[ext], ".com") == 0)) 772 { 773 dp->d_name[ext] = '\0'; 774 #if defined(__CYGWIN__) 775 strlwr(dp->d_name); 776 #endif /* __CYGWIN__ */ 777 } 778 } 779 #endif /* _UWIN || __CYGWIN__ */ 780 # ifdef FASTHASH 781 hashval = hashname(str2short(dp->d_name)); 782 bis(hashval, i); 783 if (hashdebug & 1) 784 xprintf(CGETS(13, 1, "hash=%-4d dir=%-2d prog=%s\n"), 785 hashname(str2short(dp->d_name)), i, dp->d_name); 786 # else /* OLD HASH */ 787 hashval = hash(hashname(str2short(dp->d_name)), i); 788 bis(xhash, hashval); 789 # endif /* FASTHASH */ 790 /* tw_add_comm_name (dp->d_name); */ 791 #endif /* WINNT_NATIVE */ 792 } 793 (void) closedir(dirp); 794 } 795 } 796 797 /*ARGSUSED*/ 798 void 799 dounhash(v, c) 800 Char **v; 801 struct command *c; 802 { 803 USE(c); 804 USE(v); 805 havhash = 0; 806 #ifdef FASTHASH 807 if (xhash) { 808 xfree((ptr_t) xhash); 809 xhash = NULL; 810 } 811 #endif /* FASTHASH */ 812 } 813 814 /*ARGSUSED*/ 815 void 816 hashstat(v, c) 817 Char **v; 818 struct command *c; 819 { 820 USE(c); 821 USE(v); 822 #ifdef FASTHASH 823 if (havhash && hashlength && hashwidth) 824 xprintf(CGETS(13, 2, "%d hash buckets of %d bits each\n"), 825 hashlength, hashwidth*8); 826 if (hashdebug) 827 xprintf(CGETS(13, 3, "debug mask = 0x%08x\n"), hashdebug); 828 #endif /* FASTHASH */ 829 #ifdef VFORK 830 if (hits + misses) 831 xprintf(CGETS(13, 4, "%d hits, %d misses, %d%%\n"), 832 hits, misses, 100 * hits / (hits + misses)); 833 #endif 834 } 835 836 837 /* 838 * Hash a command name. 839 */ 840 int 841 hashname(cp) 842 register Char *cp; 843 { 844 register unsigned long h; 845 846 for (h = 0; *cp; cp++) 847 h = hash(h, *cp); 848 return ((int) h); 849 } 850 851 static int 852 iscommand(name) 853 Char *name; 854 { 855 register Char **pv; 856 register Char *sav; 857 register struct varent *v; 858 register bool slash = any(short2str(name), '/'); 859 register int hashval, i; 860 861 v = adrof(STRpath); 862 if (v == NULL || v->vec == NULL || v->vec[0] == NULL || slash) 863 pv = justabs; 864 else 865 pv = v->vec; 866 sav = Strspl(STRslash, name); /* / command name for postpending */ 867 hashval = havhash ? hashname(name) : 0; 868 i = 0; 869 do { 870 if (!slash && ABSOLUTEP(pv[0]) && havhash) { 871 #ifdef FASTHASH 872 if (!bit(hashval, i)) 873 goto cont; 874 #else /* OLDHASH */ 875 int hashval1 = hash(hashval, i); 876 if (!bit(xhash, hashval1)) 877 goto cont; 878 #endif /* FASTHASH */ 879 } 880 if (pv[0][0] == 0 || eq(pv[0], STRdot)) { /* don't make ./xxx */ 881 if (executable(NULL, name, 0)) { 882 xfree((ptr_t) sav); 883 return i + 1; 884 } 885 } 886 else { 887 if (executable(*pv, sav, 0)) { 888 xfree((ptr_t) sav); 889 return i + 1; 890 } 891 } 892 cont: 893 pv++; 894 i++; 895 } while (*pv); 896 xfree((ptr_t) sav); 897 return 0; 898 } 899 900 /* Also by: 901 * Andreas Luik <luik@isaak.isa.de> 902 * I S A GmbH - Informationssysteme fuer computerintegrierte Automatisierung 903 * Azenberstr. 35 904 * D-7000 Stuttgart 1 905 * West-Germany 906 * is the executable() routine below and changes to iscommand(). 907 * Thanks again!! 908 */ 909 910 #ifndef WINNT_NATIVE 911 /* 912 * executable() examines the pathname obtained by concatenating dir and name 913 * (dir may be NULL), and returns 1 either if it is executable by us, or 914 * if dir_ok is set and the pathname refers to a directory. 915 * This is a bit kludgy, but in the name of optimization... 916 */ 917 int 918 executable(dir, name, dir_ok) 919 Char *dir, *name; 920 bool dir_ok; 921 { 922 struct stat stbuf; 923 Char path[MAXPATHLEN + 1]; 924 char *strname; 925 (void) memset(path, 0, sizeof(path)); 926 927 if (dir && *dir) { 928 copyn(path, dir, MAXPATHLEN); 929 catn(path, name, MAXPATHLEN); 930 strname = short2str(path); 931 } 932 else 933 strname = short2str(name); 934 935 return (stat(strname, &stbuf) != -1 && 936 ((dir_ok && S_ISDIR(stbuf.st_mode)) || 937 (S_ISREG(stbuf.st_mode) && 938 /* save time by not calling access() in the hopeless case */ 939 (stbuf.st_mode & (S_IXOTH | S_IXGRP | S_IXUSR)) && 940 access(strname, X_OK) == 0 941 ))); 942 } 943 #endif /*!WINNT_NATIVE*/ 944 945 int 946 tellmewhat(lexp, str) 947 struct wordent *lexp; 948 Char *str; 949 { 950 register int i; 951 register struct biltins *bptr; 952 register struct wordent *sp = lexp->next; 953 bool aliased = 0, found; 954 Char *s0, *s1, *s2, *cmd; 955 Char qc; 956 957 if (adrof1(sp->word, &aliases)) { 958 alias(lexp); 959 sp = lexp->next; 960 aliased = 1; 961 } 962 963 s0 = sp->word; /* to get the memory freeing right... */ 964 965 /* handle quoted alias hack */ 966 if ((*(sp->word) & (QUOTE | TRIM)) == QUOTE) 967 (sp->word)++; 968 969 /* do quoting, if it hasn't been done */ 970 s1 = s2 = sp->word; 971 while (*s2) 972 switch (*s2) { 973 case '\'': 974 case '"': 975 qc = *s2++; 976 while (*s2 && *s2 != qc) 977 *s1++ = *s2++ | QUOTE; 978 if (*s2) 979 s2++; 980 break; 981 case '\\': 982 if (*++s2) 983 *s1++ = *s2++ | QUOTE; 984 break; 985 default: 986 *s1++ = *s2++; 987 } 988 *s1 = '\0'; 989 990 for (bptr = bfunc; bptr < &bfunc[nbfunc]; bptr++) { 991 if (eq(sp->word, str2short(bptr->bname))) { 992 if (str == NULL) { 993 if (aliased) 994 prlex(lexp); 995 xprintf(CGETS(13, 5, "%S: shell built-in command.\n"), 996 sp->word); 997 flush(); 998 } 999 else 1000 (void) Strcpy(str, sp->word); 1001 sp->word = s0; /* we save and then restore this */ 1002 return TRUE; 1003 } 1004 } 1005 #ifdef WINNT_NATIVE 1006 for (bptr = nt_bfunc; bptr < &nt_bfunc[nt_nbfunc]; bptr++) { 1007 if (eq(sp->word, str2short(bptr->bname))) { 1008 if (str == NULL) { 1009 if (aliased) 1010 prlex(lexp); 1011 xprintf(CGETS(13, 5, "%S: shell built-in command.\n"), 1012 sp->word); 1013 flush(); 1014 } 1015 else 1016 (void) Strcpy(str, sp->word); 1017 sp->word = s0; /* we save and then restore this */ 1018 return TRUE; 1019 } 1020 } 1021 #endif /* WINNT_NATIVE*/ 1022 1023 sp->word = cmd = globone(sp->word, G_IGNORE); 1024 1025 if ((i = iscommand(sp->word)) != 0) { 1026 register Char **pv; 1027 register struct varent *v; 1028 bool slash = any(short2str(sp->word), '/'); 1029 1030 v = adrof(STRpath); 1031 if (v == NULL || v->vec == NULL || v->vec[0] == NULL || slash) 1032 pv = justabs; 1033 else 1034 pv = v->vec; 1035 1036 while (--i) 1037 pv++; 1038 if (pv[0][0] == 0 || eq(pv[0], STRdot)) { 1039 if (!slash) { 1040 sp->word = Strspl(STRdotsl, sp->word); 1041 prlex(lexp); 1042 xfree((ptr_t) sp->word); 1043 } 1044 else 1045 prlex(lexp); 1046 } 1047 else { 1048 s1 = Strspl(*pv, STRslash); 1049 sp->word = Strspl(s1, sp->word); 1050 xfree((ptr_t) s1); 1051 if (str == NULL) 1052 prlex(lexp); 1053 else 1054 (void) Strcpy(str, sp->word); 1055 xfree((ptr_t) sp->word); 1056 } 1057 found = 1; 1058 } 1059 else { 1060 if (str == NULL) { 1061 if (aliased) 1062 prlex(lexp); 1063 xprintf(CGETS(13, 6, "%S: Command not found.\n"), sp->word); 1064 flush(); 1065 } 1066 else 1067 (void) Strcpy(str, sp->word); 1068 found = 0; 1069 } 1070 sp->word = s0; /* we save and then restore this */ 1071 xfree((ptr_t) cmd); 1072 return found; 1073 } 1074 1075 /* 1076 * Builtin to look at and list all places a command may be defined: 1077 * aliases, shell builtins, and the path. 1078 * 1079 * Marc Horowitz <marc@mit.edu> 1080 * MIT Student Information Processing Board 1081 */ 1082 1083 /*ARGSUSED*/ 1084 void 1085 dowhere(v, c) 1086 register Char **v; 1087 struct command *c; 1088 { 1089 int found = 1; 1090 USE(c); 1091 for (v++; *v; v++) 1092 found &= find_cmd(*v, 1); 1093 /* Make status nonzero if any command is not found. */ 1094 if (!found) 1095 set(STRstatus, Strsave(STR1), VAR_READWRITE); 1096 } 1097 1098 int 1099 find_cmd(cmd, prt) 1100 Char *cmd; 1101 int prt; 1102 { 1103 struct varent *var; 1104 struct biltins *bptr; 1105 Char **pv; 1106 Char *sv; 1107 int hashval, i, ex, rval = 0; 1108 1109 if (prt && any(short2str(cmd), '/')) { 1110 xprintf(CGETS(13, 7, "where: / in command makes no sense\n")); 1111 return rval; 1112 } 1113 1114 /* first, look for an alias */ 1115 1116 if (prt && adrof1(cmd, &aliases)) { 1117 if ((var = adrof1(cmd, &aliases)) != NULL) { 1118 xprintf(CGETS(13, 8, "%S is aliased to "), cmd); 1119 if (var->vec != NULL) 1120 blkpr(var->vec); 1121 xputchar('\n'); 1122 rval = 1; 1123 } 1124 } 1125 1126 /* next, look for a shell builtin */ 1127 1128 for (bptr = bfunc; bptr < &bfunc[nbfunc]; bptr++) { 1129 if (eq(cmd, str2short(bptr->bname))) { 1130 rval = 1; 1131 if (prt) 1132 xprintf(CGETS(13, 9, "%S is a shell built-in\n"), cmd); 1133 else 1134 return rval; 1135 } 1136 } 1137 #ifdef WINNT_NATIVE 1138 for (bptr = nt_bfunc; bptr < &nt_bfunc[nt_nbfunc]; bptr++) { 1139 if (eq(cmd, str2short(bptr->bname))) { 1140 rval = 1; 1141 if (prt) 1142 xprintf(CGETS(13, 9, "%S is a shell built-in\n"), cmd); 1143 else 1144 return rval; 1145 } 1146 } 1147 #endif /* WINNT_NATIVE*/ 1148 1149 /* last, look through the path for the command */ 1150 1151 if ((var = adrof(STRpath)) == NULL) 1152 return rval; 1153 1154 hashval = havhash ? hashname(cmd) : 0; 1155 1156 sv = Strspl(STRslash, cmd); 1157 1158 for (pv = var->vec, i = 0; pv && *pv; pv++, i++) { 1159 if (havhash && !eq(*pv, STRdot)) { 1160 #ifdef FASTHASH 1161 if (!bit(hashval, i)) 1162 continue; 1163 #else /* OLDHASH */ 1164 int hashval1 = hash(hashval, i); 1165 if (!bit(xhash, hashval1)) 1166 continue; 1167 #endif /* FASTHASH */ 1168 } 1169 ex = executable(*pv, sv, 0); 1170 #ifdef FASTHASH 1171 if (!ex && (hashdebug & 2)) { 1172 xprintf(CGETS(13, 10, "hash miss: ")); 1173 ex = 1; /* Force printing */ 1174 } 1175 #endif /* FASTHASH */ 1176 if (ex) { 1177 rval = 1; 1178 if (prt) { 1179 xprintf("%S/", *pv); 1180 xprintf("%S\n", cmd); 1181 } 1182 else 1183 return rval; 1184 } 1185 } 1186 xfree((ptr_t) sv); 1187 return rval; 1188 } 1189 #ifdef WINNT_NATIVE 1190 int hashval_extern(cp) 1191 Char *cp; 1192 { 1193 return havhash?hashname(cp):0; 1194 } 1195 int bit_extern(val,i) 1196 int val; 1197 int i; 1198 { 1199 return bit(val,i); 1200 } 1201 void bis_extern(val,i) 1202 int val; 1203 int i; 1204 { 1205 bis(val,i); 1206 } 1207 #endif /* WINNT_NATIVE */ 1208 1209