From: Reuben Thomas Date: Mon, 22 Jan 2018 20:42:18 +0000 (+0000) Subject: Use mkstemps to make temporary file names X-Git-Tag: v3.7~78 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=3a6467dd49cbb27d742a40234bd65c4fbbd2e521;p=recode Use mkstemps to make temporary file names Remove system-specific code. --- diff --git a/bootstrap.conf b/bootstrap.conf index 8349979..ac091ca 100644 --- a/bootstrap.conf +++ b/bootstrap.conf @@ -50,6 +50,7 @@ gnulib_modules=' argmatch bootstrap canonicalize-lgpl + dirname error getopt-posix gettext-h @@ -57,6 +58,7 @@ gnulib_modules=' isatty localcharset manywarnings + mkstemps pathmax pipe-posix quotearg @@ -65,6 +67,7 @@ gnulib_modules=' sys_wait unistd utime + vasprintf vprintf-posix xalloc xbinary-io diff --git a/lib/.gitignore b/lib/.gitignore index 82ad8fb..c849c28 100644 --- a/lib/.gitignore +++ b/lib/.gitignore @@ -166,3 +166,17 @@ /isatty.c /xbinary-io.c /xbinary-io.h +/basename.c +/dirname.c +/gettimeofday.c +/localtime-buffer.c +/localtime-buffer.h +/mkstemps.c +/sys_time.in.h +/sys/time.h +/tempname.c +/tempname.h +/xstrndup.c +/xstrndup.h +/asprintf.c +/vasprintf.c diff --git a/m4/.gitignore b/m4/.gitignore index 78a5c84..b18525e 100644 --- a/m4/.gitignore +++ b/m4/.gitignore @@ -131,3 +131,10 @@ gnulib-comp.m4 /sys_stat_h.m4 /fcntl_h.m4 /isatty.m4 +/gettimeofday.m4 +/localtime-buffer.m4 +/mkstemps.m4 +/sys_time_h.m4 +/tempname.m4 +/xstrndup.m4 +/vasprintf.m4 diff --git a/src/common.h b/src/common.h index 247ca55..510d091 100644 --- a/src/common.h +++ b/src/common.h @@ -20,10 +20,6 @@ #include "config.h" #include "cleaner.h" -#if MSDOS || WIN32 || _WIN32 || OS2 -# define DOSWIN_OR_OS2 1 -#endif - #include #include #include diff --git a/src/main.c b/src/main.c index 79097ff..cd286a6 100644 --- a/src/main.c +++ b/src/main.c @@ -19,10 +19,12 @@ #include "common.h" #include +#include #include #include #include #include +#include #include #include @@ -805,7 +807,6 @@ warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n"), for (; optind < argc; optind++) { const char *input_name; - char *output_name; FILE *file; struct stat file_stat; struct utimbuf file_utime; @@ -814,8 +815,6 @@ warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n"), if (input_name == NULL) error (EXIT_FAILURE, errno, "realpath (%s)", argv[optind]); - output_name = xmalloc (strlen (input_name) + 17 + 1); /* 17 is upper limit for rec%d.tmp where %d is pid_t */ - /* Check if the file can be read and rewritten. */ if (file = fopen (input_name, "r+"), file == NULL) @@ -826,39 +825,24 @@ warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n"), fstat (fileno (file), &file_stat); fclose (file); - { - char *cursor; - - /* Choose an output file in the same directory. */ - - /* FIXME: Scott Schwartz writes: - "There's no reason to think that that name is unique." */ - - strcpy (output_name, input_name); -#if DOSWIN_OR_OS2 - for (cursor = output_name + strlen (output_name); - cursor > output_name && cursor[-1] != '/' - && cursor[-1] != '\\' && cursor[-1] != ':'; - cursor--) - ; -# if __DJGPP__ - sprintf (cursor, "rec%d.tmp", getpid ()); -# else - strcpy (cursor, "recodeXX.TMP"); -# endif -#else - for (cursor = output_name + strlen (output_name); - cursor > output_name && cursor[-1] != '/'; - cursor--) - ; - sprintf (cursor, "rec%d.tmp", getpid ()); -#endif - } + /* Choose an output file in the same directory. */ + + char *input_name_copy = xstrdup (input_name); + char *output_dir = dirname (input_name_copy); + char *output_name; + if (asprintf (&output_name, "%s/recode-XXXXXX.tmp", output_dir) == -1) + error (EXIT_FAILURE, errno, "asprintf"); + int fd = mkstemps (output_name, 4); + if (fd == -1) + error (EXIT_FAILURE, errno, "mkstemps (%s)", output_name); /* Recode the file. */ task->input.name = input_name; - task->output.name = output_name; + task->output.name = NULL; + task->output.file = fdopen (fd, "w+"); + if (task->output.file == NULL) + error (EXIT_FAILURE, errno, "fdopen ()"); if (verbose_flag) { @@ -920,6 +904,7 @@ warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n"), unlink (output_name); } free (output_name); + free (input_name_copy); } } else