> Here's a draft proposal: A new module 'strerror_r-posix'. I wouldn't want > to call it 'strerror_r' because that would be misunderstandable.
During testing, more portability problems appeared. Here's what I'm committing: 2010-11-11 Bruno Haible <br...@clisp.org> New module 'strerror_r-posix'. * lib/string.in.h (strerror_r): New declaration. * lib/strerror_r.c: New file. * m4/strerror_r.m4: New file. * m4/string_h.m4 (gl_HEADER_STRING_H_BODY): Check for the declaration of strerror_r. (gl_HEADER_STRING_H_DEFAULTS): Initialize GNULIB_STRERROR_R, HAVE_DECL_STRERROR_R, REPLACE_STRERROR_R. * modules/strerror_r-posix: New file. * modules/string (Makefile.am): Substitute GNULIB_STRERROR_R, HAVE_DECL_STRERROR_R, REPLACE_STRERROR_R. * doc/posix-functions/strerror_r.texi: Mention the new module and the portability problems. --- lib/string.in.h.orig Thu Nov 11 13:14:02 2010 +++ lib/string.in.h Thu Nov 11 11:44:28 2010 @@ -902,6 +902,35 @@ "use gnulib module strerror to guarantee non-NULL result"); #endif +/* Map any int, typically from errno, into an error message. Multithread-safe. + Uses the POSIX declaration, not the glibc declaration. */ +#if @GNULIB_STRERROR_R@ +# if @REPLACE_STRERROR_R@ +# if !(defined __cplusplus && defined GNULIB_NAMESPACE) +# undef strerror_r +# define strerror_r rpl_strerror_r +# endif +_GL_FUNCDECL_RPL (strerror_r, int, (int errnum, char *buf, size_t buflen) + _GL_ARG_NONNULL ((2))); +_GL_CXXALIAS_RPL (strerror_r, int, (int errnum, char *buf, size_t buflen)); +# else +# if !...@have_decl_strerror_r@ +_GL_FUNCDECL_SYS (strerror_r, int, (int errnum, char *buf, size_t buflen) + _GL_ARG_NONNULL ((2))); +# endif +_GL_CXXALIAS_SYS (strerror_r, int, (int errnum, char *buf, size_t buflen)); +# endif +# if @HAVE_DECL_STRERROR_R@ +_GL_CXXALIASWARN (strerror_r); +# endif +#elif defined GNULIB_POSIXCHECK +# undef strerror_r +# if HAVE_RAW_DECL_STRERROR_R +_GL_WARN_ON_USE (strerror_r, "strerror_r is unportable - " + "use gnulib module strerror_r-posix for portability"); +# endif +#endif + #if @GNULIB_STRSIGNAL@ # if @REPLACE_STRSIGNAL@ # if !(defined __cplusplus && defined GNULIB_NAMESPACE) =============================== lib/strerror_r.c =============================== /* strerror_r.c --- POSIX compatible system error routine Copyright (C) 2010 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 3 of the License, 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, see <http://www.gnu.org/licenses/>. */ /* Written by Bruno Haible <br...@clisp.org>, 2010. */ #include <config.h> /* Specification. */ #include <string.h> #include <errno.h> #if HAVE_DECL_STRERROR_R && !(__GLIBC__ >= 2) && !EXTEND_STRERROR_R /* The system's strerror_r function is OK, except that its third argument is 'int', not 'size_t'. */ # include <limits.h> int strerror_r (int errnum, char *buf, size_t buflen) # undef strerror_r { int ret; if (buflen > INT_MAX) buflen = INT_MAX; # ifdef __hpux /* On HP-UX 11.31, strerror_r always fails when buflen < 80. */ { char stackbuf[80]; if (buflen < sizeof (stackbuf)) { ret = strerror_r (errnum, stackbuf, sizeof (stackbuf)); if (ret == 0) { size_t len = strlen (stackbuf); if (len < buflen) memcpy (buf, stackbuf, len + 1); else ret = ERANGE; } } else ret = strerror_r (errnum, buf, buflen); } # else ret = strerror_r (errnum, buf, buflen); # endif # ifdef _AIX /* On AIX 6.1, strerror_r returns -1 and sets errno to EINVAL if buflen <= 1. */ if (ret < 0 && errno == EINVAL && buflen <= 1) { /* Retry with a larger buffer. */ char largerbuf[10]; ret = strerror_r (errnum, largerbuf, sizeof (largerbuf)); if (ret < 0 && errno == EINVAL) { /* errnum was out of range. */ return EINVAL; } else { /* buf was too small. */ return ERANGE; } } # endif /* Some old implementations may return (-1, EINVAL) instead of EINVAL. */ return (ret < 0 ? errno : ret); } #elif __GLIBC__ >= 2 && HAVE___XPG_STRERROR_R /* glibc >= 2.3.4 */ && !EXTEND_STRERROR_R int strerror_r (int errnum, char *buf, size_t buflen) { extern int __xpg_strerror_r (int errnum, char *buf, size_t buflen); int ret = __xpg_strerror_r (errnum, buf, buflen); return (ret < 0 ? errno : 0); } #else /* (__GLIBC__ >= 2 ? !HAVE___XPG_STRERROR_R : !HAVE_DECL_STRERROR_R) || EXTEND_STRERROR_R */ # include "glthread/lock.h" /* Use strerror(), with locking. */ /* This lock protects the buffer returned by strerror(). We assume that no other uses of strerror() exist in the program. */ gl_lock_define_initialized(static, strerror_lock) int strerror_r (int errnum, char *buf, size_t buflen) { gl_lock_lock (strerror_lock); { char *errmsg = strerror (errnum); size_t len = strlen (errmsg); int ret; if (len < buflen) { memcpy (buf, errmsg, len + 1); ret = 0; } else ret = ERANGE; gl_lock_unlock (strerror_lock); return ret; } } #endif =============================== m4/strerror_r.m4 =============================== # strerror_r.m4 serial 1 dnl Copyright (C) 2002, 2007-2010 Free Software Foundation, Inc. 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_STRERROR_R], [ AC_REQUIRE([gl_HEADER_STRING_H_DEFAULTS]) AC_REQUIRE([gl_HEADER_ERRNO_H]) AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles dnl Persuade Solaris <string.h> to declare strerror_r(). AC_REQUIRE([gl_USE_SYSTEM_EXTENSIONS]) dnl Some systems don't declare strerror_r() if _THREAD_SAFE and _REENTRANT dnl are not defined. AC_CHECK_DECLS_ONCE([strerror_r]) if test $ac_cv_have_decl_strerror_r = no; then HAVE_DECL_STRERROR_R=0 fi AC_CHECK_FUNCS([strerror_r]) if test $ac_cv_func_strerror_r = yes; then if test -z "$ERRNO_H"; then dnl The POSIX prototype is: int strerror_r (int, char *, size_t); dnl glibc's prototype: char *strerror_r (int, char *, size_t); dnl AIX 5.1, OSF/1 5.1: int strerror_r (int, char *, int); AC_CACHE_CHECK([for strerror_r with POSIX signature], [gl_cv_func_strerror_r_posix_signature], [AC_COMPILE_IFELSE( [AC_LANG_PROGRAM( [[#include <string.h> int strerror_r (int, char *, size_t); ]], [[return strerror (0);]])], [gl_cv_func_strerror_r_posix_signature=yes], [gl_cv_func_strerror_r_posix_signature=no]) ]) if test $gl_cv_func_strerror_r_posix_signature = yes; then dnl AIX 6.1 strerror_r fails by returning -1, not an error number. dnl HP-UX 11.31 strerror_r always fails when the buffer length argument dnl is less than 80. AC_CACHE_CHECK([whether strerror_r works], [gl_cv_func_strerror_r_works], [AC_RUN_IFELSE( [AC_LANG_PROGRAM( [[#include <errno.h> #include <string.h> int strerror_r (int, char *, size_t); ]], [[char buf[79]; return strerror (EACCES, buf, 0) < 0 || strerror_r (EACCES, buf, sizeof (buf)) != 0; ]])], [gl_cv_func_strerror_r_works=yes], [gl_cv_func_strerror_r_works=no], [ changequote(,)dnl case "$host_os" in # Guess no on AIX. aix*) gl_cv_func_strerror_r_works="guessing no";; # Guess no on HP-UX. hpux*) gl_cv_func_strerror_r_works="guessing no";; # Guess yes otherwise. *) gl_cv_func_strerror_r_works="guessing yes";; esac changequote([,])dnl ]) ]) case "$gl_cv_func_strerror_r_works" in *no) REPLACE_STRERROR_R=1 ;; esac else dnl The system's strerror() has a wrong signature. Replace it. REPLACE_STRERROR_R=1 dnl glibc >= 2.3.4 has a function __xpg_strerror_r. AC_CHECK_FUNCS([__xpg_strerror_r]) fi else dnl The system's strerror_r() cannot know about the new errno values we dnl add to <errno.h>. Replace it. REPLACE_STRERROR_R=1 AC_DEFINE([EXTEND_STRERROR_R], [1], [Define to 1 if strerror_r needs to be extended so that it handles the extra errno values.]) fi fi if test $HAVE_DECL_STRERROR_R = 0 || test $REPLACE_STRERROR_R = 1; then AC_LIBOBJ([strerror_r]) gl_PREREQ_STRERROR_R fi ]) # Prerequisites of lib/strerror_r.c. AC_DEFUN([gl_PREREQ_STRERROR_R], [ : ]) ================================================================================ --- m4/string_h.m4.orig Thu Nov 11 13:14:02 2010 +++ m4/string_h.m4 Tue Nov 9 01:20:59 2010 @@ -5,7 +5,7 @@ # gives unlimited permission to copy and/or distribute it, # with or without modifications, as long as this notice is preserved. -# serial 17 +# serial 18 # Written by Paul Eggert. @@ -28,8 +28,8 @@ gl_WARN_ON_USE_PREPARE([[#include <string.h> ]], [memmem mempcpy memrchr rawmemchr stpcpy stpncpy strchrnul strdup - strncat strndup strnlen strpbrk strsep strcasestr strtok_r strsignal - strverscmp]) + strncat strndup strnlen strpbrk strsep strcasestr strtok_r strerror_r + strsignal strverscmp]) ]) AC_DEFUN([gl_STRING_MODULE_INDICATOR], @@ -75,6 +75,7 @@ GNULIB_MBSSEP=0; AC_SUBST([GNULIB_MBSSEP]) GNULIB_MBSTOK_R=0; AC_SUBST([GNULIB_MBSTOK_R]) GNULIB_STRERROR=0; AC_SUBST([GNULIB_STRERROR]) + GNULIB_STRERROR_R=0; AC_SUBST([GNULIB_STRERROR_R]) GNULIB_STRSIGNAL=0; AC_SUBST([GNULIB_STRSIGNAL]) GNULIB_STRVERSCMP=0; AC_SUBST([GNULIB_STRVERSCMP]) HAVE_MBSLEN=0; AC_SUBST([HAVE_MBSLEN]) @@ -94,6 +95,7 @@ HAVE_STRSEP=1; AC_SUBST([HAVE_STRSEP]) HAVE_STRCASESTR=1; AC_SUBST([HAVE_STRCASESTR]) HAVE_DECL_STRTOK_R=1; AC_SUBST([HAVE_DECL_STRTOK_R]) + HAVE_DECL_STRERROR_R=1; AC_SUBST([HAVE_DECL_STRERROR_R]) HAVE_DECL_STRSIGNAL=1; AC_SUBST([HAVE_DECL_STRSIGNAL]) HAVE_STRVERSCMP=1; AC_SUBST([HAVE_STRVERSCMP]) REPLACE_MEMCHR=0; AC_SUBST([REPLACE_MEMCHR]) @@ -103,6 +105,7 @@ REPLACE_STRSTR=0; AC_SUBST([REPLACE_STRSTR]) REPLACE_STRCASESTR=0; AC_SUBST([REPLACE_STRCASESTR]) REPLACE_STRERROR=0; AC_SUBST([REPLACE_STRERROR]) + REPLACE_STRERROR_R=0; AC_SUBST([REPLACE_STRERROR_R]) REPLACE_STRNCAT=0; AC_SUBST([REPLACE_STRNCAT]) REPLACE_STRNDUP=0; AC_SUBST([REPLACE_STRNDUP]) REPLACE_STRNLEN=0; AC_SUBST([REPLACE_STRNLEN]) =========================== modules/strerror_r-posix =========================== Description: strerror_r() function: get string describing error code. Files: lib/strerror_r.c m4/strerror_r.m4 Depends-on: string errno extensions lock strerror configure.ac: gl_FUNC_STRERROR_R gl_STRING_MODULE_INDICATOR([strerror_r]) Makefile.am: Include: <string.h> License: LGPLv2+ Maintainer: Bruno Haible ================================================================================ --- modules/string.orig Thu Nov 11 13:14:02 2010 +++ modules/string Tue Nov 9 01:21:10 2010 @@ -60,6 +60,7 @@ -e 's|@''GNULIB_STRCASESTR''@|$(GNULIB_STRCASESTR)|g' \ -e 's|@''GNULIB_STRTOK_R''@|$(GNULIB_STRTOK_R)|g' \ -e 's|@''GNULIB_STRERROR''@|$(GNULIB_STRERROR)|g' \ + -e 's|@''GNULIB_STRERROR_R''@|$(GNULIB_STRERROR_R)|g' \ -e 's|@''GNULIB_STRSIGNAL''@|$(GNULIB_STRSIGNAL)|g' \ -e 's|@''GNULIB_STRVERSCMP''@|$(GNULIB_STRVERSCMP)|g' \ < $(srcdir)/string.in.h | \ @@ -79,6 +80,7 @@ -e 's|@''HAVE_STRSEP''@|$(HAVE_STRSEP)|g' \ -e 's|@''HAVE_STRCASESTR''@|$(HAVE_STRCASESTR)|g' \ -e 's|@''HAVE_DECL_STRTOK_R''@|$(HAVE_DECL_STRTOK_R)|g' \ + -e 's|@''HAVE_DECL_STRERROR_R''@|$(HAVE_DECL_STRERROR_R)|g' \ -e 's|@''HAVE_DECL_STRSIGNAL''@|$(HAVE_DECL_STRSIGNAL)|g' \ -e 's|@''HAVE_STRVERSCMP''@|$(HAVE_STRVERSCMP)|g' \ -e 's|@''REPLACE_STPNCPY''@|$(REPLACE_STPNCPY)|g' \ @@ -88,6 +90,7 @@ -e 's|@''REPLACE_STRDUP''@|$(REPLACE_STRDUP)|g' \ -e 's|@''REPLACE_STRSTR''@|$(REPLACE_STRSTR)|g' \ -e 's|@''REPLACE_STRERROR''@|$(REPLACE_STRERROR)|g' \ + -e 's|@''REPLACE_STRERROR_R''@|$(REPLACE_STRERROR_R)|g' \ -e 's|@''REPLACE_STRNCAT''@|$(REPLACE_STRNCAT)|g' \ -e 's|@''REPLACE_STRNDUP''@|$(REPLACE_STRNDUP)|g' \ -e 's|@''REPLACE_STRNLEN''@|$(REPLACE_STRNLEN)|g' \ --- doc/posix-functions/strerror_r.texi.orig Thu Nov 11 13:14:02 2010 +++ doc/posix-functions/strerror_r.texi Thu Nov 11 13:00:49 2010 @@ -4,17 +4,13 @@ POSIX specification:@* @url{http://www.opengroup.org/onlinepubs/9699919799/functions/strerror_r.html} -Gnulib module: --- +Gnulib module: strerror_r-posix Portability problems fixed by Gnulib: @itemize -...@end itemize - -Portability problems not fixed by Gnulib: -...@itemize @item This function is missing on some platforms: -NetBSD 3.0, HP-UX 11, IRIX 6.5, Solaris 9, mingw. +NetBSD 3.0, HP-UX 11.23, IRIX 6.5, Solaris 9, mingw. @item glibc has an incompatible version of this function. The POSIX compliant code @smallexample @@ -24,4 +20,35 @@ @smallexample char *s = strerror_r (err, buf, buflen); @end smallexample +...@item +This function is sometimes not declared in @code{<string.h>} on some platforms: +glibc 2.8, OSF/1 5.1. +...@item +The third argument is of type @code{int} instead of @code{size_t} on some +platforms: +AIX 5.1, OSF/1 5.1. +...@item +When this function fails, it returns -1 and sets @code{errno}, instead of +returning the error number, on some platforms: +glibc 2.8 with @code{-D_POSIX_C_SOURCE=200112L}, AIX 6.1, OSF/1 5.1. +...@item +This function does not support the error values that are specified by POSIX +but not defined by the system, on some platforms: +OpenBSD 4.0, OSF/1 5.1, NonStop Kernel, Cygwin 1.5.x. +...@item +This function always fails when the third argument is less than 80 on some +platforms: +HP-UX 11.31. +...@item +When the buffer is too small, this function does not fail, but instead +truncates the result and returns 0 on some platforms: +OSF/1 5.1. +...@end itemize + +Portability problems not fixed by Gnulib: +...@itemize +...@item +When the buffer is too small, this function does not fail, but instead +truncates the result and returns 0 on some platforms: +AIX 6.1. @end itemize