bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: mingw conflict between mkdtemp and sys_stat


From: Eric Blake
Subject: Re: mingw conflict between mkdtemp and sys_stat
Date: Fri, 13 Oct 2006 22:19:36 +0000 (UTC)
User-agent: Loom/3.14 (http://gmane.org/)

Eric Blake <ebb9 <at> byu.net> writes:

> 
> For that matter, it is rather enlightening to compare tmpdir.c/mkdtemp.c 
> against tempname.c/mkstemp.c - both are based on the same upstream glibc 
> files.  Bruno and Paul, would you be willing to accept a patch that does the 
> following things?
> 
> - Break tempname.c into a full-fledged module, and make the mkstemp module 
> depend on the new module rather than including the file itself.
> - Carve direxists/__path_search out of tempname.c, and make the (new) 
tempname 
> module depend on the tmpdir module instead.
> - Carve gen_tempname out of mkdtemp.c, and make the mkdtemp module depend on 
> the new tempname module instead.
> - Double check with glibc to make sure we have all the appropriate upstream 
bug 
> fixes ported to gnulib.
> - Make sure the final result behaves well on mingw.
> 

Something like this.  Using gnulib-tool --test, I tested the four modules 
tmpdir, tempname, mkstemp, and mkdtemp; separately and together; on both mingw 
(which lacks mk[sd]temp) and cygwin (which has both).  I also tested using m4, 
which uses both mkstemp and mkdtemp modules, on cygwin, mingw, and Linux.

Best of all, this patch deletes more than twice as many lines than what it adds 
in new files :)

2006-10-13  Eric Blake  <address@hidden>

        * modules/mkdtemp (Depends-on): Add tempname.
        * modules/mkstemp (Depends-on): Add tempname, drop transitive
        dependencies.
        (Files): Drop lib/tempname.c.
        * modules/tempname: New module.
        * MODULES.html.sh: Document tempname.
        * lib/tempname.h: New file.
        * lib/mkstemp.c: Simplify, thanks to tempname.h.
        * lib/tempname.c (direxists, __path_search): Remove; tmpdir covers
        these.
        * lib/mkdtemp.c (gen_tempname): Remove; tempname covers this.
        * m4/mkdtemp.m4 (gl_PREREQ_MKDTEMP): Simplify, thanks to tempname.
        * m4/mkstemp.m4 (gl_FUNC_MKSTEMP): Remove tempname checks.
        (gl_PREREQ_TEMPNAME): Move to tempname.m4.
        * m4/tempname.m4: New file.

Index: MODULES.html.sh
===================================================================
RCS file: /sources/gnulib/gnulib/MODULES.html.sh,v
retrieving revision 1.151
diff -u -r1.151 MODULES.html.sh
--- MODULES.html.sh     18 Sep 2006 13:47:29 -0000      1.151
+++ MODULES.html.sh     13 Oct 2006 22:13:08 -0000
@@ -1338,10 +1338,10 @@
     func_echo "<TD ALIGN=LEFT VALIGN=TOP WIDTH=\"20%\">$element"
 
     element=`gnulib-tool --extract-description $1 \
-             | sed -e "$sed_lt" -e "$sed_gt" -
e "$sed_remove_trailing_empty_line" \
-             | sed -e 's,^, ,' \
-             | sed -e 's,\([^a-zA-Z]\)'"${posix_functions}"'(),\1<A 
HREF="'"$POSIX2001_URL"'xsh/\2.html">\2</A>(),g' \
-             | sed -e 's,^ ,,'`
+            | sed -e "$sed_lt" -e "$sed_gt" -
e "$sed_remove_trailing_empty_line" \
+            | sed -e 's,^, ,' \
+            | sed -e 's,\([^a-zA-Z]\)'"${posix_functions}"'(),\1<A 
HREF="'"$POSIX2001_URL"'xsh/\2.html">\2</A>(),g' \
+            | sed -e 's,^ ,,'`
     func_echo "<TD ALIGN=LEFT VALIGN=TOP WIDTH=\"80%\">$element"
 
     func_end TR
