Implement mkstemps and mkstemps64.
authorUlrich Drepper <drepper@redhat.com>
Fri, 30 Oct 2009 07:11:07 +0000 (00:11 -0700)
committerUlrich Drepper <drepper@redhat.com>
Fri, 30 Oct 2009 07:11:07 +0000 (00:11 -0700)
21 files changed:
ChangeLog
NEWS
include/stdio.h
libio/oldtmpfile.c
misc/Makefile
misc/Versions
misc/mkdtemp.c
misc/mkostemp.c
misc/mkostemp64.c
misc/mkstemp.c
misc/mkstemp64.c
misc/mkstemps.c [new file with mode: 0644]
misc/mkstemps64.c [new file with mode: 0644]
misc/mktemp.c
stdio-common/tempnam.c
stdio-common/tempname.c
stdio-common/tmpfile.c
stdio-common/tmpnam.c
stdio-common/tmpnam_r.c
stdlib/stdlib.h
sysdeps/posix/tempname.c

index 247856944ec6fdad9c23114b1acbb49273a243fb..fb2c8ebf06df0bfc3dbef2e7da3889ba3baa43a6 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,27 @@
+2009-10-30  Ulrich Drepper  <drepper@redhat.com>
+
+       [BZ #10349]
+       * sysdeps/posix/tempname.c (__gen_tempname): Take new second
+       parameter, specifying length of prefix.
+       * stdio-common/tempname.c: Likewise.
+       * include/stdio.h: Adjust prototypes.
+       * libio/oldtmpfile.c: Adjust caller.
+       * misc/mkdtemp.c: Likewise.
+       * misc/mkostemp.c: Likewise.
+       * misc/mkostemp64.c: Likewise.
+       * misc/mkstemp.c:  Likewise.
+       * misc/mkstemp64.c: Likewise.
+       * misc/mktemp.c: Likewise.
+       * stdio-common/tempnam.c: Likewise.
+       * stdio-common/tmpfile.c: Likewise.
+       * stdio-common/tmpnam.c: Likewise.
+       * stdio-common/tmpnam_r.c: Likewise.
+       * misc/mkstemps.c: New file.
+       * misc/mkstemps64.c: New file.
+       * stdlib/stdlib.h: Add prototypes.
+       * misc/Makefile (routines): Add mkstemps and mkstemps64.
+       * misc/Versions: Export mkstemps and mkstemps64 for GLIBC_2.11.
+
 2009-10-29  Ulrich Drepper  <drepper@redhat.com>
 
        * locale/iso-639.def: Add sml entry.
diff --git a/NEWS b/NEWS
index 9044ec0f49a4b325138f608c9d9f0be80cd58d54..04a6b8c6bf3861853fb4655cdc2b225a2905ced3 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -1,4 +1,4 @@
-GNU C Library NEWS -- history of user-visible changes.  2009-10-15
+GNU C Library NEWS -- history of user-visible changes.  2009-10-29
 Copyright (C) 1992-2008, 2009 Free Software Foundation, Inc.
 See the end for copying conditions.
 
@@ -7,7 +7,7 @@ using `glibc' in the "product" field.
 \f
 Version 2.11
 
-* New interfaces: execvpe, pthread_sigqueue
+* New interfaces: execvpe, pthread_sigqueue, mkstemps, mkstemps64
   Implemented by Ulrich Drepper.
 
 * Checking version of longjmp added that fails if an uninitialized stack
@@ -18,7 +18,7 @@ Version 2.11
 
 * New optimized string functions for x86-64: strstr, strcasestr, memcmp,
   strcspn, strpbrk, strspn, strcpy, stpcpy, strncpy, strcmp (SSE2, SSE4.2),
-  strncmp (SSE2, SSE4.2).
+  strncmp (SSE2, SSE4.2), strchr (SSE4.2), strrchr (SSE4.2).
   Contributed by H.J. Lu.
 
   strlen, rawmemchr, strcmp (SSSE3), strncmp (SSSE3).
index 444aa39006a30412ea40b1c58f5606854a415039..6b053b8aee4dd5a490dfede33e547bf86737729a 100644 (file)
@@ -80,7 +80,8 @@ extern int __path_search (char *__tmpl, size_t __tmpl_len,
                          __const char *__dir, __const char *__pfx,
                          int __try_tempdir);
 
-extern int __gen_tempname (char *__tmpl, int __flags, int __kind);
+extern int __gen_tempname (char *__tmpl, int __suffixlen, int __flags,
+                          int __kind);
 /* The __kind argument to __gen_tempname may be one of: */
 #  define __GT_FILE    0       /* create a file */
 #  define __GT_DIR     1       /* create a directory */
index d85467a392d0c3bdd3be903a2162611ad14f4ec0..c9e4750cbaefafa0f10754ade6cc011873c7a601 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (C) 1991,1993,1996-2000,2003,2004,2007
+/* Copyright (C) 1991,1993,1996-2000,2003,2004,2007,2009
    Free Software Foundation, Inc.
    This file is part of the GNU C Library.
 
@@ -39,7 +39,7 @@ __old_tmpfile (void)
 
   if (__path_search (buf, FILENAME_MAX, NULL, "tmpf", 0))
     return NULL;
-  fd = __gen_tempname (buf, 0, __GT_FILE);
+  fd = __gen_tempname (buf, 0, 0, __GT_FILE);
   if (fd < 0)
     return NULL;
 
index 13576347b9d67a6d50400061e1e3cd74e10306f6..6243647d1d63d50ac6b8c6e0b7f9d422d2bb551e 100644 (file)
@@ -45,7 +45,7 @@ routines := brk sbrk sstk ioctl \
            gethostid sethostid \
            revoke vhangup \
            swapon swapoff mktemp mkstemp mkstemp64 mkdtemp \
-           mkostemp mkostemp64 \
+           mkostemp mkostemp64 mkstemps mkstemps64 \
            ualarm usleep \
            gtty stty \
            ptrace \
index c930eea94482ad7d531b0e93ca517219a0c8de6a..b883e3df25e7c7809c32914ea19bfd2d668e22a1 100644 (file)
@@ -140,4 +140,7 @@ libc {
   GLIBC_2.10 {
     preadv; preadv64; pwritev; pwritev64;
   }
+  GLIBC_2.11 {
+    mkstemps; mkstemps64;
+  }
 }
index 7cd3a44f943ebf07b32ddd230df386d4c9d90787..6200e9e4fb61ef84f24eb4d843cc5a674f5585c8 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (C) 1999, 2007 Free Software Foundation, Inc.
+/* Copyright (C) 1999, 2007, 2009 Free Software Foundation, Inc.
    This file is part of the GNU C Library.
 
    The GNU C Library is free software; you can redistribute it and/or
@@ -28,7 +28,7 @@ char *
 mkdtemp (template)
      char *template;
 {
-  if (__gen_tempname (template, 0, __GT_DIR))
+  if (__gen_tempname (template, 0, 0, __GT_DIR))
     return NULL;
   else
     return template;
index 372e4f3c0feb61fba13de14e26013b4c8187fb20..a2023ab4706e2fd085cce6b2e8fd85017af2feec 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (C) 1998, 1999, 2001, 2007 Free Software Foundation, Inc.
+/* Copyright (C) 1998, 1999, 2001, 2007, 2009 Free Software Foundation, Inc.
    This file is part of the GNU C Library.
 
    The GNU C Library is free software; you can redistribute it and/or
@@ -32,5 +32,5 @@ mkostemp (template, flags)
      char *template;
      int flags;
 {
-  return __gen_tempname (template, flags, __GT_FILE);
+  return __gen_tempname (template, 0, flags, __GT_FILE);
 }
index 2ae730991e8e4555d79d3ada85dd67d57171a731..18a506dd6038b4383de000a8a146b5a1e5f52707 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (C) 2000, 2007 Free Software Foundation, Inc.
+/* Copyright (C) 2000, 2007, 2009 Free Software Foundation, Inc.
    This file is part of the GNU C Library.
 
    The GNU C Library is free software; you can redistribute it and/or
@@ -29,5 +29,5 @@ mkostemp64 (template, flags)
      char *template;
      int flags;
 {
-  return __gen_tempname (template, flags | O_LARGEFILE, __GT_FILE);
+  return __gen_tempname (template, 0, flags | O_LARGEFILE, __GT_FILE);
 }
index d3edca07917ead4af721047d2bb6e559e353f985..bd3bbc88b2af735a0b00ddbc6fc0dad742500e2c 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (C) 1998, 1999, 2001, 2007 Free Software Foundation, Inc.
+/* Copyright (C) 1998, 1999, 2001, 2007, 2009 Free Software Foundation, Inc.
    This file is part of the GNU C Library.
 
    The GNU C Library is free software; you can redistribute it and/or
@@ -31,5 +31,5 @@ int
 mkstemp (template)
      char *template;
 {
-  return __gen_tempname (template, 0, __GT_FILE);
+  return __gen_tempname (template, 0, 0, __GT_FILE);
 }
index 400bf47d068c33e33aec4d37a4ae8d64ffc928b6..c384bc71e6b54c5048251bcad51e0f743c0b0432 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (C) 2000, 2007 Free Software Foundation, Inc.
+/* Copyright (C) 2000, 2007, 2009 Free Software Foundation, Inc.
    This file is part of the GNU C Library.
 
    The GNU C Library is free software; you can redistribute it and/or
@@ -28,5 +28,5 @@ int
 mkstemp64 (template)
      char *template;
 {
-  return __gen_tempname (template, O_LARGEFILE, __GT_FILE);
+  return __gen_tempname (template, 0, O_LARGEFILE, __GT_FILE);
 }
diff --git a/misc/mkstemps.c b/misc/mkstemps.c
new file mode 100644 (file)
index 0000000..e5ff089
--- /dev/null
@@ -0,0 +1,43 @@
+/* Copyright (C) 2009 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, write to the Free
+   Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
+   02111-1307 USA.  */
+
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+#ifndef __GT_FILE
+# define __GT_FILE 0
+#endif
+
+/* Generate a unique temporary file name from TEMPLATE.  The last six
+   characters before a suffix of length SUFFIXLEN of TEMPLATE must be
+   "XXXXXX"; they are replaced with a string that makes the filename
+   unique.  Then open the file and return a fd. */
+int
+mkstemps (template, suffixlen)
+     char *template;
+     int suffixlen;
+{
+  if (suffixlen < 0)
+    {
+      __set_errno (EINVAL);
+      return -1;
+    }
+
+  return __gen_tempname (template, suffixlen, 0, __GT_FILE);
+}
diff --git a/misc/mkstemps64.c b/misc/mkstemps64.c
new file mode 100644 (file)
index 0000000..cf7562b
--- /dev/null
@@ -0,0 +1,40 @@
+/* Copyright (C) 2000, 2007, 2009 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, write to the Free
+   Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
+   02111-1307 USA.  */
+
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+/* Generate a unique temporary file name from TEMPLATE.  The last six
+   characters before a suffix of length SUFFIXLEN of TEMPLATE must be
+   "XXXXXX"; they are replaced with a string that makes the filename
+   unique.  Then open the file and return a fd. */
+int
+mkstemps64 (template, suffixlen)
+     char *template;
+     int suffixlen;
+{
+  if (suffixlen < 0)
+    {
+      __set_errno (EINVAL);
+      return -1;
+    }
+
+  return __gen_tempname (template, suffixlen, O_LARGEFILE, __GT_FILE);
+}
index f600d7ea7ccc2a2f151c2057171cf3d992253915..c42fd5ec44e84147dc08e84c536570e137af3a04 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (C) 1998, 1999, 2000, 2007 Free Software Foundation, Inc.
+/* Copyright (C) 1998, 1999, 2000, 2007, 2009 Free Software Foundation, Inc.
    This file is part of the GNU C Library.
 
    The GNU C Library is free software; you can redistribute it and/or
@@ -26,7 +26,7 @@ char *
 mktemp (template)
      char *template;
 {
-  if (__gen_tempname (template, 0, __GT_NOCREATE) < 0)
+  if (__gen_tempname (template, 0, 0, __GT_NOCREATE) < 0)
     /* We return the null string if we can't find a unique file name.  */
     template[0] = '\0';
 
index 055604fb79fbcfb4e1b81b8355cbef163bbf18ee..3604a2d267e4c60a731d0e4e8ff95d8649a3c643 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (C) 1991,1993,1996-2000,2007 Free Software Foundation, Inc.
+/* Copyright (C) 1991,1993,1996-2000,2007,2009 Free Software Foundation, Inc.
    This file is part of the GNU C Library.
 
    The GNU C Library is free software; you can redistribute it and/or
@@ -34,7 +34,7 @@ tempnam (const char *dir, const char *pfx)
   if (__path_search (buf, FILENAME_MAX, dir, pfx, 1))
     return NULL;
 
-  if (__gen_tempname (buf, 0, __GT_NOCREATE))
+  if (__gen_tempname (buf, 0, 0, __GT_NOCREATE))
     return NULL;
 
   return __strdup (buf);
index 2c7bcdee0f2324d8472e43cace12ebdaf1924393..02ee34fe8e9278bbccfb3ba9a6b237049b5fde60 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (C) 1991, 92, 93, 95-98, 99, 2007 Free Software Foundation, Inc.
+/* Copyright (C) 1991, 92, 93, 95-99, 2007, 2009 Free Software Foundation, Inc.
    This file is part of the GNU C Library.
 
    The GNU C Library is free software; you can redistribute it and/or
@@ -47,8 +47,9 @@ stub_warning (__path_search)
  */
 
 int
-__gen_tempname (tmpl, flags, kind)
+__gen_tempname (tmpl, suffixlen, flags, kind)
      char *tmpl;
+     int suffixlen;
      int flags;
      int kind;
 {
index b90051346be389a4885ae770c584096b58b40361..69963fd4a92a17e5ed42b3333eabec3be213e319 100644 (file)
@@ -1,5 +1,5 @@
 /* Open a stdio stream on an anonymous temporary file.  Generic/POSIX version.
-   Copyright (C) 1991,1993,1996-2000,2002,2003,2007
+   Copyright (C) 1991,1993,1996-2000,2002,2003,2007,2009
    Free Software Foundation, Inc.
    This file is part of the GNU C Library.
 
@@ -48,7 +48,7 @@ tmpfile (void)
 #ifdef FLAGS
   flags = FLAGS;
 #endif
-  fd = __gen_tempname (buf, flags, __GT_FILE);
+  fd = __gen_tempname (buf, 0, flags, __GT_FILE);
   if (fd < 0)
     return NULL;
 
index f1c16446d516830b4f2ffc0ec43e717a096fd070..93d47a1212b05abd1d1c4ba711c10bc6c00e05db 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (C) 1991,1993,1996-1999,2000,2007 Free Software Foundation, Inc.
+/* Copyright (C) 1991,1993,1996-2000,2007,2009 Free Software Foundation, Inc.
    This file is part of the GNU C Library.
 
    The GNU C Library is free software; you can redistribute it and/or
@@ -39,7 +39,7 @@ tmpnam (char *s)
                        0))
     return NULL;
 
-  if (__builtin_expect (__gen_tempname (tmpbuf, 0, __GT_NOCREATE), 0))
+  if (__builtin_expect (__gen_tempname (tmpbuf, 0, 0, __GT_NOCREATE), 0))
     return NULL;
 
   if (s == NULL)
index 60c42866a7cef8877724ac4145e31fe019f12bfb..d252bafbb29613293a752c40d791dbcf6e4a33e0 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (C) 1991,1993,1996-1999,2000,2007 Free Software Foundation, Inc.
+/* Copyright (C) 1991,1993,1996-2000,2007,2009 Free Software Foundation, Inc.
    This file is part of the GNU C Library.
 
    The GNU C Library is free software; you can redistribute it and/or
@@ -28,7 +28,7 @@ tmpnam_r (char *s)
 
   if (__path_search (s, L_tmpnam, NULL, NULL, 0))
     return NULL;
-  if (__gen_tempname (s, 0, __GT_NOCREATE))
+  if (__gen_tempname (s, 0, 0, __GT_NOCREATE))
     return NULL;
 
   return s;
index 1b88ce6a52801e88b808f839f98b608da4362f3c..6fe6a03bf38414f9ec54aa7e4dc7544252e55f40 100644 (file)
@@ -50,7 +50,7 @@ __BEGIN_DECLS
 #  if defined __GNUC__ && !defined __cplusplus
 #   define __WAIT_INT(status) \
   (__extension__ (((union { __typeof(status) __in; int __i; }) \
-                   { .__in = (status) }).__i))
+                  { .__in = (status) }).__i))
 #  else
 #   define __WAIT_INT(status)  (*(int *) &(status))
 #  endif
@@ -609,7 +609,7 @@ extern char *mktemp (char *__template) __THROW __nonnull ((1)) __wur;
    Returns a file descriptor open on the file for reading and writing,
    or -1 if it cannot create a uniquely-named file.
 
-   This function is a possible cancellation points and therefore not
+   This function is a possible cancellation point and therefore not
    marked with __THROW.  */
 # ifndef __USE_FILE_OFFSET64
 extern int mkstemp (char *__template) __nonnull ((1)) __wur;
@@ -626,6 +626,29 @@ extern int mkstemp64 (char *__template) __nonnull ((1)) __wur;
 # endif
 #endif
 
+#ifdef __USE_MISC
+/* Similar to mkstemp, but the template can have a suffix after the
+   XXXXXX.  The length of the suffix is specified in the second
+   parameter.
+
+   This function is a possible cancellation point and therefore not
+   marked with __THROW.  */
+# ifndef __USE_FILE_OFFSET64
+extern int mkstemps (char *__template, int __suffixlen) __nonnull ((1)) __wur;
+# else
+#  ifdef __REDIRECT
+extern int __REDIRECT (mkstemps, (char *__template, int __suffixlen),
+                      mkstemps64) __nonnull ((1)) __wur;
+#  else
+#   define mkstemps mkstemps64
+#  endif
+# endif
+# ifdef __USE_LARGEFILE64
+extern int mkstemps64 (char *__template, int __suffixlen)
+     __nonnull ((1)) __wur;
+# endif
+#endif
+
 #if defined __USE_BSD || defined __USE_XOPEN2K8
 /* Create a unique temporary directory from TEMPLATE.
    The last six characters of TEMPLATE must be "XXXXXX";
index be979d8c8a48cc4b2e0c664f33ced69192c5ec39..57ce5a942c2257f66aaff88fbb341b266a75565e 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (C) 1991-2001, 2006, 2007 Free Software Foundation, Inc.
+/* Copyright (C) 1991-2001, 2006, 2007, 2009 Free Software Foundation, Inc.
    This file is part of the GNU C Library.
 
    The GNU C Library is free software; you can redistribute it and/or
@@ -210,9 +210,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
@@ -223,7 +223,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;
@@ -251,14 +251,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