summaryrefslogtreecommitdiff
path: root/libc/stdlib/mkstemp.c
diff options
context:
space:
mode:
Diffstat (limited to 'libc/stdlib/mkstemp.c')
-rw-r--r--libc/stdlib/mkstemp.c61
1 files changed, 24 insertions, 37 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);
}