diff options
-rw-r--r-- | ISSUES.markdown | 2 | ||||
-rwxr-xr-x | bin/cf | 32 | ||||
-rwxr-xr-x | bin/cfr | 34 |
3 files changed, 21 insertions, 47 deletions
diff --git a/ISSUES.markdown b/ISSUES.markdown index 8600b857..19375c83 100644 --- a/ISSUES.markdown +++ b/ISSUES.markdown @@ -18,5 +18,3 @@ Known issues * dr(1df) is probably more practical in awk * How come commands I fix with the fc builtin always seem to exit 1 even if they succeed? Did I do that or is it Bash? -* Looks like -path for find(1) isn't specified by POSIX 2003. - cf(1df) and cfr(1df) use it though. @@ -5,32 +5,20 @@ # directory if none given for dir in "${@:-.}" ; do - # Strip a trailing slash - dir=${dir%/} + # Attempt to count the files in a subshell + if count=$( + cd -- "$dir" || exit + find . ! -name . -prune -exec printf %.sx {} + | + wc -c + ) ; then - # If the path is not absolute or already pre-dotted, tack a ./ to - # the front so that find(1) doesn't choke; otherwise juse use it - # as-is - case $dir in - /*|./*) fdir=$dir ;; - *) fdir=./$dir ;; - esac + # If it worked, print the count + printf '%u\t%s\n' "$count" "$dir" + else - # Warn if a non-directory was given, flag errors, but continue - if ! [ -d "$dir" ] ; then - printf >&2 'cf: %s: not a directory\n' "$dir" + # If not, set the error flag and continue ex=1 - continue fi - - # Count the files - count=$( - find "$fdir" -path "$fdir"'/*' -prune -exec printf %.sx {} + | - wc -c - ) - - # Print the count and the dirname - printf '%u\t%s\n' "$count" "$dir" done # Exit non-zero if a non-directory was given as an argument @@ -1,36 +1,24 @@ #!/bin/sh -# Count all descendants of given directories; don't follow symlinks +# 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 - # Strip a trailing slash - dir=${dir%/} + # Attempt to count the files in a subshell + if count=$( + cd -- "$dir" || exit + find . ! -name . -exec printf %.sx {} + | + wc -c + ) ; then - # If the path is not absolute or already pre-dotted, tack a ./ to - # the front so that find(1) doesn't choke; otherwise juse use it - # as-is - case $dir in - /*|./*) fdir=$dir ;; - *) fdir=./$dir ;; - esac + # If it worked, print the count + printf '%u\t%s\n' "$count" "$dir" + else - # Warn if a non-directory was given, flag errors, but continue - if ! [ -d "$dir" ] ; then - printf >&2 'cfr: %s: Not a directory\n' "$dir" + # If not, set the error flag and continue ex=1 - continue fi - - # Count the files recursively - count=$( - find "$fdir" -path "$fdir"'/*' -exec printf %.sx {} + | - wc -c - ) - - # Print the count and the dirname - printf '%u\t%s\n' "$count" "$dir" done # Exit non-zero if a non-directory was given as an argument |