summaryrefslogtreecommitdiff
path: root/libc/stdlib
diff options
context:
space:
mode:
authorEric Andersen <andersen@codepoet.org>2001-04-18 20:13:13 +0000
committerEric Andersen <andersen@codepoet.org>2001-04-18 20:13:13 +0000
commit0fe8d5df2c21ce62cea6cfb426965dab019af3c6 (patch)
treeb302e7a7c3ff8d393183b64a4f44018e664de881 /libc/stdlib
parent7b6a9548ee32cb45913384fd321f53f46fcd1d3d (diff)
Update temp file handling and use a single function to do all
the heavy lifting (most of the heavy lifting stolen from glibc, but reduced a bit).
Diffstat (limited to 'libc/stdlib')
-rw-r--r--libc/stdlib/mkstemp.c61
-rw-r--r--libc/stdlib/mktemp.c61
2 files changed, 51 insertions, 71 deletions
diff --git a/libc/stdlib/mkstemp.c b/libc/stdlib/mkstemp.c
index 241f8af8a..d0104c8f3 100644
--- a/libc/stdlib/mkstemp.c
+++ b/libc/stdlib/mkstemp.c
@@ -1,44 +1,31 @@
-#include <string.h>
-#include <features.h>
-#include <errno.h>
-#include <unistd.h>
-#include <fcntl.h>
+/* Copyright (C) 1998 Free Software Foundation, Inc.
+ This file is part of the GNU C Library.
-int mkstemp(template)
-char *template;
-{
- int i;
- int num __attribute__ ((unused)); /* UNINITIALIZED */
- int n2;
- int l = strlen(template);
-
- if (l < 6) {
- __set_errno(EINVAL);
- return -1;
- }
+ 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.
- for (i = l - 6; i < l; i++)
- if (template[i] != 'X') {
- __set_errno(EINVAL);
- return -1;
- }
+ 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.
- again:
- n2 = num;
- for (i = l - 1; i >= l - 6; i--) {
- template[i] = '0' + n2 % 10;
- n2 /= 10;
- }
+ 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. */
- i = open(template, O_RDWR | O_EXCL | O_CREAT, 0666);
+#include <stdio.h>
+#include <stdlib.h>
- if (i == -1) {
- if (errno == EEXIST) {
- num++;
- goto again;
- } else
- return -1;
- }
+extern int __gen_tempname (char *tmpl, int openit);
- return i;
+/* Generate a unique temporary file name from TEMPLATE.
+ The last six characters 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 mkstemp (char *template)
+{
+ return __gen_tempname (template, 1);
}
diff --git a/libc/stdlib/mktemp.c b/libc/stdlib/mktemp.c
index 9d65d0441..432d64f2a 100644
--- a/libc/stdlib/mktemp.c
+++ b/libc/stdlib/mktemp.c
@@ -1,41 +1,34 @@
-#include <string.h>
-#include <features.h>
-#include <errno.h>
-#include <unistd.h>
-#include <fcntl.h>
-#include <sys/stat.h>
+/* Copyright (C) 1998 Free Software Foundation, Inc.
+ This file is part of the GNU C Library.
-char *mktemp(template)
-char *template;
-{
- int i;
- int num __attribute__ ((unused)); /* UNINITIALIZED */
- int n2;
- int l = strlen(template);
- struct stat stbuf;
+ 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.
- if (l < 6) {
- __set_errno(EINVAL);
- return 0;
- }
+ 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. */
- for (i = l - 6; i < l; i++)
- if (template[i] != 'X') {
- __set_errno(EINVAL);
- return 0;
- }
+#include <stdio.h>
+#include <stdlib.h>
- again:
- n2 = num;
- for (i = l - 1; i >= l - 6; i--) {
- template[i] = '0' + n2 % 10;
- n2 /= 10;
- }
+extern int __gen_tempname (char *tmpl, int openit);
- if (stat(template, &stbuf) == 0) {
- num++;
- goto again;
- }
+/* Generate a unique temporary file name from TEMPLATE.
+ The last six characters of TEMPLATE must be "XXXXXX";
+ they are replaced with a string that makes the filename unique. */
+char * mktemp (char *template)
+{
+ if (__gen_tempname (template, 0) < 0)
+ /* We return the null string if we can't find a unique file name. */
+ template[0] = '\0';
- return template;
+ return template;
}