#!/bin/sh
# find all commands in user's $PATH
# commands are listed as simple command names, unless -f is specified
# in which case the full path name is listed instead.
# if other args are present, they are assumend to be specific commands
# or file patterns
#
# examples:
#   commands 'e*'   finds all commands starting with e
#   commands -f col finds all 'col' commands and prints full file name(s)


mypath=`echo $PATH | sed 's/:/ /g'`

full=no
if test "$1" = "-f"
then
    full=yes
    shift
fi

if test $# = 0
then
	set '*'
fi

(
cwd=`pwd`
for pattern
do
    cat /dev/null	# must run some command to get sh to process $PATH first
    for dir in $mypath
    do
	if test ! -d $dir
	then
	    continue
	fi
	if test "$dir" = "."
	then
	    cd $cwd
	else
	    cd $dir
	fi
	for file in $pattern
	do
	    if test -d $file
	    then
		    continue
	    elif test -x $file
	    then
		    if test $full = no
		    then
			echo $file
		    else
			echo $dir/$file
		    fi
	    fi
	done
    done
done ) | sort -u | lister
