diff options
author | Kerin Millar <kfm@plushkava.net> | 2024-07-08 06:24:32 +0100 |
---|---|---|
committer | Kerin Millar <kfm@plushkava.net> | 2024-07-08 06:24:32 +0100 |
commit | 4e09cd6073e9f7906081231af7511bc74cd1d1bb (patch) | |
tree | 04ddb59ba619eaf6e2bf70de4aa690f400ba7934 | |
parent | Render _contains_all() compatible with mawk (diff) | |
download | gentoo-functions-4e09cd6073e9f7906081231af7511bc74cd1d1bb.tar.gz gentoo-functions-4e09cd6073e9f7906081231af7511bc74cd1d1bb.tar.bz2 gentoo-functions-4e09cd6073e9f7906081231af7511bc74cd1d1bb.zip |
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@plushkava.net>
-rw-r--r-- | functions.sh | 33 | ||||
-rw-r--r-- | 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 @@ -653,6 +653,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. # _has_dumb_terminal() @@ -661,7 +688,6 @@ _has_dumb_terminal() } # -# # See the definitions of oldest() and newest(). # _select_by_mtime() { @@ -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 _ } |