From bc659826df33134434096f074ed82e8094840743 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Axel=20D=C3=B6rfler?= Date: Fri, 21 Nov 2003 18:25:02 +0000 Subject: [PATCH] t[e]mpnam() stuff now works. git-svn-id: file:///srv/svn/repos/haiku/trunk/current@5433 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- .../posix/glibc/stdio-common/tempnam.c | 40 ++++++++-------- .../libroot/posix/glibc/stdio-common/tmpnam.c | 46 ++++++++++--------- .../posix/glibc/stdio-common/tmpnam_r.c | 32 +++++++------ 3 files changed, 64 insertions(+), 54 deletions(-) diff --git a/src/kernel/libroot/posix/glibc/stdio-common/tempnam.c b/src/kernel/libroot/posix/glibc/stdio-common/tempnam.c index cd3dd40f90..e01826e1bf 100644 --- a/src/kernel/libroot/posix/glibc/stdio-common/tempnam.c +++ b/src/kernel/libroot/posix/glibc/stdio-common/tempnam.c @@ -14,31 +14,35 @@ 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. */ + 02111-1307 USA. +*/ -#include + +#include #include -/* Generate a unique temporary filename using up to five characters of PFX - if it is not NULL. The directory to put this file in is searched for - as follows: First the environment variable "TMPDIR" is checked. - If it contains the name of a writable directory, that directory is used. - If not and if DIR is not NULL, that value is checked. If that fails, - P_tmpdir is tried and finally "/tmp". The storage for the filename - is allocated by `malloc'. */ + +/** Generate a unique temporary filename using up to five characters of PFX + * if it is not NULL. The directory to put this file in is searched for + * as follows: First the environment variable "TMPDIR" is checked. + * If it contains the name of a writable directory, that directory is used. + * If not and if DIR is not NULL, that value is checked. If that fails, + * P_tmpdir is tried and finally "/tmp". The storage for the filename + * is allocated by `malloc'. + */ + char * -tempnam (const char *dir, const char *pfx) +tempnam(const char *dir, const char *prefix) { - char buf[FILENAME_MAX]; + char buffer[NAME_MAX]; - if (__path_search (buf, FILENAME_MAX, dir, pfx, 1)) - return NULL; + if (__path_search(buffer, NAME_MAX, dir, prefix, 1)) + return NULL; - if (__gen_tempname (buf, __GT_NOCREATE)) - return NULL; + if (__gen_tempname(buffer, __GT_NOCREATE)) + return NULL; - return __strdup (buf); + return strdup(buffer); } -link_warning (tempnam, - "the use of `tempnam' is dangerous, better use `mkstemp'") +//link_warning(tempnam, "the use of `tempnam' is dangerous, better use `mkstemp'") diff --git a/src/kernel/libroot/posix/glibc/stdio-common/tmpnam.c b/src/kernel/libroot/posix/glibc/stdio-common/tmpnam.c index 6b26f4fff6..135c818385 100644 --- a/src/kernel/libroot/posix/glibc/stdio-common/tmpnam.c +++ b/src/kernel/libroot/posix/glibc/stdio-common/tmpnam.c @@ -14,39 +14,41 @@ 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. */ + 02111-1307 USA. +*/ -#include + +#include #include -static char tmpnam_buffer[L_tmpnam]; -/* Generate a unique filename in P_tmpdir. +/** Generate a unique filename in P_tmpdir. + * This function is *not* thread safe! + */ - This function is *not* thread safe! */ char * tmpnam (char *s) { - /* By using two buffers we manage to be thread safe in the case - where S != NULL. */ - char tmpbufmem[L_tmpnam]; - char *tmpbuf = s ?: tmpbufmem; + static char tmpnam_buffer[L_tmpnam]; - /* In the following call we use the buffer pointed to by S if - non-NULL although we don't know the size. But we limit the size - to L_tmpnam characters in any case. */ - if (__builtin_expect (__path_search (tmpbuf, L_tmpnam, NULL, NULL, 0), - 0)) - return NULL; + /* By using two buffers we manage to be thread safe in the case where S != NULL. */ + char tmpbufmem[L_tmpnam]; + char *tmpbuf = s ?: tmpbufmem; - if (__builtin_expect (__gen_tempname (tmpbuf, __GT_NOCREATE), 0)) - return NULL; + /* In the following call we use the buffer pointed to by S if + * non-NULL although we don't know the size. But we limit the size + * to L_tmpnam characters in any case. + */ + if (__builtin_expect(__path_search(tmpbuf, L_tmpnam, NULL, NULL, 0), 0)) + return NULL; - if (s == NULL) - return (char *) memcpy (tmpnam_buffer, tmpbuf, L_tmpnam); + if (__builtin_expect(__gen_tempname(tmpbuf, __GT_NOCREATE), 0)) + return NULL; - return s; + if (s == NULL) + return (char *)memcpy(tmpnam_buffer, tmpbuf, L_tmpnam); + + return s; } -link_warning (tmpnam, - "the use of `tmpnam' is dangerous, better use `mkstemp'") +//link_warning (tmpnam, "the use of `tmpnam' is dangerous, better use `mkstemp'") diff --git a/src/kernel/libroot/posix/glibc/stdio-common/tmpnam_r.c b/src/kernel/libroot/posix/glibc/stdio-common/tmpnam_r.c index 565a42401f..a566d4ccf1 100644 --- a/src/kernel/libroot/posix/glibc/stdio-common/tmpnam_r.c +++ b/src/kernel/libroot/posix/glibc/stdio-common/tmpnam_r.c @@ -14,25 +14,29 @@ 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. */ + 02111-1307 USA. +*/ -#include -/* Generate a unique filename in P_tmpdir. If S is NULL return NULL. - This makes this function thread safe. */ +#include + + +/** Generate a unique filename in P_tmpdir. If S is NULL return NULL. + * This makes this function thread safe. + */ + char * -tmpnam_r (char *s) +tmpnam_r(char *s) { - if (s == NULL) - return NULL; + if (s == NULL) + return NULL; - if (__path_search (s, L_tmpnam, NULL, NULL, 0)) - return NULL; - if (__gen_tempname (s, __GT_NOCREATE)) - return NULL; + if (__path_search(s, L_tmpnam, NULL, NULL, 0)) + return NULL; + if (__gen_tempname(s, __GT_NOCREATE)) + return NULL; - return s; + return s; } -link_warning (tmpnam_r, - "the use of `tmpnam_r' is dangerous, better use `mkstemp'") +//link_warning (tmpnam_r, "the use of `tmpnam_r' is dangerous, better use `mkstemp'")