diff options
author | Elliott Hughes <enh@google.com> | 2018-12-04 10:58:13 -0800 |
---|---|---|
committer | Rob Landley <rob@landley.net> | 2018-12-04 13:50:21 -0600 |
commit | a6ec1d989bba98cf1e4cd3b3e566e69cfc4e4939 (patch) | |
tree | 144f7b1e39c8067f58a3f7ca2465366df3ace067 /toys | |
parent | f5d9d76447d34a1aa43afc2c783e70f07fd58228 (diff) | |
download | toybox-a6ec1d989bba98cf1e4cd3b3e566e69cfc4e4939.tar.gz |
mktemp: more tests, more fixes.
I realized (after being questioned about my motivation) that I hadn't
added a test for the -u behavior. Adding the missing test confirmed the
usual "if there isn't a test, the code is broken", but now I think I
actually understand how we're supposed to choose between DIR, $TMPDIR,
and /tmp. I've added more tests to back this up, and rewritten the code
one more time so that we pass all the tests.
Diffstat (limited to 'toys')
-rw-r--r-- | toys/lsb/mktemp.c | 22 |
1 files changed, 16 insertions, 6 deletions
diff --git a/toys/lsb/mktemp.c b/toys/lsb/mktemp.c index ae97e494..112f84c4 100644 --- a/toys/lsb/mktemp.c +++ b/toys/lsb/mktemp.c @@ -17,7 +17,7 @@ config MKTEMP -d Create directory instead of file (--directory) -p Put new file in DIR (--tmpdir) -q Quiet, no error messages - -t Prepend $TMPDIR or /tmp if unset + -t Prefer $TMPDIR > DIR > /tmp (default DIR > $TMPDIR > /tmp) -u Don't create anything, just print what would be created Each X in TEMPLATE is replaced with a random printable character. The @@ -34,18 +34,28 @@ GLOBALS( void mktemp_main(void) { char *template = *toys.optargs; + int use_dir = (toys.optflags & (FLAG_p|FLAG_t)); if (!template) { - toys.optflags |= FLAG_t; template = "tmp.XXXXXXXXXX"; + use_dir = 1; } - if (!TT.p || (toys.optflags & FLAG_t)) TT.p = getenv("TMPDIR"); - if (!TT.p || !*TT.p) TT.p = "/tmp"; + // Normally, the precedence is DIR (if set), $TMPDIR (if set), /tmp. + // With -t it's $TMPDIR, DIR, /tmp. + if (use_dir) { + char *tmpdir = getenv("TMPDIR"); + + if (toys.optflags & FLAG_t) { + if (tmpdir && *tmpdir) TT.p = tmpdir; + } else { + if (!TT.p || !*TT.p) TT.p = tmpdir; + } + if (!TT.p || !*TT.p) TT.p = "/tmp"; + } // TODO: coreutils cleans paths, so -p /t/// would result in /t/xxx... - template = (strchr(template, '/') || !(toys.optflags & (FLAG_p|FLAG_t))) - ? xstrdup(template) : xmprintf("%s/%s", TT.p, template); + template = use_dir ? xmprintf("%s/%s", TT.p, template) : xstrdup(template); if (toys.optflags & FLAG_u) { xputs(mktemp(template)); |