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 | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 12 | #include "libbb.h" |
Erik Andersen | 227a59b | 2000-04-25 23:24:55 +0000 | [diff] [blame] | 13 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 14 | int mktemp_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame^] | 15 | int mktemp_main(int argc ATTRIBUTE_UNUSED, char **argv) |
Erik Andersen | 227a59b | 2000-04-25 23:24:55 +0000 | [diff] [blame] | 16 | { |
Denis Vlasenko | 65581f3 | 2008-02-09 06:26:53 +0000 | [diff] [blame] | 17 | // -d Make a directory instead of a file |
| 18 | // -q Fail silently if an error occurs [bbox: ignored] |
| 19 | // -t Generate a path rooted in temporary directory |
| 20 | // -p DIR Use DIR as a temporary directory (implies -t) |
| 21 | const char *path; |
Bernhard Reutner-Fischer | 1ac42bf | 2006-10-10 15:28:41 +0000 | [diff] [blame] | 22 | char *chp; |
Denis Vlasenko | 65581f3 | 2008-02-09 06:26:53 +0000 | [diff] [blame] | 23 | unsigned flags; |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 24 | |
Denis Vlasenko | 65581f3 | 2008-02-09 06:26:53 +0000 | [diff] [blame] | 25 | opt_complementary = "=1"; /* exactly one arg */ |
| 26 | flags = getopt32(argv, "dqtp:", &path); |
Bernhard Reutner-Fischer | 1ac42bf | 2006-10-10 15:28:41 +0000 | [diff] [blame] | 27 | chp = argv[optind]; |
| 28 | |
Denis Vlasenko | 65581f3 | 2008-02-09 06:26:53 +0000 | [diff] [blame] | 29 | if (flags & (4|8)) { /* -t and/or -p */ |
| 30 | const char *dir = getenv("TMPDIR"); |
Bernhard Reutner-Fischer | 1ac42bf | 2006-10-10 15:28:41 +0000 | [diff] [blame] | 31 | if (dir && *dir != '\0') |
Denis Vlasenko | 65581f3 | 2008-02-09 06:26:53 +0000 | [diff] [blame] | 32 | path = dir; |
| 33 | else if (!(flags & 8)) /* No -p */ |
| 34 | path = "/tmp/"; |
| 35 | /* else path comes from -p DIR */ |
| 36 | chp = concat_path_file(path, chp); |
Rob Landley | b5ca9e0 | 2005-12-02 17:54:01 +0000 | [diff] [blame] | 37 | } |
Bernhard Reutner-Fischer | 1ac42bf | 2006-10-10 15:28:41 +0000 | [diff] [blame] | 38 | |
Denis Vlasenko | 65581f3 | 2008-02-09 06:26:53 +0000 | [diff] [blame] | 39 | if (flags & 1) { /* -d */ |
Bernhard Reutner-Fischer | 1ac42bf | 2006-10-10 15:28:41 +0000 | [diff] [blame] | 40 | if (mkdtemp(chp) == NULL) |
| 41 | return EXIT_FAILURE; |
| 42 | } else { |
| 43 | if (mkstemp(chp) < 0) |
Rob Landley | b5ca9e0 | 2005-12-02 17:54:01 +0000 | [diff] [blame] | 44 | return EXIT_FAILURE; |
Glenn L McGrath | 69f28e7 | 2003-04-26 04:56:17 +0000 | [diff] [blame] | 45 | } |
| 46 | |
Bernhard Reutner-Fischer | 1ac42bf | 2006-10-10 15:28:41 +0000 | [diff] [blame] | 47 | puts(chp); |
Glenn L McGrath | 69f28e7 | 2003-04-26 04:56:17 +0000 | [diff] [blame] | 48 | |
Matt Kraai | 3e856ce | 2000-12-01 02:55:13 +0000 | [diff] [blame] | 49 | return EXIT_SUCCESS; |
Erik Andersen | 227a59b | 2000-04-25 23:24:55 +0000 | [diff] [blame] | 50 | } |