summaryrefslogtreecommitdiff
path: root/scripts/mkc_which
blob: 9b764a418e9d59cda9294e34f7af7dd4b8eddc9d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#!/bin/sh

############################################################
# Copyright (c) 2009-2012 by Aleksey Cheusov
#
# See LICENSE file in the distribution.
############################################################

# Portable replacement for which(1)

if test "$1" = "-x"; then
    failure=0
    shift
else
    failure=1
fi

if test $# -ne 1; then
    cat <<EOF
usage: mkc_which [-x] program
  -x -- exit status is always 0
EOF
    exit 1
fi

if echo "$1" | grep '^/' > /dev/null; then
    if test -x "$1"; then
	echo $1
	exit 0
    fi
else
    for i in `echo $PATH|tr : ' '`; do
	if test -x "$i/$1" -a -f "$i/$1"; then
	    echo $i/$1
	    exit 0
	fi
    done
fi

echo "Cannot find $1" >&2
exit $failure