# vim: ft=sh:et:sw=2:noml:
# print a pseudorandom string.
rand() {
  : &
  awk -v N="${!}" 'BEGIN { srand(); printf("%08x%06x", rand()*2^31-1, N) }'
}

# create a temporary directory, file or FIFO special file.
createtemp() {
  # POSIX does not specify the mktemp utility, so here comes a hacky solution.
  while t="${TMPDIR:-${TMP:-/tmp}}/hblock.${$}.$(rand)" && [ -e "${t:?}" ]; do
    sleep 1
  done
  # shellcheck disable=SC2249
  (
    umask 077
    case "${1-}" in
      'dir') mkdir -- "${t:?}" ;;
      'file') touch -- "${t:?}" ;;
      'fifo') mkfifo -- "${t:?}" ;;
    esac
    printf '%s' "${t:?}"
  )
}
