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 | 06af216 | 2007-02-03 17:28:39 +0000 | [diff] [blame] | 14 | int mktemp_main(int argc, char **argv); |
Rob Landley | dfba741 | 2006-03-06 20:47:33 +0000 | [diff] [blame] | 15 | int mktemp_main(int argc, char **argv) |
Erik Andersen | 227a59b | 2000-04-25 23:24:55 +0000 | [diff] [blame] | 16 | { |
Denis Vlasenko | fe7cd64 | 2007-08-18 15:32:12 +0000 | [diff] [blame] | 17 | unsigned long flags = getopt32(argv, "dqt"); |
Bernhard Reutner-Fischer | 1ac42bf | 2006-10-10 15:28:41 +0000 | [diff] [blame] | 18 | char *chp; |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 19 | |
Rob Landley | b5ca9e0 | 2005-12-02 17:54:01 +0000 | [diff] [blame] | 20 | if (optind + 1 != argc) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 21 | bb_show_usage(); |
Rob Landley | b5ca9e0 | 2005-12-02 17:54:01 +0000 | [diff] [blame] | 22 | |
Bernhard Reutner-Fischer | 1ac42bf | 2006-10-10 15:28:41 +0000 | [diff] [blame] | 23 | chp = argv[optind]; |
24 | |||||
25 | if (flags & 4) { | ||||
26 | char *dir = getenv("TMPDIR"); | ||||
27 | if (dir && *dir != '\0') | ||||
28 | chp = concat_path_file(dir, chp); | ||||
29 | else | ||||
30 | chp = concat_path_file("/tmp/", chp); | ||||
Rob Landley | b5ca9e0 | 2005-12-02 17:54:01 +0000 | [diff] [blame] | 31 | } |
Bernhard Reutner-Fischer | 1ac42bf | 2006-10-10 15:28:41 +0000 | [diff] [blame] | 32 | |
33 | if (flags & 1) { | ||||
34 | if (mkdtemp(chp) == NULL) | ||||
35 | return EXIT_FAILURE; | ||||
36 | } else { | ||||
37 | if (mkstemp(chp) < 0) | ||||
Rob Landley | b5ca9e0 | 2005-12-02 17:54:01 +0000 | [diff] [blame] | 38 | return EXIT_FAILURE; |
Glenn L McGrath | 69f28e7 | 2003-04-26 04:56:17 +0000 | [diff] [blame] | 39 | } |
40 | |||||
Bernhard Reutner-Fischer | 1ac42bf | 2006-10-10 15:28:41 +0000 | [diff] [blame] | 41 | puts(chp); |
Glenn L McGrath | 69f28e7 | 2003-04-26 04:56:17 +0000 | [diff] [blame] | 42 | |
Matt Kraai | 3e856ce | 2000-12-01 02:55:13 +0000 | [diff] [blame] | 43 | return EXIT_SUCCESS; |
Erik Andersen | 227a59b | 2000-04-25 23:24:55 +0000 | [diff] [blame] | 44 | } |