Erik Andersen | 227a59b | 2000-04-25 23:24:55 +0000 | [diff] [blame] | 1 | /* 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 Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 9 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Erik Andersen | 227a59b | 2000-04-25 23:24:55 +0000 | [diff] [blame] | 10 | */ |
| 11 | |
Denis Vlasenko | c05b168 | 2008-06-05 12:06:00 +0000 | [diff] [blame] | 12 | /* 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 | */ |
Denys Vlasenko | 28826ac | 2015-10-19 00:52:26 +0200 | [diff] [blame] | 33 | //config:config MKTEMP |
Denys Vlasenko | 4eed2c6 | 2017-07-18 22:01:24 +0200 | [diff] [blame^] | 34 | //config: bool "mktemp (4 kb)" |
Denys Vlasenko | 28826ac | 2015-10-19 00:52:26 +0200 | [diff] [blame] | 35 | //config: default y |
| 36 | //config: help |
| 37 | //config: mktemp is used to create unique temporary files |
| 38 | |
| 39 | //applet:IF_MKTEMP(APPLET(mktemp, BB_DIR_BIN, BB_SUID_DROP)) |
| 40 | |
| 41 | //kbuild:lib-$(CONFIG_MKTEMP) += mktemp.o |
Denis Vlasenko | c05b168 | 2008-06-05 12:06:00 +0000 | [diff] [blame] | 42 | |
Denys Vlasenko | 4ed3c52 | 2011-02-13 17:38:34 +0100 | [diff] [blame] | 43 | //usage:#define mktemp_trivial_usage |
| 44 | //usage: "[-dt] [-p DIR] [TEMPLATE]" |
| 45 | //usage:#define mktemp_full_usage "\n\n" |
| 46 | //usage: "Create a temporary file with name based on TEMPLATE and print its name.\n" |
| 47 | //usage: "TEMPLATE must end with XXXXXX (e.g. [/dir/]nameXXXXXX).\n" |
| 48 | //usage: "Without TEMPLATE, -t tmp.XXXXXX is assumed.\n" |
Denys Vlasenko | 4ed3c52 | 2011-02-13 17:38:34 +0100 | [diff] [blame] | 49 | //usage: "\n -d Make directory, not file" |
Ron Yorston | eab343e | 2012-10-08 11:47:22 +0200 | [diff] [blame] | 50 | //usage: "\n -q Fail silently on errors" |
Denys Vlasenko | 4ed3c52 | 2011-02-13 17:38:34 +0100 | [diff] [blame] | 51 | //usage: "\n -t Prepend base directory name to TEMPLATE" |
| 52 | //usage: "\n -p DIR Use DIR as a base directory (implies -t)" |
Denys Vlasenko | 75e1e7b | 2012-04-17 16:00:20 +0200 | [diff] [blame] | 53 | //usage: "\n -u Do not create anything; print a name" |
Denys Vlasenko | 4ed3c52 | 2011-02-13 17:38:34 +0100 | [diff] [blame] | 54 | //usage: "\n" |
| 55 | //usage: "\nBase directory is: -p DIR, else $TMPDIR, else /tmp" |
| 56 | //usage: |
| 57 | //usage:#define mktemp_example_usage |
| 58 | //usage: "$ mktemp /tmp/temp.XXXXXX\n" |
| 59 | //usage: "/tmp/temp.mWiLjM\n" |
| 60 | //usage: "$ ls -la /tmp/temp.mWiLjM\n" |
| 61 | //usage: "-rw------- 1 andersen andersen 0 Apr 25 17:10 /tmp/temp.mWiLjM\n" |
Denis Vlasenko | c05b168 | 2008-06-05 12:06:00 +0000 | [diff] [blame] | 62 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 63 | #include "libbb.h" |
Erik Andersen | 227a59b | 2000-04-25 23:24:55 +0000 | [diff] [blame] | 64 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 65 | int mktemp_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 66 | int mktemp_main(int argc UNUSED_PARAM, char **argv) |
Erik Andersen | 227a59b | 2000-04-25 23:24:55 +0000 | [diff] [blame] | 67 | { |
Denis Vlasenko | 65581f3 | 2008-02-09 06:26:53 +0000 | [diff] [blame] | 68 | const char *path; |
Bernhard Reutner-Fischer | 1ac42bf | 2006-10-10 15:28:41 +0000 | [diff] [blame] | 69 | char *chp; |
Denys Vlasenko | 9b814ca | 2010-06-18 03:16:27 +0200 | [diff] [blame] | 70 | unsigned opts; |
Denys Vlasenko | 4ed3c52 | 2011-02-13 17:38:34 +0100 | [diff] [blame] | 71 | enum { |
| 72 | OPT_d = 1 << 0, |
| 73 | OPT_q = 1 << 1, |
| 74 | OPT_t = 1 << 2, |
| 75 | OPT_p = 1 << 3, |
Denys Vlasenko | 75e1e7b | 2012-04-17 16:00:20 +0200 | [diff] [blame] | 76 | OPT_u = 1 << 4, |
Denys Vlasenko | 4ed3c52 | 2011-02-13 17:38:34 +0100 | [diff] [blame] | 77 | }; |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 78 | |
Denys Vlasenko | 9b814ca | 2010-06-18 03:16:27 +0200 | [diff] [blame] | 79 | path = getenv("TMPDIR"); |
| 80 | if (!path || path[0] == '\0') |
| 81 | path = "/tmp"; |
| 82 | |
Denis Vlasenko | c05b168 | 2008-06-05 12:06:00 +0000 | [diff] [blame] | 83 | opt_complementary = "?1"; /* 1 argument max */ |
Denys Vlasenko | 75e1e7b | 2012-04-17 16:00:20 +0200 | [diff] [blame] | 84 | opts = getopt32(argv, "dqtp:u", &path); |
Denys Vlasenko | 9b814ca | 2010-06-18 03:16:27 +0200 | [diff] [blame] | 85 | |
Denys Vlasenko | 4ed3c52 | 2011-02-13 17:38:34 +0100 | [diff] [blame] | 86 | chp = argv[optind]; |
| 87 | if (!chp) { |
| 88 | /* GNU coreutils 8.4: |
| 89 | * bare "mktemp" -> "mktemp -t tmp.XXXXXX" |
| 90 | */ |
| 91 | chp = xstrdup("tmp.XXXXXX"); |
| 92 | opts |= OPT_t; |
| 93 | } |
Ron Yorston | eab343e | 2012-10-08 11:47:22 +0200 | [diff] [blame] | 94 | #if 0 |
| 95 | /* Don't allow directory separator in template */ |
| 96 | if ((opts & OPT_t) && bb_basename(chp) != chp) { |
| 97 | errno = EINVAL; |
| 98 | goto error; |
Denys Vlasenko | 75e1e7b | 2012-04-17 16:00:20 +0200 | [diff] [blame] | 99 | } |
Ron Yorston | eab343e | 2012-10-08 11:47:22 +0200 | [diff] [blame] | 100 | #endif |
Denys Vlasenko | 4ed3c52 | 2011-02-13 17:38:34 +0100 | [diff] [blame] | 101 | if (opts & (OPT_t|OPT_p)) |
Denys Vlasenko | 04a5d5a | 2010-07-12 03:43:39 +0200 | [diff] [blame] | 102 | chp = concat_path_file(path, chp); |
Bernhard Reutner-Fischer | 1ac42bf | 2006-10-10 15:28:41 +0000 | [diff] [blame] | 103 | |
Ron Yorston | eab343e | 2012-10-08 11:47:22 +0200 | [diff] [blame] | 104 | if (opts & OPT_u) { |
| 105 | chp = mktemp(chp); |
| 106 | if (chp[0] == '\0') |
| 107 | goto error; |
| 108 | } else if (opts & OPT_d) { |
Bernhard Reutner-Fischer | 1ac42bf | 2006-10-10 15:28:41 +0000 | [diff] [blame] | 109 | if (mkdtemp(chp) == NULL) |
Ron Yorston | eab343e | 2012-10-08 11:47:22 +0200 | [diff] [blame] | 110 | goto error; |
Bernhard Reutner-Fischer | 1ac42bf | 2006-10-10 15:28:41 +0000 | [diff] [blame] | 111 | } else { |
| 112 | if (mkstemp(chp) < 0) |
Ron Yorston | eab343e | 2012-10-08 11:47:22 +0200 | [diff] [blame] | 113 | goto error; |
Glenn L McGrath | 69f28e7 | 2003-04-26 04:56:17 +0000 | [diff] [blame] | 114 | } |
Bernhard Reutner-Fischer | 1ac42bf | 2006-10-10 15:28:41 +0000 | [diff] [blame] | 115 | puts(chp); |
Matt Kraai | 3e856ce | 2000-12-01 02:55:13 +0000 | [diff] [blame] | 116 | return EXIT_SUCCESS; |
Ron Yorston | eab343e | 2012-10-08 11:47:22 +0200 | [diff] [blame] | 117 | error: |
| 118 | if (opts & OPT_q) |
| 119 | return EXIT_FAILURE; |
| 120 | /* don't use chp as it gets mangled in case of error */ |
| 121 | bb_perror_nomsg_and_die(); |
Erik Andersen | 227a59b | 2000-04-25 23:24:55 +0000 | [diff] [blame] | 122 | } |