summaryrefslogtreecommitdiff
path: root/src/mktemp.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/mktemp.c')
-rw-r--r--src/mktemp.c60
1 files changed, 24 insertions, 36 deletions
diff --git a/src/mktemp.c b/src/mktemp.c
index aef140af..2bd80bae 100644
--- a/src/mktemp.c
+++ b/src/mktemp.c
@@ -1,5 +1,5 @@
/* Create a temporary file or directory, safely.
- Copyright (C) 2007-2013 Free Software Foundation, Inc.
+ Copyright (C) 2007-2014 Free Software Foundation, Inc.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
@@ -26,7 +26,6 @@
#include "error.h"
#include "filenamecat.h"
#include "quote.h"
-#include "stdio--.h"
#include "tempname.h"
/* The official name of this program (e.g., no 'g' prefix). */
@@ -43,7 +42,6 @@ static const char *default_template = "tmp.XXXXXXXXXX";
enum
{
SUFFIX_OPTION = CHAR_MAX + 1,
- TMPDIR_OPTION
};
static struct option const longopts[] =
@@ -52,7 +50,7 @@ static struct option const longopts[] =
{"quiet", no_argument, NULL, 'q'},
{"dry-run", no_argument, NULL, 'u'},
{"suffix", required_argument, NULL, SUFFIX_OPTION},
- {"tmpdir", optional_argument, NULL, TMPDIR_OPTION},
+ {"tmpdir", optional_argument, NULL, 'p'},
{GETOPT_HELP_OPTION_DECL},
{GETOPT_VERSION_OPTION_DECL},
{NULL, 0, NULL, 0}
@@ -81,24 +79,21 @@ Files are created u+rw, and directories u+rwx, minus umask restrictions.\n\
-q, --quiet suppress diagnostics about file/dir-creation failure\n\
"), stdout);
fputs (_("\
- --suffix=SUFF append SUFF to TEMPLATE. SUFF must not contain slash.\n\
- This option is implied if TEMPLATE does not end in X.\n\
+ --suffix=SUFF append SUFF to TEMPLATE; SUFF must not contain a slash.\n\
+ This option is implied if TEMPLATE does not end in X\n\
"), stdout);
fputs (_("\
- --tmpdir[=DIR] interpret TEMPLATE relative to DIR. If DIR is not\n\
+ -p DIR, --tmpdir[=DIR] interpret TEMPLATE relative to DIR; if DIR is not\n\
specified, use $TMPDIR if set, else /tmp. With\n\
- this option, TEMPLATE must not be an absolute name.\n\
- Unlike with -t, TEMPLATE may contain slashes, but\n\
+ this option, TEMPLATE must not be an absolute name;\n\
+ unlike with -t, TEMPLATE may contain slashes, but\n\
mktemp creates only the final component\n\
"), stdout);
- fputs ("\n", stdout);
fputs (_("\
- -p DIR use DIR as a prefix; implies -t [deprecated]\n\
-t interpret TEMPLATE as a single file name component,\n\
relative to a directory: $TMPDIR, if set; else the\n\
directory specified via -p; else /tmp [deprecated]\n\
"), stdout);
- fputs ("\n", stdout);
fputs (HELP_OPTION_DESCRIPTION, stdout);
fputs (VERSION_OPTION_DESCRIPTION, stdout);
emit_ancillary_info ();
@@ -151,7 +146,7 @@ main (int argc, char **argv)
{
char const *dest_dir;
char const *dest_dir_arg = NULL;
- bool suppress_stderr = false;
+ bool suppress_file_err = false;
int c;
unsigned int n_args;
char *template;
@@ -185,7 +180,7 @@ main (int argc, char **argv)
use_dest_dir = true;
break;
case 'q':
- suppress_stderr = true;
+ suppress_file_err = true;
break;
case 't':
use_dest_dir = true;
@@ -195,11 +190,6 @@ main (int argc, char **argv)
dry_run = true;
break;
- case TMPDIR_OPTION:
- use_dest_dir = true;
- dest_dir_arg = optarg;
- break;
-
case SUFFIX_OPTION:
suffix = optarg;
break;
@@ -214,15 +204,6 @@ main (int argc, char **argv)
}
}
- if (suppress_stderr)
- {
- /* From here on, redirect stderr to /dev/null.
- A diagnostic from getopt_long, above, would still go to stderr. */
- if (!freopen ("/dev/null", "wb", stderr))
- error (EXIT_FAILURE, errno,
- _("failed to redirect stderr to /dev/null"));
- }
-
n_args = argc - optind;
if (2 <= n_args)
{
@@ -283,9 +264,12 @@ main (int argc, char **argv)
if (deprecated_t_option)
{
char *env = getenv ("TMPDIR");
- dest_dir = (env && *env
- ? env
- : (dest_dir_arg ? dest_dir_arg : "/tmp"));
+ if (env && *env)
+ dest_dir = env;
+ else if (dest_dir_arg && *dest_dir_arg)
+ dest_dir = dest_dir_arg;
+ else
+ dest_dir = "/tmp";
if (last_component (template) != template)
error (EXIT_FAILURE, 0,
@@ -323,8 +307,9 @@ main (int argc, char **argv)
int err = mkdtemp_len (dest_name, suffix_len, x_count, dry_run);
if (err != 0)
{
- error (0, errno, _("failed to create directory via template %s"),
- quote (template));
+ if (!suppress_file_err)
+ error (0, errno, _("failed to create directory via template %s"),
+ quote (template));
status = EXIT_FAILURE;
}
}
@@ -333,8 +318,9 @@ main (int argc, char **argv)
int fd = mkstemp_len (dest_name, suffix_len, x_count, dry_run);
if (fd < 0 || (!dry_run && close (fd) != 0))
{
- error (0, errno, _("failed to create file via template %s"),
- quote (template));
+ if (!suppress_file_err)
+ error (0, errno, _("failed to create file via template %s"),
+ quote (template));
status = EXIT_FAILURE;
}
}
@@ -348,7 +334,9 @@ main (int argc, char **argv)
{
int saved_errno = errno;
remove (dest_name);
- error (EXIT_FAILURE, saved_errno, _("write error"));
+ if (!suppress_file_err)
+ error (0, saved_errno, _("write error"));
+ status = EXIT_FAILURE;
}
}