From: Paul Eggert Date: Wed, 26 Aug 2020 22:30:53 +0000 (-0700) Subject: getcwd: help the merge back into glibc X-Git-Tag: v1.0~3674 X-Git-Url: https://gitweb.git.savannah.gnu.org/gitweb/?a=commitdiff_plain;h=615b43e1f914687b6ccd40c79a05626098a361d7;p=gnulib.git getcwd: help the merge back into glibc This patch was inspired by Adhemerval Zanella’s proposed glibc patches: https://sourceware.org/pipermail/libc-alpha/2020-August/117294.html The idea is to make it easier for Gnulib lib/getcwd.c to match glibc io/getcwd-generic.c. * lib/getcwd.c [_LIBC]: Do not include pathmax.h. Include not-cancel.h. (HAVE_OPENAT, D_INO_IN_DIRENT, HAVE_MSVC_INVALID_PARAMETER_HANDLER) (HAVE_MINIMALLY_WORKING_GETCWD): Define for the _LIBC case. (__getcwd, __lstat, __readdir) [!_LIBC]: Remove these macros. (__close_nocancel_nostatus, __getcwd_generic, stat64, __fstat64) (__fstatat64, __lstat64, __readdir64, __fdopendir, __openat) (__rewinddir, __openat64, dirent64) [_LIBC]: New macros. (__getcwd_generic): Rename from __getcwd. Use the abovementioned macros for consistency with glibc. (weak_alias): Remove. --- diff --git a/ChangeLog b/ChangeLog index a97600d7f3..ada6bdf440 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,23 @@ +2020-08-26 Paul Eggert + + getcwd: help the merge back into glibc + This patch was inspired by Adhemerval Zanella’s proposed glibc patches: + https://sourceware.org/pipermail/libc-alpha/2020-August/117294.html + The idea is to make it easier for Gnulib lib/getcwd.c to match + glibc io/getcwd-generic.c. + * lib/getcwd.c [_LIBC]: Do not include pathmax.h. + Include not-cancel.h. + (HAVE_OPENAT, D_INO_IN_DIRENT, HAVE_MSVC_INVALID_PARAMETER_HANDLER) + (HAVE_MINIMALLY_WORKING_GETCWD): Define for the _LIBC case. + ( + (__getcwd, __lstat, __readdir) [!_LIBC]: Remove these macros. + (__close_nocancel_nostatus, __getcwd_generic, stat64, __fstat64) + (__fstatat64, __lstat64, __readdir64, __fdopendir, __openat) + (__rewinddir, __openat64, dirent64) [_LIBC]: New macros. + (__getcwd_generic): Rename from __getcwd. + Use the abovementioned macros for consistency with glibc. + (weak_alias): Remove. + 2020-08-25 Bruno Haible verify: Avoid warnings when assume(0) is used. diff --git a/lib/getcwd.c b/lib/getcwd.c index 07c7927e48..61d2d06106 100644 --- a/lib/getcwd.c +++ b/lib/getcwd.c @@ -1,22 +1,29 @@ -/* Copyright (C) 1991-1999, 2004-2020 Free Software Foundation, Inc. +/* Copyright (C) 1991-2020 Free Software Foundation, Inc. This file is part of the GNU C Library. - 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 3 of the License, or - (at your option) any later version. + 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. - This program is distributed in the hope that it will be useful, + 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 General Public License for more details. + 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 General Public License - along with this program. If not, see . */ + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ #if !_LIBC # include # include +# include "pathmax.h" +#else +# define HAVE_OPENAT 1 +# define D_INO_IN_DIRENT 1 +# define HAVE_MSVC_INVALID_PARAMETER_HANDLER 0 +# define HAVE_MINIMALLY_WORKING_GETCWD 0 #endif #include @@ -65,8 +72,6 @@ # define MIN(a, b) ((a) < (b) ? (a) : (b)) #endif -#include "pathmax.h" - /* In this file, PATH_MAX only serves as a threshold for choosing among two algorithms. */ #ifndef PATH_MAX @@ -84,11 +89,22 @@ #endif #if !_LIBC -# define __getcwd rpl_getcwd -# define __lstat lstat +# define __close_nocancel_nostatus close +# define __getcwd_generic rpl_getcwd +# define stat64 stat +# define __fstat64 fstat +# define __fstatat64 fstatat +# define __lstat64 lstat # define __closedir closedir # define __opendir opendir -# define __readdir readdir +# define __readdir64 readdir +# define __fdopendir fdopendir +# define __openat openat +# define __rewinddir rewinddir +# define __openat64 openat +# define dirent64 dirent +#else +# include #endif /* The results of opendir() in this file are not used with dirfd and fchdir, @@ -139,7 +155,7 @@ getcwd_nothrow (char *buf, size_t size) bytes long, unless SIZE == 0, in which case it is as big as necessary. */ char * -__getcwd (char *buf, size_t size) +__getcwd_generic (char *buf, size_t size) { /* Lengths of big file name components and entire file names, and a deep level of file name nesting. These numbers are not upper @@ -167,7 +183,7 @@ __getcwd (char *buf, size_t size) ino_t rootino, thisino; char *dir; register char *dirp; - struct stat st; + struct stat64 st; size_t allocated = size; size_t used; @@ -210,7 +226,6 @@ __getcwd (char *buf, size_t size) return NULL; # endif #endif - if (size == 0) { if (buf != NULL) @@ -234,19 +249,19 @@ __getcwd (char *buf, size_t size) dirp = dir + allocated; *--dirp = '\0'; - if (__lstat (".", &st) < 0) + if (__lstat64 (".", &st) < 0) goto lose; thisdev = st.st_dev; thisino = st.st_ino; - if (__lstat ("/", &st) < 0) + if (__lstat64 ("/", &st) < 0) goto lose; rootdev = st.st_dev; rootino = st.st_ino; while (!(thisdev == rootdev && thisino == rootino)) { - struct dirent *d; + struct dirent64 *d; dev_t dotdev; ino_t dotino; bool mount_point; @@ -257,16 +272,16 @@ __getcwd (char *buf, size_t size) /* Look at the parent directory. */ #if HAVE_OPENAT_SUPPORT - fd = openat (fd, "..", O_RDONLY); + fd = __openat64 (fd, "..", O_RDONLY); if (fd < 0) goto lose; fd_needs_closing = true; - parent_status = fstat (fd, &st); + parent_status = __fstat64 (fd, &st); #else dotlist[dotlen++] = '.'; dotlist[dotlen++] = '.'; dotlist[dotlen] = '\0'; - parent_status = __lstat (dotlist, &st); + parent_status = __lstat64 (dotlist, &st); #endif if (parent_status != 0) goto lose; @@ -284,7 +299,7 @@ __getcwd (char *buf, size_t size) /* Search for the last directory. */ #if HAVE_OPENAT_SUPPORT - dirstream = fdopendir (fd); + dirstream = __fdopendir (fd); if (dirstream == NULL) goto lose; fd_needs_closing = false; @@ -299,7 +314,7 @@ __getcwd (char *buf, size_t size) /* Clear errno to distinguish EOF from error if readdir returns NULL. */ __set_errno (0); - d = __readdir (dirstream); + d = __readdir64 (dirstream); /* When we've iterated through all directory entries without finding one with a matching d_ino, rewind the stream and consider each @@ -311,8 +326,8 @@ __getcwd (char *buf, size_t size) if (d == NULL && errno == 0 && use_d_ino) { use_d_ino = false; - rewinddir (dirstream); - d = __readdir (dirstream); + __rewinddir (dirstream); + d = __readdir64 (dirstream); } if (d == NULL) @@ -338,7 +353,7 @@ __getcwd (char *buf, size_t size) { int entry_status; #if HAVE_OPENAT_SUPPORT - entry_status = fstatat (fd, d->d_name, &st, AT_SYMLINK_NOFOLLOW); + entry_status = __fstatat64 (fd, d->d_name, &st, AT_SYMLINK_NOFOLLOW); #else /* Compute size needed for this file name, or for the file name ".." in the same directory, whichever is larger. @@ -375,7 +390,7 @@ __getcwd (char *buf, size_t size) } memcpy (dotlist + dotlen, d->d_name, _D_ALLOC_NAMLEN (d)); - entry_status = __lstat (dotlist, &st); + entry_status = __lstat64 (dotlist, &st); #endif /* We don't fail here if we cannot stat() a directory entry. This can happen when (network) file systems fail. If this @@ -461,7 +476,7 @@ __getcwd (char *buf, size_t size) __closedir (dirstream); #if HAVE_OPENAT_SUPPORT if (fd_needs_closing) - close (fd); + __close_nocancel_nostatus (fd); #else if (dotlist != dots) free (dotlist); @@ -472,7 +487,3 @@ __getcwd (char *buf, size_t size) } return NULL; } - -#ifdef weak_alias -weak_alias (__getcwd, getcwd) -#endif