summaryrefslogtreecommitdiff
path: root/lib-src/posix-defuns.sh
blob: db821d5e56723d8e5f8d5a0c18dc1c245a363f0f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# POSIX sh-compatible shell definitions
#
# 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 <https://www.gnu.org/licenses/>.

tab="$(printf '\t')"
cchars="$(printf '*[\001-\037\177]*')"

fail () {
    echo >&2 "${0##*/}: $*"
    exit 127
}

dir_contents () {
    ( cd "$1"; find . ! -name . ! -name "$cchars" -print -prune )
}

# 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
    )
}