| 1 | #!/bin/shscip-shell filesystem . . `/bin/sh`. |
| 2 | # Create a temporary directory, much like mktemp -d does. |
| 3 | |
| 4 | # Copyright (C) 2007-2026 Free Software Foundation, Inc. |
| 5 | |
| 6 | # This program is free software: you can redistribute it and/or modify |
| 7 | # it under the terms of the GNU General Public License as published by |
| 8 | # the Free Software Foundation, either version 3 of the License, or |
| 9 | # (at your option) any later version. |
| 10 | |
| 11 | # This program is distributed in the hope that it will be useful, |
| 12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | # GNU General Public License for more details. |
| 15 | |
| 16 | # You should have received a copy of the GNU General Public License |
| 17 | # along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 18 | |
| 19 | # Written by Jim Meyering. |
| 20 | |
| 21 | # Usage: mktempd /tmp phoey.XXXXXXXXXX |
| 22 | |
| 23 | # First, try to use the mktemp program. |
| 24 | # Failing that, we'll roll our own mktemp-like function: |
| 25 | # - try to get random bytes from /dev/urandom |
| 26 | # - failing that, generate output from a combination of quickly-varying |
| 27 | # sources and gzip. Ignore non-varying gzip header, and extract |
| 28 | # "random" bits from there. |
| 29 | # - given those bits, map to file-name bytes using tr, and try to create |
| 30 | # the desired directory. |
| 31 | # - make only $MAX_TRIES attempts |
| 32 | |
| 33 | ME=`basenamescip-shell system . . basename. "$0"` |
| 34 | diescip-shell shell parted 3.7-1 die().() { echoscip-shell system . . echo. >&2 "$ME: $@"; exit 1; } |
| 35 | |
| 36 | MAX_TRIES=4 |
| 37 | |
| 38 | rand_bytesscip-shell shell parted 3.7-1 rand_bytes().() |
| 39 | { |
| 40 | n=$1 |
| 41 | |
| 42 | chars=abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 |
| 43 | |
| 44 | dev_rand=/dev/urandomscip-shell filesystem . . `/dev/urandom`. |
| 45 | if testscip-shell system . . test. -r "$dev_rand"; then |
| 46 | # Note: 256-length($chars) == 194; 3 copies of $chars is 186 + 8 = 194. |
| 47 | ddscip-shell system . . dd. ibs=$n count=1 if="$dev_rand" 2>/dev/nullscip-shell filesystem . . `/dev/null`. \ |
| 48 | | trscip-shell system . . tr. -c $chars 01234567$chars$chars$chars |
| 49 | return |
| 50 | fi |
| 51 | |
| 52 | cmds='date; date +%N; free; who -a; w; ps auxww; ps -ef' |
| 53 | data=` (eval "$cmds") 2>&1 | gzipscip-shell system . . gzip. ` |
| 54 | |
| 55 | n_plus_50=`exprscip-shell system . . expr. $n + 50` |
| 56 | |
| 57 | # Ensure that $data has length at least 50+$n |
| 58 | while :; do |
| 59 | len=`echoscip-shell system . . echo. "$data"|wcscip-shell system . . wc. -c` |
| 60 | testscip-shell system . . test. $n_plus_50 -le $len && break; |
| 61 | data=` (echoscip-shell system . . echo. "$data"; eval "$cmds") 2>&1 | gzipscip-shell system . . gzip. ` |
| 62 | done |
| 63 | |
| 64 | echoscip-shell system . . echo. "$data" \ |
| 65 | | ddscip-shell system . . dd. bs=1 skip=50 count=$n 2>/dev/nullscip-shell filesystem . . `/dev/null`. \ |
| 66 | | trscip-shell system . . tr. -c $chars 01234567$chars$chars$chars |
| 67 | } |
| 68 | |
| 69 | mktempdscip-shell shell parted 3.7-1 mktempd().() |
| 70 | { |
| 71 | case $# in |
| 72 | 2);; |
| 73 | *) diescip-shell shell parted 3.7-1 die(). "Usage: $ME DIR TEMPLATE";; |
| 74 | esac |
| 75 | |
| 76 | destdir=$1 |
| 77 | template=$2 |
| 78 | |
| 79 | # Disallow any trailing slash on specified destdir: |
| 80 | # it would subvert the post-mktemp "case"-based destdir test. |
| 81 | case $destdir in |
| 82 | /) ;; |
| 83 | */) diescip-shell shell parted 3.7-1 die(). "invalid destination dir: remove trailing slash(es)";; |
| 84 | esac |
| 85 | |
| 86 | case $template in |
| 87 | -*) diescip-shell shell parted 3.7-1 die(). "invalid template: $template (cannot start with '-')";; |
| 88 | *XXXX) ;; |
| 89 | *) diescip-shell shell parted 3.7-1 die(). "invalid template: $template (must have a suffix of at least 4 X's)";; |
| 90 | esac |
| 91 | |
| 92 | fail=0 |
| 93 | |
| 94 | # First, try GNU mktemp, where -t has no option-argument. |
| 95 | # Put -t last, as GNU mktemp allows, so that the incompatible NetBSD mktemp |
| 96 | # (where -t has an option-argument) fails instead of creating a junk dir. |
| 97 | d=`envscip-shell system . . env. -u TMPDIR mktemp -d -p "$destdir" "$template" -t 2>/dev/nullscip-shell filesystem . . `/dev/null`.` \ |
| 98 | || fail=1 |
| 99 | |
| 100 | # The resulting name must be in the specified directory. |
| 101 | case $d in "$destdir"*);; *) fail=1;; esac |
| 102 | |
| 103 | # It must have created the directory. |
| 104 | testscip-shell system . . test. -d "$d" || fail=1 |
| 105 | |
| 106 | # It must have 0700 permissions. Handle sticky "S" bits. |
| 107 | perms=`lsscip-shell system . . ls. -dgo "$d" 2>/dev/nullscip-shell filesystem . . `/dev/null`.|trscip-shell system . . tr. S -` || fail=1 |
| 108 | case $perms in drwx------*) ;; *) fail=1;; esac |
| 109 | |
| 110 | testscip-shell system . . test. $fail = 0 && { |
| 111 | echoscip-shell system . . echo. "$d" |
| 112 | return |
| 113 | } |
| 114 | |
| 115 | # If we reach this point, we'll have to create a directory manually. |
| 116 | |
| 117 | # Get a copy of the template without its suffix of X's. |
| 118 | base_template=`echoscip-shell system . . echo. "$template"|sedscip-shell system . . sed. 's/XX*$//'` |
| 119 | |
| 120 | # Calculate how many X's we've just removed. |
| 121 | template_length=`echoscip-shell system . . echo. "$template" | wcscip-shell system . . wc. -c` |
| 122 | nx=`echoscip-shell system . . echo. "$base_template" | wcscip-shell system . . wc. -c` |
| 123 | nx=`exprscip-shell system . . expr. $template_length - $nx` |
| 124 | |
| 125 | err= |
| 126 | i=1 |
| 127 | while :; do |
| 128 | X=`rand_bytesscip-shell shell parted 3.7-1 rand_bytes(). $nx` |
| 129 | candidate_dir="$destdir/$base_template$X" |
| 130 | err=`mkdirscip-shell system . . mkdir. -m 0700 "$candidate_dir" 2>&1` \ |
| 131 | && { echoscip-shell system . . echo. "$candidate_dir"; return; } |
| 132 | testscip-shell system . . test. $MAX_TRIES -le $i && break; |
| 133 | i=`exprscip-shell system . . expr. $i + 1` |
| 134 | done |
| 135 | diescip-shell shell parted 3.7-1 die(). "$err" |
| 136 | } |
| 137 | |
| 138 | mktempdscip-shell shell parted 3.7-1 mktempd(). "$@" |