summaryrefslogtreecommitdiff
path: root/examples/scripts.noah/mktmp.bash
blob: 3ea43ad9f4105fe0464800fd7101b28be1678eb5 (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
55
56
57
58
59
60
61
62
63
64
65
66
# mktmp.bash
# Author: Noah Friedman <friedman@prep.ai.mit.edu>
# Created: 1993-02-03
# Last modified: 1993-02-03
# Public domain

# Conversion to bash v2 syntax done by Chet Ramey

# Commentary:
# Code:

#:docstring mktmp:
# Usage: mktmp [template] {createp}
#
# Generate a unique filename from TEMPLATE by appending a random number to
# the end. 
#
# If optional 2nd arg CREATEP is non-null, file will be created atomically
# before returning.  This is to avoid the race condition that in between
# the time that the temporary name is returned and the caller uses it,
# someone else creates the file. 
#:end docstring:

###;;;autoload
function mktmp ()
{
    local template="$1"
    local tmpfile="${template}${RANDOM}"
    local createp="$2"
    local noclobber_status

    case "$-" in
    *C*)	noclobber_status=set;;
    esac

    if [ "${createp:+set}" = "set" ]; then
       # Version which creates file atomically through noclobber test.   
       set -o noclobber
       (> "${tmpfile}") 2> /dev/null
       while [ $? -ne 0 ] ; do
          # Detect whether file really exists or creation lost because of
          # some other permissions problem.  If the latter, we don't want
          # to loop forever.
          if [ ! -e "${tmpfile}" ]; then
             # Trying to create file again creates stderr message.
             echo -n "mktmp: " 1>&2
             > "${tmpfile}"
             return 1
          fi
          tmpfile="${template}${RANDOM}"
          (> "${tmpfile}") 2> /dev/null
       done
       test "${noclobber_status}" != "set" && set +o noclobber
    else
       # Doesn't create file, so it introduces race condition for caller. 
       while [ -e "${tmpfile}" ]; do
          tmpfile="${template}${RANDOM}"
       done
    fi

    echo "${tmpfile}"
}

provide mktmp

# mktmp.bash ends here