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 | * |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 9 | * Licensed under the GPL v2 or later, see the file LICENSE in this tarball. |
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 | */ |
| 33 | |
| 34 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 35 | #include "libbb.h" |
Erik Andersen | 227a59b | 2000-04-25 23:24:55 +0000 | [diff] [blame] | 36 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 37 | int mktemp_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 38 | int mktemp_main(int argc UNUSED_PARAM, char **argv) |
Erik Andersen | 227a59b | 2000-04-25 23:24:55 +0000 | [diff] [blame] | 39 | { |
Denis Vlasenko | 65581f3 | 2008-02-09 06:26:53 +0000 | [diff] [blame] | 40 | const char *path; |
Bernhard Reutner-Fischer | 1ac42bf | 2006-10-10 15:28:41 +0000 | [diff] [blame] | 41 | char *chp; |
Denis Vlasenko | c05b168 | 2008-06-05 12:06:00 +0000 | [diff] [blame] | 42 | unsigned opt; |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 43 | |
Denis Vlasenko | c05b168 | 2008-06-05 12:06:00 +0000 | [diff] [blame] | 44 | opt_complementary = "?1"; /* 1 argument max */ |
| 45 | opt = getopt32(argv, "dqtp:", &path); |
Denis Vlasenko | f2cccbc | 2008-06-08 20:40:33 +0000 | [diff] [blame] | 46 | chp = argv[optind] ? argv[optind] : xstrdup("tmp.XXXXXX"); |
Bernhard Reutner-Fischer | 1ac42bf | 2006-10-10 15:28:41 +0000 | [diff] [blame] | 47 | |
Denis Vlasenko | c05b168 | 2008-06-05 12:06:00 +0000 | [diff] [blame] | 48 | if (opt & (4|8)) { /* -t and/or -p */ |
Denis Vlasenko | 65581f3 | 2008-02-09 06:26:53 +0000 | [diff] [blame] | 49 | const char *dir = getenv("TMPDIR"); |
Bernhard Reutner-Fischer | 1ac42bf | 2006-10-10 15:28:41 +0000 | [diff] [blame] | 50 | if (dir && *dir != '\0') |
Denis Vlasenko | 65581f3 | 2008-02-09 06:26:53 +0000 | [diff] [blame] | 51 | path = dir; |
Denis Vlasenko | c05b168 | 2008-06-05 12:06:00 +0000 | [diff] [blame] | 52 | else if (!(opt & 8)) /* no -p */ |
Denis Vlasenko | 65581f3 | 2008-02-09 06:26:53 +0000 | [diff] [blame] | 53 | path = "/tmp/"; |
| 54 | /* else path comes from -p DIR */ |
| 55 | chp = concat_path_file(path, chp); |
Rob Landley | b5ca9e0 | 2005-12-02 17:54:01 +0000 | [diff] [blame] | 56 | } |
Bernhard Reutner-Fischer | 1ac42bf | 2006-10-10 15:28:41 +0000 | [diff] [blame] | 57 | |
Denis Vlasenko | c05b168 | 2008-06-05 12:06:00 +0000 | [diff] [blame] | 58 | if (opt & 1) { /* -d */ |
Bernhard Reutner-Fischer | 1ac42bf | 2006-10-10 15:28:41 +0000 | [diff] [blame] | 59 | if (mkdtemp(chp) == NULL) |
| 60 | return EXIT_FAILURE; |
| 61 | } else { |
| 62 | if (mkstemp(chp) < 0) |
Rob Landley | b5ca9e0 | 2005-12-02 17:54:01 +0000 | [diff] [blame] | 63 | return EXIT_FAILURE; |
Glenn L McGrath | 69f28e7 | 2003-04-26 04:56:17 +0000 | [diff] [blame] | 64 | } |
| 65 | |
Bernhard Reutner-Fischer | 1ac42bf | 2006-10-10 15:28:41 +0000 | [diff] [blame] | 66 | puts(chp); |
Glenn L McGrath | 69f28e7 | 2003-04-26 04:56:17 +0000 | [diff] [blame] | 67 | |
Matt Kraai | 3e856ce | 2000-12-01 02:55:13 +0000 | [diff] [blame] | 68 | return EXIT_SUCCESS; |
Erik Andersen | 227a59b | 2000-04-25 23:24:55 +0000 | [diff] [blame] | 69 | } |