@@ -1354,42 +1354,42 @@
     func_echo "<TD ALIGN=LEFT VALIGN=TOP>$element"
 
     element=`gnulib-tool --extract-include-directive $1 \
-             | sed -e "$sed_lt" -e "$sed_gt" -
e "$sed_remove_trailing_empty_line" \
-             | sed -e 's,^#include "\(.*\)"$,#include "<A HREF="lib/
\1">\1</A>",' \
-                    -e 's,^#include &lt;'"${posix_headers}"'\.h&gt;$,#include 
\&lt;<A HREF="'"$POSIX2001_URL"'xbd/\1.h.html">\1.h</A>\&gt;,' \
-             | sed -e 's/$/<BR>/' | tr -d "$trnl" | sed -e 's/<BR>$//'`
+            | sed -e "$sed_lt" -e "$sed_gt" -
e "$sed_remove_trailing_empty_line" \
+            | sed -e 's,^#include "\(.*\)"$,#include "<A HREF="lib/
\1">\1</A>",' \
+                   -e 's,^#include &lt;'"${posix_headers}"'\.h&gt;$,#include 
\&lt;<A HREF="'"$POSIX2001_URL"'xbd/\1.h.html">\1.h</A>\&gt;,' \
+            | sed -e 's/$/<BR>/' | tr -d "$trnl" | sed -e 's/<BR>$//'`
     test -n "$element" || element='---'
     func_echo "<TD ALIGN=LEFT VALIGN=TOP>$element"
 
     sed_choose_unconditional_nonstandard_include='s,^#include "\(.*\)"$,\1,p'
     includefile=`gnulib-tool --extract-include-directive $1 \
-                 | sed -n -e "$sed_choose_unconditional_nonstandard_include" \
-                 | sed -e "$sed_escape_dot" | tr -d "$trnl"`
+                | sed -n -e "$sed_choose_unconditional_nonstandard_include" \
+                | sed -e "$sed_escape_dot" | tr -d "$trnl"`
     sed_choose_lib_files='s,^lib/\(.*\)$,\1,p'
     element=`gnulib-tool --extract-filelist $1 \
-             | sed -e '/^$/d' \
-             | sed -n -e "$sed_choose_lib_files" \
-             | sed -e '/^'"${includefile}"'$/d' \
-             | sed -e 's,^\(.*\)$,<A HREF="lib/\1">\1</A>,' \
-             | sed -e 's/$/<BR>/' | tr -d "$trnl" | sed -e 's/<BR>$//'`
+            | sed -e '/^$/d' \
+            | sed -n -e "$sed_choose_lib_files" \
+            | sed -e '/^'"${includefile}"'$/d' \
+            | sed -e 's,^\(.*\)$,<A HREF="lib/\1">\1</A>,' \
+            | sed -e 's/$/<BR>/' | tr -d "$trnl" | sed -e 's/<BR>$//'`
     test -n "$element" || element='---'
     func_echo "<TD ALIGN=LEFT VALIGN=TOP>$element"
 
     sed_choose_m4_files='s,^m4/\(.*\)$,\1,p'
     element=`(gnulib-tool --extract-filelist $1 \
-              | sed -e "$sed_remove_trailing_empty_line" \
-              | sed -n -e "$sed_choose_m4_files" \
-              | sed -e '/^onceonly/d' \
-              | sed -e 's,^\(.*\)$,<A HREF="m4/\1">\1</A>,'; \
-              gnulib-tool --extract-autoconf-snippet $1 \
-              | sed -e "$sed_remove_trailing_empty_line") \
-              | sed -e 's/$/<BR>/' | tr -d "$trnl" | sed -e 's/<BR>$//'`
+             | sed -e "$sed_remove_trailing_empty_line" \
+             | sed -n -e "$sed_choose_m4_files" \
+             | sed -e '/^onceonly/d' \
+             | sed -e 's,^\(.*\)$,<A HREF="m4/\1">\1</A>,'; \
+             gnulib-tool --extract-autoconf-snippet $1 \
+             | sed -e "$sed_remove_trailing_empty_line") \
+             | sed -e 's/$/<BR>/' | tr -d "$trnl" | sed -e 's/<BR>$//'`
     test -n "$element" || element='---'
     func_echo "<TD ALIGN=LEFT VALIGN=TOP>$element"
 
     element=`gnulib-tool --extract-dependencies $1 \
