blob: 602309cc74fd9656721a1ee4b3b5f0d3364e038b [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 *
5 *
6 * Copyright (C) 2000 by Daniel Jacobowitz
7 * Written by Daniel Jacobowitz <dan@debian.org>
8 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02009 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Erik Andersen227a59b2000-04-25 23:24:55 +000010 */
11
Denis Vlasenkoc05b1682008-06-05 12:06:00 +000012/* Coreutils 6.12 man page says:
13 * mktemp [OPTION]... [TEMPLATE]
14 * Create a temporary file or directory, safely, and print its name. If
15 * TEMPLATE is not specified, use tmp.XXXXXXXXXX.
16 * -d, --directory
17 * create a directory, not a file
18 * -q, --quiet
19 * suppress diagnostics about file/dir-creation failure
20 * -u, --dry-run
21 * do not create anything; merely print a name (unsafe)
22 * --tmpdir[=DIR]
23 * interpret TEMPLATE relative to DIR. If DIR is not specified,
24 * use $TMPDIR if set, else /tmp. With this option, TEMPLATE must
25 * not be an absolute name. Unlike with -t, TEMPLATE may contain
26 * slashes, but even here, mktemp still creates only the final com-
27 * ponent.
28 * -p DIR use DIR as a prefix; implies -t [deprecated]
29 * -t interpret TEMPLATE as a single file name component, relative to
30 * a directory: $TMPDIR, if set; else the directory specified via
31 * -p; else /tmp [deprecated]
32 */
33
Denys Vlasenko4ed3c522011-02-13 17:38:34 +010034//usage:#define mktemp_trivial_usage
35//usage: "[-dt] [-p DIR] [TEMPLATE]"
36//usage:#define mktemp_full_usage "\n\n"
37//usage: "Create a temporary file with name based on TEMPLATE and print its name.\n"
38//usage: "TEMPLATE must end with XXXXXX (e.g. [/dir/]nameXXXXXX).\n"
39//usage: "Without TEMPLATE, -t tmp.XXXXXX is assumed.\n"
Denys Vlasenko4ed3c522011-02-13 17:38:34 +010040//usage: "\n -d Make directory, not file"
Tanguy Pruvot823694d2012-11-18 13:20:29 +010041//usage: "\n -q Fail silently on errors"
Denys Vlasenko4ed3c522011-02-13 17:38:34 +010042//usage: "\n -t Prepend base directory name to TEMPLATE"
43//usage: "\n -p DIR Use DIR as a base directory (implies -t)"
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +020044//usage: "\n -u Do not create anything; print a name"
Denys Vlasenko4ed3c522011-02-13 17:38:34 +010045//usage: "\n"
Tanguy Pruvotc7dcb612013-07-31 21:43:02 +020046//usage: "\nBase directory is: -p DIR, else $TMPDIR, else /data/local/tmp"
Denys Vlasenko4ed3c522011-02-13 17:38:34 +010047//usage:
48//usage:#define mktemp_example_usage
49//usage: "$ mktemp /tmp/temp.XXXXXX\n"
50//usage: "/tmp/temp.mWiLjM\n"
51//usage: "$ ls -la /tmp/temp.mWiLjM\n"
52//usage: "-rw------- 1 andersen andersen 0 Apr 25 17:10 /tmp/temp.mWiLjM\n"
Denis Vlasenkoc05b1682008-06-05 12:06:00 +000053
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000054#include "libbb.h"
Erik Andersen227a59b2000-04-25 23:24:55 +000055
maxwen27116ba2015-08-14 21:41:28 +020056#ifdef __BIONIC__
57#define mktemp(s) bb_mktemp(s)
58#endif
59
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000060int mktemp_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000061int mktemp_main(int argc UNUSED_PARAM, char **argv)
Erik Andersen227a59b2000-04-25 23:24:55 +000062{
Denis Vlasenko65581f32008-02-09 06:26:53 +000063 const char *path;
Bernhard Reutner-Fischer1ac42bf2006-10-10 15:28:41 +000064 char *chp;
Denys Vlasenko9b814ca2010-06-18 03:16:27 +020065 unsigned opts;
Denys Vlasenko4ed3c522011-02-13 17:38:34 +010066 enum {
67 OPT_d = 1 << 0,
68 OPT_q = 1 << 1,
69 OPT_t = 1 << 2,
70 OPT_p = 1 << 3,
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +020071 OPT_u = 1 << 4,
Denys Vlasenko4ed3c522011-02-13 17:38:34 +010072 };
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000073
Denys Vlasenko9b814ca2010-06-18 03:16:27 +020074 path = getenv("TMPDIR");
75 if (!path || path[0] == '\0')
Tanguy Pruvotc7dcb612013-07-31 21:43:02 +020076 path = "/data/local/tmp";
Denys Vlasenko9b814ca2010-06-18 03:16:27 +020077
Denis Vlasenkoc05b1682008-06-05 12:06:00 +000078 opt_complementary = "?1"; /* 1 argument max */
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +020079 opts = getopt32(argv, "dqtp:u", &path);
Denys Vlasenko9b814ca2010-06-18 03:16:27 +020080
Denys Vlasenko4ed3c522011-02-13 17:38:34 +010081 chp = argv[optind];
82 if (!chp) {
83 /* GNU coreutils 8.4:
84 * bare "mktemp" -> "mktemp -t tmp.XXXXXX"
85 */
86 chp = xstrdup("tmp.XXXXXX");
87 opts |= OPT_t;
88 }
Tanguy Pruvot823694d2012-11-18 13:20:29 +010089#if 0
90 /* Don't allow directory separator in template */
91 if ((opts & OPT_t) && bb_basename(chp) != chp) {
92 errno = EINVAL;
93 goto error;
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +020094 }
Tanguy Pruvot823694d2012-11-18 13:20:29 +010095#endif
Denys Vlasenko4ed3c522011-02-13 17:38:34 +010096 if (opts & (OPT_t|OPT_p))
Denys Vlasenko04a5d5a2010-07-12 03:43:39 +020097 chp = concat_path_file(path, chp);
Bernhard Reutner-Fischer1ac42bf2006-10-10 15:28:41 +000098
Tanguy Pruvot823694d2012-11-18 13:20:29 +010099 if (opts & OPT_u) {
Tanguy Pruvotc7dcb612013-07-31 21:43:02 +0200100 chp = mktemp(chp);
Steven Luo3f2a90a2013-07-31 13:17:34 -0700101 if (chp[0] == '\0')
102 goto error;
Tanguy Pruvot823694d2012-11-18 13:20:29 +0100103 } else if (opts & OPT_d) {
Bernhard Reutner-Fischer1ac42bf2006-10-10 15:28:41 +0000104 if (mkdtemp(chp) == NULL)
Tanguy Pruvot823694d2012-11-18 13:20:29 +0100105 goto error;
Bernhard Reutner-Fischer1ac42bf2006-10-10 15:28:41 +0000106 } else {
107 if (mkstemp(chp) < 0)
Tanguy Pruvot823694d2012-11-18 13:20:29 +0100108 goto error;
Glenn L McGrath69f28e72003-04-26 04:56:17 +0000109 }
Bernhard Reutner-Fischer1ac42bf2006-10-10 15:28:41 +0000110 puts(chp);
Matt Kraai3e856ce2000-12-01 02:55:13 +0000111 return EXIT_SUCCESS;
Tanguy Pruvot823694d2012-11-18 13:20:29 +0100112 error:
113 if (opts & OPT_q)
114 return EXIT_FAILURE;
115 /* don't use chp as it gets mangled in case of error */
116 bb_perror_nomsg_and_die();
Erik Andersen227a59b2000-04-25 23:24:55 +0000117}