On 8/22/22 19:16, Bruno Haible wrote:
At this occasion: How about moving __path_search and direxists to a
different source file in glibc?

Thanks, good idea. I installed the attached into Gnulib and plan to follow up on the glibc side.
From 044bf893acee0a55b22b4be0ede0e3ce010c480a Mon Sep 17 00:00:00 2001
From: Paul Eggert <egg...@cs.ucla.edu>
Date: Thu, 25 Aug 2022 13:35:23 -0500
Subject: [PATCH] tempname: simplify by omitting _LIBC code

* lib/tempname.c [_LIBC]: Simplify.
Suggested by Bruno Haible in:
https://lists.gnu.org/r/bug-gnulib/2022-08/msg00077.html
I plan to propose a corresponding patch to glibc.
---
 ChangeLog      |  8 +++++
 lib/tempname.c | 82 --------------------------------------------------
 2 files changed, 8 insertions(+), 82 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 4d1c92cd81..904980df7d 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+2022-08-25  Paul Eggert  <egg...@cs.ucla.edu>
+
+	tempname: simplify by omitting _LIBC code
+	* lib/tempname.c [_LIBC]: Simplify.
+	Suggested by Bruno Haible in:
+	https://lists.gnu.org/r/bug-gnulib/2022-08/msg00077.html
+	I plan to propose a corresponding patch to glibc.
+
 2022-08-22  Paul Eggert  <egg...@cs.ucla.edu>
 
 	tempname: don't lose entropy in seed
diff --git a/lib/tempname.c b/lib/tempname.c
index 0e2f29f5de..11b4796b34 100644
--- a/lib/tempname.c
+++ b/lib/tempname.c
@@ -20,16 +20,10 @@
 # include "tempname.h"
 #endif
 
-#include <sys/types.h>
-#include <assert.h>
 #include <stdbool.h>
-
 #include <errno.h>
 
 #include <stdio.h>
-#ifndef P_tmpdir
-# define P_tmpdir "/tmp"
-#endif
 #ifndef TMP_MAX
 # define TMP_MAX 238328
 #endif
@@ -43,7 +37,6 @@
 # error report this to bug-gnulib@gnu.org
 #endif
 
-#include <stddef.h>
 #include <stdlib.h>
 #include <string.h>
 
@@ -55,14 +48,12 @@
 
 #if _LIBC
 # define struct_stat64 struct __stat64_t64
-# define __secure_getenv __libc_secure_getenv
 #else
 # define struct_stat64 struct stat
 # define __gen_tempname gen_tempname
 # define __mkdir mkdir
 # define __open open
 # define __lstat64_time64(file, buf) lstat (file, buf)
-# define __stat64(file, buf) stat (file, buf)
 # define __getrandom getrandom
 # define __clock_gettime64 clock_gettime
 # define __timespec64 timespec
@@ -127,79 +118,6 @@ random_bits (random_value *r, random_value s)
   return false;
 }
 
-#if _LIBC
-/* Return nonzero if DIR is an existent directory.  */
-static int
-direxists (const char *dir)
-{
-  struct_stat64 buf;
-  return __stat64_time64 (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;
-}
-#endif /* _LIBC */
-
 #if _LIBC
 static int try_tempname_len (char *, int, void *, int (*) (char *, void *),
                              size_t);
-- 
2.37.2

Reply via email to