aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRob Landley <rob@landley.net>2019-09-17 06:00:14 -0500
committerRob Landley <rob@landley.net>2019-09-17 06:00:14 -0500
commit0dedd13253f4e3fc4ddaf912f503b30261d2ee63 (patch)
tree1088306acbfab223bd4af9bf3ba6421a61e38127
parent861d1c76cd2ddf3c2feab0cd439f325b96541610 (diff)
downloadtoybox-0dedd13253f4e3fc4ddaf912f503b30261d2ee63.tar.gz
Allow --tmpdir's argument to be optional.
-rwxr-xr-xtests/mktemp.test12
-rw-r--r--toys/lsb/mktemp.c22
2 files changed, 23 insertions, 11 deletions
diff --git a/tests/mktemp.test b/tests/mktemp.test
index 9715be5d..adf8666e 100755
--- a/tests/mktemp.test
+++ b/tests/mktemp.test
@@ -16,11 +16,17 @@ testing "-t TEMPLATE uses \$TMPDIR" "TMPDIR=/t mktemp -u -t hello.XXXXXXXX | gre
testing "-t and TEMPLATE but no \$TMPDIR uses /tmp" "TMPDIR= mktemp -u -t hello.XXXXXXXX | grep -q '^/tmp/hello\.........$' && echo yes" "yes\n" "" ""
testing "-p DIR and TEMPLATE should use DIR" "TMPDIR=/t mktemp -u -p DIR hello.XXXXXXXX | grep -q '^DIR/hello\.........$' && echo yes" "yes\n" "" ""
testing "-p DIR and -t: -t wins" "TMPDIR=/t mktemp -u -p DIR -t hello.XXXXXXXX | grep -q '^/t/hello\.........$' && echo yes" "yes\n" "" ""
-testing "-p DIR -t TEMPLATE but no \$TMPDIR (DIR wins)" "TMPDIR= mktemp -u -p DIR -t hello.XXXXXXXX | grep -q '^DIR/hello\.........$' && echo yes" "yes\n" "" ""
+testing "-p DIR -t TEMPLATE but no \$TMPDIR (DIR wins)" "TMPDIR= mktemp -u -p DIR -t hello.XXXXXXXX | grep -q '^DIR/hello\.........\$' && echo yes" "yes\n" "" ""
testing "-u doesn't need to be able to write to dir" \
- "mktemp -u -p /proc | grep -q '^/proc/tmp\...........$' && echo yes" "yes\n" \
+ "mktemp -u -p /proc | grep -q '^/proc/tmp\...........\$' && echo yes" "yes\n" \
"" ""
-testing "needs at least XX in template" \
+testing "needs at least XXX in template" \
"mktemp -u helloX 2>/dev/null || echo error" "error\n" "" ""
testing "-q shouldn't print path" \
"mktemp -p /proc -q || echo only-failure" "only-failure\n" "" ""
+testing "--tmpdir doesn't need argument" \
+ "TMPDIR= mktemp -u helloXXX --tmpdir | grep -q '^/tmp/hello...\$' && echo yes" \
+ "yes\n" "" ""
+testing "--tmpdir can have argument" \
+ "TMPDIR= mktemp -u helloXXX --tmpdir=abc | grep -q '^abc/hello...\$' && echo yes" \
+ "yes\n" "" ""
diff --git a/toys/lsb/mktemp.c b/toys/lsb/mktemp.c
index 22c880fa..0986b4fd 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):t", TOYFLAG_BIN))
+USE_MKTEMP(NEWTOY(mktemp, ">1(tmpdir);:uqd(directory)p:t", TOYFLAG_BIN))
config MKTEMP
bool "mktemp"
@@ -28,14 +28,20 @@ config MKTEMP
#include "toys.h"
GLOBALS(
- char *p;
+ char *p, *tmpdir;
)
void mktemp_main(void)
{
- char *template = *toys.optargs, *dir = TT.p, *te = getenv("TMPDIR");
+ char *template = *toys.optargs, *dir, *te = getenv("TMPDIR");
int len;
+ // --tmpdir's argument is optional's but -p is mandatory, so can't combine
+ if (!TT.p && FLAG(tmpdir)) {
+ TT.p = TT.tmpdir ? TT.tmpdir : "";
+ toys.optflags |= FLAG_p;
+ }
+ dir = TT.p;
// if template, no prefix unless -pt. if !template, always prefix
if (!dir || !*dir || (FLAG(t) && te && *te)) dir = te;
if (!dir || !*dir) dir = "/tmp";
@@ -52,7 +58,7 @@ void mktemp_main(void)
// In theory you just xputs(mktemp(template)) for -u, in practice there's
// link-time deprecation warnings if you do that. So we fake up our own:
- if (toys.optflags & FLAG_u) {
+ if (FLAG(u)) {
long long rr;
char *s = template+len;
@@ -72,12 +78,12 @@ void mktemp_main(void)
if (*s>'Z') (*s) += 6;
rr>>=6;
}
- } else if ((toys.optflags & FLAG_d) ? !mkdtemp(template) : mkstemp(template) == -1) {
- if (toys.optflags & FLAG_q) {
+ } else if (FLAG(d) ? !mkdtemp(template) : mkstemp(template) == -1) {
+ if (FLAG(q)) {
toys.exitval = 1;
return;
- } else perror_exit("Failed to create %s %s/%s",
- (toys.optflags & FLAG_d) ? "directory" : "file", TT.p, template);
+ } else perror_exit("Failed to create %s %s",
+ FLAG(d) ? "directory" : "file", template);
}
xputs(template);