2011-02-11 00:48:18 +08:00
|
|
|
/* GNU variant of strerror_r. */
|
2002-05-25 07:44:39 +08:00
|
|
|
/*
|
|
|
|
FUNCTION
|
|
|
|
<<strerror_r>>---convert error number to string and copy to buffer
|
|
|
|
|
|
|
|
INDEX
|
|
|
|
strerror_r
|
|
|
|
|
2017-11-30 16:20:06 +08:00
|
|
|
SYNOPSIS
|
2002-05-25 07:44:39 +08:00
|
|
|
#include <string.h>
|
2011-02-11 00:48:18 +08:00
|
|
|
#ifdef _GNU_SOURCE
|
2002-05-25 07:44:39 +08:00
|
|
|
char *strerror_r(int <[errnum]>, char *<[buffer]>, size_t <[n]>);
|
2011-02-11 00:48:18 +08:00
|
|
|
#else
|
|
|
|
int strerror_r(int <[errnum]>, char *<[buffer]>, size_t <[n]>);
|
|
|
|
#endif
|
2002-05-25 07:44:39 +08:00
|
|
|
|
|
|
|
DESCRIPTION
|
|
|
|
<<strerror_r>> converts the error number <[errnum]> into a
|
|
|
|
string and copies the result into the supplied <[buffer]> for
|
2011-02-11 00:48:18 +08:00
|
|
|
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
|
2002-05-25 07:44:39 +08:00
|
|
|
error number, the result is the empty string.
|
|
|
|
|
|
|
|
See <<strerror>> for how strings are mapped to <<errnum>>.
|
|
|
|
|
|
|
|
RETURNS
|
2011-02-11 00:48:18 +08:00
|
|
|
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>>.
|
2002-05-25 07:44:39 +08:00
|
|
|
|
|
|
|
PORTABILITY
|
2011-02-11 00:48:18 +08:00
|
|
|
<<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,
|
2011-05-26 02:41:10 +08:00
|
|
|
or if it is thread-safe and uses separate storage according to whether
|
|
|
|
the second argument of that function is non-zero. For more details
|
|
|
|
on <<_user_strerror>>, see the <<strerror>> documentation.
|
2011-02-11 00:48:18 +08:00
|
|
|
|
|
|
|
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
|
2011-05-26 02:41:10 +08:00
|
|
|
a non-empty alternate string without assigning into its third argument.
|
2002-05-25 07:44:39 +08:00
|
|
|
|
|
|
|
<<strerror_r>> requires no supporting OS subroutines.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
#undef __STRICT_ANSI__
|
2011-02-11 00:48:18 +08:00
|
|
|
#define _GNU_SOURCE
|
2002-05-25 07:44:39 +08:00
|
|
|
#include <errno.h>
|
|
|
|
#include <string.h>
|
2011-02-11 00:48:18 +08:00
|
|
|
#undef strerror_r
|
2002-05-25 07:44:39 +08:00
|
|
|
|
2011-02-11 00:48:18 +08:00
|
|
|
/* For backwards-compatible linking, this must be the GNU signature;
|
|
|
|
see xpg_strerror_r.c for the POSIX version. */
|
2002-05-25 07:44:39 +08:00
|
|
|
char *
|
|
|
|
_DEFUN (strerror_r, (errnum, buffer, n),
|
|
|
|
int errnum _AND
|
|
|
|
char *buffer _AND
|
|
|
|
size_t n)
|
|
|
|
{
|
2011-05-26 02:41:10 +08:00
|
|
|
char *error = _strerror_r (_REENT, errnum, 1, NULL);
|
2002-05-25 07:44:39 +08:00
|
|
|
|
2011-02-11 00:48:18 +08:00
|
|
|
if (strlen (error) >= n)
|
|
|
|
return error;
|
|
|
|
return strcpy (buffer, error);
|
2002-05-25 07:44:39 +08:00
|
|
|
}
|