From 0fe8d5df2c21ce62cea6cfb426965dab019af3c6 Mon Sep 17 00:00:00 2001 From: Eric Andersen Date: Wed, 18 Apr 2001 20:13:13 +0000 Subject: 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). --- libc/stdlib/mkstemp.c | 61 ++++++++++++++++++++------------------------------- libc/stdlib/mktemp.c | 61 +++++++++++++++++++++++---------------------------- 2 files changed, 51 insertions(+), 71 deletions(-) (limited to 'libc/stdlib') 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 -#include -#include -#include -#include +/* 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 +#include - 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 -#include -#include -#include -#include -#include +/* 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 +#include - 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; } -- cgit v1.2.3