No need to make a wrapper that burns static storage when we can just use stack storage.
* modules/perror (Files): Drop strerror-impl.h. * lib/perror.c (perror): Use our own stack buffer, rather than calling a wrapper that uses static storage. * doc/posix-functions/perror.texi (perror): Document a limitation of our replacement. Signed-off-by: Eric Blake <ebl...@redhat.com> --- > So about the only other thing we can do is make both strerror.c and > strerror_r.c call into a third file that implements the gnulib > replacement strings, and thus make it so that strerror no longer > directly depends on strerror_r.c; patches welcome. Thoughts on this series before I push it? ChangeLog | 7 +++++++ doc/posix-functions/perror.texi | 4 ++++ lib/perror.c | 14 +++++++++++--- modules/perror | 1 - 4 files changed, 22 insertions(+), 4 deletions(-) diff --git a/ChangeLog b/ChangeLog index 3dc7091..a66e44d 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,12 @@ 2011-05-24 Eric Blake <ebl...@redhat.com> + perror: call strerror_r directly + * modules/perror (Files): Drop strerror-impl.h. + * lib/perror.c (perror): Use our own stack buffer, rather than + calling a wrapper that uses static storage. + * doc/posix-functions/perror.texi (perror): Document a limitation + of our replacement. + strerror_r: fix missing header * lib/strerror_r.c: Avoid compiler warning about snprintf. diff --git a/doc/posix-functions/perror.texi b/doc/posix-functions/perror.texi index cf6ac79..167cf39 100644 --- a/doc/posix-functions/perror.texi +++ b/doc/posix-functions/perror.texi @@ -24,4 +24,8 @@ perror POSIX requires that this function set the stream error bit (detected by @code{ferror}) on write failure, but not all platforms do this: glibc 2.13. +@item +POSIX requires that this function not alter stream orientation, but +the gnulib replacement locks in byte orientation and fails on wide +character streams. @end itemize diff --git a/lib/perror.c b/lib/perror.c index 29af3c5..88981d1 100644 --- a/lib/perror.c +++ b/lib/perror.c @@ -40,10 +40,18 @@ void perror (const char *string) { - const char *errno_description = my_strerror (errno); + char stackbuf[256]; + int ret; + + /* Our implementation guarantees that this will be a non-empty + string, even if it returns EINVAL; and stackbuf should be sized + large enough to avoid ERANGE. */ + ret = strerror_r (errno, stackbuf, sizeof stackbuf); + if (ret == ERANGE) + abort (); if (string != NULL && *string != '\0') - fprintf (stderr, "%s: %s\n", string, errno_description); + fprintf (stderr, "%s: %s\n", string, stackbuf); else - fprintf (stderr, "%s\n", errno_description); + fprintf (stderr, "%s\n", stackbuf); } diff --git a/modules/perror b/modules/perror index 1ff9ec2..38e28ce 100644 --- a/modules/perror +++ b/modules/perror @@ -3,7 +3,6 @@ perror() function: print a message describing error code. Files: lib/perror.c -lib/strerror-impl.h m4/perror.m4 Depends-on: -- 1.7.4.4