#!/usr/bin/env tsh : : tsh - " Force sh(1), csh(1), and other shells to exit w/ error! " <'' ;;; : : " @(#)$Id: 46d9b7287fe2497abcdf6f85577b68747d5ffdab $ " : : " The author of this file, J.A. Neitzel , " : " hereby grants it to the public domain. " : : : " Given a name, or list of names, locate the program file(s) in the " : " user's command search PATH which would be executed if the name(s) " : " were given to the shell as command(s) in a command line. Print " : " the resulting pathname of each name to the standard output if " : " found, or print a `name: not found' diagnostic if not. " : : " If any given name is not found, or is not executable by the user, " : " exit with a non-zero status. Otherwise, exit with a zero status. " : : " NOTE: Blanks in the value of PATH or in any given name will cause " : " unexpected results. Thus, all blanks in the value of PATH " : " are explicitly removed before which performs its search. " : " However, the same is not done for any given name. Such " : " a name will cause a diagnostic to be printed in all cases. " : : " usage: which name1 [... [name9]] " : : : " Check for correct usage, and create temporary directory. " : : >>$0'' <'' killer $0 which : >>$1'' <'' ; : >>$2'' <'' ; : >>$3'' <'' : >>$4'' <'' ; : >>$5'' <'' ; : >>$6'' <'' : >>$7'' <'' ; : >>$8'' <'' ; : >>$9'' <'' if X$1 = X if { exit } pusage $0 '"name1 [... [name9]]"' if ! { mkdir -m 0700 /tmp/which-$$ } if { exit } false ( \ echo 'tsh - \' ; \ printenv PATH | tr -d ' \t' | \ sed -e '/^$/q' \ -e 's!:! !g ; s! ! . !g ; s!^ !. ! ; s! $! .!' \ -e 's! !/$1 !g ; s!$!/$1! ; s!//*!/!g' \ ) >/tmp/which-$$/search head -4 >>/tmp/which-$$/search ; goto Ready : Next if X$1 = X if { exit } false if -f $1 -a -x $1 if { exit } echo $1 shift ; goto Next : Ready fd2 -f /tmp/which-$$/error -- tsh -$$ $1 $2 $3 $4 $5 $6 $7 $8 $9 ; \ fd2 -e cat /tmp/which-$$/status ; \ /tmp/which$0/gotslash if -s /tmp/which$0/gotslash goto GotSlash if ! { tsh /tmp/which$0/search $1 } \ fd2 -e echo $1': not found' shift ; goto Search : GotSlash if ! { if -f $1 -a -x $1 echo $1 } \ fd2 -e echo $1': not found' shift ; goto Search