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/licenses/LICENSE
18
19##
20# Some "constants"
21##
22pathname=$PATH
23echo="yes"
24
25##
26# Find out what OS we are running for later on
27##
28os=`(uname) 2>/dev/null`
29
30##
31# Parse command line
32##
33for args in $*
34do
35    case $args in
36	-s  ) echo="no" ;;
37	-p* ) pathname="`echo $args | sed 's/^..//'`" ;;
38	*   ) programs="$programs $args" ;;
39    esac
40done
41
42##
43# Now we make the adjustments required for OS/2 and everyone
44# else :)
45#
46# First of all, all OS/2 programs have the '.exe' extension.
47# Next, we adjust PATH (or what was given to us as PATH) to
48# be whitespace separated directories.
49# Finally, we try to determine the best flag to use for
50# test/[] to look for an executable file. OS/2 just has '-r'
51# but with other OSs, we do some funny stuff to check to see
52# if test/[] knows about -x, which is the preferred flag.
53##
54
55if [ "x$os" = "xOS/2" ]
56then
57    ext=".exe"
58    pathname=`echo -E $pathname |
59     sed 's/^;/.;/
60	  s/;;/;.;/g
61	  s/;$/;./
62	  s/;/ /g
63	  s/\\\\/\\//g' `
64    test_exec_flag="-r"
65else
66    ext=""	# No default extensions
67    pathname=`echo $pathname |
68     sed 's/^:/.:/
69	  s/::/:.:/g
70	  s/:$/:./
71	  s/:/ /g' `
72    # Here is how we test to see if test/[] can handle -x
73    testfile="pp.t.$$"
74
75    cat > $testfile <<ENDTEST
76#!/bin/sh
77if [ -x / ] || [ -x /bin ] || [ -x /bin/ls ]; then
78    exit 0
79fi
80exit 1
81ENDTEST
82
83    if `/bin/sh $testfile 2>/dev/null`; then
84	test_exec_flag="-x"
85    else
86	test_exec_flag="-r"
87    fi
88    rm -f $testfile
89fi
90
91for program in $programs
92do
93    for path in $pathname
94    do
95	if [ $test_exec_flag $path/${program}${ext} ] && \
96	   [ ! -d $path/${program}${ext} ]; then
97	    if [ "x$echo" = "xyes" ]; then
98		echo $path/${program}${ext}
99	    fi
100	    exit 0
101	fi
102
103# Next try without extension (if one was used above)
104	if [ "x$ext" != "x" ]; then
105            if [ $test_exec_flag $path/${program} ] && \
106               [ ! -d $path/${program} ]; then
107                if [ "x$echo" = "xyes" ]; then
108                    echo $path/${program}
109                fi
110                exit 0
111            fi
112        fi
113    done
114done
115exit 1
116
117