|
| 1 | +#!/bin/sh |
| 2 | + |
| 3 | +###################################################################### |
| 4 | +# |
| 5 | +# MKTEMP - The "mktemp" Command Also Works On Just A POSIX Environment |
| 6 | +# |
| 7 | +# USAGE: mktemp [--by-myself] [options] [template [...]] |
| 8 | +# |
| 9 | +# --by-myself ..... (write as the 1st argument when use) |
| 10 | +# Not use the "built-in" mktemp command |
| 11 | +# and always do mktemp by myself but it is |
| 12 | +# inferior to the built-in in performance |
| 13 | +# -d .............. Makes a directory instead of a file |
| 14 | +# -p <DIR> ........ Defines the temporary directory instead of |
| 15 | +# $TMPDIR |
| 16 | +# -q .............. Quiet even if some error occurred |
| 17 | +# -u .............. Makes a file but remove it immediately |
| 18 | +# -t .............. Adds the temporary directory as a prefix |
| 19 | +# in front of the filepath |
| 20 | +# --suffix=<suff> . Adds the suffix behind the filename |
| 21 | +# |
| 22 | +# Written by Shell-Shoccar Japan (@shellshoccarjpn) on 2017-09-12 |
| 23 | +# |
| 24 | +# This is a public-domain software (CC0). It means that all of the |
| 25 | +# people can use this for any purposes with no restrictions at all. |
| 26 | +# By the way, We are fed up with the side effects which are brought |
| 27 | +# about by the major licenses. |
| 28 | +# |
| 29 | +###################################################################### |
| 30 | + |
| 31 | + |
| 32 | +###################################################################### |
| 33 | +# Initial Configuration |
| 34 | +###################################################################### |
| 35 | + |
| 36 | +# === Initialize shell environment =================================== |
| 37 | +set -u |
| 38 | +umask 0022 |
| 39 | +export LC_ALL=C |
| 40 | +type command >/dev/null 2>&1 && type getconf >/dev/null 2>&1 && |
| 41 | +export PATH="$(command -p getconf PATH)${PATH+:}${PATH-}" |
| 42 | +export UNIX_STD=2003 # to make HP-UX conform to POSIX |
| 43 | + |
| 44 | +# === Define the functions for printing usage and error message ====== |
| 45 | +print_usage_and_exit () { |
| 46 | + cat <<-USAGE 1>&2 |
| 47 | + Usage : ${0##*/} [--by-myself] [options] [template [...]] |
| 48 | + Arg&Opts: Almost compatible with GNU mktemp |
| 49 | + The following options are available and compatible with it |
| 50 | + -d, -p <DIR>, -q, -u, -t, --suffix=<suff> |
| 51 | + But --by-myself is the only original option, it prevent |
| 52 | + from use the built-in same name command even if available |
| 53 | + Version : 2017-09-12 00:40:36 JST |
| 54 | + (POSIX Bourne Shell/POSIX commands) |
| 55 | + USAGE |
| 56 | + exit 1 |
| 57 | +} |
| 58 | +error_exit() { |
| 59 | + ${2+:} false && echo "${0##*/}: $2" 1>&2 |
| 60 | + exit $1 |
| 61 | +} |
| 62 | + |
| 63 | +# === Exec the built-in mktemp command if OK and exists ============== |
| 64 | +by_myself=0 |
| 65 | +case "${1:-}" in |
| 66 | + '--by-myself') |
| 67 | + shift; by_myself=1 |
| 68 | + ;; |
| 69 | + *) export mydir=$(d=${0%/*}/;[ "_$d" = "_$0/" ]||cd "$d";echo "$(pwd)") |
| 70 | + path0=${PATH:-} |
| 71 | + PATH=$(printf '%s\n' "$path0" | |
| 72 | + tr ':' '\n' | |
| 73 | + awk '$0!=ENVIRON["mydir"]{print;}' | |
| 74 | + tr '\n' ':' | |
| 75 | + grep -v '^:$' | |
| 76 | + sed 's/:$//' ) |
| 77 | + CMD_builtin=$(command -v mktemp 2>/dev/null || :) |
| 78 | + case "$CMD_builtin" in '') by_myself=1;; esac |
| 79 | + PATH=$path0 |
| 80 | + unset mydir |
| 81 | + ;; |
| 82 | +esac |
| 83 | +case $by_myself in 0) exec "$CMD_builtin" ${1+"$@"}; exit 1;; esac |
| 84 | + |
| 85 | +# === Investigate the temporary directory if set ===================== |
| 86 | +Dir_tmp=$(set | grep ^TMPDIR= | sed 's/^[^=]\{1,\}=//'); |
| 87 | +case "$Dir_tmp" in |
| 88 | + '') Dir_tmp='/tmp' ;; |
| 89 | + /) Dir_tmp='/.' ;; |
| 90 | + *) Dir_tmp=${Dir_tmp%/};; |
| 91 | +esac |
| 92 | + |
| 93 | +# === Misc definitions =============================================== |
| 94 | +chrs='0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_' |
| 95 | +max_retry_when_failed=10 |
| 96 | +LFs=$(printf '\\\n_');LFs=${LFs%_} |
| 97 | + |
| 98 | + |
| 99 | +###################################################################### |
| 100 | +# Parse Arguments |
| 101 | +###################################################################### |
| 102 | + |
| 103 | +# === Get the options and the filepath =============================== |
| 104 | +# --- initialize option parameters ----------------------------------- |
| 105 | +optd=0 |
| 106 | +optu=0 |
| 107 | +optq=0 |
| 108 | +optt=0 |
| 109 | +opts='' |
| 110 | +# |
| 111 | +# --- get them ------------------------------------------------------- |
| 112 | +optmode='' |
| 113 | +while [ $# -gt 0 ]; do |
| 114 | + case $# in 0) break;; esac |
| 115 | + case "$optmode" in |
| 116 | + '') case "$1" in |
| 117 | + -[duqtp]*) s=$(printf '%s\n' "${1#-}" | |
| 118 | + awk '{d = "_"; u = "_"; q = "_"; # |
| 119 | + t = "_"; p = "_"; err = 0; # |
| 120 | + for (i=1;i<=length($0);i++) { # |
| 121 | + s = substr($0,i,1); # |
| 122 | + if (s == "d") { d = "d"; } # |
| 123 | + else if (s == "u") { u = "u"; } # |
| 124 | + else if (s == "q") { q = "q"; } # |
| 125 | + else if (s == "t") { t = "t"; } # |
| 126 | + else if (s == "p") { p = p ; } # |
| 127 | + else { err = 1 ; } # |
| 128 | + } # |
| 129 | + p = (substr($0,i-1)=="p") ? "p" : "_"; # |
| 130 | + printf("%s%s%s%s%s%s",d,u,q,t,p,err); }') |
| 131 | + case "$s" in *1*) print_usage_and_exit;; esac |
| 132 | + case "$s" in *d*) optd=1 ;; esac |
| 133 | + case "$s" in *u*) optu=1 ;; esac |
| 134 | + case "$s" in *q*) optq=1 ;; esac |
| 135 | + case "$s" in *t*) optt=1 ;; esac |
| 136 | + case "$s" in *p*) optmode='p' ;; esac |
| 137 | + shift; continue |
| 138 | + ;; |
| 139 | + --directory) optd=1 ; shift; continue;; |
| 140 | + --dry-run) optu=1 ; shift; continue;; |
| 141 | + --quiet) optq=1 ; shift; continue;; |
| 142 | + --tmpdir) shift; continue;; |
| 143 | + --tmpdir=*) optmode='p' |
| 144 | + s=${1#--tmpdir=} ;; |
| 145 | + --suffix=*) optmode='s' |
| 146 | + s=${1#--suffix=} ;; |
| 147 | + -*) print_usage_and_exit ;; |
| 148 | + esac ;; |
| 149 | + *) s=$1 ;; |
| 150 | + esac |
| 151 | + case "$optmode" in |
| 152 | + p) [ -d "$s" ] || error_exit 1 'Invalid path by -p,--tmpdir option' |
| 153 | + Dir_tmp=${s%/}; [ -n "$Dir_tmp" ] || Dir_tmp='/.' |
| 154 | + optmode=''; shift; continue ;; |
| 155 | + s) { printf '%s' "$s" | grep -q '/'; } && { |
| 156 | + error_exit 1 'Invalid suffix option' |
| 157 | + } |
| 158 | + opts=$s |
| 159 | + optmode=''; shift; continue ;; |
| 160 | + esac |
| 161 | + break |
| 162 | +done |
| 163 | + |
| 164 | + |
| 165 | +###################################################################### |
| 166 | +# Prepare for the Main Routine |
| 167 | +###################################################################### |
| 168 | + |
| 169 | +# ===== FUNC: random string generator ================================ |
| 170 | +# arg : $1=length |
| 171 | +# ret : 0 |
| 172 | +# stdout: generated random string |
| 173 | +random_string () { |
| 174 | + # calculate the number of words which required |
| 175 | + nw=$(echo "${1}*l(${#chrs})/11.09+1" | # 11.09=ln(65536) |
| 176 | + bc -l | |
| 177 | + sed 's/\..*$//' ) |
| 178 | + |
| 179 | + # make a random hexadecimal digit |
| 180 | + if [ -c /dev/urandom ]; then |
| 181 | + hstr=$(dd if=/dev/urandom bs=2 count=$nw 2>/dev/null | |
| 182 | + od -A n -t x2 -v | |
| 183 | + tr 'abcdef ' 'ABCDEF\n' | |
| 184 | + tr -Cd 0123456789ABCDEF ) |
| 185 | + else |
| 186 | + hstr=$( (ps -Ao pid,etime,pcpu,vsz; date) | |
| 187 | + od -t d4 -A n -v | |
| 188 | + sed 's/[^0-9]\{1,\}/'"$LFs"'/g' | |
| 189 | + grep '[0-9]' | |
| 190 | + tail -n 42 | |
| 191 | + sed 's/.*\(.\{8\}\)$/\1/g' | |
| 192 | + awk 'BEGIN{a=-2147483648;} # |
| 193 | + {a+=$1; } # |
| 194 | + END { # |
| 195 | + srand(a); # |
| 196 | + for(i=0;i<'$nw';i++){ # |
| 197 | + printf("%02X",int(rand()*65536)); # |
| 198 | + } # |
| 199 | + }' ) |
| 200 | + fi |
| 201 | + |
| 202 | + # make a random string from the hexadecimal digit |
| 203 | + echo "obase=${#chrs};ibase=16;$hstr" | |
| 204 | + bc | |
| 205 | + tr -d '\\\n' | |
| 206 | + tr ' ' '\n' | |
| 207 | + awk 'BEGIN {for(i=1;i<'$1';i++){print 0;}} # |
| 208 | + /[0-9]/{print; }' | |
| 209 | + awk 'BEGIN {ORS=""; # |
| 210 | + s="'"$chrs"'"; # |
| 211 | + for(i=0;i<length(s);i++){ # |
| 212 | + c[i]=substr(s,i+1,1); # |
| 213 | + } } # |
| 214 | + /[0-9]/{print c[$0*1]; }' | |
| 215 | + tail -c $1 |
| 216 | +} |
| 217 | + |
| 218 | + |
| 219 | +###################################################################### |
| 220 | +# Main Routine |
| 221 | +###################################################################### |
| 222 | + |
| 223 | +case $# in 0) optt=1; set -- 'tmp.XXXXXXXXXX';; esac |
| 224 | + |
| 225 | +err=0 |
| 226 | +for arg in "$@"; do |
| 227 | + |
| 228 | + # --- Make dir string for printing --------------------------------- |
| 229 | + Dir_trg=${arg%/*} |
| 230 | + case "$Dir_trg" in |
| 231 | + "$arg") case $optt in |
| 232 | + 0) Dir_trg='' |
| 233 | + ;; |
| 234 | + *) Dir_trg="$Dir_tmp/" |
| 235 | + case "$Dir_trg" in '/./') Dir_trg=/;; esac |
| 236 | + ;; |
| 237 | + esac |
| 238 | + ;; |
| 239 | + *) Dir_trg="$Dir_trg/" |
| 240 | + ;; |
| 241 | + esac |
| 242 | + |
| 243 | + n=$max_retry_when_failed |
| 244 | + while [ $n -ge 0 ]; do |
| 245 | + |
| 246 | + # --- Making the file path --------------------------------------- |
| 247 | + # |
| 248 | + # replace the end of "XXX..." string to a random one |
| 249 | + Path_target=$arg |
| 250 | + s=$(printf '%s\n' "$Path_target" | sed 's/X*$//') |
| 251 | + i=$((${#Path_target}-${#s})) |
| 252 | + case $i in |
| 253 | + 0) : ;; |
| 254 | + *) Path_target="${s}$(random_string $i)";; |
| 255 | + esac |
| 256 | + # |
| 257 | + # add the suffix |
| 258 | + Path_target="${Path_target}${opts}" |
| 259 | + # |
| 260 | + # add the prefix if the option -t is enabled |
| 261 | + case $optt in 1) Path_target="${Dir_tmp}/${Path_target}";; esac |
| 262 | + # |
| 263 | + # normalize |
| 264 | + Path_target=$(printf '%s\n' "$Path_target" | |
| 265 | + sed 's!//!/!g' | |
| 266 | + sed 's!/./!/!g' ) |
| 267 | + # |
| 268 | + # for security |
| 269 | + case "$Path_target" in |
| 270 | + -|/*|./*|../*) : ;; |
| 271 | + *) Path_target="./$Path_target";; |
| 272 | + esac |
| 273 | + |
| 274 | + # --- Making the file -------------------------------------------- |
| 275 | + case $optd in |
| 276 | + 0) (set -C; umask 177; : > "$Path_target") 2>/dev/null || { |
| 277 | + [ -f "$Path_target" ] && { n=$((n-1)); continue; } |
| 278 | + n=-1; break; |
| 279 | + } |
| 280 | + ;; |
| 281 | + 1) umask 077; mkdir "$Path_target" 2>/dev/null || { |
| 282 | + [ -d "$Path_target" ] && { n=$((n-1)); continue; } |
| 283 | + n=-1; break; |
| 284 | + } |
| 285 | + ;; |
| 286 | + esac |
| 287 | + |
| 288 | + break |
| 289 | + done |
| 290 | + |
| 291 | + # --- Print error message when failed to make a file --------------- |
| 292 | + case "${optq}${n}" in |
| 293 | + '0-1') printf '%s\n' "${0##*/}: failed on $Dir_trg${Path_target##*/}" 1>&2 |
| 294 | + err=1 |
| 295 | + continue |
| 296 | + ;; |
| 297 | + '1-1') err=1 |
| 298 | + continue |
| 299 | + ;; |
| 300 | + esac |
| 301 | + |
| 302 | + # --- Print the path of the file ----------------------------------- |
| 303 | + printf '%s%s\n' "$Dir_trg" "${Path_target##*/}" |
| 304 | + |
| 305 | + # --- Remove the file if -u,--dry-run option is set ---------------- |
| 306 | + case $optu in 1) rm -rf "$Path_target";; esac |
| 307 | + |
| 308 | +done |
| 309 | + |
| 310 | + |
| 311 | +###################################################################### |
| 312 | +# Finish |
| 313 | +###################################################################### |
| 314 | + |
| 315 | +exit $err |
0 commit comments