PrintPath 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. #!/bin/sh
  2. #
  3. # Licensed to the Apache Software Foundation (ASF) under one or more
  4. # contributor license agreements. See the NOTICE file distributed with
  5. # this work for additional information regarding copyright ownership.
  6. # The ASF licenses this file to You under the Apache License, Version 2.0
  7. # (the "License"); you may not use this file except in compliance with
  8. # the License. You may obtain a copy of the License at
  9. #
  10. # http://www.apache.org/licenses/LICENSE-2.0
  11. #
  12. # Unless required by applicable law or agreed to in writing, software
  13. # distributed under the License is distributed on an "AS IS" BASIS,
  14. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. # See the License for the specific language governing permissions and
  16. # limitations under the License.
  17. #
  18. #
  19. # Look for program[s] somewhere in $PATH.
  20. #
  21. # Options:
  22. # -s
  23. # Do not print out full pathname. (silent)
  24. # -pPATHNAME
  25. # Look in PATHNAME instead of $PATH
  26. #
  27. # Usage:
  28. # PrintPath [-s] [-pPATHNAME] program [program ...]
  29. #
  30. # Initially written by Jim Jagielski for the Apache configuration mechanism
  31. # (with kudos to Kernighan/Pike)
  32. ##
  33. # Some "constants"
  34. ##
  35. pathname=$PATH
  36. echo="yes"
  37. ##
  38. # Find out what OS we are running for later on
  39. ##
  40. os=`(uname) 2>/dev/null`
  41. ##
  42. # Parse command line
  43. ##
  44. for args in $*
  45. do
  46. case $args in
  47. -s ) echo="no" ;;
  48. -p* ) pathname="`echo $args | sed 's/^..//'`" ;;
  49. * ) programs="$programs $args" ;;
  50. esac
  51. done
  52. ##
  53. # Now we make the adjustments required for OS/2 and everyone
  54. # else :)
  55. #
  56. # First of all, all OS/2 programs have the '.exe' extension.
  57. # Next, we adjust PATH (or what was given to us as PATH) to
  58. # be whitespace separated directories.
  59. # Finally, we try to determine the best flag to use for
  60. # test/[] to look for an executable file. OS/2 just has '-r'
  61. # but with other OSs, we do some funny stuff to check to see
  62. # if test/[] knows about -x, which is the prefered flag.
  63. ##
  64. if [ "x$os" = "xOS/2" ]
  65. then
  66. ext=".exe"
  67. pathname=`echo -E $pathname |
  68. sed 's/^;/.;/
  69. s/;;/;.;/g
  70. s/;$/;./
  71. s/;/ /g
  72. s/\\\\/\\//g' `
  73. test_exec_flag="-r"
  74. else
  75. ext="" # No default extensions
  76. pathname=`echo $pathname |
  77. sed 's/^:/.:/
  78. s/::/:.:/g
  79. s/:$/:./
  80. s/:/ /g' `
  81. # Here is how we test to see if test/[] can handle -x
  82. testfile="pp.t.$$"
  83. cat > $testfile <<ENDTEST
  84. #!/bin/sh
  85. if [ -x / ] || [ -x /bin ] || [ -x /bin/ls ]; then
  86. exit 0
  87. fi
  88. exit 1
  89. ENDTEST
  90. if `/bin/sh $testfile 2>/dev/null`; then
  91. test_exec_flag="-x"
  92. else
  93. test_exec_flag="-r"
  94. fi
  95. rm -f $testfile
  96. fi
  97. for program in $programs
  98. do
  99. for path in $pathname
  100. do
  101. if [ $test_exec_flag $path/${program}${ext} ] && \
  102. [ ! -d $path/${program}${ext} ]; then
  103. if [ "x$echo" = "xyes" ]; then
  104. echo $path/${program}${ext}
  105. fi
  106. exit 0
  107. fi
  108. # Next try without extension (if one was used above)
  109. if [ "x$ext" != "x" ]; then
  110. if [ $test_exec_flag $path/${program} ] && \
  111. [ ! -d $path/${program} ]; then
  112. if [ "x$echo" = "xyes" ]; then
  113. echo $path/${program}
  114. fi
  115. exit 0
  116. fi
  117. fi
  118. done
  119. done
  120. exit 1