#!/bin/sh
#
# cfr - count entries in a given set of directories

# iterate over remaining non-option arguments (directories); default to current
# directory if none given
for dir in "${@:-.}"; do

  # attempt to count the files in a subshell
  if count=$(
    cd -- "${dir}" || exit
    find . ! -name . -exec printf %.sx {} + \
      | wc -c
  ); then

    # if it worked, print the count
    printf '%u\t%s\n' "${count}" "${dir}"
  else

    # if not, set the error flag and continue
    ex=1
  fi
done

# exit non-zero if a non-directory was given as an argument
exit "${ex:-0}"
