Created
April 12, 2022 11:33
-
-
Save sideeffect42/12d89d9088fe5bd05327c5a7520b90a9 to your computer and use it in GitHub Desktop.
awk-based mktemp(1) implementation (no guarantees!)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/awk -f | |
function count_x(tmpl) { | |
match(tmpl, "X+$") | |
return RLENGTH | |
} | |
function replace_x(tmpl, tok) { | |
# ATTENTION: this function destroys the template if the tok contains more | |
# characters than the tmpl contains Xs | |
return substr(tmpl, 1, length(tmpl) - length(tok)) tok | |
} | |
function random_tok(len, _cmd, _ln) { | |
_cmd = "LC_ALL=C tr -cd 'A-Za-z0-9' </dev/urandom | dd bs=1 count=" int(len) " 2>/dev/null" | |
_cmd | getline _ln | |
close(_cmd) | |
return _ln | |
} | |
function creat_excl(path) { | |
gsub(/'/, "'\\''", path) | |
return !system("exec >/dev/null 2>&1 && umask 077 && set -C && :>'" path "'") | |
} | |
BEGIN { | |
tmpl = ARGV[1] | |
num_x = count_x(tmpl) | |
while (1) { | |
tok = random_tok(num_x) | |
if (length(tok) != num_x) continue # assert length is okay | |
fname = replace_x(tmpl, tok) | |
if (creat_excl(fname)) break | |
} | |
print fname | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment