* libc/include/string.h (strerror_r): Update declaration.
* libc/string/strerror.c (strerror): Update documentation.
* libc/string/strerror_r.c (strerror_r): Always return
NUL-terminated string; don't overwrite too-short buf.
* libc/string/xpg_strerror_r.c (__xpg_strerror_r): Implement POSIX
variant.
* libc/string/Makefile.am (GENERAL_SOURCES): Build new file.
* libc/string/Makefile.in: Regenerate.
+2011-02-09 Eric Blake <eblake@redhat.com>
+
+ * libc/include/string.h (strerror_r): Update declaration.
+ * libc/string/strerror.c (strerror): Update documentation.
+ * libc/string/strerror_r.c (strerror_r): Always return
+ NUL-terminated string; don't overwrite too-short buf.
+ * libc/string/xpg_strerror_r.c (__xpg_strerror_r): Implement POSIX
+ variant.
+ * libc/string/Makefile.am (GENERAL_SOURCES): Build new file.
+ * libc/string/Makefile.in: Regenerate.
+
2011-01-28 Corinna Vinschen <vinschen@redhat.com>
* libc/stdio/fclose.c: Only use sfp lock to guard non-atomic
char *_EXFUN(_strdup_r,(struct _reent *, const char *));
char *_EXFUN(strndup,(const char *, size_t));
char *_EXFUN(_strndup_r,(struct _reent *, const char *, size_t));
-char *_EXFUN(strerror_r,(int, char *, size_t));
+/* There are two common strerror_r variants. If you request
+ _GNU_SOURCE, you get the GNU version; otherwise you get the POSIX
+ version. POSIX requires that #undef strerror_r will still let you
+ invoke the underlying function, but that requires gcc support. */
+#ifdef _GNU_SOURCE
+char *_EXFUN(strerror_r,(int, char *, size_t));
+#else
+# ifdef __GNUC__
+int _EXFUN(strerror_r,(int, char *, size_t)) __asm__ ("__xpg_strerror_r");
+# else
+int _EXFUN(__xpg_strerror_r,(int, char *, size_t));
+# define strerror_r __xpg_strerror_r
+# endif
+#endif
size_t _EXFUN(strlcat,(char *, const char *, size_t));
size_t _EXFUN(strlcpy,(char *, const char *, size_t));
int _EXFUN(strncasecmp,(const char *, const char *, size_t));
wmemcmp.c \
wmemcpy.c \
wmemmove.c \
- wmemset.c
+ wmemset.c \
+ xpg_strerror_r.c
if ELIX_LEVEL_1
ELIX_2_SOURCES =
lib_a-wcsxfrm.$(OBJEXT) lib_a-wcwidth.$(OBJEXT) \
lib_a-wmemchr.$(OBJEXT) lib_a-wmemcmp.$(OBJEXT) \
lib_a-wmemcpy.$(OBJEXT) lib_a-wmemmove.$(OBJEXT) \
- lib_a-wmemset.$(OBJEXT)
+ lib_a-wmemset.$(OBJEXT) lib_a-xpg_strerror_r.$(OBJEXT)
@ELIX_LEVEL_1_FALSE@am__objects_2 = lib_a-bcmp.$(OBJEXT) \
@ELIX_LEVEL_1_FALSE@ lib_a-memccpy.$(OBJEXT) \
@ELIX_LEVEL_1_FALSE@ lib_a-mempcpy.$(OBJEXT) \
wcslcpy.lo wcslen.lo wcsncat.lo wcsncmp.lo wcsncpy.lo \
wcsnlen.lo wcspbrk.lo wcsrchr.lo wcsspn.lo wcsstr.lo wcstok.lo \
wcswidth.lo wcsxfrm.lo wcwidth.lo wmemchr.lo wmemcmp.lo \
- wmemcpy.lo wmemmove.lo wmemset.lo
+ wmemcpy.lo wmemmove.lo wmemset.lo xpg_strerror_r.lo
@ELIX_LEVEL_1_FALSE@am__objects_5 = bcmp.lo memccpy.lo mempcpy.lo \
@ELIX_LEVEL_1_FALSE@ stpcpy.lo stpncpy.lo strndup.lo \
@ELIX_LEVEL_1_FALSE@ strcasestr.lo strndup_r.lo wcpcpy.lo \
NEWLIB_CFLAGS = @NEWLIB_CFLAGS@
NM = @NM@
NMEDIT = @NMEDIT@
+NO_INCLUDE_LIST = @NO_INCLUDE_LIST@
OBJDUMP = @OBJDUMP@
OBJEXT = @OBJEXT@
OTOOL = @OTOOL@
wmemcmp.c \
wmemcpy.c \
wmemmove.c \
- wmemset.c
+ wmemset.c \
+ xpg_strerror_r.c
@ELIX_LEVEL_1_FALSE@ELIX_2_SOURCES = \
@ELIX_LEVEL_1_FALSE@ bcmp.c \
lib_a-wmemset.obj: wmemset.c
$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(lib_a_CFLAGS) $(CFLAGS) -c -o lib_a-wmemset.obj `if test -f 'wmemset.c'; then $(CYGPATH_W) 'wmemset.c'; else $(CYGPATH_W) '$(srcdir)/wmemset.c'; fi`
+lib_a-xpg_strerror_r.o: xpg_strerror_r.c
+ $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(lib_a_CFLAGS) $(CFLAGS) -c -o lib_a-xpg_strerror_r.o `test -f 'xpg_strerror_r.c' || echo '$(srcdir)/'`xpg_strerror_r.c
+
+lib_a-xpg_strerror_r.obj: xpg_strerror_r.c
+ $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(lib_a_CFLAGS) $(CFLAGS) -c -o lib_a-xpg_strerror_r.obj `if test -f 'xpg_strerror_r.c'; then $(CYGPATH_W) 'xpg_strerror_r.c'; else $(CYGPATH_W) '$(srcdir)/xpg_strerror_r.c'; fi`
+
lib_a-bcmp.o: bcmp.c
$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(lib_a_CFLAGS) $(CFLAGS) -c -o lib_a-bcmp.o `test -f 'bcmp.c' || echo '$(srcdir)/'`bcmp.c
result string; therefore portable code cannot depend on the reentrancy
of this subroutine.
+Although this implementation of <<strerror>> guarantees a non-null
+result with a NUL-terminator, some implementations return <<NULL>>
+on failure. Although POSIX allows <<strerror>> to set <<errno>>
+to EINVAL on failure, this implementation does not do so (unless
+you provide <<_user_strerror>>).
+
+POSIX recommends that unknown <[errnum]> result in a message
+including that value, however it is not a requirement and this
+implementation does not provide that information (unless you
+provide <<_user_strerror>>).
+
This implementation of <<strerror>> provides for user-defined
extensibility. <<errno.h>> defines <[__ELASTERROR]>, which can be
used as a base for user-defined error values. If the user supplies a
<<_user_strerror>> returns <[NULL]>. The default <<_user_strerror>>
returns <[NULL]> for all input values.
+Note that <<_user_sterror>> must be thread-safe and not alter <<errno>>
+if <<strerror_r>> is to comply with POSIX.
+
<<strerror>> requires no supporting OS subroutines.
QUICKREF
+/* GNU variant of strerror_r. */
/*
FUNCTION
<<strerror_r>>---convert error number to string and copy to buffer
ANSI_SYNOPSIS
#include <string.h>
+ #ifdef _GNU_SOURCE
char *strerror_r(int <[errnum]>, char *<[buffer]>, size_t <[n]>);
+ #else
+ int strerror_r(int <[errnum]>, char *<[buffer]>, size_t <[n]>);
+ #endif
TRAD_SYNOPSIS
#include <string.h>
DESCRIPTION
<<strerror_r>> converts the error number <[errnum]> into a
string and copies the result into the supplied <[buffer]> for
-a length up to <[n]>, including the NUL terminator. The value of
-<[errnum]> is usually a copy of <<errno>>. If <<errnum>> is not a known
+a length up to <[n]>, including the NUL terminator. The value of
+<[errnum]> is usually a copy of <<errno>>. If <<errnum>> is not a known
error number, the result is the empty string.
See <<strerror>> for how strings are mapped to <<errnum>>.
RETURNS
-This function returns a pointer to a string. Your application must
-not modify that string.
+There are two variants: the GNU version always returns a NUL-terminated
+string, which is <[buffer]> if all went well, but which is another
+pointer if <[n]> was too small (leaving <[buffer]> untouched). If the
+return is not <[buffer]>, your application must not modify that string.
+The POSIX version returns 0 on success, <[EINVAL]> if <<errnum>> was not
+recognized, and <[ERANGE]> if <[n]> was too small. The variant chosen
+depends on macros that you define before inclusion of <<string.h>>.
PORTABILITY
-<<strerror_r>> is a GNU extension.
+<<strerror_r>> with a <[char *]> result is a GNU extension.
+<<strerror_r>> with an <[int]> result is required by POSIX 2001.
+This function is compliant only if <<_user_strerror>> is not provided,
+or if it is thread-safe and does not modify <<errno>>.
+
+POSIX states that the contents of <[buf]> are unspecified on error,
+although this implementation guarantees a NUL-terminated string for
+all except <[n]> of 0.
+
+POSIX recommends that unknown <[errnum]> result in a message including
+that value, however it is not a requirement and this implementation
+provides only an empty string (unless you provide <<_user_strerror>>).
+POSIX also recommends that unknown <[errnum]> fail with EINVAL even
+when providing such a message, however it is not a requirement and
+this implementation will return success if <<_user_strerror>> provided
+a non-empty alternate string.
<<strerror_r>> requires no supporting OS subroutines.
*/
#undef __STRICT_ANSI__
+#define _GNU_SOURCE
#include <errno.h>
#include <string.h>
+#undef strerror_r
+/* For backwards-compatible linking, this must be the GNU signature;
+ see xpg_strerror_r.c for the POSIX version. */
char *
_DEFUN (strerror_r, (errnum, buffer, n),
int errnum _AND
char *buffer _AND
size_t n)
{
- char *error;
- error = strerror (errnum);
+ char *error = strerror (errnum);
- return strncpy (buffer, (const char *)error, n);
+ if (strlen (error) >= n)
+ return error;
+ return strcpy (buffer, error);
}
--- /dev/null
+/* POSIX variant of strerror_r. */
+#undef __STRICT_ANSI__
+#include <errno.h>
+#include <string.h>
+
+int
+_DEFUN (__xpg_strerror_r, (errnum, buffer, n),
+ int errnum _AND
+ char *buffer _AND
+ size_t n)
+{
+ char *error;
+
+ if (!n)
+ return ERANGE;
+ error = strerror (errnum);
+ if (strlen (error) >= n)
+ {
+ memcpy (buffer, error, n - 1);
+ buffer[n - 1] = '\0';
+ return ERANGE;
+ }
+ strcpy (buffer, error);
+ return *error ? 0 : EINVAL;
+}