blob: 5393320a559f923cd1e54d2b541571349c30eb36 [file] [log] [blame]
Erik Andersen227a59b2000-04-25 23:24:55 +00001/* vi: set sw=4 ts=4: */
2/*
3 * Mini mktemp implementation for busybox
4 *
Erik Andersen227a59b2000-04-25 23:24:55 +00005 * Copyright (C) 2000 by Daniel Jacobowitz
6 * Written by Daniel Jacobowitz <dan@debian.org>
7 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02008 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Erik Andersen227a59b2000-04-25 23:24:55 +00009 */
Denis Vlasenkoc05b1682008-06-05 12:06:00 +000010/* Coreutils 6.12 man page says:
11 * mktemp [OPTION]... [TEMPLATE]
12 * Create a temporary file or directory, safely, and print its name. If
13 * TEMPLATE is not specified, use tmp.XXXXXXXXXX.
14 * -d, --directory
15 * create a directory, not a file
16 * -q, --quiet
17 * suppress diagnostics about file/dir-creation failure
18 * -u, --dry-run
19 * do not create anything; merely print a name (unsafe)
20 * --tmpdir[=DIR]
21 * interpret TEMPLATE relative to DIR. If DIR is not specified,
22 * use $TMPDIR if set, else /tmp. With this option, TEMPLATE must
23 * not be an absolute name. Unlike with -t, TEMPLATE may contain
24 * slashes, but even here, mktemp still creates only the final com-
25 * ponent.
26 * -p DIR use DIR as a prefix; implies -t [deprecated]
27 * -t interpret TEMPLATE as a single file name component, relative to
28 * a directory: $TMPDIR, if set; else the directory specified via
29 * -p; else /tmp [deprecated]
30 */
Denys Vlasenko28826ac2015-10-19 00:52:26 +020031//config:config MKTEMP
Denys Vlasenkob097a842018-12-28 03:20:17 +010032//config: bool "mktemp (4.2 kb)"
Denys Vlasenko28826ac2015-10-19 00:52:26 +020033//config: default y
34//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020035//config: mktemp is used to create unique temporary files
Denys Vlasenko28826ac2015-10-19 00:52:26 +020036
Denys Vlasenko6bec24c2017-08-04 17:39:05 +020037//applet:IF_MKTEMP(APPLET_NOEXEC(mktemp, mktemp, BB_DIR_BIN, BB_SUID_DROP, mktemp))
Denys Vlasenko28826ac2015-10-19 00:52:26 +020038
39//kbuild:lib-$(CONFIG_MKTEMP) += mktemp.o
Denis Vlasenkoc05b1682008-06-05 12:06:00 +000040
Denys Vlasenko4ed3c522011-02-13 17:38:34 +010041//usage:#define mktemp_trivial_usage
42//usage: "[-dt] [-p DIR] [TEMPLATE]"
43//usage:#define mktemp_full_usage "\n\n"
44//usage: "Create a temporary file with name based on TEMPLATE and print its name.\n"
45//usage: "TEMPLATE must end with XXXXXX (e.g. [/dir/]nameXXXXXX).\n"
46//usage: "Without TEMPLATE, -t tmp.XXXXXX is assumed.\n"
Denys Vlasenko4ed3c522011-02-13 17:38:34 +010047//usage: "\n -d Make directory, not file"
Ron Yorstoneab343e2012-10-08 11:47:22 +020048//usage: "\n -q Fail silently on errors"
Denys Vlasenko4ed3c522011-02-13 17:38:34 +010049//usage: "\n -t Prepend base directory name to TEMPLATE"
50//usage: "\n -p DIR Use DIR as a base directory (implies -t)"
Denys Vlasenko75e1e7b2012-04-17 16:00:20 +020051//usage: "\n -u Do not create anything; print a name"
Denys Vlasenko4ed3c522011-02-13 17:38:34 +010052//usage: "\n"
53//usage: "\nBase directory is: -p DIR, else $TMPDIR, else /tmp"
54//usage:
55//usage:#define mktemp_example_usage
56//usage: "$ mktemp /tmp/temp.XXXXXX\n"
57//usage: "/tmp/temp.mWiLjM\n"
58//usage: "$ ls -la /tmp/temp.mWiLjM\n"
59//usage: "-rw------- 1 andersen andersen 0 Apr 25 17:10 /tmp/temp.mWiLjM\n"
Denis Vlasenkoc05b1682008-06-05 12:06:00 +000060
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000061#include "libbb.h"
Erik Andersen227a59b2000-04-25 23:24:55 +000062
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000063int mktemp_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000064int mktemp_main(int argc UNUSED_PARAM, char **argv)
Erik Andersen227a59b2000-04-25 23:24:55 +000065{
Denis Vlasenko65581f32008-02-09 06:26:53 +000066 const char *path;
Bernhard Reutner-Fischer1ac42bf2006-10-10 15:28:41 +000067 char *chp;
Denys Vlasenko9b814ca2010-06-18 03:16:27 +020068 unsigned opts;
Denys Vlasenko4ed3c522011-02-13 17:38:34 +010069 enum {
70 OPT_d = 1 << 0,
71 OPT_q = 1 << 1,
72 OPT_t = 1 << 2,
73 OPT_p = 1 << 3,
Denys Vlasenko75e1e7b2012-04-17 16:00:20 +020074 OPT_u = 1 << 4,
Denys Vlasenko4ed3c522011-02-13 17:38:34 +010075 };
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000076
Denys Vlasenko9b814ca2010-06-18 03:16:27 +020077 path = getenv("TMPDIR");
78 if (!path || path[0] == '\0')
79 path = "/tmp";
80
Denys Vlasenko22542ec2017-08-08 21:55:02 +020081 opts = getopt32(argv, "^" "dqtp:u" "\0" "?1"/*1 arg max*/, &path);
Denys Vlasenko9b814ca2010-06-18 03:16:27 +020082
Denys Vlasenko4ed3c522011-02-13 17:38:34 +010083 chp = argv[optind];
84 if (!chp) {
85 /* GNU coreutils 8.4:
86 * bare "mktemp" -> "mktemp -t tmp.XXXXXX"
87 */
88 chp = xstrdup("tmp.XXXXXX");
89 opts |= OPT_t;
90 }
Ron Yorstoneab343e2012-10-08 11:47:22 +020091#if 0
92 /* Don't allow directory separator in template */
93 if ((opts & OPT_t) && bb_basename(chp) != chp) {
94 errno = EINVAL;
95 goto error;
Denys Vlasenko75e1e7b2012-04-17 16:00:20 +020096 }
Ron Yorstoneab343e2012-10-08 11:47:22 +020097#endif
Denys Vlasenko4ed3c522011-02-13 17:38:34 +010098 if (opts & (OPT_t|OPT_p))
Denys Vlasenko04a5d5a2010-07-12 03:43:39 +020099 chp = concat_path_file(path, chp);
Bernhard Reutner-Fischer1ac42bf2006-10-10 15:28:41 +0000100
Ron Yorstoneab343e2012-10-08 11:47:22 +0200101 if (opts & OPT_u) {
102 chp = mktemp(chp);
103 if (chp[0] == '\0')
104 goto error;
105 } else if (opts & OPT_d) {
Bernhard Reutner-Fischer1ac42bf2006-10-10 15:28:41 +0000106 if (mkdtemp(chp) == NULL)
Ron Yorstoneab343e2012-10-08 11:47:22 +0200107 goto error;
Bernhard Reutner-Fischer1ac42bf2006-10-10 15:28:41 +0000108 } else {
109 if (mkstemp(chp) < 0)
Ron Yorstoneab343e2012-10-08 11:47:22 +0200110 goto error;
Glenn L McGrath69f28e72003-04-26 04:56:17 +0000111 }
Bernhard Reutner-Fischer1ac42bf2006-10-10 15:28:41 +0000112 puts(chp);
Matt Kraai3e856ce2000-12-01 02:55:13 +0000113 return EXIT_SUCCESS;
Ron Yorstoneab343e2012-10-08 11:47:22 +0200114 error:
115 if (opts & OPT_q)
116 return EXIT_FAILURE;
117 /* don't use chp as it gets mangled in case of error */
118 bb_perror_nomsg_and_die();
Erik Andersen227a59b2000-04-25 23:24:55 +0000119}