changeset 12233:49e87af14a05

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. Signed-off-by: Eric Blake <ebb9@byu.net>
author Eric Blake <ebb9@byu.net>
date Mon, 02 Nov 2009 13:19:27 -0700
parents 993b8355a1cf
children b45701f2ff71
files ChangeLog NEWS lib/mkdtemp.c lib/mkostemp.c lib/mkstemp.c lib/tempname.c lib/tempname.h lib/tmpfile.c
diffstat 8 files changed, 67 insertions(+), 27 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog	Wed Nov 04 06:19:23 2009 -0700
+++ b/ChangeLog	Mon Nov 02 13:19:27 2009 -0700
@@ -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.
--- a/NEWS	Wed Nov 04 06:19:23 2009 -0700
+++ b/NEWS	Mon Nov 02 13:19:27 2009 -0700
@@ -6,6 +6,9 @@
 
 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
--- a/lib/mkdtemp.c	Wed Nov 04 06:19:23 2009 -0700
+++ b/lib/mkdtemp.c	Mon Nov 02 13:19:27 2009 -0700
@@ -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;
--- a/lib/mkostemp.c	Wed Nov 04 06:19:23 2009 -0700
+++ b/lib/mkostemp.c	Mon Nov 02 13:19:27 2009 -0700
@@ -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 @@
      char *template;
      int flags;
 {
-  return __gen_tempname (template, flags, __GT_FILE);
+  return __gen_tempname (template, 0, flags, __GT_FILE);
 }
--- a/lib/mkstemp.c	Wed Nov 04 06:19:23 2009 -0700
+++ b/lib/mkstemp.c	Mon Nov 02 13:19:27 2009 -0700
@@ -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 @@
 mkstemp (template)
      char *template;
 {
-  return __gen_tempname (template, 0, __GT_FILE);
+  return __gen_tempname (template, 0, 0, __GT_FILE);
 }
--- a/lib/tempname.c	Wed Nov 04 06:19:23 2009 -0700
+++ b/lib/tempname.c	Mon Nov 02 13:19:27 2009 -0700
@@ -40,9 +40,13 @@
 # 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>
@@ -60,11 +64,12 @@
 # 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 @@
 "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 @@
 
    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 @@
 #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 @@
 	{
 	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 @@
 
 	default:
 	  assert (! "invalid KIND in __gen_tempname");
+	  abort ();
 	}
 
       if (fd >= 0)
--- a/lib/tempname.h	Wed Nov 04 06:19:23 2009 -0700
+++ b/lib/tempname.h	Mon Nov 02 13:19:27 2009 -0700
@@ -17,14 +17,25 @@
 
 /* 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 */
--- a/lib/tmpfile.c	Wed Nov 04 06:19:23 2009 -0700
+++ b/lib/tmpfile.c	Mon Nov 02 13:19:27 2009 -0700
@@ -83,7 +83,7 @@
 	  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;