summaryrefslogtreecommitdiff
path: root/lib-src
diff options
context:
space:
mode:
authorSean Whitton <spwhitton@spwhitton.name>2023-01-21 10:36:23 -0700
committerSean Whitton <spwhitton@spwhitton.name>2023-01-27 15:16:03 -0700
commite44af5d89c4d34591f2dfb4a208756db6f2c24d9 (patch)
tree1cdc401d1a3de586e5c2cd223232b6c49adfa550 /lib-src
parent9d9d6c00d461f554fa5b988d20e69f47ffb6bdcf (diff)
downloaddotfiles-e44af5d89c4d34591f2dfb4a208756db6f2c24d9.tar.gz
hstow: reduce number of ls(1), dirname(1), rm(1) and rmdir(1) calls
Also improve comment: 'xargs readlink' can be used like 'xargs ls -ld'.
Diffstat (limited to 'lib-src')
-rw-r--r--lib-src/posix-defuns.sh42
1 files changed, 42 insertions, 0 deletions
diff --git a/lib-src/posix-defuns.sh b/lib-src/posix-defuns.sh
new file mode 100644
index 00000000..edacea49
--- /dev/null
+++ b/lib-src/posix-defuns.sh
@@ -0,0 +1,42 @@
+# POSIX sh-compatible shell functions
+#
+# Copyright (C) 2021, 2023 Sean Whitton <spwhitton@spwhitton.name>
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or (at
+# your option) any later version.
+#
+# This program is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+# General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+# Copied from src/connection.lisp in Consfigurator, which see for commentary.
+mkstemp () {
+ local template="${1:-${TMPDIR:-/tmp}/tmp.XXXXXX}" err=
+ # Use a subshell in case the parent script is using the third file
+ # descriptor and to avoid changing its umask, though chances are we were
+ # called in a subshell, e.g. "$(mkstemp)", anyway.
+ ( umask 077
+ exec 3>&1
+ if err="$(if command -v m4 >/dev/null; then
+ echo "mkstemp($template)" | m4 2>&1 1>&3
+ else
+ mktemp "$template" 2>&1 1>&3
+ fi)"; then
+ case "$err" in
+ ?*) printf >&2 "%s\n" "$err"; exit 1 ;;
+ *) exit 0 ;;
+ esac
+ else
+ case "$err" in
+ ?*) printf >&2 "%s\n" "$err" ;;
+ esac
+ exit 1
+ fi
+ )
+}