Implement mkstemps and mkstemps64.

This commit is contained in:
Ulrich Drepper 2009-10-30 00:11:07 -07:00
parent 471d4931f8
commit 7f3146e789
21 changed files with 173 additions and 38 deletions

View File

@ -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> 2009-10-29 Ulrich Drepper <drepper@redhat.com>
* locale/iso-639.def: Add sml entry. * locale/iso-639.def: Add sml entry.

6
NEWS
View File

@ -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. Copyright (C) 1992-2008, 2009 Free Software Foundation, Inc.
See the end for copying conditions. See the end for copying conditions.
@ -7,7 +7,7 @@ using `glibc' in the "product" field.
Version 2.11 Version 2.11
* New interfaces: execvpe, pthread_sigqueue * New interfaces: execvpe, pthread_sigqueue, mkstemps, mkstemps64
Implemented by Ulrich Drepper. Implemented by Ulrich Drepper.
* Checking version of longjmp added that fails if an uninitialized stack * 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, * New optimized string functions for x86-64: strstr, strcasestr, memcmp,
strcspn, strpbrk, strspn, strcpy, stpcpy, strncpy, strcmp (SSE2, SSE4.2), 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. Contributed by H.J. Lu.
strlen, rawmemchr, strcmp (SSSE3), strncmp (SSSE3). strlen, rawmemchr, strcmp (SSSE3), strncmp (SSSE3).

View File

@ -80,7 +80,8 @@ extern int __path_search (char *__tmpl, size_t __tmpl_len,
__const char *__dir, __const char *__pfx, __const char *__dir, __const char *__pfx,
int __try_tempdir); 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: */ /* The __kind argument to __gen_tempname may be one of: */
# define __GT_FILE 0 /* create a file */ # define __GT_FILE 0 /* create a file */
# define __GT_DIR 1 /* create a directory */ # define __GT_DIR 1 /* create a directory */

View 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. Free Software Foundation, Inc.
This file is part of the GNU C Library. 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)) if (__path_search (buf, FILENAME_MAX, NULL, "tmpf", 0))
return NULL; return NULL;
fd = __gen_tempname (buf, 0, __GT_FILE); fd = __gen_tempname (buf, 0, 0, __GT_FILE);
if (fd < 0) if (fd < 0)
return NULL; return NULL;

View File

@ -45,7 +45,7 @@ routines := brk sbrk sstk ioctl \
gethostid sethostid \ gethostid sethostid \
revoke vhangup \ revoke vhangup \
swapon swapoff mktemp mkstemp mkstemp64 mkdtemp \ swapon swapoff mktemp mkstemp mkstemp64 mkdtemp \
mkostemp mkostemp64 \ mkostemp mkostemp64 mkstemps mkstemps64 \
ualarm usleep \ ualarm usleep \
gtty stty \ gtty stty \
ptrace \ ptrace \

View File

@ -140,4 +140,7 @@ libc {
GLIBC_2.10 { GLIBC_2.10 {
preadv; preadv64; pwritev; pwritev64; preadv; preadv64; pwritev; pwritev64;
} }
GLIBC_2.11 {
mkstemps; mkstemps64;
}
} }

View 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. This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or The GNU C Library is free software; you can redistribute it and/or
@ -28,7 +28,7 @@ char *
mkdtemp (template) mkdtemp (template)
char *template; char *template;
{ {
if (__gen_tempname (template, 0, __GT_DIR)) if (__gen_tempname (template, 0, 0, __GT_DIR))
return NULL; return NULL;
else else
return template; return template;

View 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. This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or The GNU C Library is free software; you can redistribute it and/or
@ -32,5 +32,5 @@ mkostemp (template, flags)
char *template; char *template;
int flags; int flags;
{ {
return __gen_tempname (template, flags, __GT_FILE); return __gen_tempname (template, 0, flags, __GT_FILE);
} }

View 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. This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or The GNU C Library is free software; you can redistribute it and/or
@ -29,5 +29,5 @@ mkostemp64 (template, flags)
char *template; char *template;
int flags; int flags;
{ {
return __gen_tempname (template, flags | O_LARGEFILE, __GT_FILE); return __gen_tempname (template, 0, flags | O_LARGEFILE, __GT_FILE);
} }

View 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. This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or The GNU C Library is free software; you can redistribute it and/or
@ -31,5 +31,5 @@ int
mkstemp (template) mkstemp (template)
char *template; char *template;
{ {
return __gen_tempname (template, 0, __GT_FILE); return __gen_tempname (template, 0, 0, __GT_FILE);
} }

View 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. This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or The GNU C Library is free software; you can redistribute it and/or
@ -28,5 +28,5 @@ int
mkstemp64 (template) mkstemp64 (template)
char *template; char *template;
{ {
return __gen_tempname (template, O_LARGEFILE, __GT_FILE); return __gen_tempname (template, 0, O_LARGEFILE, __GT_FILE);
} }

43
misc/mkstemps.c Normal file
View File

@ -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);
}

40
misc/mkstemps64.c Normal file
View File

@ -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);
}

View 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. This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or The GNU C Library is free software; you can redistribute it and/or
@ -26,7 +26,7 @@ char *
mktemp (template) mktemp (template)
char *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. */ /* We return the null string if we can't find a unique file name. */
template[0] = '\0'; template[0] = '\0';

View 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. This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or 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)) if (__path_search (buf, FILENAME_MAX, dir, pfx, 1))
return NULL; return NULL;
if (__gen_tempname (buf, 0, __GT_NOCREATE)) if (__gen_tempname (buf, 0, 0, __GT_NOCREATE))
return NULL; return NULL;
return __strdup (buf); return __strdup (buf);

View 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. This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or The GNU C Library is free software; you can redistribute it and/or
@ -47,8 +47,9 @@ stub_warning (__path_search)
*/ */
int int
__gen_tempname (tmpl, flags, kind) __gen_tempname (tmpl, suffixlen, flags, kind)
char *tmpl; char *tmpl;
int suffixlen;
int flags; int flags;
int kind; int kind;
{ {

View File

@ -1,5 +1,5 @@
/* Open a stdio stream on an anonymous temporary file. Generic/POSIX version. /* 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. Free Software Foundation, Inc.
This file is part of the GNU C Library. This file is part of the GNU C Library.
@ -48,7 +48,7 @@ tmpfile (void)
#ifdef FLAGS #ifdef FLAGS
flags = FLAGS; flags = FLAGS;
#endif #endif
fd = __gen_tempname (buf, flags, __GT_FILE); fd = __gen_tempname (buf, 0, flags, __GT_FILE);
if (fd < 0) if (fd < 0)
return NULL; return NULL;

View 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. This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or The GNU C Library is free software; you can redistribute it and/or
@ -39,7 +39,7 @@ tmpnam (char *s)
0)) 0))
return NULL; 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; return NULL;
if (s == NULL) if (s == NULL)

View 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. This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or 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)) if (__path_search (s, L_tmpnam, NULL, NULL, 0))
return NULL; return NULL;
if (__gen_tempname (s, 0, __GT_NOCREATE)) if (__gen_tempname (s, 0, 0, __GT_NOCREATE))
return NULL; return NULL;
return s; return s;

View File

@ -50,7 +50,7 @@ __BEGIN_DECLS
# if defined __GNUC__ && !defined __cplusplus # if defined __GNUC__ && !defined __cplusplus
# define __WAIT_INT(status) \ # define __WAIT_INT(status) \
(__extension__ (((union { __typeof(status) __in; int __i; }) \ (__extension__ (((union { __typeof(status) __in; int __i; }) \
{ .__in = (status) }).__i)) { .__in = (status) }).__i))
# else # else
# define __WAIT_INT(status) (*(int *) &(status)) # define __WAIT_INT(status) (*(int *) &(status))
# endif # 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, Returns a file descriptor open on the file for reading and writing,
or -1 if it cannot create a uniquely-named file. 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. */ marked with __THROW. */
# ifndef __USE_FILE_OFFSET64 # ifndef __USE_FILE_OFFSET64
extern int mkstemp (char *__template) __nonnull ((1)) __wur; extern int mkstemp (char *__template) __nonnull ((1)) __wur;
@ -626,6 +626,29 @@ extern int mkstemp64 (char *__template) __nonnull ((1)) __wur;
# endif # endif
#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 #if defined __USE_BSD || defined __USE_XOPEN2K8
/* Create a unique temporary directory from TEMPLATE. /* Create a unique temporary directory from TEMPLATE.
The last six characters of TEMPLATE must be "XXXXXX"; The last six characters of TEMPLATE must be "XXXXXX";

View 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. This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or The GNU C Library is free software; you can redistribute it and/or
@ -210,9 +210,9 @@ static const char letters[] =
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
/* Generate a temporary file name based on TMPL. TMPL must match the /* 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 rules for mk[s]temp (i.e. end in "XXXXXX", possibly with a suffix).
does not exist at the time of the call to __gen_tempname. TMPL is The name constructed does not exist at the time of the call to
overwritten with the result. __gen_tempname. TMPL is overwritten with the result.
KIND may be one of: KIND may be one of:
__GT_NOCREATE: simply verify that the name does not exist __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. */ We use a clever algorithm to get hard-to-predict names. */
int int
__gen_tempname (char *tmpl, int flags, int kind) __gen_tempname (char *tmpl, int suffixlen, int flags, int kind)
{ {
int len; int len;
char *XXXXXX; char *XXXXXX;
@ -251,14 +251,14 @@ __gen_tempname (char *tmpl, int flags, int kind)
#endif #endif
len = strlen (tmpl); 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); __set_errno (EINVAL);
return -1; return -1;
} }
/* This is where the Xs start. */ /* This is where the Xs start. */
XXXXXX = &tmpl[len - 6]; XXXXXX = &tmpl[len - 6 - suffixlen];
/* Get some more or less random data. */ /* Get some more or less random data. */
#ifdef RANDOM_BITS #ifdef RANDOM_BITS