tempname: resync from glibc
authorEric Blake <ebb9@byu.net>
Mon, 2 Nov 2009 20:19:27 +0000 (13:19 -0700)
committerEric Blake <ebb9@byu.net>
Thu, 5 Nov 2009 02:07:39 +0000 (19:07 -0700)
* lib/tempname.c (__gen_tempname): Add suffixlen argument.  Use
same values for __GT_FILE as glibc.  Abort even when assertions
are disabled.
* lib/tempname.h (GT_FILE): Use glibc __GT_FILE, if available, and
match its value otherwise.  Allow idempotent inclusion.
* lib/mkdtemp.c (mkdtemp): Adjust caller.
* lib/mkostemp.c (mkostemp): Likewise.
* lib/mkstemp.c (mkstemp): Likewise.
* lib/tmpfile.c (tmpfile): Likewise.
* NEWS: Document this.

Signed-off-by: Eric Blake <ebb9@byu.net>
ChangeLog
NEWS
lib/mkdtemp.c
lib/mkostemp.c
lib/mkstemp.c
lib/tempname.c
lib/tempname.h
lib/tmpfile.c

index 57a7fdb..e57ce69 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,17 @@
 2009-11-04  Eric Blake  <ebb9@byu.net>
 
+       tempname: resync from glibc
+       * lib/tempname.c (__gen_tempname): Add suffixlen argument.  Use
+       same values for __GT_FILE as glibc.  Abort even when assertions
+       are disabled.
+       * lib/tempname.h (GT_FILE): Use glibc __GT_FILE, if available, and
+       match its value otherwise.  Allow idempotent inclusion.
+       * lib/mkdtemp.c (mkdtemp): Adjust caller.
+       * lib/mkostemp.c (mkostemp): Likewise.
+       * lib/mkstemp.c (mkstemp): Likewise.
+       * lib/tmpfile.c (tmpfile): Likewise.
+       * NEWS: Document this.
+
        utimens: fix use of futimens on older Linux
        * lib/utimens.c (fdutimens): Use updated, rather than original,
        timespec to avoid bug in older Linux kernel.
diff --git a/NEWS b/NEWS
index fcf8fe5..a635150 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -6,6 +6,9 @@ User visible incompatible changes
 
 Date        Modules         Changes
 
+2009-11-04  tempname        The gen_tempname function takes an additional
+                            'suffixlen' argument. You can safely pass 0.
+
 2009-11-04  nproc           The num_processors function now takes an argument.
 
 2009-11-02  inet_pton       The use of this module now requires linking with
