1 /* Create a temporary file or directory, safely.
2 Copyright (C) 2007-2009 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/>. */
17 /* Written by Jim Meyering. */
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
)
129 char const *dest_dir
;
130 char const *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 if (!freopen ("/dev/null", "wb", stderr
))
192 error (EXIT_FAILURE
, errno
,
193 _("failed to redirect stderr to /dev/null"));
196 n_args
= argc
- optind
;
199 error (0, 0, _("too many templates"));
200 usage (EXIT_FAILURE
);
206 template = (char *) default_template
;
210 template = argv
[optind
];
213 x_count
= count_trailing_X_s (template);
215 error (EXIT_FAILURE
, 0, _("too few X's in template %s"), quote (template));
219 if (deprecated_t_option
)
221 char *env
= getenv ("TMPDIR");
222 dest_dir
= (env
&& *env
224 : (dest_dir_arg
? dest_dir_arg
: "/tmp"));
226 if (last_component (template) != template)
227 error (EXIT_FAILURE
, 0,
228 _("invalid template, %s, contains directory separator"),
233 if (dest_dir_arg
&& *dest_dir_arg
)
234 dest_dir
= dest_dir_arg
;
237 char *env
= getenv ("TMPDIR");
238 dest_dir
= (env
&& *env
? env
: "/tmp");
240 if (IS_ABSOLUTE_FILE_NAME (template))
241 error (EXIT_FAILURE
, 0,
242 _("invalid template, %s; with --tmpdir,"
243 " it may not be absolute"),
247 template = file_name_concat (dest_dir
, template, NULL
);
251 template = xstrdup (template);
254 /* Make a copy to be used in case of diagnostic, since failing
255 mkstemp may leave the buffer in an undefined state. */
256 dest_name
= xstrdup (template);
258 if (create_directory
)
260 int err
= mkdtemp_len (dest_name
, x_count
, dry_run
);
263 error (0, errno
, _("failed to create directory via template %s"),
265 status
= EXIT_FAILURE
;
270 int fd
= mkstemp_len (dest_name
, x_count
, dry_run
);
271 if (fd
< 0 || (!dry_run
&& close (fd
) != 0))
273 error (0, errno
, _("failed to create file via template %s"),
275 status
= EXIT_FAILURE
;
279 if (status
== EXIT_SUCCESS
)