diff options
Diffstat (limited to 'lib-src/posix-defuns.sh')
-rw-r--r-- | lib-src/posix-defuns.sh | 42 |
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 + ) +} |