1 /* Copyright (c) 2002 Jeff Johnston <jjohnstn@redhat.com> */
2 /* GNU variant of strerror_r. */
3 /*
4 FUNCTION
5 <<strerror_r>>---convert error number to string and copy to buffer
6
7 INDEX
8 strerror_r
9
10 SYNOPSIS
11 #include <string.h>
12 #ifdef _GNU_SOURCE
13 char *strerror_r(int <[errnum]>, char *<[buffer]>, size_t <[n]>);
14 #else
15 int strerror_r(int <[errnum]>, char *<[buffer]>, size_t <[n]>);
16 #endif
17
18 DESCRIPTION
19 <<strerror_r>> converts the error number <[errnum]> into a
20 string and copies the result into the supplied <[buffer]> for
21 a length up to <[n]>, including the NUL terminator. The value of
22 <[errnum]> is usually a copy of <<errno>>. If <<errnum>> is not a known
23 error number, the result is the empty string.
24
25 See <<strerror>> for how strings are mapped to <<errnum>>.
26
27 RETURNS
28 There are two variants: the GNU version always returns a NUL-terminated
29 string, which is <[buffer]> if all went well, but which is another
30 pointer if <[n]> was too small (leaving <[buffer]> untouched). If the
31 return is not <[buffer]>, your application must not modify that string.
32 The POSIX version returns 0 on success, <[EINVAL]> if <<errnum>> was not
33 recognized, and <[ERANGE]> if <[n]> was too small. The variant chosen
34 depends on macros that you define before inclusion of <<string.h>>.
35
36 PORTABILITY
37 <<strerror_r>> with a <[char *]> result is a GNU extension.
38 <<strerror_r>> with an <[int]> result is required by POSIX 2001.
39 This function is compliant only if <<_user_strerror>> is not provided,
40 or if it is thread-safe and uses separate storage according to whether
41 the second argument of that function is non-zero. For more details
42 on <<_user_strerror>>, see the <<strerror>> documentation.
43
44 POSIX states that the contents of <[buf]> are unspecified on error,
45 although this implementation guarantees a NUL-terminated string for
46 all except <[n]> of 0.
47
48 POSIX recommends that unknown <[errnum]> result in a message including
49 that value, however it is not a requirement and this implementation
50 provides only an empty string (unless you provide <<_user_strerror>>).
51 POSIX also recommends that unknown <[errnum]> fail with EINVAL even
52 when providing such a message, however it is not a requirement and
53 this implementation will return success if <<_user_strerror>> provided
54 a non-empty alternate string without assigning into its third argument.
55
56 <<strerror_r>> requires no supporting OS subroutines.
57
58 */
59
60 #undef __STRICT_ANSI__
61 #define _GNU_SOURCE
62 #include <errno.h>
63 #include <string.h>
64 #undef strerror_r
65
66 /* For backwards-compatible linking, this must be the GNU signature;
67 see xpg_strerror_r.c for the POSIX version. */
68 char *
strerror_r(int errnum,char * buffer,size_t n)69 strerror_r (int errnum,
70 char *buffer,
71 size_t n)
72 {
73 char *error = _strerror_r (errnum, 1, NULL);
74
75 if (strlen (error) >= n)
76 return error;
77 return strcpy (buffer, error);
78 }
79