From 7f3146e7895f248dd2f655e9e5895abebb2bf506 Mon Sep 17 00:00:00 2001 From: Ulrich Drepper Date: Fri, 30 Oct 2009 00:11:07 -0700 Subject: [PATCH] Implement mkstemps and mkstemps64. --- ChangeLog | 24 ++++++++++++++++++++++ NEWS | 6 +++--- include/stdio.h | 3 ++- libio/oldtmpfile.c | 4 ++-- misc/Makefile | 2 +- misc/Versions | 3 +++ misc/mkdtemp.c | 4 ++-- misc/mkostemp.c | 4 ++-- misc/mkostemp64.c | 4 ++-- misc/mkstemp.c | 4 ++-- misc/mkstemp64.c | 4 ++-- misc/mkstemps.c | 43 ++++++++++++++++++++++++++++++++++++++++ misc/mkstemps64.c | 40 +++++++++++++++++++++++++++++++++++++ misc/mktemp.c | 4 ++-- stdio-common/tempnam.c | 4 ++-- stdio-common/tempname.c | 5 +++-- stdio-common/tmpfile.c | 4 ++-- stdio-common/tmpnam.c | 4 ++-- stdio-common/tmpnam_r.c | 4 ++-- stdlib/stdlib.h | 27 +++++++++++++++++++++++-- sysdeps/posix/tempname.c | 14 ++++++------- 21 files changed, 173 insertions(+), 38 deletions(-) create mode 100644 misc/mkstemps.c create mode 100644 misc/mkstemps64.c diff --git a/ChangeLog b/ChangeLog index 247856944e..fb2c8ebf06 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,27 @@ +2009-10-30 Ulrich Drepper + + [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 * locale/iso-639.def: Add sml entry. diff --git a/NEWS b/NEWS index 9044ec0f49..04a6b8c6bf 100644 --- 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. 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). diff --git a/include/stdio.h b/include/stdio.h index 444aa39006..6b053b8aee 100644 --- a/include/stdio.h +++ b/include/stdio.h @@ -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 */ diff --git a/libio/oldtmpfile.c b/libio/oldtmpfile.c index d85467a392..c9e4750cba 100644 --- a/libio/oldtmpfile.c +++ b/libio/oldtmpfile.c @@ -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; diff --git a/misc/Makefile b/misc/Makefile index 13576347b9..6243647d1d 100644 --- a/misc/Makefile +++ b/misc/Makefile @@ -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 \ diff --git a/misc/Versions b/misc/Versions index c930eea944..b883e3df25 100644 --- a/misc/Versions +++ b/misc/Versions @@ -140,4 +140,7 @@ libc { GLIBC_2.10 { preadv; preadv64; pwritev; pwritev64; } + GLIBC_2.11 { + mkstemps; mkstemps64; + } } diff --git a/misc/mkdtemp.c b/misc/mkdtemp.c index 7cd3a44f94..6200e9e4fb 100644 --- a/misc/mkdtemp.c +++ b/misc/mkdtemp.c @@ -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; diff --git a/misc/mkostemp.c b/misc/mkostemp.c index 372e4f3c0f..a2023ab470 100644 --- a/misc/mkostemp.c +++ b/misc/mkostemp.c @@ -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); } diff --git a/misc/mkostemp64.c b/misc/mkostemp64.c index 2ae730991e..18a506dd60 100644 --- a/misc/mkostemp64.c +++ b/misc/mkostemp64.c @@ -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); } diff --git a/misc/mkstemp.c b/misc/mkstemp.c index d3edca0791..bd3bbc88b2 100644 --- a/misc/mkstemp.c +++ b/misc/mkstemp.c @@ -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); } diff --git a/misc/mkstemp64.c b/misc/mkstemp64.c index 400bf47d06..c384bc71e6 100644 --- a/misc/mkstemp64.c +++ b/misc/mkstemp64.c @@ -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 index 0000000000..e5ff089443 --- /dev/null +++ b/misc/mkstemps.c @@ -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 +#include +#include + +#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 index 0000000000..cf7562b48f --- /dev/null +++ b/misc/mkstemps64.c @@ -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 +#include +#include +#include + +/* 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); +} diff --git a/misc/mktemp.c b/misc/mktemp.c index f600d7ea7c..c42fd5ec44 100644 --- a/misc/mktemp.c +++ b/misc/mktemp.c @@ -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'; diff --git a/stdio-common/tempnam.c b/stdio-common/tempnam.c index 055604fb79..3604a2d267 100644 --- a/stdio-common/tempnam.c +++ b/stdio-common/tempnam.c @@ -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); diff --git a/stdio-common/tempname.c b/stdio-common/tempname.c index 2c7bcdee0f..02ee34fe8e 100644 --- a/stdio-common/tempname.c +++ b/stdio-common/tempname.c @@ -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; { diff --git a/stdio-common/tmpfile.c b/stdio-common/tmpfile.c index b90051346b..69963fd4a9 100644 --- a/stdio-common/tmpfile.c +++ b/stdio-common/tmpfile.c @@ -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; diff --git a/stdio-common/tmpnam.c b/stdio-common/tmpnam.c index f1c16446d5..93d47a1212 100644 --- a/stdio-common/tmpnam.c +++ b/stdio-common/tmpnam.c @@ -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) diff --git a/stdio-common/tmpnam_r.c b/stdio-common/tmpnam_r.c index 60c42866a7..d252bafbb2 100644 --- a/stdio-common/tmpnam_r.c +++ b/stdio-common/tmpnam_r.c @@ -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; diff --git a/stdlib/stdlib.h b/stdlib/stdlib.h index 1b88ce6a52..6fe6a03bf3 100644 --- a/stdlib/stdlib.h +++ b/stdlib/stdlib.h @@ -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"; diff --git a/sysdeps/posix/tempname.c b/sysdeps/posix/tempname.c index be979d8c8a..57ce5a942c 100644 --- a/sysdeps/posix/tempname.c +++ b/sysdeps/posix/tempname.c @@ -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