summaryrefslogtreecommitdiff
path: root/libc/stdio
diff options
context:
space:
mode:
Diffstat (limited to 'libc/stdio')
-rw-r--r--libc/stdio/Makefile2
-rw-r--r--libc/stdio/tmpfile.c51
-rw-r--r--libc/stdio/tmpnam.c83
-rw-r--r--libc/stdio/tmpnam_r.c67
4 files changed, 116 insertions, 87 deletions
diff --git a/libc/stdio/Makefile b/libc/stdio/Makefile
index 26ab9cfcc..3aec83dd1 100644
--- a/libc/stdio/Makefile
+++ b/libc/stdio/Makefile
@@ -41,7 +41,7 @@ MOBJ2=printf.o sprintf.o fprintf.o vprintf.o vsprintf.o vfprintf.o snprintf.o \
MSRC3=scanf.c
MOBJ3=scanf.o sscanf.o fscanf.o vscanf.o vsscanf.o vfscanf.o
-CSRC=popen.c perror.c remove.c getdelim.c getline.c tmpnam.c tmpnam_r.c
+CSRC=popen.c perror.c remove.c getdelim.c getline.c tmpfile.c tmpnam.c tmpnam_r.c
COBJS=$(patsubst %.c,%.o, $(CSRC))
OBJS=$(MOBJ) $(MOBJ2) $(MOBJ3) $(COBJS)
diff --git a/libc/stdio/tmpfile.c b/libc/stdio/tmpfile.c
new file mode 100644
index 000000000..8c11d2ff7
--- /dev/null
+++ b/libc/stdio/tmpfile.c
@@ -0,0 +1,51 @@
+/* Copyright (C) 1991, 1993, 1996, 1997, 1998 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., 59 Temple Place - Suite 330,
+ Boston, MA 02111-1307, USA. */
+
+#include <stdio.h>
+#include <unistd.h>
+
+extern int __path_search (char *tmpl, size_t tmpl_len, const char *dir, const char *pfx,
+ int try_tmpdir);
+extern int __gen_tempname (char *tmpl, int openit);
+
+/* This returns a new stream opened on a temporary file (generated
+ by tmpnam). The file is opened with mode "w+b" (binary read/write).
+ If we couldn't generate a unique filename or the file couldn't
+ be opened, NULL is returned. */
+FILE * tmpfile (void)
+{
+ char buf[FILENAME_MAX];
+ int fd;
+ FILE *f;
+
+ if (__path_search (buf, FILENAME_MAX, NULL, "tmpf", 0))
+ return NULL;
+ fd = __gen_tempname (buf, 1);
+ if (fd < 0)
+ return NULL;
+
+ /* Note that this relies on the Unix semantics that
+ a file is not really removed until it is closed. */
+ (void) remove (buf);
+
+ if ((f = fdopen (fd, "w+b")) == NULL)
+ close (fd);
+
+ return f;
+}
+
diff --git a/libc/stdio/tmpnam.c b/libc/stdio/tmpnam.c
index 3a3ef67ca..296f67f27 100644
--- a/libc/stdio/tmpnam.c
+++ b/libc/stdio/tmpnam.c
@@ -1,68 +1,51 @@
-/* vi: set sw=4 ts=4: */
-/*
- * tmpnam for uClibc
- *
- * Copyright (C) 2000 by David Whedon <dwhedon@gordian.com>
- *
- * This program 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.
- *
- * This program 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 this program; if not, write to the Free Software Foundation,
- * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- *
- * Modified by Erik Andersen <anderse@debian.org> to be reentrant for
- * the case when S != NULL...
- */
+/* Copyright (C) 1991, 1993, 1996, 1997, 1998 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., 59 Temple Place - Suite 330,
+ Boston, MA 02111-1307, USA. */
#include <stdio.h>
#include <string.h>
-#include <unistd.h>
-#include <sys/types.h>
-#include <sys/stat.h>
+extern int __path_search (char *tmpl, size_t tmpl_len, const char *dir, const char *pfx,
+ int try_tmpdir);
+extern int __gen_tempname (char *tmpl, int openit);
static char tmpnam_buffer[L_tmpnam];
+/* Generate a unique filename in P_tmpdir.
-/* Generate a unique filename in /tmp */
+ This function is *not* thread safe when S == NULL! */
char * tmpnam (char *s)
{
- int num __attribute__ ((unused)); /* UNINITIALIZED, so we get whatever crap
- happens to be in memory, producing (in theory)
- pseudo-random tmpname results... */
- int n2;
- char buf[L_tmpnam], *ptr;
- struct stat statbuf;
- unsigned char l, i;
-
- ptr=s ? s : buf;
-
- l = snprintf(ptr, L_tmpnam, "%s/tmp.", P_tmpdir);
+ /* By using two buffers we manage to be thread safe in the case
+ where S != NULL. */
+ char tmpbuf[L_tmpnam];
-again:
- n2 = num;
- for (i = l ; i < l + 6; i++) {
- ptr[i] = '0' + n2 % 10;
- n2 /= 10;
- }
+ /* 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 (__path_search (s ? : tmpbuf, L_tmpnam, NULL, NULL, 0))
+ return NULL;
- if (stat (ptr, &statbuf) == 0){
- num++;
- goto again;
- }
+ if (__gen_tempname (s ? : tmpbuf, 0))
+ return NULL;
if (s == NULL)
- return (char *) memcpy (tmpnam_buffer, ptr, L_tmpnam);
+ return (char *) memcpy (tmpnam_buffer, tmpbuf, L_tmpnam);
- return ptr;
+ return s;
}
diff --git a/libc/stdio/tmpnam_r.c b/libc/stdio/tmpnam_r.c
index 5533a399f..630d96eb3 100644
--- a/libc/stdio/tmpnam_r.c
+++ b/libc/stdio/tmpnam_r.c
@@ -1,43 +1,38 @@
-/* vi: set sw=4 ts=4: */
-/*
- * tmpnam for uClibc
- *
- * Copyright (C) 2000 by Lineo, inc. Written by Erik Andersen
- * <andersen@lineo.com>, <andersee@debian.org>
- *
- * This program 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.
- *
- * This program 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 this program; if not, write to the Free Software Foundation,
- * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- *
- * Modified by Erik Andersen <anderse@debian.org> to be reentrant for
- * the case when S != NULL...
- */
+/* Copyright (C) 1991, 1993, 1996, 1997, 1998 Free Software Foundation, Inc.
+ This file is part of the GNU C Library.
-#include <stdio.h>
-#include <string.h>
-#include <unistd.h>
-#include <sys/types.h>
-#include <sys/stat.h>
+ 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., 59 Temple Place - Suite 330,
+ Boston, MA 02111-1307, USA. */
+
+#include <stdio.h>
-/* Generate a unique filename in /tmp.
- * If s is NULL return NULL, making this function thread safe. */
+extern int __path_search (char *tmpl, size_t tmpl_len, const char *dir, const char *pfx,
+ int try_tmpdir);
+extern int __gen_tempname (char *tmpl, int openit);
+/* 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;
- else
- return (tmpnam(s));
-}
+ if (s == NULL)
+ return NULL;
+
+ if (__path_search (s, L_tmpnam, NULL, NULL, 0))
+ return NULL;
+ if (__gen_tempname (s, 0))
+ return NULL;
+ return s;
+}