[PATCH] mktemp: make -u option work more like coreutils

Ron Yorston rmy at tigress.co.uk
Fri Sep 21 15:10:54 UTC 2012


BusyBox mktemp uses tempnam to generate filenames with the -u option.
tempnam works differently from the mechanism used by coreutils:  it pays
too much attention to TMPDIR and it only uses the first five characters
of the template.

Consider the following commands:

   mktemp -u temp.XXXXXX
   mktemp -u -t temp.XXXXXX
   mktemp -u temp123.XXXXXX
   mktemp -u t/temp.XXXXXX
   mktemp -u tmpdir/temp.XXXXXX
   mktemp -u /var/tmp/temp.XXXXXX

coreutils mktemp returns (with TMPDIR unset in the left column and set
to /tmp in the right):

   temp.N5sFF8                temp.szUHKA
   /tmp/temp.7uPmpr           /tmp/temp.EnN3Cb
   temp123.pCKP0t             temp123.mLLGKy
   t/temp.y1Tr2X              t/temp.j91VbW
   tmpdir/temp.9bHHbd         tmpdir/temp.WR2iXj
   /var/tmp/temp.H6ypnZ       /var/tmp/temp.uOWZM5

Original BusyBox mktemp returns:

   temp.AwvZ1U                mp/temp.MjmUfi
   /tmp/temp.eUYIwR           /tmp/temp.yJX2ox
   temp15ugCZW                mp/temp18qPhgB
   t/temABm01N                mp/t/tem6acA5F
   tmpdivTbGrF                mp/tmpdi6aop9L
   /var/bN6XBU                mp//var/6jeTv6

With this patch BusyBox mktemp returns:

   temp.r0jnvN                temp.Fi7Zgh
   /tmp/temp.2Hi55L           /tmp/temp.oQLmgi
   temp123.rCzPdM             temp123.tUqsBg
   t/temp.21IcyP              t/temp.cgeAmg
   tmpdir/temp.FV5FxO         tmpdir/temp.VJW19j
   /var/tmp/temp.E45xYO       /var/tmp/temp.sYHyok

Signed-off-by: Ron Yorston <rmy at tigress.co.uk>

---
 debianutils/mktemp.c |   22 ++++++----------------
 1 files changed, 6 insertions(+), 16 deletions(-)

diff --git a/debianutils/mktemp.c b/debianutils/mktemp.c
index dbe4309..1f3bc9b 100644
--- a/debianutils/mktemp.c
+++ b/debianutils/mktemp.c
@@ -84,25 +84,15 @@ int mktemp_main(int argc UNUSED_PARAM, char **argv)
 		opts |= OPT_t;
 	}
 
-	if (opts & OPT_u) {
-		/* Remove (up to) 6 X's */
-		unsigned len = strlen(chp);
-		int cnt = len > 6 ? 6 : len;
-		while (--cnt >= 0 && chp[--len] == 'X')
-			chp[len] = '\0';
-
-		chp = tempnam(opts & (OPT_t|OPT_p) ? path : "./", chp);
-		if (!chp)
-			return EXIT_FAILURE;
-		if (!(opts & (OPT_t|OPT_p)))
-			chp += 2;
-		goto ret;
-	}
-
 	if (opts & (OPT_t|OPT_p))
 		chp = concat_path_file(path, chp);
 
-	if (opts & OPT_d) {
+	if (opts & OPT_u) {
+		chp = mktemp(chp);
+		if ( chp[0] == '\0' )
+			return EXIT_FAILURE;
+	}
+	else if (opts & OPT_d) {
 		if (mkdtemp(chp) == NULL)
 			return EXIT_FAILURE;
 	} else {
-- 
1.7.4.4



More information about the busybox mailing list