1
0
Fork 0
mirror of https://github.com/notwa/rc synced 2024-09-19 14:54:05 -07:00
rc/sh/witch

63 lines
1.1 KiB
Text
Raw Normal View History

#!/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.
2024-07-13 02:57:14 -07:00
### the silent (`-s`) flag from Ubuntu has been added.
set -ef
2024-07-13 02:57:14 -07:00
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;;
2024-07-13 02:57:14 -07:00
(s*) silent=1;;
(*)
printf >&2 'Illegal option: -%.1s\n' "$flag"
printf 'Usage: %s\n' "$0 [-as] args"
exit 2;;
esac
done;;
2024-07-19 22:31:08 -07:00
(*) break;;
esac
done
2024-07-13 02:55:28 -07:00
[ "$#" = 0 ] && res=1 || res=0
2024-07-13 02:57:14 -07:00
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
2024-07-13 02:57:14 -07:00
puts "$prog"
err=0
fi;;
(*)
set -- $(printf %s: "$PATH")
for sub; do
[ -n "$sub" ] || sub=.
if [ -f "$sub/$prog" ] && [ -x "$sub/$prog" ]; then
2024-07-13 02:57:14 -07:00
puts "$sub/$prog"
err=0
[ "$all" = 1 ] || break
fi
done;;
esac
[ "$err" = 0 ] || res=1
done
exit "$res"