From patchwork Thu Aug 25 21:46:34 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Eggert X-Patchwork-Id: 57071 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 3C55F38515D1 for ; Thu, 25 Aug 2022 21:47:31 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from eggs.gnu.org (eggs.gnu.org [IPv6:2001:470:142:3::10]) by sourceware.org (Postfix) with ESMTPS id 577D63851145 for ; Thu, 25 Aug 2022 21:47:03 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 577D63851145 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=cs.ucla.edu Authentication-Results: sourceware.org; spf=fail smtp.mailfrom=cs.ucla.edu Received: from zimbra.cs.ucla.edu ([131.179.128.68]:53746) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oRKgn-0006rv-0u for libc-alpha@gnu.org; Thu, 25 Aug 2022 17:47:02 -0400 Received: from localhost (localhost [127.0.0.1]) by zimbra.cs.ucla.edu (Postfix) with ESMTP id 0E4CF1600B0 for ; Thu, 25 Aug 2022 14:47:00 -0700 (PDT) Received: from zimbra.cs.ucla.edu ([127.0.0.1]) by localhost (zimbra.cs.ucla.edu [127.0.0.1]) (amavisd-new, port 10032) with ESMTP id IDJ2cVnA9KgL; Thu, 25 Aug 2022 14:46:58 -0700 (PDT) Received: from localhost (localhost [127.0.0.1]) by zimbra.cs.ucla.edu (Postfix) with ESMTP id 62A381600AF; Thu, 25 Aug 2022 14:46:58 -0700 (PDT) X-Virus-Scanned: amavisd-new at zimbra.cs.ucla.edu Received: from zimbra.cs.ucla.edu ([127.0.0.1]) by localhost (zimbra.cs.ucla.edu [127.0.0.1]) (amavisd-new, port 10026) with ESMTP id NDIxJejPhPhv; Thu, 25 Aug 2022 14:46:58 -0700 (PDT) Received: from penguin.cs.ucla.edu (Penguin.CS.UCLA.EDU [131.179.64.200]) by zimbra.cs.ucla.edu (Postfix) with ESMTPSA id 34E2116009C; Thu, 25 Aug 2022 14:46:58 -0700 (PDT) From: Paul Eggert To: libc-alpha@gnu.org Subject: [PATCH v2 2/2] Split __path_search into a separate file Date: Thu, 25 Aug 2022 14:46:34 -0700 Message-Id: <20220825214634.3561419-3-eggert@cs.ucla.edu> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20220825214634.3561419-1-eggert@cs.ucla.edu> References: <20220825214634.3561419-1-eggert@cs.ucla.edu> MIME-Version: 1.0 Received-SPF: pass client-ip=131.179.128.68; envelope-from=eggert@cs.ucla.edu; helo=zimbra.cs.ucla.edu X-Spam_score_int: -41 X-Spam_score: -4.2 X-Spam_bar: ---- X-Spam_report: (-4.2 / 5.0 requ) BAYES_00=-1.9, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-Spam-Status: No, score=-14.8 required=5.0 tests=BAYES_00, GIT_PATCH_0, JMQ_SPF_NEUTRAL, KAM_DMARC_STATUS, KAM_SHORT, SPF_HELO_PASS, TXREP, T_SCC_BODY_TEXT_LINE, T_SPF_PERMERROR autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" This simplifies sharing tempname with Gnulib. Suggested by Bruno Haible in: https://lists.gnu.org/r/bug-gnulib/2022-08/msg00077.html * stdio-common/Makefile (routines): Add path-search. (CFLAGS-path-search.c): New macro, which has -fexceptions because tempname.c has it. * stdio-common/path-search.c, sysdeps/posix/path-search.c: New files. * stdio-common/tempname.c (__need_size_t): Do not define. No need to include stddef.h, since stdio.h declares size_t. (__path_search): Move to path-search.c. * sysdeps/posix/tempname.c: Don't include sys/types.h, assert.h, stddef.h; not needed. (P_tmpdir): Remove. (__secure_getenv) [_LIBC]: Remove. (__stat64) [!_LIBC]: Remove. (direxists, __path_search) [_LIBC]: Move to path-search.c. --- stdio-common/Makefile | 2 + stdio-common/path-search.c | 31 +++++++++++++ stdio-common/tempname.c | 14 ------ sysdeps/posix/path-search.c | 92 +++++++++++++++++++++++++++++++++++++ sysdeps/posix/tempname.c | 82 --------------------------------- 5 files changed, 125 insertions(+), 96 deletions(-) create mode 100644 stdio-common/path-search.c create mode 100644 sysdeps/posix/path-search.c diff --git a/stdio-common/Makefile b/stdio-common/Makefile index b1e9144de0..1543b5414b 100644 --- a/stdio-common/Makefile +++ b/stdio-common/Makefile @@ -49,6 +49,7 @@ routines := \ itoa-digits \ itoa-udigits \ itowa-digits \ + path-search \ perror \ printf \ printf-prs \ @@ -351,6 +352,7 @@ $(objpfx)tst-errno-manual.out: tst-errno-manual.py \ CFLAGS-vfprintf.c += -Wno-uninitialized CFLAGS-vfwprintf.c += -Wno-uninitialized +CFLAGS-path-search.c += -fexceptions CFLAGS-tmpfile.c += -fexceptions CFLAGS-tmpfile64.c += -fexceptions CFLAGS-tempname.c += -fexceptions diff --git a/stdio-common/path-search.c b/stdio-common/path-search.c new file mode 100644 index 0000000000..6ad7002d7c --- /dev/null +++ b/stdio-common/path-search.c @@ -0,0 +1,31 @@ +/* Copyright (C) 1991-2022 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 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 + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include +#include + +/* Perform the "SVID path search malarkey" on DIR and PFX. Write a + template suitable for use in __gen_tempname into TMPL, bounded + by TMPL_LEN. */ +int +__path_search (char *tmpl, size_t tmpl_len, const char *dir, const char *pfx, + int try_tmpdir) +{ + __set_errno (ENOSYS); + return -1; +} +stub_warning (__path_search) diff --git a/stdio-common/tempname.c b/stdio-common/tempname.c index 621876a9d9..6538fb1777 100644 --- a/stdio-common/tempname.c +++ b/stdio-common/tempname.c @@ -15,23 +15,9 @@ License along with the GNU C Library; if not, see . */ -#define __need_size_t -#include #include #include -/* Perform the "SVID path search malarkey" on DIR and PFX. Write a - template suitable for use in __gen_tempname into TMPL, bounded - by TMPL_LEN. */ -int -__path_search (char *tmpl, size_t tmpl_len, const char *dir, const char *pfx, - int try_tmpdir) -{ - __set_errno (ENOSYS); - return -1; -} -stub_warning (__path_search) - /* Generate a (hopefully) unique temporary filename in DIR (if applicable), using template TMPL. KIND determines what to do with that name. It may be one of: diff --git a/sysdeps/posix/path-search.c b/sysdeps/posix/path-search.c new file mode 100644 index 0000000000..ae2f404f58 --- /dev/null +++ b/sysdeps/posix/path-search.c @@ -0,0 +1,92 @@ +/* Copyright (C) 1991-2022 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 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 + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include +#include +#include +#include + +/* Return nonzero if DIR is an existent directory. */ +static int +direxists (const char *dir) +{ + struct __stat64_t64 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 = __libc_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; +} diff --git a/sysdeps/posix/tempname.c b/sysdeps/posix/tempname.c index 0e2f29f5de..11b4796b34 100644 --- a/sysdeps/posix/tempname.c +++ b/sysdeps/posix/tempname.c @@ -20,16 +20,10 @@ # include "tempname.h" #endif -#include -#include #include - #include #include -#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 #include #include @@ -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);