From: Bruno Haible Date: Mon, 19 Aug 2024 14:20:08 +0000 (+0200) Subject: hasmntopt: New module. X-Git-Url: https://gitweb.git.savannah.gnu.org/gitweb/?a=commitdiff_plain;h=b1c67ccfeab56c6d2f452f03f01cdb5cbaedb8b6;p=gnulib.git hasmntopt: New module. * lib/mntent.in.h: Add config.h check. (hasmntopt): New declaration. * lib/hasmntopt.c: New file, based on glibc/misc/mntent_r.c. * m4/hasmntopt.m4: New file. * m4/mntent_h.m4 (gl_MNTENT_H): Test whether hasmntopt is declared. (gl_MNTENT_H_REQUIRE_DEFAULTS): Initialize GNULIB_HASMNTOPT. (gl_MNTENT_H_DEFAULTS): Initialize HAVE_HASMNTOPT, HAVE_SETMNTENT, REPLACE_HASMNTOPT. * modules/mntent (Depends-on): Add snippet/arg-nonnull, snippet/c++defs, snippet/warn-on-use. (Makefile.am): Insert c++defs.h, arg-nonnull.h, warn-on-use.h into mntent.h. Substitute GNULIB_HASMNTOPT, HAVE_HASMNTOPT, HAVE_SETMNTENT, REPLACE_HASMNTOPT. * modules/hasmntopt: New file. * doc/glibc-functions/hasmntopt.texi: Mention the new module and the musl bug. * doc/glibc-functions/setmntent.texi: Update info about Android. * doc/glibc-functions/getmntent_r.texi: Likewise. * doc/glibc-functions/endmntent.texi: Likewise. --- diff --git a/ChangeLog b/ChangeLog index 390733f163..8cbd4b8a80 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,26 @@ +2024-08-19 Bruno Haible + + hasmntopt: New module. + * lib/mntent.in.h: Add config.h check. + (hasmntopt): New declaration. + * lib/hasmntopt.c: New file, based on glibc/misc/mntent_r.c. + * m4/hasmntopt.m4: New file. + * m4/mntent_h.m4 (gl_MNTENT_H): Test whether hasmntopt is declared. + (gl_MNTENT_H_REQUIRE_DEFAULTS): Initialize GNULIB_HASMNTOPT. + (gl_MNTENT_H_DEFAULTS): Initialize HAVE_HASMNTOPT, HAVE_SETMNTENT, + REPLACE_HASMNTOPT. + * modules/mntent (Depends-on): Add snippet/arg-nonnull, snippet/c++defs, + snippet/warn-on-use. + (Makefile.am): Insert c++defs.h, arg-nonnull.h, warn-on-use.h into + mntent.h. Substitute GNULIB_HASMNTOPT, HAVE_HASMNTOPT, HAVE_SETMNTENT, + REPLACE_HASMNTOPT. + * modules/hasmntopt: New file. + * doc/glibc-functions/hasmntopt.texi: Mention the new module and the + musl bug. + * doc/glibc-functions/setmntent.texi: Update info about Android. + * doc/glibc-functions/getmntent_r.texi: Likewise. + * doc/glibc-functions/endmntent.texi: Likewise. + 2024-08-19 Bruno Haible mntent: New module. diff --git a/doc/glibc-functions/endmntent.texi b/doc/glibc-functions/endmntent.texi index 501083ca9b..b887e0ee67 100644 --- a/doc/glibc-functions/endmntent.texi +++ b/doc/glibc-functions/endmntent.texi @@ -25,5 +25,5 @@ Portability problems not fixed by Gnulib: @itemize @item This function is missing on some platforms: -macOS 14, FreeBSD 14.0, NetBSD 10.0, OpenBSD 7.5, Minix 3.1.8, Solaris 11.4, mingw, MSVC 14, Android 4.4. +macOS 14, FreeBSD 14.0, NetBSD 10.0, OpenBSD 7.5, Minix 3.1.8, Solaris 11.4, mingw, MSVC 14, Android API level 20. @end itemize diff --git a/doc/glibc-functions/getmntent_r.texi b/doc/glibc-functions/getmntent_r.texi index bbd2ee97f4..12629a13a8 100644 --- a/doc/glibc-functions/getmntent_r.texi +++ b/doc/glibc-functions/getmntent_r.texi @@ -25,5 +25,5 @@ Portability problems not fixed by Gnulib: @itemize @item This function is missing on many non-glibc platforms: -macOS 14, FreeBSD 14.0, NetBSD 10.0, OpenBSD 7.5, Minix 3.1.8, AIX 5.1, HP-UX 11, Solaris 11.4, Cygwin 1.7.x, mingw, MSVC 14, Android 4.4. +macOS 14, FreeBSD 14.0, NetBSD 10.0, OpenBSD 7.5, Minix 3.1.8, AIX 5.1, HP-UX 11, Solaris 11.4, Cygwin 1.7.x, mingw, MSVC 14, Android API level 20. @end itemize diff --git a/doc/glibc-functions/hasmntopt.texi b/doc/glibc-functions/hasmntopt.texi index a4784c6eb6..ebc92fe6c8 100644 --- a/doc/glibc-functions/hasmntopt.texi +++ b/doc/glibc-functions/hasmntopt.texi @@ -15,15 +15,22 @@ Documentation: @uref{https://www.kernel.org/doc/man-pages/online/pages/man3/hasmntopt.3.html,,man hasmntopt}. @end itemize -Gnulib module: --- +Gnulib module: hasmntopt Portability problems fixed by Gnulib: @itemize +@item +This function is missing on some platforms: +Cygwin 3.5.3, Android API level 25. +@item +This function returns wrong results on some platforms: +@c https://www.openwall.com/lists/musl/2024/08/19/1 +musl libc 1.2.5. @end itemize Portability problems not fixed by Gnulib: @itemize @item This function is missing on some platforms: -macOS 14, FreeBSD 14.0, NetBSD 10.0, OpenBSD 7.5, Minix 3.1.8, Cygwin 2.9, mingw, MSVC 14, Android 7.1. +macOS 14, FreeBSD 14.0, NetBSD 10.0, OpenBSD 7.5, Minix 3.1.8, mingw, MSVC 14. @end itemize diff --git a/doc/glibc-functions/setmntent.texi b/doc/glibc-functions/setmntent.texi index 291b71492c..930c432e8f 100644 --- a/doc/glibc-functions/setmntent.texi +++ b/doc/glibc-functions/setmntent.texi @@ -25,5 +25,5 @@ Portability problems not fixed by Gnulib: @itemize @item This function is missing on some platforms: -macOS 14, FreeBSD 14.0, NetBSD 10.0, OpenBSD 7.5, Minix 3.1.8, Solaris 11.4, mingw, MSVC 14, Android 4.4. +macOS 14, FreeBSD 14.0, NetBSD 10.0, OpenBSD 7.5, Minix 3.1.8, Solaris 11.4, mingw, MSVC 14, Android API level 20. @end itemize diff --git a/lib/hasmntopt.c b/lib/hasmntopt.c new file mode 100644 index 0000000000..15c7e824f0 --- /dev/null +++ b/lib/hasmntopt.c @@ -0,0 +1,44 @@ +/* hasmntopt() function: test for the presence of a mount option. + + Copyright (C) 1995-2024 Free Software Foundation, Inc. + + This file 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 file 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 this program. If not, see . */ + +#include + +/* Specification. */ +#include + +#include + +char * +hasmntopt (const struct mntent *mnt, const char *opt) +{ + const size_t optlen = strlen (opt); + char *rest = mnt->mnt_opts, *p; + + while ((p = strstr (rest, opt)) != NULL) + { + if ((p == rest || p[-1] == ',') + && (p[optlen] == '\0' || p[optlen] == '=' || p[optlen] == ',')) + return p; + + rest = strchr (p, ','); + if (rest == NULL) + break; + ++rest; + } + + return NULL; +} diff --git a/lib/mntent.in.h b/lib/mntent.in.h index 4da9d85314..c8750fec66 100644 --- a/lib/mntent.in.h +++ b/lib/mntent.in.h @@ -28,11 +28,56 @@ #ifndef _@GUARD_PREFIX@_MNTENT_H #define _@GUARD_PREFIX@_MNTENT_H +/* This file uses GNULIB_POSIXCHECK, HAVE_RAW_DECL_*. */ +#if !_GL_CONFIG_H_INCLUDED + #error "Please include config.h first." +#endif + +/* The definitions of _GL_FUNCDECL_RPL etc. are copied here. */ + +/* The definition of _GL_ARG_NONNULL is copied here. */ + +/* The definition of _GL_WARN_ON_USE is copied here. */ + #ifdef __cplusplus extern "C" { #endif +#if @GNULIB_HASMNTOPT@ && @HAVE_SETMNTENT@ +/* Searches ME->mnt_opts for an option matching OPTION. + Returns the address of the substring, or null if none found. */ +# if @REPLACE_HASMNTOPT@ +# if !(defined __cplusplus && defined GNULIB_NAMESPACE) +# undef hasmntopt +# define hasmntopt rpl_hasmntopt +# endif +_GL_FUNCDECL_RPL (hasmntopt, char *, + (const struct mntent *me, const char *option), + _GL_ARG_NONNULL ((1, 2))); +_GL_CXXALIAS_RPL (hasmntopt, char *, + (const struct mntent *me, const char *option)); +# else +# if !@HAVE_HASMNTOPT@ +_GL_FUNCDECL_SYS (hasmntopt, char *, + (const struct mntent *me, const char *option), + _GL_ARG_NONNULL ((1, 2))); +# endif +_GL_CXXALIAS_SYS (hasmntopt, char *, + (const struct mntent *me, const char *option)); +# endif +# if __GLIBC__ >= 2 +_GL_CXXALIASWARN (hasmntopt); +# endif +#elif defined GNULIB_POSIXCHECK +# undef hasmntopt +# if HAVE_RAW_DECL_HASMNTOPT +_GL_WARN_ON_USE (hasmntopt, "hasmntopt is unportable - " + "use gnulib module hasmntopt for portability"); +# endif +#endif + + #ifdef __cplusplus } #endif diff --git a/m4/hasmntopt.m4 b/m4/hasmntopt.m4 new file mode 100644 index 0000000000..2dcaa7db56 --- /dev/null +++ b/m4/hasmntopt.m4 @@ -0,0 +1,75 @@ +# hasmntopt.m4 +# serial 1 +dnl Copyright (C) 2024 Free Software Foundation, Inc. +dnl +dnl This file is free software; the Free Software Foundation +dnl gives unlimited permission to copy and/or distribute it, +dnl with or without modifications, as long as this notice is preserved. + +AC_DEFUN([gl_FUNC_HASMNTOPT], +[ + AC_REQUIRE([gl_MNTENT_H_DEFAULTS]) + AC_REQUIRE([AC_CANONICAL_HOST]) + + gl_CHECK_FUNCS_ANDROID([setmntent], + [[#include + #include + ]]) + if test $ac_cv_func_setmntent != yes; then + HAVE_SETMNTENT=0 + fi + + gl_CHECK_FUNCS_ANDROID([hasmntopt], + [[#include + #include + ]]) + if test $ac_cv_func_hasmntopt = yes; then + dnl hasmntopt has the dumbest possible implementation on musl libc 1.2.5: + dnl it confuses the "atime" option with the "noatime" option. + AC_CACHE_CHECK([whether hasmntopt works], + [gl_cv_func_hasmntopt_works], + [AC_RUN_IFELSE( + [AC_LANG_PROGRAM( + [[#include + #include + ]], + [[int result = 0; + /* This test fails on musl libc 1.2.5. */ + { + char options[] = "rw,noatime"; + struct mntent me; + me.mnt_opts = options; + if (hasmntopt (&me, "atime") != NULL) + result |= 1; + } + return result; + ]]) + ], + [gl_cv_func_hasmntopt_works=yes], + [gl_cv_func_hasmntopt_works=no], + [case "$host_os" in + # Guess yes on glibc systems. + *-gnu* | gnu*) gl_cv_func_hasmntopt_works="guessing yes" ;; + # Guess no on musl systems. + *-musl* | midipix*) gl_cv_func_hasmntopt_works="guessing no" ;; + *) gl_cv_func_hasmntopt_works="$gl_cross_guess_normal" ;; + esac + ]) + ]) + case "$gl_cv_func_hasmntopt_works" in + *yes) ;; + *) REPLACE_HASMNTOPT=1 ;; + esac + else + HAVE_HASMNTOPT=0 + case "$gl_cv_onwards_func_hasmntopt" in + future*) REPLACE_HASMNTOPT=1 ;; + esac + fi +]) + +# Prerequisites of lib/hasmntopt.c. +AC_DEFUN([gl_PREREQ_HASMNTOPT], +[ + : +]) diff --git a/m4/mntent_h.m4 b/m4/mntent_h.m4 index f067cd0293..30eaee90c2 100644 --- a/m4/mntent_h.m4 +++ b/m4/mntent_h.m4 @@ -1,5 +1,5 @@ # mntent_h.m4 -# serial 1 +# serial 2 dnl Copyright (C) 2024 Free Software Foundation, Inc. dnl This file is free software; the Free Software Foundation dnl gives unlimited permission to copy and/or distribute it, @@ -20,6 +20,12 @@ AC_DEFUN_ONCE([gl_MNTENT_H], else GL_GENERATE_MNTENT_H=false fi + + dnl Check for declarations of anything we want to poison if the + dnl corresponding gnulib module is not in use. + gl_WARN_ON_USE_PREPARE([[#include + ]], + [hasmntopt]) ]) # gl_MNTENT_MODULE_INDICATOR([modulename]) @@ -41,6 +47,7 @@ AC_DEFUN([gl_MNTENT_MODULE_INDICATOR], AC_DEFUN([gl_MNTENT_H_REQUIRE_DEFAULTS], [ m4_defun(GL_MODULE_INDICATOR_PREFIX[_MNTENT_H_MODULE_INDICATOR_DEFAULTS], [ + gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_HASMNTOPT]) ]) m4_require(GL_MODULE_INDICATOR_PREFIX[_MNTENT_H_MODULE_INDICATOR_DEFAULTS]) AC_REQUIRE([gl_MNTENT_H_DEFAULTS]) @@ -49,4 +56,7 @@ AC_DEFUN([gl_MNTENT_H_REQUIRE_DEFAULTS], AC_DEFUN([gl_MNTENT_H_DEFAULTS], [ dnl Assume proper GNU behavior unless another module says otherwise. + HAVE_HASMNTOPT=1; AC_SUBST([HAVE_HASMNTOPT]) + HAVE_SETMNTENT=1; AC_SUBST([HAVE_SETMNTENT]) + REPLACE_HASMNTOPT=0; AC_SUBST([REPLACE_HASMNTOPT]) ]) diff --git a/modules/hasmntopt b/modules/hasmntopt new file mode 100644 index 0000000000..6fdd897f0b --- /dev/null +++ b/modules/hasmntopt @@ -0,0 +1,32 @@ +Description: +hasmntopt() function: test for the presence of a mount option. + +Files: +lib/hasmntopt.c +m4/hasmntopt.m4 + +Depends-on: +mntent + +configure.ac: +gl_FUNC_HASMNTOPT +gl_CONDITIONAL([GL_COND_OBJ_HASMNTOPT], + [test $HAVE_SETMNTENT = 1 && { test $HAVE_HASMNTOPT = 0 || test $REPLACE_HASMNTOPT = 1; }]) +AM_COND_IF([GL_COND_OBJ_HASMNTOPT], [ + gl_PREREQ_HASMNTOPT +]) +gl_MNTENT_MODULE_INDICATOR([hasmntopt]) + +Makefile.am: +if GL_COND_OBJ_HASMNTOPT +lib_SOURCES += hasmntopt.c +endif + +Include: + + +License: +LGPLv2+ + +Maintainer: +glibc diff --git a/modules/mntent b/modules/mntent index eb30116fad..8dc48c38d0 100644 --- a/modules/mntent +++ b/modules/mntent @@ -8,6 +8,9 @@ m4/mntent_h.m4 Depends-on: gen-header include_next +snippet/arg-nonnull +snippet/c++defs +snippet/warn-on-use configure.ac: gl_MNTENT_H @@ -19,7 +22,7 @@ BUILT_SOURCES += $(MNTENT_H) # We need the following in order to create when desired. if GL_GENERATE_MNTENT_H -mntent.h: mntent.in.h $(top_builddir)/config.status +mntent.h: mntent.in.h $(top_builddir)/config.status $(CXXDEFS_H) $(ARG_NONNULL_H) $(WARN_ON_USE_H) $(AM_V_GEN)$(MKDIR_P) '%reldir%' $(AM_V_at)$(SED_HEADER_STDOUT) \ -e 's|@''GUARD_PREFIX''@|${gl_include_guard_prefix}|g' \ @@ -27,6 +30,13 @@ mntent.h: mntent.in.h $(top_builddir)/config.status -e 's|@''PRAGMA_SYSTEM_HEADER''@|@PRAGMA_SYSTEM_HEADER@|g' \ -e 's|@''PRAGMA_COLUMNS''@|@PRAGMA_COLUMNS@|g' \ -e 's|@''NEXT_MNTENT_H''@|$(NEXT_MNTENT_H)|g' \ + -e 's/@''GNULIB_HASMNTOPT''@/$(GNULIB_HASMNTOPT)/g' \ + -e 's|@''HAVE_HASMNTOPT''@|$(HAVE_HASMNTOPT)|g' \ + -e 's|@''HAVE_SETMNTENT''@|$(HAVE_SETMNTENT)|g' \ + -e 's|@''REPLACE_HASMNTOPT''@|$(REPLACE_HASMNTOPT)|g' \ + -e '/definitions of _GL_FUNCDECL_RPL/r $(CXXDEFS_H)' \ + -e '/definition of _GL_ARG_NONNULL/r $(ARG_NONNULL_H)' \ + -e '/definition of _GL_WARN_ON_USE/r $(WARN_ON_USE_H)' \ $(srcdir)/mntent.in.h > $@-t $(AM_V_at)mv $@-t $@ else