mktemp: rearrange --help output
[coreutils.git] / src / mktemp.c
blobe967d8eca8433c5970a9a8052bfba73d882a8f79
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. */
19 #include <config.h>
20 #include <stdio.h>
21 #include <sys/types.h>
22 #include <getopt.h>
24 #include "system.h"
26 #include "close-stream.h"
27 #include "error.h"
28 #include "filenamecat.h"
29 #include "quote.h"
30 #include "tempname.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. */
41 enum
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},
54 {NULL, 0, NULL, 0}
57 void
58 usage (int status)
60 if (status != EXIT_SUCCESS)
61 fprintf (stderr, _("Try `%s --help' for more information.\n"),
62 program_name);
63 else
65 printf (_("Usage: %s [OPTION]... [TEMPLATE]\n"), program_name);
66 fputs (_("\
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\
70 "), stdout);
71 fputs ("\n", stdout);
72 fputs (_("\
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\
76 "), stdout);
77 fputs (_("\
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\
83 "), stdout);
84 fputs ("\n", stdout);
85 fputs (_("\
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\
90 "), stdout);
91 fputs ("\n", stdout);
92 fputs (HELP_OPTION_DESCRIPTION, stdout);
93 fputs (VERSION_OPTION_DESCRIPTION, stdout);
94 emit_ancillary_info ();
97 exit (status);
100 static size_t
101 count_trailing_X_s (const char *s)
103 size_t len = strlen (s);
104 size_t n = 0;
105 for ( ; len && s[len-1] == 'X'; len--)
106 ++n;
107 return n;
110 static int
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);
116 static int
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;
128 int c;
129 unsigned int n_args;
130 char *template;
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;
136 size_t x_count;
137 char *dest_name;
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)
149 switch (c)
151 case 'd':
152 create_directory = true;
153 break;
154 case 'p':
155 dest_dir_arg = optarg;
156 use_dest_dir = true;
157 break;
158 case 'q':
159 suppress_stderr = true;
160 break;
161 case 't':
162 use_dest_dir = true;
163 deprecated_t_option = true;
164 break;
165 case 'u':
166 dry_run = true;
167 break;
169 case TMPDIR_OPTION:
170 use_dest_dir = true;
171 dest_dir_arg = optarg;
172 break;
174 case_GETOPT_HELP_CHAR;
176 case 'V': /* Undocumented alias. FIXME: remove in 2011. */
177 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
178 default:
179 usage (EXIT_FAILURE);
183 if (suppress_stderr)
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;
193 if (2 <= n_args)
195 error (0, 0, _("too many templates"));
196 usage (EXIT_FAILURE);
199 if (n_args == 0)
201 use_dest_dir = true;
202 template = (char *) default_template;
204 else
206 template = argv[optind];
209 x_count = count_trailing_X_s (template);
210 if (x_count < 3)
211 error (EXIT_FAILURE, 0, _("too few X's in template %s"), quote (template));
213 if (use_dest_dir)
215 if (deprecated_t_option)
217 char *env = getenv ("TMPDIR");
218 dest_dir = (env && *env
219 ? 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"),
225 quote (template));
227 else
229 if (dest_dir_arg && *dest_dir_arg)
230 dest_dir = dest_dir_arg;
231 else
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"),
240 quote (template));
243 template = file_name_concat (dest_dir, template, NULL);
245 else
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);
257 if (err != 0)
259 error (0, errno, _("failed to create directory via template %s"),
260 quote (template));
261 status = EXIT_FAILURE;
264 else
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"),
270 quote (template));
271 status = EXIT_FAILURE;
275 if (status == EXIT_SUCCESS)
277 puts (dest_name);
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;
283 remove (dest_name);
284 error (EXIT_FAILURE, saved_errno, _("write error"));
288 #ifdef lint
289 free (dest_name);
290 free (template);
291 #endif
293 exit (status);