2 ### emacs-shell-lib - shared code for Emacs shell scripts
4 ## Copyright (C) 2022-2024 Free Software Foundation, Inc.
6 ## Author: Stefan Kangas <stefankangas@gmail.com>
8 ## This file is part of GNU Emacs.
10 ## GNU Emacs is free software: you can redistribute it and/or modify
11 ## it under the terms of the GNU General Public License as published by
12 ## the Free Software Foundation, either version 3 of the License, or
13 ## (at your option) any later version.
15 ## GNU Emacs is distributed in the hope that it will be useful,
16 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ## GNU General Public License for more details.
20 ## You should have received a copy of the GNU General Public License
21 ## along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
25 # Set an explicit umask.
28 # Treat unset variables as an error.
31 # Exit immediately on error.
34 # Avoid non-standard command output from non-C locales.
35 unset LANG LC_ALL LC_MESSAGES
37 PN
=${0##*/} # basename of script
38 PD
=${0%/*} # script directory
40 [ "$PD" = "$0" ] && PD
=.
# if PATH includes PWD
42 die
() # write error to stderr and exit
44 [ $# -gt 0 ] && echo "$PN: $@" >&2
50 emacs_tempfiles_cleanup
()
52 for file in ${emacs_tempfiles[@]}; do
53 rm -f "${file}" 2> /dev
/null
59 emacs_tempfiles_cleanup
65 local readonly file="${1-}"
69 if [ -z "$file" ]; then
75 if [ -x "$(command -v mktemp)" ]; then
76 tempfile
=$
(mktemp
"${TMPDIR-/tmp}/${prefix}.XXXXXXXXXX")
78 tempfile
="${TMPDIR-/tmp}/${prefix}.$RANDOM$$"
79 (umask 077 && touch "$tempfile")
82 [ -z "${tempfile}" ] && die
"Creating temporary file failed"
84 emacs_tempfiles
+=("${tempfile}")