aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xtests/mktemp.test21
-rw-r--r--toys/lsb/mktemp.c29
2 files changed, 37 insertions, 13 deletions
diff --git a/tests/mktemp.test b/tests/mktemp.test
new file mode 100755
index 00000000..d09d2c4c
--- /dev/null
+++ b/tests/mktemp.test
@@ -0,0 +1,21 @@
+#!/bin/bash
+
+[ -f testing.sh ] && . testing.sh
+
+#testing "name" "command" "result" "infile" "stdin"
+
+# mktemp by default should use tmp.XXXXXXXXXX as the template,
+# and $TMPDIR as the directory.
+testing "mktemp" "TMPDIR=/t mktemp -u | grep -q '^/t/tmp\...........$' && echo yes" "yes\n" "" ""
+
+# mktemp with a template should *not* use $TMPDIR.
+testing "mktemp TEMPLATE" "TMPDIR=/t mktemp -u hello.XXXXXXXX | grep -q '^hello\.........$' && echo yes" "yes\n" "" ""
+
+# mktemp with -t and a template should use $TMPDIR.
+testing "mktemp -t TEMPLATE" "TMPDIR=/t mktemp -u -t hello.XXXXXXXX | grep -q '^/t/hello\.........$' && echo yes" "yes\n" "" ""
+
+# mktemp with -p DIR and a template should use DIR.
+testing "mktemp -p DIR TEMPLATE" "TMPDIR=/t mktemp -u -p DIR hello.XXXXXXXX | grep -q '^DIR/hello\.........$' && echo yes" "yes\n" "" ""
+
+# mktemp -p DIR and -t: -t wins.
+testing "mktemp -p DIR -t TEMPLATE" "TMPDIR=/t mktemp -u -p DIR -t hello.XXXXXXXX | grep -q '^/t/hello\.........$' && echo yes" "yes\n" "" ""
diff --git a/toys/lsb/mktemp.c b/toys/lsb/mktemp.c
index 21bb9b36..440cf6b1 100644
--- a/toys/lsb/mktemp.c
+++ b/toys/lsb/mktemp.c
@@ -4,7 +4,7 @@
*
* http://refspecs.linuxfoundation.org/LSB_4.1.0/LSB-Core-generic/LSB-Core-generic/mktemp.html
-USE_MKTEMP(NEWTOY(mktemp, ">1uqd(directory)p(tmpdir):", TOYFLAG_BIN))
+USE_MKTEMP(NEWTOY(mktemp, ">1uqd(directory)p(tmpdir):t", TOYFLAG_BIN))
config MKTEMP
bool "mktemp"
@@ -17,11 +17,11 @@ 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
-u Don't create anything, just print what would be created
Each X in TEMPLATE is replaced with a random printable character. The
- default TEMPLATE is tmp.XXXXXX, and the default DIR is $TMPDIR if set,
- else "/tmp".
+ default TEMPLATE is tmp.XXXXXXXXXX.
*/
#define FOR_mktemp
@@ -33,24 +33,27 @@ GLOBALS(
void mktemp_main(void)
{
- int d_flag = toys.optflags & FLAG_d;
char *template = *toys.optargs;
- if (!template) template = "tmp.XXXXXX";
+ if (!template) {
+ toys.optflags |= FLAG_t;
+ template = "tmp.XXXXXXXXXX";
+ }
- if (!TT.p) TT.p = getenv("TMPDIR");
+ if (!TT.p || (toys.optflags & FLAG_t)) TT.p = getenv("TMPDIR");
if (!TT.p || !*TT.p) TT.p = "/tmp";
- template = strchr(template, '/') ? xstrdup(template)
- : xmprintf("%s/%s", TT.p, template);
+ // 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);
- if (d_flag ? !mkdtemp(template) : mkstemp(template) == -1) {
+ if (toys.optflags & FLAG_u) {
+ mktemp(template);
+ xputs(template);
+ } else if (toys.optflags & FLAG_d ? !mkdtemp(template) : mkstemp(template) == -1) {
if (toys.optflags & FLAG_q) toys.exitval = 1;
else perror_exit("Failed to create %s %s/%s",
- d_flag ? "directory" : "file", TT.p, template);
- } else {
- if (toys.optflags & FLAG_u) unlink(template);
- xputs(template);
+ toys.optflags & FLAG_d ? "directory" : "file", TT.p, template);
}
if (CFG_TOYBOX_FREE) free(template);