1 /* Create a temporary file or directory, safely.
2 Copyright (C) 2007 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, or (at your option)
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, write to the Free Software Foundation,
16 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
22 #include <sys/types.h>
28 #include "filenamecat.h"
29 #include "long-options.h"
33 /* The official name of this program (e.g., no `g' prefix). */
34 #define PROGRAM_NAME "mktemp"
36 #define AUTHORS "Jim Meyering"
38 static const char *default_template
= "tmp.XXXXXXXXXX";
40 /* The name this program was run with. */
43 /* For long options that have no equivalent short option, use a
44 non-character as a pseudo short option, starting with CHAR_MAX + 1. */
47 TMPDIR_OPTION
= CHAR_MAX
+ 1
50 static struct option
const longopts
[] =
52 {"directory", no_argument
, NULL
, 'd'},
53 {"quiet", no_argument
, NULL
, 'q'},
54 {"dry-run", no_argument
, NULL
, 'u'},
55 {"tmpdir", optional_argument
, NULL
, TMPDIR_OPTION
},
56 {GETOPT_HELP_OPTION_DECL
},
57 {GETOPT_VERSION_OPTION_DECL
},
64 if (status
!= EXIT_SUCCESS
)
65 fprintf (stderr
, _("Try `%s --help' for more information.\n"),
69 printf (_("Usage: %s [OPTION]... [TEMPLATE]\n"), program_name
);
71 Create a temporary file or directory, safely, and print its name.\n\
72 If TEMPLATE is not specified, use tmp.XXXXXXXXXX.\n\
76 -d, --directory create a directory, not a file\n\
79 -q, --quiet suppress diagnostics about file/dir-creation failure\n\
82 -u, --dry-run do not create anything; merely print a name (unsafe)\n\
85 --tmpdir[=DIR] interpret TEMPLATE relative to DIR. If DIR is\n\
86 not specified, use $TMPDIR if set, else /tmp.\n\
87 With this option, TEMPLATE must not be an absolute name.\n\
88 Unlike with -t, TEMPLATE may contain slashes, but even\n\
89 here, mktemp still creates only the final component.\n\
93 -p DIR use DIR as a prefix; implies -t [deprecated]\n\
96 -t interpret TEMPLATE as a single file name component,\n\
97 relative to a directory: $TMPDIR, if set; else the\n\
98 directory specified via -p; else /tmp [deprecated]\n\
100 fputs ("\n", stdout
);
101 fputs (HELP_OPTION_DESCRIPTION
, stdout
);
102 fputs (VERSION_OPTION_DESCRIPTION
, stdout
);
103 emit_bug_reporting_address ();
110 count_trailing_X_s (const char *s
)
112 size_t len
= strlen (s
);
114 for ( ; len
&& s
[len
-1] == 'X'; len
--)
120 mkstemp_len (char *tmpl
, size_t suff_len
, bool dry_run
)
122 return gen_tempname_len (tmpl
, dry_run
? GT_NOCREATE
: GT_FILE
, suff_len
);
126 mkdtemp_len (char *tmpl
, size_t suff_len
, bool dry_run
)
128 return gen_tempname_len (tmpl
, dry_run
? GT_NOCREATE
: GT_DIR
, suff_len
);
132 main (int argc
, char **argv
)
135 char *dest_dir_arg
= NULL
;
136 bool suppress_stderr
= false;
140 bool use_dest_dir
= false;
141 bool deprecated_t_option
= false;
142 bool create_directory
= false;
143 bool dry_run
= false;
144 int status
= EXIT_SUCCESS
;
148 initialize_main (&argc
, &argv
);
149 program_name
= argv
[0];
150 setlocale (LC_ALL
, "");
151 bindtextdomain (PACKAGE
, LOCALEDIR
);
152 textdomain (PACKAGE
);
154 atexit (close_stdout
);
156 while ((c
= getopt_long (argc
, argv
, "dp:qtuV", longopts
, NULL
)) != -1)
161 create_directory
= true;
164 dest_dir_arg
= optarg
;
168 suppress_stderr
= true;
172 deprecated_t_option
= true;
180 dest_dir_arg
= optarg
;
183 case_GETOPT_HELP_CHAR
;
186 case_GETOPT_VERSION_CHAR (PROGRAM_NAME
, AUTHORS
);
188 usage (EXIT_FAILURE
);
194 /* From here on, redirect stderr to /dev/null.
195 A diagnostic from getopt_long, above, would still go to stderr. */
196 freopen ("/dev/null", "wb", stderr
);
199 n_args
= argc
- optind
;
202 error (0, 0, _("too many templates"));
203 usage (EXIT_FAILURE
);
209 template = (char *) default_template
;
213 template = argv
[optind
];
216 x_count
= count_trailing_X_s (template);
218 error (EXIT_FAILURE
, 0, _("too few X's in template %s"), quote (template));
222 if (deprecated_t_option
)
224 char *env
= getenv ("TMPDIR");
225 dest_dir
= (env
&& *env
227 : (dest_dir_arg
? dest_dir_arg
: "/tmp"));
229 if (last_component (template) != template)
230 error (EXIT_FAILURE
, 0,
231 _("invalid template, %s, contains directory separator"),
236 if (dest_dir_arg
&& *dest_dir_arg
)
237 dest_dir
= dest_dir_arg
;
240 char *env
= getenv ("TMPDIR");
241 dest_dir
= (env
&& *env
? env
: "/tmp");
243 if (IS_ABSOLUTE_FILE_NAME (template))
244 error (EXIT_FAILURE
, 0,
245 _("invalid template, %s; with --tmpdir,"
246 " it may not be absolute"),
250 dest_name
= file_name_concat (dest_dir
, template, NULL
);
254 dest_name
= xstrdup (template);
257 if (create_directory
)
259 int err
= mkdtemp_len (dest_name
, x_count
, dry_run
);
262 error (0, errno
, _("failed to create directory via template %s"),
264 status
= EXIT_FAILURE
;
269 int fd
= mkstemp_len (dest_name
, x_count
, dry_run
);
270 if (fd
< 0 || (!dry_run
&& close (fd
) != 0))
272 error (0, errno
, _("failed to create file via template %s"),
274 status
= EXIT_FAILURE
;
278 if (status
== EXIT_SUCCESS
)
290 * indent-tabs-mode: nil