1 /* Create a temporary file or directory, safely.
2 Copyright (C) 2007-2008 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21 #include <sys/types.h>
27 #include "filenamecat.h"
31 /* The official name of this program (e.g., no `g' prefix). */
32 #define PROGRAM_NAME "mktemp"
34 #define AUTHORS proper_name ("Jim Meyering")
36 static const char *default_template = "tmp.XXXXXXXXXX";
38 /* For long options that have no equivalent short option, use a
39 non-character as a pseudo short option, starting with CHAR_MAX + 1. */
42 TMPDIR_OPTION = CHAR_MAX + 1
45 static struct option const longopts[] =
47 {"directory", no_argument, NULL, 'd'},
48 {"quiet", no_argument, NULL, 'q'},
49 {"dry-run", no_argument, NULL, 'u'},
50 {"tmpdir", optional_argument, NULL, TMPDIR_OPTION},
51 {GETOPT_HELP_OPTION_DECL},
52 {GETOPT_VERSION_OPTION_DECL},
59 if (status != EXIT_SUCCESS)
60 fprintf (stderr, _("Try `%s --help' for more information.\n"),
64 printf (_("Usage: %s [OPTION]... [TEMPLATE]\n"), program_name);
66 Create a temporary file or directory, safely, and print its name.\n\
67 If TEMPLATE is not specified, use tmp.XXXXXXXXXX.\n\
71 -d, --directory create a directory, not a file\n\
74 -q, --quiet suppress diagnostics about file/dir-creation failure\n\
77 -u, --dry-run do not create anything; merely print a name (unsafe)\n\
80 --tmpdir[=DIR] interpret TEMPLATE relative to DIR. If DIR is\n\
81 not specified, use $TMPDIR if set, else /tmp.\n\
82 With this option, TEMPLATE must not be an absolute name.\n\
83 Unlike with -t, TEMPLATE may contain slashes, but even\n\
84 here, mktemp still creates only the final component.\n\
88 -p DIR use DIR as a prefix; implies -t [deprecated]\n\
91 -t interpret TEMPLATE as a single file name component,\n\
92 relative to a directory: $TMPDIR, if set; else the\n\
93 directory specified via -p; else /tmp [deprecated]\n\
96 fputs (HELP_OPTION_DESCRIPTION, stdout);
97 fputs (VERSION_OPTION_DESCRIPTION, stdout);
98 emit_bug_reporting_address ();
105 count_trailing_X_s (const char *s)
107 size_t len = strlen (s);
109 for ( ; len && s[len-1] == 'X'; len--)
115 mkstemp_len (char *tmpl, size_t suff_len, bool dry_run)
117 return gen_tempname_len (tmpl, dry_run ? GT_NOCREATE : GT_FILE, suff_len);
121 mkdtemp_len (char *tmpl, size_t suff_len, bool dry_run)
123 return gen_tempname_len (tmpl, dry_run ? GT_NOCREATE : GT_DIR, suff_len);
127 main (int argc, char **argv)
130 char *dest_dir_arg = NULL;
131 bool suppress_stderr = false;
135 bool use_dest_dir = false;
136 bool deprecated_t_option = false;
137 bool create_directory = false;
138 bool dry_run = false;
139 int status = EXIT_SUCCESS;
143 initialize_main (&argc, &argv);
144 set_program_name (argv[0]);
145 setlocale (LC_ALL, "");
146 bindtextdomain (PACKAGE, LOCALEDIR);
147 textdomain (PACKAGE);
149 atexit (close_stdout);
151 while ((c = getopt_long (argc, argv, "dp:qtuV", longopts, NULL)) != -1)
156 create_directory = true;
159 dest_dir_arg = optarg;
163 suppress_stderr = true;
167 deprecated_t_option = true;
175 dest_dir_arg = optarg;
178 case_GETOPT_HELP_CHAR;
181 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
183 usage (EXIT_FAILURE);
189 /* From here on, redirect stderr to /dev/null.
190 A diagnostic from getopt_long, above, would still go to stderr. */
191 freopen ("/dev/null", "wb", stderr);
194 n_args = argc - optind;
197 error (0, 0, _("too many templates"));
198 usage (EXIT_FAILURE);
204 template = (char *) default_template;
208 template = argv[optind];
211 x_count = count_trailing_X_s (template);
213 error (EXIT_FAILURE, 0, _("too few X's in template %s"), quote (template));
217 if (deprecated_t_option)
219 char *env = getenv ("TMPDIR");
220 dest_dir = (env && *env
222 : (dest_dir_arg ? dest_dir_arg : "/tmp"));
224 if (last_component (template) != template)
225 error (EXIT_FAILURE, 0,
226 _("invalid template, %s, contains directory separator"),
231 if (dest_dir_arg && *dest_dir_arg)
232 dest_dir = dest_dir_arg;
235 char *env = getenv ("TMPDIR");
236 dest_dir = (env && *env ? env : "/tmp");
238 if (IS_ABSOLUTE_FILE_NAME (template))
239 error (EXIT_FAILURE, 0,
240 _("invalid template, %s; with --tmpdir,"
241 " it may not be absolute"),
245 dest_name = file_name_concat (dest_dir, template, NULL);
249 dest_name = xstrdup (template);
252 if (create_directory)
254 int err = mkdtemp_len (dest_name, x_count, dry_run);
257 error (0, errno, _("failed to create directory via template %s"),
259 status = EXIT_FAILURE;
264 int fd = mkstemp_len (dest_name, x_count, dry_run);
265 if (fd < 0 || (!dry_run && close (fd) != 0))
267 error (0, errno, _("failed to create file via template %s"),
269 status = EXIT_FAILURE;
273 if (status == EXIT_SUCCESS)
285 * indent-tabs-mode: nil