1STRERROR(3) Linux Programmer's Manual STRERROR(3)
2
3
4
6 strerror, strerror_r, strerror_l - return string describing error num‐
7 ber
8
10 #include <string.h>
11
12 char *strerror(int errnum);
13
14 int strerror_r(int errnum, char *buf, size_t buflen);
15 /* XSI-compliant */
16
17 char *strerror_r(int errnum, char *buf, size_t buflen);
18 /* GNU-specific */
19
20 char *strerror_l(int errnum, locale_t locale);
21
22 Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
23
24 strerror_r():
25 The XSI-compliant version is provided if:
26 (_POSIX_C_SOURCE >= 200112L) && ! _GNU_SOURCE
27 Otherwise, the GNU-specific version is provided.
28
30 The strerror() function returns a pointer to a string that describes
31 the error code passed in the argument errnum, possibly using the
32 LC_MESSAGES part of the current locale to select the appropriate lan‐
33 guage. (For example, if errnum is EINVAL, the returned description
34 will be "Invalid argument".) This string must not be modified by the
35 application, but may be modified by a subsequent call to strerror() or
36 strerror_l(). No other library function, including perror(3), will
37 modify this string.
38
39 strerror_r()
40 The strerror_r() function is similar to strerror(), but is thread safe.
41 This function is available in two versions: an XSI-compliant version
42 specified in POSIX.1-2001 (available since glibc 2.3.4, but not POSIX-
43 compliant until glibc 2.13), and a GNU-specific version (available
44 since glibc 2.0). The XSI-compliant version is provided with the fea‐
45 ture test macros settings shown in the SYNOPSIS; otherwise the GNU-spe‐
46 cific version is provided. If no feature test macros are explicitly
47 defined, then (since glibc 2.4) _POSIX_C_SOURCE is defined by default
48 with the value 200112L, so that the XSI-compliant version of str‐
49 error_r() is provided by default.
50
51 The XSI-compliant strerror_r() is preferred for portable applications.
52 It returns the error string in the user-supplied buffer buf of length
53 buflen.
54
55 The GNU-specific strerror_r() returns a pointer to a string containing
56 the error message. This may be either a pointer to a string that the
57 function stores in buf, or a pointer to some (immutable) static string
58 (in which case buf is unused). If the function stores a string in buf,
59 then at most buflen bytes are stored (the string may be truncated if
60 buflen is too small and errnum is unknown). The string always includes
61 a terminating null byte ('\0').
62
63 strerror_l()
64 strerror_l() is like strerror(), but maps errnum to a locale-dependent
65 error message in the locale specified by locale. The behavior of str‐
66 error_l() is undefined if locale is the special locale object
67 LC_GLOBAL_LOCALE or is not a valid locale object handle.
68
70 The strerror(), strerror_l(), and the GNU-specific strerror_r() func‐
71 tions return the appropriate error description string, or an "Unknown
72 error nnn" message if the error number is unknown.
73
74 The XSI-compliant strerror_r() function returns 0 on success. On
75 error, a (positive) error number is returned (since glibc 2.13), or -1
76 is returned and errno is set to indicate the error (glibc versions
77 before 2.13).
78
79 POSIX.1-2001 and POSIX.1-2008 require that a successful call to str‐
80 error() or strerror_l() shall leave errno unchanged, and note that,
81 since no function return value is reserved to indicate an error, an
82 application that wishes to check for errors should initialize errno to
83 zero before the call, and then check errno after the call.
84
86 EINVAL The value of errnum is not a valid error number.
87
88 ERANGE Insufficient storage was supplied to contain the error descrip‐
89 tion string.
90
92 The strerror_l() function first appeared in glibc 2.6.
93
95 For an explanation of the terms used in this section, see
96 attributes(7).
97
98 ┌───────────────┬───────────────┬─────────────────────────┐
99 │Interface │ Attribute │ Value │
100 ├───────────────┼───────────────┼─────────────────────────┤
101 │strerror() │ Thread safety │ MT-Unsafe race:strerror │
102 ├───────────────┼───────────────┼─────────────────────────┤
103 │strerror_r(), │ Thread safety │ MT-Safe │
104 │strerror_l() │ │ │
105 └───────────────┴───────────────┴─────────────────────────┘
107 strerror() is specified by POSIX.1-2001, POSIX.1-2008, C89, and C99.
108 strerror_r() is specified by POSIX.1-2001 and POSIX.1-2008.
109
110 strerror_l() is specified in POSIX.1-2008.
111
112 The GNU-specific strerror_r() function is a nonstandard extension.
113
114 POSIX.1-2001 permits strerror() to set errno if the call encounters an
115 error, but does not specify what value should be returned as the func‐
116 tion result in the event of an error. On some systems, strerror()
117 returns NULL if the error number is unknown. On other systems, str‐
118 error() returns a string something like "Error nnn occurred" and sets
119 errno to EINVAL if the error number is unknown. C99 and POSIX.1-2008
120 require the return value to be non-NULL.
121
123 The GNU C Library uses a buffer of 1024 characters for strerror().
124 This buffer size therefore should be sufficient to avoid an ERANGE
125 error when calling strerror_r().
126
128 err(3), errno(3), error(3), perror(3), strsignal(3), locale(7)
129
131 This page is part of release 5.04 of the Linux man-pages project. A
132 description of the project, information about reporting bugs, and the
133 latest version of this page, can be found at
134 https://www.kernel.org/doc/man-pages/.
135
136
137
138 2019-03-06 STRERROR(3)