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