OSDN Git Service

lsb/mktemp: Add -u flag
authorKylie McClain <somasis@exherbo.org>
Thu, 24 Dec 2015 05:20:24 +0000 (00:20 -0500)
committerRob Landley <rob@landley.net>
Thu, 31 Dec 2015 02:49:57 +0000 (20:49 -0600)
The -u flag creates a file, and unlinks it before exiting.
This is usually known as "unsafe mode", or "dry-run" mode.

GNU mktemp has it, as does Busybox's mktemp and likely many others.

toys/lsb/mktemp.c

index cee62c1..f1f9d88 100644 (file)
@@ -4,19 +4,20 @@
  *
  * http://refspecs.linuxfoundation.org/LSB_4.1.0/LSB-Core-generic/LSB-Core-generic/mktemp.html
 
-USE_MKTEMP(NEWTOY(mktemp, ">1qd(directory)p(tmpdir):", TOYFLAG_BIN))
+USE_MKTEMP(NEWTOY(mktemp, ">1uqd(directory)p(tmpdir):", TOYFLAG_BIN))
 
 config MKTEMP
   bool "mktemp"
   default y
   help
-    usage: mktemp [-dq] [-p DIR] [TEMPLATE]
+    usage: mktemp [-dqu] [-p DIR] [TEMPLATE]
 
     Safely create a new file "DIR/TEMPLATE" and print its name.
 
     -d Create directory instead of file (--directory)
     -p Put new file in DIR (--tmpdir)
     -q Quiet, no error messages
+    -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,
@@ -48,6 +49,7 @@ void mktemp_main(void)
     else perror_exit("Failed to create %s %s/%s",
                      d_flag ? "directory" : "file", TT.tmpdir, template);
   } else xputs(template);
+  if (toys.optflags & FLAG_u) unlink(template);
 
   if (CFG_TOYBOX_FREE) free(template);
 }