commit:     4e09cd6073e9f7906081231af7511bc74cd1d1bb
Author:     Kerin Millar <kfm <AT> plushkava <DOT> net>
AuthorDate: Mon Jul  8 05:24:32 2024 +0000
Commit:     Sam James <sam <AT> gentoo <DOT> org>
CommitDate: Mon Jul  8 05:24:32 2024 +0000
URL:        
https://gitweb.gentoo.org/proj/gentoo-functions.git/commit/?id=4e09cd60

Add the _find0() helper function

The function is a simple wrapper around GNU find(1) which presumes the
intent to use the -files0-from option to read NUL-delimited path names
from the standard input. The benefit in having it is twofold. Firstly,
the _select_by_mtime() and is_older_than() functions are thereby made a
little easier to read. Secondly, the genfun_bin_find variable is now
initialised lazily.

Signed-off-by: Kerin Millar <kfm <AT> plushkava.net>

 functions.sh    | 33 ++++++++++++++++++++++++++++-----
 functions/rc.sh |  2 +-
 2 files changed, 29 insertions(+), 6 deletions(-)

diff --git a/functions.sh b/functions.sh
index 82f59a3..dd4eef8 100644
--- a/functions.sh
+++ b/functions.sh
@@ -652,6 +652,33 @@ whenceforth()
 
 
#------------------------------------------------------------------------------#
 
+#
+# See the definitions of _select_by_mtime() and is_older_than().
+#
+_find0()
+{
+       # Store the name of the GNU find binary, which may be "gfind".
+       hash gfind 2>/dev/null && genfun_bin_find=gfind || genfun_bin_find=find
+
+       _find0()
+       {
+               local opt
+
+               case $1 in
+                       -[HL])
+                               opt=$1
+                               shift
+                               set -- "${opt}" -files0-from - "$@"
+                               ;;
+                       *)
+                               set -- -files0-from - "$@"
+               esac
+               "${genfun_bin_find}" "$@"
+       }
+
+       _find0 "$@"
+}
+
 #
 # Determines whether the terminal is a dumb one.
 #
@@ -660,7 +687,6 @@ _has_dumb_terminal()
        ! case ${TERM} in *dumb*) false ;; esac
 }
 
-#
 #
 # See the definitions of oldest() and newest().
 #
@@ -674,7 +700,7 @@ _select_by_mtime() {
        else
                cat
        fi \
-       | "${genfun_bin_find}" -files0-from - -maxdepth 0 ! -path 
"*${genfun_newline}*" -printf '%T+ %p\n' \
+       | _find0 -maxdepth 0 ! -path "*${genfun_newline}*" -printf '%T+ %p\n' \
        | sort "${sort_opt}" \
        | { IFS= read -r line && printf '%s\n' "${line#* }"; }
 }
@@ -841,9 +867,6 @@ if [ ! "${genfun_basedir+set}" ]; then
        genfun_basedir=${genfun_prefix}/lib/gentoo
 fi
 
-# Store the name of the GNU find binary. Some platforms may have it as "gfind".
-hash gfind 2>/dev/null && genfun_bin_find=gfind || genfun_bin_find=find
-
 # Assign the LF ('\n') character for later expansion. POSIX Issue 8 permits
 # $'\n' but it may take years for it to be commonly implemented.
 genfun_newline='

diff --git a/functions/rc.sh b/functions/rc.sh
index 2f2dc52..9129b32 100644
--- a/functions/rc.sh
+++ b/functions/rc.sh
@@ -213,7 +213,7 @@ is_older_than()
        fi
        shift
        { test "$#" -gt 0 && printf '%s\0' "$@"; } \
-       | "${genfun_bin_find}" -L -files0-from - ${ref:+-newermm} 
${ref:+"${ref}"} -printf '\n' -quit \
+       | _find0 -L ${ref:+-newermm} ${ref:+"${ref}"} -printf '\n' -quit \
        | read -r _
 }
 

Reply via email to