aboutsummaryrefslogtreecommitdiff
path: root/toys/lsb/mktemp.c
diff options
context:
space:
mode:
authorRob Landley <rob@landley.net>2012-08-25 14:25:22 -0500
committerRob Landley <rob@landley.net>2012-08-25 14:25:22 -0500
commit3a9241add947cb6d24b5de7a8927517426a78795 (patch)
treed122ab6570439cd6b17c7d73ed8d4e085e0b8a95 /toys/lsb/mktemp.c
parent689f095bc976417bf50810fe59a3b3ac32b21105 (diff)
downloadtoybox-3a9241add947cb6d24b5de7a8927517426a78795.tar.gz
Move commands into "posix", "lsb", and "other" menus/directories.
Diffstat (limited to 'toys/lsb/mktemp.c')
-rw-r--r--toys/lsb/mktemp.c54
1 files changed, 54 insertions, 0 deletions
diff --git a/toys/lsb/mktemp.c b/toys/lsb/mktemp.c
new file mode 100644
index 00000000..e82256de
--- /dev/null
+++ b/toys/lsb/mktemp.c
@@ -0,0 +1,54 @@
+/* vi: set sw=4 ts=4:
+ *
+ * mktemp.c - Create a temporary file or directory.
+ *
+ * Copyright 2012 Elie De Brauwer <eliedebrauwer@gmail.com>
+ *
+ * Not in SUSv4.
+
+USE_MKTEMP(NEWTOY(mktemp, ">1(directory)d(tmpdir)p:", TOYFLAG_BIN))
+
+config MKTEMP
+ bool "mktemp"
+ default y
+ help
+ usage: mktemp [OPTION] [TEMPLATE]
+
+ Safely create a temporary file or directory and print its name.
+ TEMPLATE should end in 6 consecutive X's, the default
+ template is tmp.XXXXXX and the default directory is /tmp/.
+
+ -d, --directory Create a directory, instead of a file
+ -p DIR, --tmpdir=DIR Use DIR as a base path
+
+*/
+
+#include "toys.h"
+
+DEFINE_GLOBALS(
+ char * tmpdir;
+)
+#define TT this.mktemp
+
+void mktemp_main(void)
+{
+ int d_flag = toys.optflags & 2;
+ char *tmp, *path;
+
+ tmp = *toys.optargs;
+ if (!tmp) tmp = "tmp.XXXXXX";
+ if (!TT.tmpdir) TT.tmpdir = "/tmp/";
+
+ tmp = xmsprintf("%s/%s", TT.tmpdir, tmp);
+
+ if (d_flag ? mkdtemp(tmp) == NULL : mkstemp(tmp) == -1)
+ perror_exit("Failed to create temporary %s",
+ d_flag ? "directory" : "file");
+
+ xputs(path = xrealpath(tmp));
+
+ if (CFG_TOYBOX_FREE) {
+ free(path);
+ free(tmp);
+ }
+}