mirror of
https://github.com/notwa/rc
synced 2024-11-05 07:29:04 -08:00
62 lines
1.1 KiB
Bash
Executable file
62 lines
1.1 KiB
Bash
Executable file
#!/usr/bin/env sh
|
|
# FAKE_COMPAT
|
|
### @ 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.
|
|
### the silent (`-s`) flag from Ubuntu has been added.
|
|
|
|
set -ef
|
|
|
|
all=0 silent=0
|
|
for flag; do
|
|
case "$flag" in
|
|
(--) shift; break;;
|
|
(-?*)
|
|
shift
|
|
while flag="${flag#?}"; [ -n "$flag" ]; do
|
|
case "$flag" in
|
|
(a*) all=1;;
|
|
(s*) silent=1;;
|
|
(*)
|
|
printf >&2 'Illegal option: -%.1s\n' "$flag"
|
|
printf 'Usage: %s\n' "$0 [-a] args"
|
|
exit 2;;
|
|
esac
|
|
done;;
|
|
(*) break
|
|
esac
|
|
done
|
|
|
|
[ "$#" = 0 ] && res=1 || res=0
|
|
|
|
if [ "$silent" = 0 ]; then
|
|
puts() { printf %s\\n "$@"; }
|
|
else
|
|
puts() { :; }
|
|
fi
|
|
|
|
IFS=:
|
|
for prog; do
|
|
err=1
|
|
case "$prog" in
|
|
(*/*)
|
|
if [ -f "$prog" ] && [ -x "$prog" ]; then
|
|
puts "$prog"
|
|
err=0
|
|
fi;;
|
|
(*)
|
|
set -- $(printf %s: "$PATH")
|
|
for sub; do
|
|
[ -n "$sub" ] || sub=.
|
|
if [ -f "$sub/$prog" ] && [ -x "$sub/$prog" ]; then
|
|
puts "$sub/$prog"
|
|
err=0
|
|
[ "$all" = 1 ] || break
|
|
fi
|
|
done;;
|
|
esac
|
|
[ "$err" = 0 ] || res=1
|
|
done
|
|
|
|
exit "$res"
|