2022-09-01 17:48:14 -07:00
|
|
|
#!/usr/bin/env sh
|
2024-07-13 02:29:12 -07:00
|
|
|
# FAKE_COMPAT
|
2024-07-13 02:17:38 -07:00
|
|
|
### @ witch
|
|
|
|
### this is a personal rewrite of `which` from Debian.
|
|
|
|
### the original version didn't run on certain shells,
|
|
|
|
### and inherited inconsistent behaviors from getopts.
|
2022-09-01 17:48:14 -07:00
|
|
|
|
|
|
|
set -ef
|
|
|
|
|
2024-07-13 02:17:38 -07:00
|
|
|
all=0
|
|
|
|
for flag; do
|
|
|
|
case "$flag" in
|
|
|
|
(--) shift; break;;
|
|
|
|
(-?*)
|
|
|
|
shift
|
|
|
|
while flag="${flag#?}"; [ -n "$flag" ]; do
|
|
|
|
case "$flag" in
|
|
|
|
(a*) all=1;;
|
|
|
|
(*)
|
|
|
|
printf >&2 'Illegal option: -%.1s\n' "$flag"
|
|
|
|
printf 'Usage: %s\n' "$0 [-a] args"
|
|
|
|
exit 2;;
|
|
|
|
esac
|
|
|
|
done;;
|
|
|
|
(*) break
|
2022-09-01 17:48:14 -07:00
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
2024-07-13 02:55:28 -07:00
|
|
|
[ "$#" = 0 ] && res=1 || res=0
|
2024-07-13 02:17:38 -07:00
|
|
|
|
|
|
|
IFS=:
|
|
|
|
for prog; do
|
|
|
|
err=1
|
|
|
|
case "$prog" in
|
|
|
|
(*/*)
|
|
|
|
if [ -f "$prog" ] && [ -x "$prog" ]; then
|
|
|
|
printf %s\\n "$prog"
|
|
|
|
err=0
|
|
|
|
fi;;
|
|
|
|
(*)
|
|
|
|
set -- $(printf %s: "$PATH")
|
|
|
|
for sub; do
|
|
|
|
[ -n "$sub" ] || sub=.
|
|
|
|
if [ -f "$sub/$prog" ] && [ -x "$sub/$prog" ]; then
|
|
|
|
printf %s\\n "$sub/$prog"
|
|
|
|
err=0
|
|
|
|
[ "$all" = 1 ] || break
|
2022-09-01 17:48:14 -07:00
|
|
|
fi
|
2024-07-13 02:17:38 -07:00
|
|
|
done;;
|
2022-09-01 17:48:14 -07:00
|
|
|
esac
|
2024-07-13 02:17:38 -07:00
|
|
|
[ "$err" = 0 ] || res=1
|
2022-09-01 17:48:14 -07:00
|
|
|
done
|
|
|
|
|
2024-07-13 02:17:38 -07:00
|
|
|
exit "$res"
|