fork/fish-shell

git clone https://git.julia.blue/fork/fish-shell

Mahmoud Al-QudsiMerge branch 'ctest'069d396eb

master
2.9 KiB99 linesraw
1# GNU and BSD mktemp differ in their handling of arguments
2# Let's expose a simplified common interface
3function mktemp
4    # usage: mktemp [-d] [-t] [template]
5    #
6    # If the -d flag is given, create a directory.
7    #
8    # If the -t flag is given, treat the template as a filename relative
9    # to the temporary directory. The template may contain slashes but only
10    # the final path component is created by mktemp. The template must not be
11    # absolute
12    #
13    # If no template is given, assume tmp.XXXXXXXXXX and -t.
14    set -l opts
15    while set -q argv[1]
16        switch $argv[1]
17            case -d
18                set opts $opts d
19            case -t
20                set opts $opts t
21            case -u
22                set opts $opts u
23            case --
24                set -e argv[1]
25                break
26            case '-*'
27                echo "mktemp: unknown flag $argv[1]" >&2
28                _mktemp_help >&2
29                exit 2
30            case '*'
31                break
32        end
33        set -e argv[1]
34    end
35
36    set -l template
37    if set -q argv[1]
38        set template $argv[1]
39    else
40        set template 'tmp.XXXXXXXXXX'
41        set opts $opts t
42    end
43
44    if set -q argv[2]
45        echo 'mktemp: too many templates' >&2
46        _mktemp_help >&2
47        exit 1
48    end
49
50    # GNU mktemp treats the final occurrence of a sequence of X's as the template token.
51    # BSD mktemp only treats X's as the template token if they suffix the string.
52    # So let's outlaw them anywhere besides the end.
53    # Similarly GNU mktemp requires at least 3 X's, BSD mktemp requires none. Let's require 3.
54    begin
55        # Look for at least three Xs that are not the end of the template
56        if string match -rq -- 'XXX[^X].*$' "$template"
57            echo "mktemp: X's may only occur at the end of the template '$template'" >&2
58            _mktemp_help >&2
59            exit 1
60        end
61
62        # Look for too few X incidences at the end of the template
63        if ! string match -rq -- 'XXX$' "$template"
64            echo "mktemp: too few trailing X's in template '$template'" >&2
65            _mktemp_usage >&2
66            exit 1
67        end
68    end
69
70    set -l args
71    if contains u $opts
72        set args $args -u
73    end
74    if contains d $opts
75        set args $args -d
76    end
77    if contains t $opts
78        switch $template
79            case '/*'
80                echo "mktemp: invalid template '$template' with -t, template must not be absolute" >&2
81                _mktemp_help >&2
82                exit 1
83        end
84
85        if set -q TMPDIR[1]
86            set template $TMPDIR/$template
87        else
88            set template /tmp/$template
89        end
90    end
91    set args $args $template
92
93    realpath (command mktemp $args)
94end
95
96function _mktemp_help
97    echo 'usage: mktemp [-d] [-t] [template]'
98    echo 'note: mktemp is a test function, see tests/test_functions/mktemp.fish'
99end