fork/fish-shell
git clone https://git.julia.blue/fork/fish-shell
069d396eb
master
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. 14set -l opts 15while set -q argv[ 1 ] 16switch $argv [ 1 ] 17case -d 18set opts$opts d 19case -t 20set opts$opts t 21case -u 22set opts$opts u 23case -- 24set -e argv[ 1 ] 25break 26case '-*' 27echo "mktemp: unknown flag $argv [ 1 ] " >&2 28_mktemp_help >&2 29exit 2 30case '*' 31break 32end 33set -e argv[ 1 ] 34end 35 36set -l template 37if set -q argv[ 1 ] 38set template$argv [ 1 ] 39else 40set template'tmp.XXXXXXXXXX' 41set opts$opts t 42end 43 44if set -q argv[ 2 ] 45echo 'mktemp: too many templates' >&2 46_mktemp_help >&2 47exit 1 48end 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. 54begin 55# Look for at least three Xs that are not the end of the template 56if string match -rq --'XXX[^X].*$' " $template " 57echo "mktemp: X's may only occur at the end of the template ' $template '" >&2 58_mktemp_help >&2 59exit 1 60end 61 62# Look for too few X incidences at the end of the template 63if ! string match -rq --'XXX$' " $template " 64echo "mktemp: too few trailing X's in template ' $template '" >&2 65_mktemp_usage >&2 66exit 1 67end 68end 69 70set -l args 71if contains u$opts 72set args$args -u 73end 74if contains d$opts 75set args$args -d 76end 77if contains t$opts 78switch $template 79case '/*' 80echo "mktemp: invalid template ' $template ' with -t, template must not be absolute" >&2 81_mktemp_help >&2 82exit 1 83end 84 85if set -q TMPDIR[ 1 ] 86set template$TMPDIR /$template 87else 88set template /tmp/$template 89end 90end 91set args$args $template 92 93realpath ( command mktemp$args ) 94end 95 96function _mktemp_help 97echo 'usage: mktemp [-d] [-t] [template]' 98echo 'note: mktemp is a test function, see tests/test_functions/mktemp.fish' 99end