]> granicus.if.org Git - recode/commitdiff
Use mkstemps to make temporary file names
authorReuben Thomas <rrt@sc3d.org>
Mon, 22 Jan 2018 20:42:18 +0000 (20:42 +0000)
committerReuben Thomas <rrt@sc3d.org>
Tue, 23 Jan 2018 07:16:10 +0000 (07:16 +0000)
Remove system-specific code.

bootstrap.conf
lib/.gitignore
m4/.gitignore
src/common.h
src/main.c

index 83499795e51b5900647c44cb25eee873df6d2549..ac091caeddc68b709a2dc74eec9b74311092f6b3 100644 (file)
@@ -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
index 82ad8fbe354b265413705946abe084550c5b811a..c849c28e0a2b62df5c4d5d327ab42c550def001c 100644 (file)
 /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
index 78a5c845c9d0b594a522c206aaab2a17bbe52d33..b18525e14f040a54496a40629549b564e0afa79b 100644 (file)
@@ -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
index 247ca554fb440acc8b37a967ced28cb7915c0f17..510d091e94e890c06ab25e172f8870bccce67463 100644 (file)
 #include "config.h"
 #include "cleaner.h"
 
-#if MSDOS || WIN32 || _WIN32 || OS2
-# define DOSWIN_OR_OS2 1
-#endif
-
 #include <assert.h>
 #include <stdio.h>
 #include <stdlib.h>
index 79097ffad9d59af40b56f27af2e758920e5466a5..cd286a6ae79a718ba7dd9bd1c9b0b057b2658ce5 100644 (file)
 #include "common.h"
 
 #include <stdlib.h>
+#include <stdio.h>
 #include <ctype.h>
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <unistd.h>
+#include <libgen.h>
 #include <utime.h>
 #include <setjmp.h>
 
@@ -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 <schwartz@bio.cse.psu.edu> 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