-             | sed -e "$sed_remove_trailing_empty_line" \
-             | sed -e 's/$/<BR>/' | tr -d "$trnl" | sed -e 's/<BR>$//'`
+            | sed -e "$sed_remove_trailing_empty_line" \
+            | sed -e 's/$/<BR>/' | tr -d "$trnl" | sed -e 's/<BR>$//'`
     test -n "$element" || element='---'
     func_echo "<TD ALIGN=LEFT VALIGN=TOP>$element"
 
@@ -1401,12 +1401,12 @@
     sed_removal_prefix='s,^,/^,'
     sed_removal_suffix='s,$,\$/d,'
     sed_remove_files=`echo '{'; \
-                      for file in $files; do \
-                        echo $file \
-                        | sed -e "$sed_escape_dot" -e "$sed_escape_slash" \
-                        | sed -e "$sed_removal_prefix" -
e "$sed_removal_suffix"; \
-                      done; \
-                      echo '}'`
+                     for file in $files; do \
+                       echo $file \
+                       | sed -e "$sed_escape_dot" -e "$sed_escape_slash" \
+                       | sed -e "$sed_removal_prefix" -
e "$sed_removal_suffix"; \
+                     done; \
+                     echo '}'`
     missed_files=`echo "$missed_files" | sed -e "$sed_remove_files"`
 
   fi
@@ -1843,6 +1843,7 @@
   func_module poll
   func_module readlink
   func_module lstat
+  func_module tempname
   func_module time_r
   func_module timespec
   func_module nanosleep
Index: modules/mkdtemp
===================================================================
RCS file: /sources/gnulib/gnulib/modules/mkdtemp,v
retrieving revision 1.6
diff -u -r1.6 mkdtemp
--- modules/mkdtemp     13 Oct 2006 12:40:23 -0000      1.6
+++ modules/mkdtemp     13 Oct 2006 22:13:08 -0000
@@ -8,6 +8,7 @@
 
 Depends-on:
 stdint
+tempname
 unistd
 
 configure.ac:
Index: modules/mkstemp
===================================================================
RCS file: /sources/gnulib/gnulib/modules/mkstemp,v
retrieving revision 1.13
diff -u -r1.13 mkstemp
--- modules/mkstemp     13 Oct 2006 12:40:23 -0000      1.13
+++ modules/mkstemp     13 Oct 2006 22:13:08 -0000
@@ -4,14 +4,11 @@
 Files:
 lib/mkstemp.h
 lib/mkstemp.c
-lib/tempname.c
 m4/mkstemp.m4
 
 Depends-on:
 extensions
-stat-macros
-stdint
-sys_stat
+tempname
 
 configure.ac:
 gl_FUNC_MKSTEMP
Index: modules/tempname
===================================================================
RCS file: modules/tempname
diff -N modules/tempname
--- /dev/null   1 Jan 1970 00:00:00 -0000
+++ modules/tempname    13 Oct 2006 22:13:08 -0000
@@ -0,0 +1,27 @@
+Description:
+gen_tempname() function: create a private temporary file or directory.
+
+Files:
+lib/tempname.h
+lib/tempname.c
+m4/tempname.m4
+
+Depends-on:
+extensions
+stat-macros
+stdint
+sys_stat
+
+configure.ac:
+gl_FUNC_GEN_TEMPNAME
+
+Makefile.am:
+
+Include:
+"tempname.h"
+
+License:
+GPL
+
+Maintainer:
+Eric Blake and Jim Meyering
Index: m4/mkdtemp.m4
===================================================================
RCS file: /sources/gnulib/gnulib/m4/mkdtemp.m4,v
retrieving revision 1.4
diff -u -r1.4 mkdtemp.m4
--- m4/mkdtemp.m4       11 Sep 2006 12:35:15 -0000      1.4
+++ m4/mkdtemp.m4       13 Oct 2006 22:13:08 -0000
@@ -15,8 +15,6 @@
 # Prerequisites of lib/mkdtemp.c
 AC_DEFUN([gl_PREREQ_MKDTEMP],
 [
-  AC_REQUIRE([AC_HEADER_STAT])
-  AC_CHECK_HEADERS_ONCE(sys/time.h unistd.h)
-  AC_CHECK_HEADERS(time.h)
-  AC_CHECK_FUNCS(gettimeofday)
+  :
+  AC_CHECK_HEADERS_ONCE([unistd.h])
 ])
Index: m4/mkstemp.m4
===================================================================
RCS file: /sources/gnulib/gnulib/m4/mkstemp.m4,v
retrieving revision 1.19
diff -u -r1.19 mkstemp.m4
--- m4/mkstemp.m4       20 Sep 2006 18:44:04 -0000      1.19
+++ m4/mkstemp.m4       13 Oct 2006 22:13:08 -0000
@@ -1,4 +1,4 @@
-#serial 14
+#serial 15
 
 # Copyright (C) 2001, 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
 # This file is free software; the Free Software Foundation
@@ -46,9 +46,7 @@
     AC_DEFINE([__MKSTEMP_PREFIX], [[rpl_]],
       [Define to rpl_ if the mkstemp replacement function should be used.])
     AC_LIBOBJ(mkstemp)
-    AC_LIBOBJ(tempname)
     gl_PREREQ_MKSTEMP
-    gl_PREREQ_TEMPNAME
   fi
 ])
 
@@ -56,10 +54,3 @@
 AC_DEFUN([gl_PREREQ_MKSTEMP],
 [
 ])
-
-# Prerequisites of lib/tempname.c.
-AC_DEFUN([gl_PREREQ_TEMPNAME],
-[
-  AC_CHECK_HEADERS_ONCE(sys/time.h)
-  AC_CHECK_FUNCS(__secure_getenv gettimeofday)
-])
Index: m4/tempname.m4
===================================================================
RCS file: m4/tempname.m4
diff -N m4/tempname.m4
--- /dev/null   1 Jan 1970 00:00:00 -0000
+++ m4/tempname.m4      13 Oct 2006 22:13:08 -0000
@@ -0,0 +1,27 @@
+#serial 1
+
+# Copyright (C) 2006 Free Software Foundation, Inc.
+# This file is free software; the Free Software Foundation
+# gives unlimited permission to copy and/or distribute it,
+# with or without modifications, as long as this notice is preserved.
+
+# Some systems provide gen_tempname as a wrapper for mk[ds]temp.  This
+# provides it for systems that are lacking.
+AC_DEFUN([gl_FUNC_GEN_TEMPNAME],
+[
+  AC_REQUIRE([AC_SYS_LARGEFILE])
+
+  AC_CHECK_FUNCS_ONCE([gen_tempname])
+
+  if test $ac_cv_func_gen_tempname != yes; then
+    AC_LIBOBJ([tempname])
+    gl_PREREQ_TEMPNAME
+  fi
+])
+
+# Prerequisites of lib/tempname.c.
+AC_DEFUN([gl_PREREQ_TEMPNAME],
+[
+  AC_CHECK_HEADERS_ONCE([sys/time.h])
+  AC_CHECK_FUNCS_ONCE([gettimeofday])
+])
Index: lib/mkdtemp.c
===================================================================
RCS file: /sources/gnulib/gnulib/lib/mkdtemp.c,v
retrieving revision 1.12
diff -u -r1.12 mkdtemp.c
--- lib/mkdtemp.c       20 Sep 2006 20:18:02 -0000      1.12
+++ lib/mkdtemp.c       13 Oct 2006 22:13:08 -0000
@@ -22,176 +22,9 @@
 /* Specification.  */
 #include "mkdtemp.h"
 
-#include <errno.h>
-#ifndef __set_errno
-# define __set_errno(Val) errno = (Val)
-#endif
+#include "tempname.h"
 
-#include <stddef.h>
-#include <stdint.h>
 #include <stdlib.h>
-#include <string.h>
-
-#include <stdio.h>
-#ifndef TMP_MAX
-# define TMP_MAX 238328
-#endif
-
-#include <unistd.h>
-
-#if HAVE_GETTIMEOFDAY || _LIBC
-# if HAVE_SYS_TIME_H || _LIBC
-#  include <sys/time.h>
-# endif
-#else
-# if HAVE_TIME_H || _LIBC
-#  include <time.h>
-# endif
-#endif
-
-#include <sys/stat.h>
-#if !defined S_ISDIR && defined S_IFDIR
-# define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)
-#endif
-#if !S_IRUSR && S_IREAD
-# define S_IRUSR S_IREAD
-#endif
-#if !S_IRUSR
-# define S_IRUSR 00400
-#endif
-#if !S_IWUSR && S_IWRITE
-# define S_IWUSR S_IWRITE
-#endif
-#if !S_IWUSR
-# define S_IWUSR 00200
-#endif
-#if !S_IXUSR && S_IEXEC
-# define S_IXUSR S_IEXEC
-#endif
-#if !S_IXUSR
-# define S_IXUSR 00100
-#endif
-
-#ifdef __MINGW32__
-# include <io.h>
-/* mingw's _mkdir() function has 1 argument, but we pass 2 arguments.
-   Therefore we have to disable the argument count checking.  */
-# define mkdir ((int (*)()) _mkdir)
-#endif
-
-#if !_LIBC
-# define __getpid getpid
-# define __gettimeofday gettimeofday
-# define __mkdir mkdir
-#endif
-
-/* Use the widest available unsigned type if uint64_t is not
-   available.  The algorithm below extracts a number less than 62**6
-   (approximately 2**35.725) from uint64_t, so ancient hosts where
-   uintmax_t is only 32 bits lose about 3.725 bits of randomness,
-   which is better than not having mkstemp at all.  */
-#if !defined UINT64_MAX && !defined uint64_t
-# define uint64_t uintmax_t
-#endif
-
-/* These are the characters used in temporary filenames.  */
-static const char letters[] =
-"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
-
-/* Generate a temporary file name based on TMPL.  TMPL must match the
-   rules for mk[s]temp (i.e. end in "XXXXXX").  The name constructed
-   does not exist at the time of the call to __gen_tempname.  TMPL is
-   overwritten with the result.
-
-   KIND is:
-   __GT_DIR:           create a directory, which will be mode 0700.
-
-   We use a clever algorithm to get hard-to-predict names. */
-static int
-gen_tempname (char *tmpl)
-{
-  int len;
-  char *XXXXXX;
-  static uint64_t value;
-  uint64_t random_time_bits;
-  unsigned int count;
-  int fd = -1;
-  int save_errno = errno;
-
-  /* A lower bound on the number of temporary files to attempt to
-     generate.  The maximum total number of temporary file names that
-     can exist for a given template is 62**6.  It should never be
-     necessary to try all these combinations.  Instead if a reasonable
-     number of names is tried (we define reasonable as 62**3) fail to
-     give the system administrator the chance to remove the problems.  */
-#define ATTEMPTS_MIN (62 * 62 * 62)
-
-  /* The number of times to attempt to generate a temporary file.  To
-     conform to POSIX, this must be no smaller than TMP_MAX.  */
-#if ATTEMPTS_MIN < TMP_MAX
-  unsigned int attempts = TMP_MAX;
-#else
-  unsigned int attempts = ATTEMPTS_MIN;
-#endif
-
-  len = strlen (tmpl);
-  if (len < 6 || strcmp (&tmpl[len - 6], "XXXXXX"))
-    {
-      __set_errno (EINVAL);
-      return -1;
-    }
-
-  /* This is where the Xs start.  */
-  XXXXXX = &tmpl[len - 6];
-
-  /* Get some more or less random data.  */
-#ifdef RANDOM_BITS
-  RANDOM_BITS (random_time_bits);
-#else
-# if HAVE_GETTIMEOFDAY || _LIBC
-  {
-    struct timeval tv;
-    __gettimeofday (&tv, NULL);
-    random_time_bits = ((uint64_t) tv.tv_usec << 16) ^ tv.tv_sec;
-  }
-# else
-  random_time_bits = time (NULL);
-# endif
-#endif
-  value += random_time_bits ^ __getpid ();
-
-  for (count = 0; count < attempts; value += 7777, ++count)
-    {
-      uint64_t v = value;
-
-      /* Fill in the random bits.  */
-      XXXXXX[0] = letters[v % 62];
-      v /= 62;
-      XXXXXX[1] = letters[v % 62];
-      v /= 62;
-      XXXXXX[2] = letters[v % 62];
-      v /= 62;
-      XXXXXX[3] = letters[v % 62];
-      v /= 62;
-      XXXXXX[4] = letters[v % 62];
-      v /= 62;
-      XXXXXX[5] = letters[v % 62];
-
-      fd = __mkdir (tmpl, S_IRUSR | S_IWUSR | S_IXUSR);
-
-      if (fd >= 0)
-       {
-         __set_errno (save_errno);
-         return fd;
-       }
-      else if (errno != EEXIST)
-       return -1;
-    }
-
-  /* We got out of the loop because we ran out of combinations to try.  */
-  __set_errno (EEXIST);
-  return -1;
-}
 
 /* Generate a unique temporary directory from TEMPLATE.
    The last six characters of TEMPLATE must be "XXXXXX";
@@ -201,7 +34,7 @@
 char *
 mkdtemp (char *template)
 {
-  if (gen_tempname (template))
+  if (gen_tempname (template, __GT_DIR))
     return NULL;
   else
     return template;
Index: lib/mkstemp.c
===================================================================
RCS file: /sources/gnulib/gnulib/lib/mkstemp.c,v
retrieving revision 1.8
diff -u -r1.8 mkstemp.c
--- lib/mkstemp.c       20 Sep 2006 18:44:04 -0000      1.8
+++ lib/mkstemp.c       13 Oct 2006 22:13:08 -0000
@@ -15,26 +15,17 @@
    with this program; if not, write to the Free Software Foundation,
    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
 
-#if !_LIBC
-# include <config.h>
-# include "mkstemp.h"
-int __gen_tempname ();
-#endif
+#include <config.h>
 
-#include <stdio.h>
-#include <stdlib.h>
-
-#ifndef __GT_FILE
-# define __GT_FILE 0
-#endif
+#include "mkstemp.h"
+#include "tempname.h"
 
 /* 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 file name unique.
    Then open the file and return a fd. */
 int
-mkstemp (template)
-     char *template;
+mkstemp (char *template)
 {
-  return __gen_tempname (template, __GT_FILE);
+  return gen_tempname (template, __GT_FILE);
 }
Index: lib/tempname.c
===================================================================
RCS file: /sources/gnulib/gnulib/lib/tempname.c,v
retrieving revision 1.21
diff -u -r1.21 tempname.c
--- lib/tempname.c      20 Sep 2006 18:38:14 -0000      1.21
+++ lib/tempname.c      13 Oct 2006 22:13:08 -0000
@@ -21,6 +21,8 @@
 # include <config.h>
 #endif
 
+#include "tempname.h"
+
 #include <sys/types.h>
 #include <assert.h>
 
@@ -30,18 +32,9 @@
 #endif
 
 #include <stdio.h>
-#ifndef P_tmpdir
-# define P_tmpdir "/tmp"
-#endif
 #ifndef TMP_MAX
 # define TMP_MAX 238328
 #endif
-#ifndef __GT_FILE
-# define __GT_FILE     0
-# define __GT_BIGFILE  1
-# define __GT_DIR      2
-# define __GT_NOCREATE 3
-#endif
 
 #include <stddef.h>
 #include <stdlib.h>
@@ -62,6 +55,7 @@
 # define struct_stat64 struct stat64
 # define small_open __open
 # define large_open __open64
+# define gen_tempname __gen_tempname
 #else
 # include "stat-macros.h"
 # define struct_stat64 struct stat
@@ -74,10 +68,6 @@
 # define __xstat64(version, file, buf) stat (file, buf)
 #endif
 
-#if ! (HAVE___SECURE_GETENV || _LIBC)
-# define __secure_getenv getenv
-#endif
-
 #ifdef _LIBC
 # include <hp-timing.h>
 # if HP_TIMING_AVAIL
@@ -106,76 +96,6 @@
 # define uint64_t uintmax_t
 #endif
 
-/* Return nonzero if DIR is an existent directory.  */
-static int
-direxists (const char *dir)
-{
-  struct_stat64 buf;
-  return __xstat64 (_STAT_VER, dir, &buf) == 0 && S_ISDIR (buf.st_mode);
-}
-
-/* Path search algorithm, for tmpnam, tmpfile, etc.  If DIR is
-   non-null and exists, uses it; otherwise uses the first of $TMPDIR,
-   P_tmpdir, /tmp that exists.  Copies into TMPL a template suitable
-   for use with mk[s]temp.  Will fail (-1) if DIR is non-null and
-   doesn't exist, none of the searched dirs exists, or there's not
-   enough space in TMPL. */
-int
-__path_search (char *tmpl, size_t tmpl_len, const char *dir, const char *pfx,
-              int try_tmpdir)
-{
-  const char *d;
-  size_t dlen, plen;
-
-  if (!pfx || !pfx[0])
-    {
-      pfx = "file";
-      plen = 4;
-    }
-  else
-    {
-      plen = strlen (pfx);
-      if (plen > 5)
-       plen = 5;
-    }
-
-  if (try_tmpdir)
-    {
-      d = __secure_getenv ("TMPDIR");
-      if (d != NULL && direxists (d))
-       dir = d;
-      else if (dir != NULL && direxists (dir))
-       /* nothing */ ;
-      else
-       dir = NULL;
-    }
-  if (dir == NULL)
-    {
-      if (direxists (P_tmpdir))
-       dir = P_tmpdir;
-      else if (strcmp (P_tmpdir, "/tmp") != 0 && direxists ("/tmp"))
-       dir = "/tmp";
-      else
-       {
-         __set_errno (ENOENT);
-         return -1;
-       }
-    }
-
-  dlen = strlen (dir);
-  while (dlen > 1 && dir[dlen - 1] == '/')
-    dlen--;                    /* remove trailing slashes */
-
-  /* check we have room for "${dir}/${pfx}XXXXXX\0" */
-  if (tmpl_len < dlen + 1 + plen + 6 + 1)
-    {
-      __set_errno (EINVAL);
-      return -1;
-    }
-
-  sprintf (tmpl, "%.*s/%.*sXXXXXX", (int) dlen, dir, (int) plen, pfx);
-  return 0;
-}
 
 /* These are the characters used in temporary file names.  */
 static const char letters[] =
@@ -196,7 +116,7 @@
 
    We use a clever algorithm to get hard-to-predict names. */
 int
-__gen_tempname (char *tmpl, int kind)
+gen_tempname (char *tmpl, int kind)
 {
   int len;
   char *XXXXXX;
Index: lib/tempname.h
===================================================================
RCS file: lib/tempname.h
diff -N lib/tempname.h
--- /dev/null   1 Jan 1970 00:00:00 -0000
+++ lib/tempname.h      13 Oct 2006 22:13:08 -0000
@@ -0,0 +1,39 @@
+/* Create a temporary file or directory.
+   Copyright (C) 2006 Free Software Foundation, Inc.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 2, 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 General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program; if not, write to the Free Software Foundation,
+   Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
+
+#ifndef __GT_FILE
+# define __GT_FILE     0
+# define __GT_BIGFILE  1
+# define __GT_DIR      2
+# define __GT_NOCREATE 3
+#endif
+
+/* Generate a temporary file name based on TMPL.  TMPL must match the
+   rules for mk[s]temp (i.e. end in "XXXXXX").  The name constructed
+   does not exist at the time of the call to gen_tempname.  TMPL is
+   overwritten with the result.
+
+   KIND may be one of:
+   __GT_NOCREATE:      simply verify that the name does not exist
+                       at the time of the call.
+   __GT_FILE:          create the file using open(O_CREAT|O_EXCL)
+                       and return a read-write fd.  The file is mode 0600.
+   __GT_BIGFILE:       same as __GT_FILE but use open64().
+   __GT_DIR:           create a directory, which will be mode 0700.
+
+   We use a clever algorithm to get hard-to-predict names. */
+extern int gen_tempname (char *tmpl, int kind);
Index: lib/tmpdir.c
===================================================================
RCS file: /sources/gnulib/gnulib/lib/tmpdir.c,v
retrieving revision 1.3
diff -u -r1.3 tmpdir.c
--- lib/tmpdir.c        14 Sep 2006 14:18:36 -0000      1.3
+++ lib/tmpdir.c        13 Oct 2006 22:13:08 -0000
@@ -40,24 +40,6 @@
 #if !defined S_ISDIR && defined S_IFDIR
 # define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)
 #endif
-#if !S_IRUSR && S_IREAD
-# define S_IRUSR S_IREAD
-#endif
-#if !S_IRUSR
-# define S_IRUSR 00400
-#endif
-#if !S_IWUSR && S_IWRITE
-# define S_IWUSR S_IWRITE
-#endif
-#if !S_IWUSR
-# define S_IWUSR 00200
-#endif
-#if !S_IXUSR && S_IEXEC
-# define S_IXUSR S_IEXEC
-#endif
-#if !S_IXUSR
-# define S_IXUSR 00100
-#endif
 
 #if _LIBC
 # define struct_stat64 struct stat64







reply via email to

[Prev in Thread] Current Thread [Next in Thread]