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>
26 #include "close-stream.h"
28 #include "filenamecat.h"
32 /* The official name of this program (e.g., no `g' prefix). */
33 #define PROGRAM_NAME "mktemp"
35 #define AUTHORS proper_name ("Jim Meyering")
37 static const char *default_template
= "tmp.XXXXXXXXXX";
39 /* For long options that have no equivalent short option, use a
40 non-character as a pseudo short option, starting with CHAR_MAX + 1. */
43 TMPDIR_OPTION
= CHAR_MAX
+ 1
46 static struct option
const longopts
[] =
48 {"directory", no_argument
, NULL
, 'd'},
49 {"quiet", no_argument
, NULL
, 'q'},
50 {"dry-run", no_argument
, NULL
, 'u'},
51 {"tmpdir", optional_argument
, NULL
, TMPDIR_OPTION
},
52 {GETOPT_HELP_OPTION_DECL
},
53 {GETOPT_VERSION_OPTION_DECL
},
60 if (status
!= EXIT_SUCCESS
)
61 fprintf (stderr
, _("Try `%s --help' for more information.\n"),
65 printf (_("Usage: %s [OPTION]... [TEMPLATE]\n"), program_name
);
67 Create a temporary file or directory, safely, and print its name.\n\
68 TEMPLATE must end in at least 3 consecutive `X's.\n\
69 If TEMPLATE is not specified, use tmp.XXXXXXXXXX, and --tmpdir is implied.\n\
73 -d, --directory create a directory, not a file\n\
74 -u, --dry-run do not create anything; merely print a name (unsafe)\n\
75 -q, --quiet suppress diagnostics about file/dir-creation failure\n\
78 --tmpdir[=DIR] interpret TEMPLATE relative to DIR. If DIR is not\n\
79 specified, use $TMPDIR if set, else /tmp. With\n\
80 this option, TEMPLATE must not be an absolute name.\n\
81 Unlike with -t, TEMPLATE may contain slashes, but\n\
82 mktemp creates only the final component.\n\
86 -p DIR use DIR as a prefix; implies -t [deprecated]\n\
87 -t interpret TEMPLATE as a single file name component,\n\
88 relative to a directory: $TMPDIR, if set; else the\n\
89 directory specified via -p; else /tmp [deprecated]\n\
92 fputs (HELP_OPTION_DESCRIPTION
, stdout
);
93 fputs (VERSION_OPTION_DESCRIPTION
, stdout
);
94 emit_ancillary_info ();
101 count_trailing_X_s (const char *s
)
103 size_t len
= strlen (s
);
105 for ( ; len
&& s
[len
-1] == 'X'; len
--)
111 mkstemp_len (char *tmpl
, size_t suff_len
, bool dry_run
)
113 return gen_tempname_len (tmpl
, 0, dry_run
? GT_NOCREATE
: GT_FILE
, suff_len
);
117 mkdtemp_len (char *tmpl
, size_t suff_len
, bool dry_run
)
119 return gen_tempname_len (tmpl
, 0, dry_run
? GT_NOCREATE
: GT_DIR
, suff_len
);
123 main (int argc
, char **argv
)
125 char const *dest_dir
;
126 char const *dest_dir_arg
= NULL
;
127 bool suppress_stderr
= false;
131 bool use_dest_dir
= false;
132 bool deprecated_t_option
= false;
133 bool create_directory
= false;
134 bool dry_run
= false;
135 int status
= EXIT_SUCCESS
;
139 initialize_main (&argc
, &argv
);
140 set_program_name (argv
[0]);
141 setlocale (LC_ALL
, "");
142 bindtextdomain (PACKAGE
, LOCALEDIR
);
143 textdomain (PACKAGE
);
145 atexit (close_stdout
);
147 while ((c
= getopt_long (argc
, argv
, "dp:qtuV", longopts
, NULL
)) != -1)
152 create_directory
= true;
155 dest_dir_arg
= optarg
;
159 suppress_stderr
= true;
163 deprecated_t_option
= true;
171 dest_dir_arg
= optarg
;
174 case_GETOPT_HELP_CHAR
;
176 case 'V': /* Undocumented alias. FIXME: remove in 2011. */
177 case_GETOPT_VERSION_CHAR (PROGRAM_NAME
, AUTHORS
);
179 usage (EXIT_FAILURE
);
185 /* From here on, redirect stderr to /dev/null.
186 A diagnostic from getopt_long, above, would still go to stderr. */
187 if (!freopen ("/dev/null", "wb", stderr
))
188 error (EXIT_FAILURE
, errno
,
189 _("failed to redirect stderr to /dev/null"));
192 n_args
= argc
- optind
;
195 error (0, 0, _("too many templates"));
196 usage (EXIT_FAILURE
);
202 template = (char *) default_template
;
206 template = argv
[optind
];
209 x_count
= count_trailing_X_s (template);
211 error (EXIT_FAILURE
, 0, _("too few X's in template %s"), quote (template));
215 if (deprecated_t_option
)
217 char *env
= getenv ("TMPDIR");
218 dest_dir
= (env
&& *env
220 : (dest_dir_arg
? dest_dir_arg
: "/tmp"));
222 if (last_component (template) != template)
223 error (EXIT_FAILURE
, 0,
224 _("invalid template, %s, contains directory separator"),
229 if (dest_dir_arg
&& *dest_dir_arg
)
230 dest_dir
= dest_dir_arg
;
233 char *env
= getenv ("TMPDIR");
234 dest_dir
= (env
&& *env
? env
: "/tmp");
236 if (IS_ABSOLUTE_FILE_NAME (template))
237 error (EXIT_FAILURE
, 0,
238 _("invalid template, %s; with --tmpdir,"
239 " it may not be absolute"),
243 template = file_name_concat (dest_dir
, template, NULL
);
247 template = xstrdup (template);
250 /* Make a copy to be used in case of diagnostic, since failing
251 mkstemp may leave the buffer in an undefined state. */
252 dest_name
= xstrdup (template);
254 if (create_directory
)
256 int err
= mkdtemp_len (dest_name
, x_count
, dry_run
);
259 error (0, errno
, _("failed to create directory via template %s"),
261 status
= EXIT_FAILURE
;
266 int fd
= mkstemp_len (dest_name
, x_count
, dry_run
);
267 if (fd
< 0 || (!dry_run
&& close (fd
) != 0))
269 error (0, errno
, _("failed to create file via template %s"),
271 status
= EXIT_FAILURE
;
275 if (status
== EXIT_SUCCESS
)
278 /* If we created a file, but then failed to output the file
279 name, we should clean up the mess before failing. */
280 if (!dry_run
&& close_stream (stdout
))
282 int saved_errno
= errno
;
284 error (EXIT_FAILURE
, saved_errno
, _("write error"));