This is the mail archive of the binutils@sources.redhat.com mailing list for the binutils project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: mktemp() vs mkstemp()


On Sat, Jun 08, 2002 at 07:59:18PM -0700, H . J . Lu wrote:
> On Sun, Jun 09, 2002 at 02:51:57AM +0300, Elias Athanasopoulos wrote:
> > Hi,
> > 
> > In binutils/bucomm.c there are some calls of mktemp(). Are there any 
> > objections to replace them with mkstemp()?
> > 
> 
> Don't touch it. From binutils/ChangeLog:
> 
> 2001-11-29  H.J. Lu <hjl@gnu.org>
> 
>         * bucomm.c (make_tempname): Revert the changes made on
>         2001-11-14 and 2001-11-12. They won't work with directories.

  the patch below does it (was made for 2.11.2, but should apply fairly
easy to binutils -current). code using make_tempname() needs the extra arg.
  if the system lacks mkdtemp(), it should fallback to mktemp(), or
perhaps implement mkdtemp() in libiberty.
  this looks more like something that belongs to libiberty than anything
else, imho.

  f.-

--- binutils/bucomm.c.orig	Mon Jun 11 07:04:27 2001
+++ binutils/bucomm.c	Sun Mar 10 02:28:35 2002
@@ -208,12 +208,14 @@ print_arelt_descr (file, abfd, verbose)
 /* Return the name of a temporary file in the same directory as FILENAME.  */
 
 char *
-make_tempname (filename)
+make_tempname (filename, isdir)
      char *filename;
+     int isdir;
 {
   static char template[] = "stXXXXXX";
   char *tmpname;
   char *slash = strrchr (filename, '/');
+  char c;
 
 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
   {
@@ -228,8 +230,6 @@ make_tempname (filename)
 
   if (slash != (char *) NULL)
     {
-      char c;
-
       c = *slash;
       *slash = 0;
       tmpname = xmalloc (strlen (filename) + sizeof (template) + 2);
@@ -243,15 +243,31 @@ make_tempname (filename)
 #endif
       strcat (tmpname, "/");
       strcat (tmpname, template);
-      mktemp (tmpname);
-      *slash = c;
     }
   else
     {
       tmpname = xmalloc (sizeof (template));
       strcpy (tmpname, template);
-      mktemp (tmpname);
     }
+
+  if (isdir)
+    {
+      if (mkdtemp (tmpname) != (char *) NULL)
+      tmpname = NULL;
+    }
+  else
+    {
+      int fd;
+
+      fd = mkstemp (tmpname);
+      if (fd == -1)
+      tmpname = NULL;
+      else
+      close (fd);
+    }
+  if (slash != (char *) NULL)
+    *slash = c;
+
   return tmpname;
 }
--- binutils/bucomm.h.orig	Thu Jun  7 00:12:27 2001
+++ binutils/bucomm.h	Sun Mar 10 02:28:35 2002
@@ -166,7 +166,7 @@ void list_supported_targets PARAMS ((con
 
 void print_arelt_descr PARAMS ((FILE *file, bfd *abfd, boolean verbose));
 
-char *make_tempname PARAMS ((char *));
+char *make_tempname PARAMS ((char *, int));
 
 bfd_vma parse_vma PARAMS ((const char *, const char *));
 


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]