aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorElliot Hughes <enh@google.com>2015-02-07 19:27:59 -0600
committerElliot Hughes <enh@google.com>2015-02-07 19:27:59 -0600
commit831a085a8d36cfb4fa064bdddb6bd0a434d4b3e1 (patch)
tree1ba9e529c3aae7847974956141939ef13e5c8826
parent912b2be5e53f2656727beb3158c8d97ad6b1f6f2 (diff)
downloadtoybox-831a085a8d36cfb4fa064bdddb6bd0a434d4b3e1.tar.gz
Use $TMPDIR if set (necessary on Android, where there is no /tmp).
Include full template in error messages. Don't report success on failure with -q. Avoid unnecessary allocation. Fix "xxxxxx" versus "XXXXXX" confusion.
-rw-r--r--toys/lsb/mktemp.c37
1 files changed, 20 insertions, 17 deletions
diff --git a/toys/lsb/mktemp.c b/toys/lsb/mktemp.c
index c1175fec..52e53ee6 100644
--- a/toys/lsb/mktemp.c
+++ b/toys/lsb/mktemp.c
@@ -12,8 +12,8 @@ config MKTEMP
help
usage: mktemp [-dq] [-p DIR] [TEMPLATE]
- Safely create new file and print its name. Default TEMPLATE is
- /tmp/tmp.XXXXXX and each trailing X is replaced with random char.
+ Safely create a new file and print its name. The default TEMPLATE is
+ tmp.XXXXXX. The default DIR is $TMPDIR, or /tmp if $TMPDIR is not set.
-d, --directory Create directory instead of file
-p DIR, --tmpdir=DIR Put new file in DIR
@@ -29,24 +29,27 @@ GLOBALS(
void mktemp_main(void)
{
- int d_flag = toys.optflags & FLAG_d;
- char *tmp;
+ int d_flag = toys.optflags & FLAG_d;
+ char *template = *toys.optargs;
+ int success;
- tmp = *toys.optargs;
-
- if (!tmp) {
- if (!TT.tmpdir) TT.tmpdir = "/tmp";
- tmp = "tmp.xxxxxx";
+ if (!template) {
+ template = "tmp.XXXXXX";
}
- if (TT.tmpdir) tmp = xmprintf("%s/%s", TT.tmpdir ? TT.tmpdir : "/tmp",
- *toys.optargs ? *toys.optargs : "tmp.XXXXXX");
- if (d_flag ? mkdtemp(tmp) == NULL : mkstemp(tmp) == -1)
- if (toys.optflags & FLAG_q)
- perror_exit("Failed to create temporary %s",
- d_flag ? "directory" : "file");
+ if (!TT.tmpdir) TT.tmpdir = getenv("TMPDIR");
+ if (!TT.tmpdir) TT.tmpdir = "/tmp";
+
+ snprintf(toybuf, sizeof(toybuf), "%s/%s", TT.tmpdir, template);
- xputs(tmp);
+ if (d_flag ? mkdtemp(toybuf) == NULL : mkstemp(toybuf) == -1) {
+ if (toys.optflags & FLAG_q) {
+ toys.exitval = 1;
+ } else {
+ perror_exit("Failed to create temporary %s with template %s/%s",
+ d_flag ? "directory" : "file", TT.tmpdir, template);
+ }
+ }
- if (CFG_TOYBOX_FREE && TT.tmpdir) free(tmp);
+ xputs(toybuf);
}