home *** CD-ROM | disk | FTP | other *** search
/ PC Online 1999 April / PCO0499.ISO / filesbbs / os2 / apach134.arj / APACH134.ZIP / src / helpers / PrintPath < prev    next >
Encoding:
Text File  |  1998-12-02  |  2.0 KB  |  103 lines

  1. #!/bin/sh
  2. # Look for program[s] somewhere in $PATH.
  3. #
  4. # Options:
  5. #  -s
  6. #    Do not print out full pathname. (silent)
  7. #  -pPATHNAME
  8. #    Look in PATHNAME instead of $PATH
  9. #
  10. # Usage:
  11. #  PrintPath [-s] [-pPATHNAME] program [program ...]
  12. #
  13. # This script falls under the Apache License.
  14. # See http://www.apache.org/docs/LICENSE
  15.  
  16. ##
  17. # Some "constants"
  18. ##
  19. pathname=$PATH
  20. echo="yes"
  21.  
  22. ##
  23. # Find out what OS we are running for later on
  24. ##
  25. os=`(uname) 2>/dev/null`
  26.  
  27. ##
  28. # Parse command line
  29. ##
  30. for args in $*
  31. do
  32.     case $args in
  33.     -s  ) echo="no" ;;
  34.     -p* ) pathname="`echo $args | sed 's/^..//'`" ;;
  35.     *   ) programs="$programs $args" ;;
  36.     esac
  37. done
  38.  
  39. ##
  40. # Now we make the adjustments required for OS/2 and everyone
  41. # else :)
  42. #
  43. # First of all, all OS/2 programs have the '.exe' extension.
  44. # Next, we adjust PATH (or what was given to us as PATH) to
  45. # be whitespace seperated directories.
  46. # Finally, we try to determine the best flag to use for
  47. # test/[] to look for an executable file. OS/2 just has '-r'
  48. # but with other OSs, we do some funny stuff to check to see
  49. # if test/[] knows about -x, which is the prefered flag.
  50. ##
  51.  
  52. if [ "x$os" = "xOS/2" ]
  53. then
  54.     ext=".exe"
  55.     pathname=`echo -E $pathname |
  56.      sed 's/^;/.;/
  57.       s/;;/;.;/g
  58.       s/;$/;./
  59.       s/;/ /g
  60.       s/\\\\/\\//g' `
  61.     test_exec_flag="-r"
  62. else
  63.     ext=""    # No default extensions
  64.     pathname=`echo $pathname |
  65.      sed 's/^:/.:/
  66.       s/::/:.:/g
  67.       s/:$/:./
  68.       s/:/ /g' `
  69.     # Here is how we test to see if test/[] can handle -x
  70.     testfile="pp.t.$$"
  71.  
  72.     cat > $testfile <<ENDTEST
  73. #!/bin/sh
  74. if [ -x / ] || [ -x /bin ] || [ -x /bin/ls ]; then
  75.     exit 0
  76. fi
  77. exit 1
  78. ENDTEST
  79.  
  80.     if `/bin/sh $testfile 2>/dev/null`; then
  81.     test_exec_flag="-x"
  82.     else
  83.     test_exec_flag="-r"
  84.     fi
  85.     rm -f $testfile
  86. fi
  87.  
  88. for program in $programs
  89. do
  90.     for path in $pathname
  91.     do
  92.     if [ $test_exec_flag $path/${program}${ext} ] && \
  93.        [ ! -d $path/${program}${ext} ]; then
  94.         if [ "$echo" = "yes" ]; then
  95.         echo $path/${program}${ext}
  96.         fi
  97.         exit 0
  98.     fi
  99.     done
  100. done
  101. exit 1
  102.  
  103.