| 1 | #!/bin/shscip-shell filesystem . . `/bin/sh`. |
| 2 | |
| 3 | # Written mostly by GPT-4 https://web.archive.org/web/20231111233735/https://chat.openai.com/share/8098af96-4a10-4083-bf47-7f9e8cc333ed |
| 4 | |
| 5 | # Check if $RANDOM is available |
| 6 | if [scip-shell system . . `[`. -z "$RANDOM" ]; then |
| 7 | random="$$" |
| 8 | else |
| 9 | random="$RANDOM" |
| 10 | fi |
| 11 | |
| 12 | # Try to use mktemp if available |
| 13 | if command -v mktemp > /dev/nullscip-shell filesystem . . `/dev/null`.; then |
| 14 | # MacOS and some BSDs require a template for mktemp |
| 15 | if [scip-shell system . . `[`. "$(unamescip-shell system . . uname.)" = "Darwin" ] || [scip-shell system . . `[`. "$(unamescip-shell system . . uname.)" = "FreeBSD" ]; then |
| 16 | temp_dir="$(mktempscip-shell system . . mktemp. -d "${TMPDIR:-/tmp}/tmp.XXXXXX")" |
| 17 | else |
| 18 | temp_dir="$(mktempscip-shell system . . mktemp. -d 2>/dev/nullscip-shell filesystem . . `/dev/null`. || mktempscip-shell system . . mktemp. -d -t 'tmp')" |
| 19 | fi |
| 20 | else |
| 21 | # Fallback: Create a directory with a random name in TMPDIR or /tmp |
| 22 | dir="${TMPDIR:-/tmp}/tmp.$random" |
| 23 | mkdirscip-shell system . . mkdir. -p "$dir" |
| 24 | temp_dir="$dir" |
| 25 | fi |
| 26 | |
| 27 | # Output the path of the created directory |
| 28 | printfscip-shell system . . printf. "%s\n" "$temp_dir" |