index b5181d1..7f07ee4 100644 (file)
@@ -31,7 +31,7 @@
 char *
 mkdtemp (char *template)
 {
-  if (gen_tempname (template, 0, GT_DIR))
+  if (gen_tempname (template, 0, 0, GT_DIR))
     return NULL;
   else
     return template;
index 6b94d40..02a97e6 100644 (file)
@@ -1,4 +1,5 @@
-/* Copyright (C) 1998, 1999, 2001, 2005, 2006, 2007, 2009 Free Software Foundation, Inc.
+/* Copyright (C) 1998, 1999, 2001, 2005, 2006, 2007, 2009 Free
+   Software Foundation, Inc.
    This file is derived from the one in the GNU C Library.
 
    This program is free software: you can redistribute it and/or modify
@@ -23,7 +24,9 @@
 #if !_LIBC
 # include "tempname.h"
 # define __gen_tempname gen_tempname
-# define __GT_FILE GT_FILE
+# ifndef __GTFILE
+#  define __GT_FILE GT_FILE
+# endif
 #endif
 
 #include <stdio.h>
@@ -41,5 +44,5 @@ mkostemp (template, flags)
      char *template;
      int flags;
 {
-  return __gen_tempname (template, flags, __GT_FILE);
+  return __gen_tempname (template, 0, flags, __GT_FILE);
 }
index ccf7a7b..684c983 100644 (file)
@@ -1,4 +1,5 @@
-/* Copyright (C) 1998, 1999, 2001, 2005, 2006, 2007, 2009 Free Software Foundation, Inc.
+/* Copyright (C) 1998, 1999, 2001, 2005, 2006, 2007, 2009 Free
+   Software Foundation, Inc.
    This file is derived from the one in the GNU C Library.
 
    This program is free software: you can redistribute it and/or modify
@@ -23,7 +24,9 @@
 #if !_LIBC
 # include "tempname.h"
 # define __gen_tempname gen_tempname
-# define __GT_FILE GT_FILE
+# ifndef __GT_FILE
+#  define __GT_FILE GT_FILE
+# endif
 #endif
 
 #include <stdio.h>
@@ -40,5 +43,5 @@ int
 mkstemp (template)
      char *template;
 {
-  return __gen_tempname (template, 0, __GT_FILE);
+  return __gen_tempname (template, 0, 0, __GT_FILE);
 }
index 4102134..2da5afe 100644 (file)
 # define TMP_MAX 238328
 #endif
 #ifndef __GT_FILE
-# define __GT_FILE     1
-# define __GT_DIR      2
-# define __GT_NOCREATE 3
+# define __GT_FILE     0
+# define __GT_DIR      1
+# define __GT_NOCREATE 2
+#endif
+#if !_LIBC && (GT_FILE != __GT_FILE || GT_DIR != __GT_DIR       \
+               || GT_NOCREATE != __GT_NOCREATE)
+# error report this to bug-gnulib@gnu.org
 #endif
 
 #include <stddef.h>
 # define struct_stat64 struct stat64
 #else
 # define struct_stat64 struct stat
-# define __open open
 # define __gen_tempname gen_tempname
 # define __getpid getpid
 # define __gettimeofday gettimeofday
 # define __mkdir mkdir
+# define __open open
+# define __open64 open
 # define __lxstat64(version, file, buf) lstat (file, buf)
 # define __xstat64(version, file, buf) stat (file, buf)
 #endif
@@ -179,9 +184,9 @@ static const char letters[] =
 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
 
 /* Generate a temporary file name based on TMPL.  TMPL must match the
-   rules for mk[s]temp (i.e. end in "XXXXXX").  The name constructed
-   does not exist at the time of the call to __gen_tempname.  TMPL is
-   overwritten with the result.
+   rules for mk[s]temp (i.e. end in "XXXXXX", possibly with a suffix).
+   The name constructed does not exist at the time of the call to
+   __gen_tempname.  TMPL is overwritten with the result.
 
    KIND may be one of:
    __GT_NOCREATE:      simply verify that the name does not exist
@@ -192,7 +197,7 @@ static const char letters[] =
 
    We use a clever algorithm to get hard-to-predict names. */
 int
-__gen_tempname (char *tmpl, int flags, int kind)
+__gen_tempname (char *tmpl, int suffixlen, int flags, int kind)
 {
   int len;
   char *XXXXXX;
@@ -220,14 +225,14 @@ __gen_tempname (char *tmpl, int flags, int kind)
 #endif
 
   len = strlen (tmpl);
-  if (len < 6 || strcmp (&tmpl[len - 6], "XXXXXX"))
+  if (len < 6 + suffixlen || memcmp (&tmpl[len - 6 - suffixlen], "XXXXXX", 6))
     {
       __set_errno (EINVAL);
       return -1;
     }
 
   /* This is where the Xs start.  */
-  XXXXXX = &tmpl[len - 6];
+  XXXXXX = &tmpl[len - 6 - suffixlen];
 
   /* Get some more or less random data.  */
 #ifdef RANDOM_BITS
@@ -262,8 +267,8 @@ __gen_tempname (char *tmpl, int flags, int kind)
        {
        case __GT_FILE:
          fd = __open (tmpl,
-                      (flags & ~0777) | O_RDWR | O_CREAT | O_EXCL,
-                      S_IRUSR | S_IWUSR);
+                      (flags & ~O_ACCMODE)
+                      | O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
          break;
 
        case __GT_DIR:
@@ -290,6 +295,7 @@ __gen_tempname (char *tmpl, int flags, int kind)
 
        default:
          assert (! "invalid KIND in __gen_tempname");
+         abort ();
        }
 
       if (fd >= 0)
index edf7074..cd69e7d 100644 (file)
 
 /* header written by Eric Blake */
 
-#define GT_FILE                1
-#define GT_DIR         2
-#define GT_NOCREATE    3
+#ifndef GL_TEMPNAME_H
+# define GL_TEMPNAME_H
+
+# include <stdio.h>
+
+# ifdef __GT_FILE
+#  define GT_FILE     __GT_FILE
+#  define GT_DIR      __GT_DIR
+#  define GT_NOCREATE __GT_NOCREATE
+# else
+#  define GT_FILE     0
+#  define GT_DIR      1
+#  define GT_NOCREATE 2
+# endif
 
 /* Generate a temporary file name based on TMPL.  TMPL must match the
-   rules for mk[s]temp (i.e. end in "XXXXXX").  The name constructed
-   does not exist at the time of the call to gen_tempname.  TMPL is
-   overwritten with the result.
+   rules for mk[s]temp (i.e. end in "XXXXXX", possibly with a suffix).
+   The name constructed does not exist at the time of the call to
+   gen_tempname.  TMPL is overwritten with the result.
 
    KIND may be one of:
    GT_NOCREATE:                simply verify that the name does not exist
@@ -34,4 +45,6 @@
    GT_DIR:             create a directory, which will be mode 0700.
 
    We use a clever algorithm to get hard-to-predict names. */
-extern int gen_tempname (char *tmpl, int flags, int kind);
+extern int gen_tempname (char *tmpl, int suffixlen, int flags, int kind);
+
+#endif /* GL_TEMPNAME_H */
index 19c8a5e..83df9fe 100644 (file)
@@ -83,7 +83,7 @@ tmpfile (void)
          do
            {
              memcpy (&xtemplate[len - 6], "XXXXXX", 6);
-             if (gen_tempname (xtemplate, 0, GT_NOCREATE) < 0)
+             if (gen_tempname (xtemplate, 0, 0, GT_NOCREATE) < 0)
                {
                  fd = -1;
                  break;