aboutsummaryrefslogtreecommitdiff
path: root/toys
diff options
context:
space:
mode:
authorRob Landley <rob@landley.net>2012-06-01 13:51:22 -0500
committerRob Landley <rob@landley.net>2012-06-01 13:51:22 -0500
commit0f6f98009a7631e613711304f99205de7e19c123 (patch)
treeea6e3314b8bc59f48854e3717abe47f0b7608f0d /toys
parenteec463764dbc2e83325695583a1b73bebc492a9e (diff)
downloadtoybox-0f6f98009a7631e613711304f99205de7e19c123.tar.gz
Convert mktemp to use xrealpath, and general clean up while there.
Diffstat (limited to 'toys')
-rw-r--r--toys/mktemp.c38
1 files changed, 18 insertions, 20 deletions
diff --git a/toys/mktemp.c b/toys/mktemp.c
index 91caf20e..e82256de 100644
--- a/toys/mktemp.c
+++ b/toys/mktemp.c
@@ -17,6 +17,7 @@ config MKTEMP
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
@@ -31,26 +32,23 @@ DEFINE_GLOBALS(
void mktemp_main(void)
{
- int p_flag = (toys.optflags & 1);
- int d_flag = (toys.optflags & 2) >> 1;
- char * result;
-
- int size = snprintf(toybuf, sizeof(toybuf)-1, "%s/%s",
- (p_flag && TT.tmpdir)?TT.tmpdir:"/tmp/",
- (toys.optargs[0])?toys.optargs[0]:"tmp.XXXXXX");
- toybuf[size] = 0;
-
- if (d_flag) {
- if (mkdtemp(toybuf) == NULL)
- perror_exit("Failed to create temporary directory");
- } else {
- if (mkstemp(toybuf) == -1)
- perror_exit("Failed to create temporary file");
- }
+ 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);
- result = realpath(toybuf, NULL);
- xputs(result);
+ if (d_flag ? mkdtemp(tmp) == NULL : mkstemp(tmp) == -1)
+ perror_exit("Failed to create temporary %s",
+ d_flag ? "directory" : "file");
- if (CFG_TOYBOX_FREE)
- free(result);
+ xputs(path = xrealpath(tmp));
+
+ if (CFG_TOYBOX_FREE) {
+ free(path);
+ free(tmp);
+ }
}