summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2011-02-13 17:38:34 +0100
committerDenys Vlasenko <vda.linux@googlemail.com>2011-02-13 17:38:34 +0100
commit4ed3c52ce9b3ce5604c4fa075fda374f8cd01eea (patch)
treea8ea24dc52ac00ed72df6d64d43f0076acae9887
parent07cda2268a6cff59378af16eabc4968d9bebe915 (diff)
mktemp: make it more compatible with GNU coreutils 8.4
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r--debianutils/mktemp.c40
-rw-r--r--include/usage.src.h20
2 files changed, 36 insertions, 24 deletions
diff --git a/debianutils/mktemp.c b/debianutils/mktemp.c
index 86881f86d..f4961af59 100644
--- a/debianutils/mktemp.c
+++ b/debianutils/mktemp.c
@@ -31,6 +31,25 @@
* -p; else /tmp [deprecated]
*/
+//usage:#define mktemp_trivial_usage
+//usage: "[-dt] [-p DIR] [TEMPLATE]"
+//usage:#define mktemp_full_usage "\n\n"
+//usage: "Create a temporary file with name based on TEMPLATE and print its name.\n"
+//usage: "TEMPLATE must end with XXXXXX (e.g. [/dir/]nameXXXXXX).\n"
+//usage: "Without TEMPLATE, -t tmp.XXXXXX is assumed.\n"
+//usage: "\nOptions:"
+//usage: "\n -d Make directory, not file"
+////usage: "\n -q Fail silently on errors" - we ignore this opt
+//usage: "\n -t Prepend base directory name to TEMPLATE"
+//usage: "\n -p DIR Use DIR as a base directory (implies -t)"
+//usage: "\n"
+//usage: "\nBase directory is: -p DIR, else $TMPDIR, else /tmp"
+//usage:
+//usage:#define mktemp_example_usage
+//usage: "$ mktemp /tmp/temp.XXXXXX\n"
+//usage: "/tmp/temp.mWiLjM\n"
+//usage: "$ ls -la /tmp/temp.mWiLjM\n"
+//usage: "-rw------- 1 andersen andersen 0 Apr 25 17:10 /tmp/temp.mWiLjM\n"
#include "libbb.h"
@@ -40,20 +59,33 @@ int mktemp_main(int argc UNUSED_PARAM, char **argv)
const char *path;
char *chp;
unsigned opts;
+ enum {
+ OPT_d = 1 << 0,
+ OPT_q = 1 << 1,
+ OPT_t = 1 << 2,
+ OPT_p = 1 << 3,
+ };
path = getenv("TMPDIR");
if (!path || path[0] == '\0')
path = "/tmp";
- /* -q and -t are ignored */
+ /* -q is ignored */
opt_complementary = "?1"; /* 1 argument max */
opts = getopt32(argv, "dqtp:", &path);
- chp = argv[optind] ? argv[optind] : xstrdup("tmp.XXXXXX");
- if (!strchr(chp, '/') || (opts & 8))
+ chp = argv[optind];
+ if (!chp) {
+ /* GNU coreutils 8.4:
+ * bare "mktemp" -> "mktemp -t tmp.XXXXXX"
+ */
+ chp = xstrdup("tmp.XXXXXX");
+ opts |= OPT_t;
+ }
+ if (opts & (OPT_t|OPT_p))
chp = concat_path_file(path, chp);
- if (opts & 1) { /* -d */
+ if (opts & OPT_d) {
if (mkdtemp(chp) == NULL)
return EXIT_FAILURE;
} else {
diff --git a/include/usage.src.h b/include/usage.src.h
index ebe80f8e1..c2575b561 100644
--- a/include/usage.src.h
+++ b/include/usage.src.h
@@ -2451,26 +2451,6 @@ INSERT
"\nOptions:" \
"\n -L LBL Label" \
-#define mktemp_trivial_usage \
- "[-dt] [-p DIR] [TEMPLATE]"
-#define mktemp_full_usage "\n\n" \
- "Create a temporary file with name based on TEMPLATE and print its name.\n" \
- "TEMPLATE must end with XXXXXX (e.g. [/dir/]nameXXXXXX).\n" \
- "\nOptions:" \
- "\n -d Make a directory instead of a file" \
-/* "\n -q Fail silently if an error occurs" - we ignore it */ \
- "\n -t Generate a path rooted in temporary directory" \
- "\n -p DIR Use DIR as a temporary directory (implies -t)" \
- "\n" \
- "\nFor -t or -p, directory is chosen as follows:" \
- "\n$TMPDIR if set, else -p DIR, else /tmp" \
-
-#define mktemp_example_usage \
- "$ mktemp /tmp/temp.XXXXXX\n" \
- "/tmp/temp.mWiLjM\n" \
- "$ ls -la /tmp/temp.mWiLjM\n" \
- "-rw------- 1 andersen andersen 0 Apr 25 17:10 /tmp/temp.mWiLjM\n"
-
#define more_trivial_usage \
"[FILE]..."
#define more_full_usage "\n\n" \