about summary refs log tree commit diff
path: root/stdio-common
diff options
context:
space:
mode:
Diffstat (limited to 'stdio-common')
-rw-r--r--stdio-common/tempnam.c14
-rw-r--r--stdio-common/temptest.c3
-rw-r--r--stdio-common/tmpfile.c8
-rw-r--r--stdio-common/tmpnam.c43
-rw-r--r--stdio-common/tmpnam_r.c37
5 files changed, 75 insertions, 30 deletions
diff --git a/stdio-common/tempnam.c b/stdio-common/tempnam.c
index 14988a8656..a86ac08610 100644
--- a/stdio-common/tempnam.c
+++ b/stdio-common/tempnam.c
@@ -1,4 +1,4 @@
-/* Copyright (C) 1991, 1993 Free Software Foundation, Inc.
+/* Copyright (C) 1991, 1993, 1996 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
@@ -16,7 +16,6 @@ License along with the GNU C Library; see the file COPYING.LIB.  If
 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
 Cambridge, MA 02139, USA.  */
 
-#include <ansidecl.h>
 #include <errno.h>
 #include <stddef.h>
 #include <stdio.h>
@@ -32,19 +31,20 @@ Cambridge, MA 02139, USA.  */
    P_tmpdir is tried and finally "/tmp".  The storage for the filename
    is allocated by `malloc'.  */
 char *
-DEFUN(tempnam, (dir, pfx), CONST char *dir AND CONST char *pfx)
+tempnam (const char *dir, const char *pfx)
 {
+  char buf[FILENAME_MAX];
   size_t len;
   register char *s;
-  register char *t = __stdio_gen_tempname(dir, pfx, 1, &len, (FILE **) NULL);
+  register char *t = __stdio_gen_tempname (buf, sizeof (buf), dir, pfx, 1,
+					   &len, (FILE **) NULL);
 
   if (t == NULL)
     return NULL;
 
-  s = (char *) malloc(len);
+  s = (char *) malloc (len);
   if (s == NULL)
     return NULL;
 
-  (void) memcpy(s, t, len);
-  return s;
+  return (char *) memcpy (s, t, len);
 }
diff --git a/stdio-common/temptest.c b/stdio-common/temptest.c
index 374719896a..09786b25e7 100644
--- a/stdio-common/temptest.c
+++ b/stdio-common/temptest.c
@@ -7,12 +7,13 @@ char *files[500];
 int
 main ()
 {
+  char buf[FILENAME_MAX];
   char *fn;
   FILE *fp;
   int i;
 
   for (i = 0; i < 500; i++) {
-    fn = __stdio_gen_tempname((CONST char *) NULL,
+    fn = __stdio_gen_tempname(buf, sizeof (buf), (CONST char *) NULL,
 	"file", 0, (size_t *) NULL, (FILE **) NULL);
     if (fn == NULL) {
       printf ("__stdio_gen_tempname failed\n");
diff --git a/stdio-common/tmpfile.c b/stdio-common/tmpfile.c
index dfe11ada50..924df9b6bb 100644
--- a/stdio-common/tmpfile.c
+++ b/stdio-common/tmpfile.c
@@ -1,4 +1,4 @@
-/* Copyright (C) 1991, 1993 Free Software Foundation, Inc.
+/* Copyright (C) 1991, 1993, 1996 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
@@ -16,7 +16,6 @@ License along with the GNU C Library; see the file COPYING.LIB.  If
 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
 Cambridge, MA 02139, USA.  */
 
-#include <ansidecl.h>
 #include <stdio.h>
 
 
@@ -25,12 +24,13 @@ Cambridge, MA 02139, USA.  */
    If we couldn't generate a unique filename or the file couldn't
    be opened, NULL is returned.  */
 FILE *
-DEFUN_VOID(tmpfile)
+tmpfile ()
 {
+  char buf[FILENAME_MAX];
   char *filename;
   FILE *f;
 
-  filename = __stdio_gen_tempname ((char *) NULL, "tmpf", 0,
+  filename = __stdio_gen_tempname (buf, sizeof (buf), (char *) NULL, "tmpf", 0,
 				   (size_t *) NULL, &f);
   if (filename == NULL)
     return NULL;
diff --git a/stdio-common/tmpnam.c b/stdio-common/tmpnam.c
index 88dd0a4ca5..44397bc3f2 100644
--- a/stdio-common/tmpnam.c
+++ b/stdio-common/tmpnam.c
@@ -1,4 +1,4 @@
-/* Copyright (C) 1991, 1993 Free Software Foundation, Inc.
+/* Copyright (C) 1991, 1993, 1996 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
@@ -16,27 +16,34 @@ License along with the GNU C Library; see the file COPYING.LIB.  If
 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
 Cambridge, MA 02139, USA.  */
 
-#include <ansidecl.h>
-#include <stddef.h>
 #include <stdio.h>
 #include <string.h>
 
 
-/* Generate a unique filename in P_tmpdir.  */
+/* Generate a unique filename in P_tmpdir.
+
+   This function is *not* thread safe!  */
 char *
-DEFUN(tmpnam, (s), register char *s)
+tmpnam (char *s)
 {
-  register char *t = __stdio_gen_tempname((CONST char *) NULL,
-					  (CONST char *) NULL, 0,
-					  (size_t *) NULL, (FILE **) NULL);
-
-  if (t == NULL)
-    return NULL;
-
-  if (s != NULL)
-    (void) strcpy(s, t);
-  else
-    s = t;
-
-  return s;
+  /* By using two buffers we manage to be thread safe in the case
+     where S != NULL.  */
+  static char buf[L_tmpnam];
+  char *tmpbuf[L_tmpnam];
+  char *result;
+
+  /* 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 FILENAME_MAX characters in any case.  */
+  result = __stdio_gen_tempname (s ?: tmpbuf, L_tmpnam, (const char *) NULL,
+				 (const char *) NULL, 0,
+				 (size_t *) NULL, (FILE **) NULL);
+
+  if (result != NULL && s == NULL)
+    {
+      memcpy (buf, result, L_tmpnam);
+      result = buf;
+    }
+
+  return result;
 }
diff --git a/stdio-common/tmpnam_r.c b/stdio-common/tmpnam_r.c
new file mode 100644
index 0000000000..2794e7728e
--- /dev/null
+++ b/stdio-common/tmpnam_r.c
@@ -0,0 +1,37 @@
+/* Copyright (C) 1991, 1993, 1996 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 Library General Public License as
+published by the Free Software Foundation; either version 2 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
+Library General Public License for more details.
+
+You should have received a copy of the GNU Library General Public
+License along with the GNU C Library; see the file COPYING.LIB.  If
+not, write to the Free Software Foundation, Inc., 675 Mass Ave,
+Cambridge, MA 02139, USA.  */
+
+#include <stdio.h>
+#include <string.h>
+
+
+/* Generate a unique filename in P_tmpdir.  If S is NULL return NULL.
+   This makes this function thread safe.  */
+char *
+tmpnam_r (char *s)
+{
+  if (s == NULL)
+    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.  */
+  return __stdio_gen_tempname (s, L_tmpnam, (const char *) NULL,
+			       (const char *) NULL, 0,
+			       (size_t *) NULL, (FILE **) NULL);
+}