Changes to update Tomato RAF.
[tomato.git] / release / src / router / busybox / debianutils / mktemp.c
blobdbe43095551563864db79759fdb9b9ae9331bf16
1 /* vi: set sw=4 ts=4: */
2 /*
3 * Mini mktemp implementation for busybox
6 * Copyright (C) 2000 by Daniel Jacobowitz
7 * Written by Daniel Jacobowitz <dan@debian.org>
9 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
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]
34 //usage:#define mktemp_trivial_usage
35 //usage: "[-dt] [-p DIR] [TEMPLATE]"
36 //usage:#define mktemp_full_usage "\n\n"
37 //usage: "Create a temporary file with name based on TEMPLATE and print its name.\n"
38 //usage: "TEMPLATE must end with XXXXXX (e.g. [/dir/]nameXXXXXX).\n"
39 //usage: "Without TEMPLATE, -t tmp.XXXXXX is assumed.\n"
40 //usage: "\n -d Make directory, not file"
41 ////usage: "\n -q Fail silently on errors" - we ignore this opt
42 //usage: "\n -t Prepend base directory name to TEMPLATE"
43 //usage: "\n -p DIR Use DIR as a base directory (implies -t)"
44 //usage: "\n -u Do not create anything; print a name"
45 //usage: "\n"
46 //usage: "\nBase directory is: -p DIR, else $TMPDIR, else /tmp"
47 //usage:
48 //usage:#define mktemp_example_usage
49 //usage: "$ mktemp /tmp/temp.XXXXXX\n"
50 //usage: "/tmp/temp.mWiLjM\n"
51 //usage: "$ ls -la /tmp/temp.mWiLjM\n"
52 //usage: "-rw------- 1 andersen andersen 0 Apr 25 17:10 /tmp/temp.mWiLjM\n"
54 #include "libbb.h"
56 int mktemp_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
57 int mktemp_main(int argc UNUSED_PARAM, char **argv)
59 const char *path;
60 char *chp;
61 unsigned opts;
62 enum {
63 OPT_d = 1 << 0,
64 OPT_q = 1 << 1,
65 OPT_t = 1 << 2,
66 OPT_p = 1 << 3,
67 OPT_u = 1 << 4,
70 path = getenv("TMPDIR");
71 if (!path || path[0] == '\0')
72 path = "/tmp";
74 /* -q is ignored */
75 opt_complementary = "?1"; /* 1 argument max */
76 opts = getopt32(argv, "dqtp:u", &path);
78 chp = argv[optind];
79 if (!chp) {
80 /* GNU coreutils 8.4:
81 * bare "mktemp" -> "mktemp -t tmp.XXXXXX"
83 chp = xstrdup("tmp.XXXXXX");
84 opts |= OPT_t;
87 if (opts & OPT_u) {
88 /* Remove (up to) 6 X's */
89 unsigned len = strlen(chp);
90 int cnt = len > 6 ? 6 : len;
91 while (--cnt >= 0 && chp[--len] == 'X')
92 chp[len] = '\0';
94 chp = tempnam(opts & (OPT_t|OPT_p) ? path : "./", chp);
95 if (!chp)
96 return EXIT_FAILURE;
97 if (!(opts & (OPT_t|OPT_p)))
98 chp += 2;
99 goto ret;
102 if (opts & (OPT_t|OPT_p))
103 chp = concat_path_file(path, chp);
105 if (opts & OPT_d) {
106 if (mkdtemp(chp) == NULL)
107 return EXIT_FAILURE;
108 } else {
109 if (mkstemp(chp) < 0)
110 return EXIT_FAILURE;
112 ret:
113 puts(chp);
114 return EXIT_SUCCESS;