-if [ $UNAME = "FreeBSD" ]; then
- PS_FLAGS="-a"; PS_PIDCOL="1"; HEAD_FLAGS="-n 1"
-elif [ $UNAME = "A/UX" ]; then
- PS_FLAGS="-ae"; PS_PIDCOL="1"; HEAD_FLAGS="-1"
-elif [ $UNAME = "GNU" ]; then
- PS_FLAGS="-ax"; PS_PIDCOL="2"; HEAD_FLAGS="-n 1"
-elif [ $UNAME = "Linux" ]; then
- PS_FLAGS="ax"; PS_PIDCOL="1"; HEAD_FLAGS="-n 1"
-elif [ $UNAME = "SunOS" ]; then
- PS_FLAGS="-af"; PS_PIDCOL=2; HEAD_FLAGS="-n 1"
-else
- PS_FLAGS="-af"; PS_PIDCOL="2"; HEAD_FLAGS="-n 1"
- ps $PS_FLAGS > /dev/null 2>&1
- if [ $? -ne 0 ]; then PS_FLAGS="a"; PS_PIDCOL="1"; fi
+
+# Use pgrep(1) whenever possible
+if [ -x /usr/bin/pgrep ]; then
+ case "$UNAME" in
+ "FreeBSD")
+ PGREP_FLAGS="-a"
+ ;;
+ *)
+ PGREP_FLAGS=""
+ esac
+ if [ -n "${LOGNAME:-}" ] || [ -n "${USER:-}" ]; then
+ # Try to narrow the search down to the current user ...
+ exec /usr/bin/pgrep $PGREP_FLAGS -n -u "${LOGNAME:-$USER}" "$1"
+ else
+ # ... but neither LOGNAME nor USER were set!
+ exec /usr/bin/pgrep $PGREP_FLAGS -n "$1"
+ fi