PrintPath 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. # Initially written by Jim Jagielski for the Apache configuration mechanism
  14. # (with kudos to Kernighan/Pike)
  15. #
  16. # This script falls under the Apache License.
  17. # See http://www.apache.org/docs/LICENSE
  18. ##
  19. # Some "constants"
  20. ##
  21. pathname=$PATH
  22. echo="yes"
  23. ##
  24. # Find out what OS we are running for later on
  25. ##
  26. os=`(uname) 2>/dev/null`
  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. # Now we make the adjustments required for OS/2 and everyone
  40. # else :)
  41. #
  42. # First of all, all OS/2 programs have the '.exe' extension.
  43. # Next, we adjust PATH (or what was given to us as PATH) to
  44. # be whitespace seperated directories.
  45. # Finally, we try to determine the best flag to use for
  46. # test/[] to look for an executable file. OS/2 just has '-r'
  47. # but with other OSs, we do some funny stuff to check to see
  48. # if test/[] knows about -x, which is the prefered flag.
  49. ##
  50. if [ "x$os" = "xOS/2" ]
  51. then
  52. ext=".exe"
  53. pathname=`echo -E $pathname |
  54. sed 's/^;/.;/
  55. s/;;/;.;/g
  56. s/;$/;./
  57. s/;/ /g
  58. s/\\\\/\\//g' `
  59. test_exec_flag="-r"
  60. else
  61. ext="" # No default extensions
  62. pathname=`echo $pathname |
  63. sed 's/^:/.:/
  64. s/::/:.:/g
  65. s/:$/:./
  66. s/:/ /g' `
  67. # Here is how we test to see if test/[] can handle -x
  68. testfile="pp.t.$$"
  69. cat > $testfile <<ENDTEST
  70. #!/bin/sh
  71. if [ -x / ] || [ -x /bin ] || [ -x /bin/ls ]; then
  72. exit 0
  73. fi
  74. exit 1
  75. ENDTEST
  76. if `/bin/sh $testfile 2>/dev/null`; then
  77. test_exec_flag="-x"
  78. else
  79. test_exec_flag="-r"
  80. fi
  81. rm -f $testfile
  82. fi
  83. for program in $programs
  84. do
  85. for path in $pathname
  86. do
  87. if [ $test_exec_flag $path/${program}${ext} ] && \
  88. [ ! -d $path/${program}${ext} ]; then
  89. if [ "x$echo" = "xyes" ]; then
  90. echo $path/${program}${ext}
  91. fi
  92. exit 0
  93. fi
  94. # Next try without extension (if one was used above)
  95. if [ "x$ext" != "x" ]; then
  96. if [ $test_exec_flag $path/${program} ] && \
  97. [ ! -d $path/${program} ]; then
  98. if [ "x$echo" = "xyes" ]; then
  99. echo $path/${program}
  100. fi
  101. exit 0
  102. fi
  103. fi
  104. done
  105. done
  106. exit 1