malloc.3: ffix
[man-pages.git] / man3 / strerror.3
blob5edaeded2861d42c46bffe247fb0de72bc201fb0
1 .\" Copyright (C) 1993 David Metcalfe (david@prism.demon.co.uk)
2 .\" and Copyright (C) 2005, 2014, 2020 Michael Kerrisk <mtk.manpages@gmail.com>
3 .\"
4 .\" %%%LICENSE_START(VERBATIM)
5 .\" Permission is granted to make and distribute verbatim copies of this
6 .\" manual provided the copyright notice and this permission notice are
7 .\" preserved on all copies.
8 .\"
9 .\" Permission is granted to copy and distribute modified versions of this
10 .\" manual under the conditions for verbatim copying, provided that the
11 .\" entire resulting derived work is distributed under the terms of a
12 .\" permission notice identical to this one.
13 .\"
14 .\" Since the Linux kernel and libraries are constantly changing, this
15 .\" manual page may be incorrect or out-of-date.  The author(s) assume no
16 .\" responsibility for errors or omissions, or for damages resulting from
17 .\" the use of the information contained herein.  The author(s) may not
18 .\" have taken the same level of care in the production of this manual,
19 .\" which is licensed free of charge, as they might when working
20 .\" professionally.
21 .\"
22 .\" Formatted or processed versions of this manual, if unaccompanied by
23 .\" the source, must acknowledge the copyright and authors of this work.
24 .\" %%%LICENSE_END
25 .\"
26 .\" References consulted:
27 .\"     Linux libc source code
28 .\"     Lewine's _POSIX Programmer's Guide_ (O'Reilly & Associates, 1991)
29 .\"     386BSD man pages
30 .\" Modified Sat Jul 24 18:05:30 1993 by Rik Faith <faith@cs.unc.edu>
31 .\" Modified Fri Feb 16 14:25:17 1996 by Andries Brouwer <aeb@cwi.nl>
32 .\" Modified Sun Jul 21 20:55:44 1996 by Andries Brouwer <aeb@cwi.nl>
33 .\" Modified Mon Oct 15 21:16:25 2001 by John Levon <moz@compsoc.man.ac.uk>
34 .\" Modified Tue Oct 16 00:04:43 2001 by Andries Brouwer <aeb@cwi.nl>
35 .\" Modified Fri Jun 20 03:04:30 2003 by Andries Brouwer <aeb@cwi.nl>
36 .\" 2005-12-13, mtk, Substantial rewrite of strerror_r() description
37 .\"         Addition of extra material on portability and standards.
38 .\"
39 .TH STRERROR 3  2021-03-22 "" "Linux Programmer's Manual"
40 .SH NAME
41 strerror, strerrorname_np, strerrordesc_np, strerror_r, strerror_l \- return string describing error number
42 .SH SYNOPSIS
43 .nf
44 .B #include <string.h>
45 .PP
46 .BI "char *strerror(int " errnum );
47 .BI "const char *strerrorname_np(int " errnum );
48 .BI "const char *strerrordesc_np(int " errnum );
49 .PP
50 .BI "int strerror_r(int " errnum ", char *" buf ", size_t " buflen );
51                /* XSI-compliant */
52 .PP
53 .BI "char *strerror_r(int " errnum ", char *" buf ", size_t " buflen );
54                /* GNU-specific */
55 .PP
56 .BI "char *strerror_l(int " errnum ", locale_t " locale );
57 .fi
58 .PP
59 .RS -4
60 Feature Test Macro Requirements for glibc (see
61 .BR feature_test_macros (7)):
62 .RE
63 .PP
64 .BR strerrorname_np (),
65 .BR strerrordesc_np ():
66 .nf
67     _GNU_SOURCE
68 .fi
69 .PP
70 .BR strerror_r ():
71 .nf
72     The XSI-compliant version is provided if:
73         (_POSIX_C_SOURCE >= 200112L) && ! _GNU_SOURCE
74     Otherwise, the GNU-specific version is provided.
75 .fi
76 .SH DESCRIPTION
77 The
78 .BR strerror ()
79 function returns a pointer to a string that describes the error
80 code passed in the argument
81 .IR errnum ,
82 possibly using the
83 .B LC_MESSAGES
84 part of the current locale to select the appropriate language.
85 (For example, if
86 .I errnum
88 .BR EINVAL ,
89 the returned description will be "Invalid argument".)
90 This string must not be modified by the application, but may be
91 modified by a subsequent call to
92 .BR strerror ()
94 .BR strerror_l ().
95 No other library function, including
96 .BR perror (3),
97 will modify this string.
98 .PP
99 Like
100 .BR strerror (),
102 .BR strerrordesc_np ()
103 function returns a pointer to a string that describes the error
104 code passed in the argument
105 .IR errnum ,
106 with the difference that the returned string is not translated
107 according to the current locale.
110 .BR strerrorname_np ()
111 function returns a pointer to a string containing the name of the error
112 code passed in the argument
113 .IR errnum .
114 For example, given
115 .BR EPERM
116 as an argument, this function returns a pointer to the string "EPERM".
118 .SS strerror_r()
120 .BR strerror_r ()
121 function is similar to
122 .BR strerror (),
123 but is
124 thread safe.
125 This function is available in two versions:
126 an XSI-compliant version specified in POSIX.1-2001
127 (available since glibc 2.3.4, but not POSIX-compliant until glibc 2.13),
128 and a GNU-specific version (available since glibc 2.0).
129 The XSI-compliant version is provided with the feature test macros
130 settings shown in the SYNOPSIS;
131 otherwise the GNU-specific version is provided.
132 If no feature test macros are explicitly defined,
133 then (since glibc 2.4)
134 .B _POSIX_C_SOURCE
135 is defined by default with the value
136 200112L, so that the XSI-compliant version of
137 .BR strerror_r ()
138 is provided by default.
140 The XSI-compliant
141 .BR strerror_r ()
142 is preferred for portable applications.
143 It returns the error string in the user-supplied buffer
144 .I buf
145 of length
146 .IR buflen .
148 The GNU-specific
149 .BR strerror_r ()
150 returns a pointer to a string containing the error message.
151 This may be either a pointer to a string that the function stores in
152 .IR buf ,
153 or a pointer to some (immutable) static string
154 (in which case
155 .I buf
156 is unused).
157 If the function stores a string in
158 .IR buf ,
159 then at most
160 .I buflen
161 bytes are stored (the string may be truncated if
162 .I buflen
163 is too small and
164 .I errnum
165 is unknown).
166 The string always includes a terminating null byte (\(aq\e0\(aq).
168 .SS strerror_l()
169 .BR strerror_l ()
170 is like
171 .BR strerror (),
172 but maps
173 .I errnum
174 to a locale-dependent error message in the locale specified by
175 .IR locale .
176 The behavior of
177 .BR strerror_l ()
178 is undefined if
179 .I locale
180 is the special locale object
181 .BR LC_GLOBAL_LOCALE
182 or is not a valid locale object handle.
183 .SH RETURN VALUE
185 .BR strerror (),
186 .BR strerror_l (),
187 and the GNU-specific
188 .BR strerror_r ()
189 functions return
190 the appropriate error description string,
191 or an "Unknown error nnn" message if the error number is unknown.
193 On success,
194 .BR strerrorname_np ()
196 .BR strerrordesc_np ()
197 return the appropriate error description string.
199 .I errnum
200 is an invalid error number, these functions return NULL.
202 The XSI-compliant
203 .BR strerror_r ()
204 function returns 0 on success.
205 On error,
206 a (positive) error number is returned (since glibc 2.13),
207 or \-1 is returned and
208 .I errno
209 is set to indicate the error (glibc versions before 2.13).
211 POSIX.1-2001 and POSIX.1-2008 require that a successful call to
212 .BR strerror ()
214 .BR strerror_l ()
215 shall leave
216 .I errno
217 unchanged, and note that,
218 since no function return value is reserved to indicate an error,
219 an application that wishes to check for errors should initialize
220 .I errno
221 to zero before the call,
222 and then check
223 .I errno
224 after the call.
225 .SH ERRORS
227 .B EINVAL
228 The value of
229 .I errnum
230 is not a valid error number.
232 .B ERANGE
233 Insufficient storage was supplied to contain the error description string.
234 .SH VERSIONS
236 .BR strerror_l ()
237 function first appeared in glibc 2.6.
240 .BR strerrorname_np ()
242 .BR strerrordesc_np ()
243 functions first appeared in glibc 2.32.
244 .SH ATTRIBUTES
245 For an explanation of the terms used in this section, see
246 .BR attributes (7).
247 .ad l
250 allbox;
251 lb lb lbx
252 l l l.
253 Interface       Attribute       Value
255 .BR strerror ()
256 T}      Thread safety   T{
257 MT-Unsafe race:strerror
260 .BR strerrorname_np (),
261 .BR strerrordesc_np ()
262 T}      Thread safety   MT-Safe
264 .BR strerror_r (),
265 .BR strerror_l ()
266 T}      Thread safety   MT-Safe
270 .sp 1
271 .SH CONFORMING TO
272 .BR strerror ()
273 is specified by POSIX.1-2001, POSIX.1-2008, C89, and C99.
274 .BR strerror_r ()
275 is specified by POSIX.1-2001 and POSIX.1-2008.
276 .\" FIXME . for later review when Issue 8 is one day released...
277 .\" A future POSIX.1 may remove strerror_r()
278 .\" http://austingroupbugs.net/tag_view_page.php?tag_id=8
279 .\" http://austingroupbugs.net/view.php?id=508
281 .BR strerror_l ()
282 is specified in POSIX.1-2008.
284 The GNU-specific functions
285 .BR strerror_r (),
286 .BR strerrorname_np (),
288 .BR strerrordesc_np ()
289 are nonstandard extensions.
291 POSIX.1-2001 permits
292 .BR strerror ()
293 to set
294 .I errno
295 if the call encounters an error, but does not specify what
296 value should be returned as the function result in the event of an error.
297 On some systems,
298 .\" e.g., Solaris 8, HP-UX 11
299 .BR strerror ()
300 returns NULL if the error number is unknown.
301 On other systems,
302 .\" e.g., FreeBSD 5.4, Tru64 5.1B
303 .BR strerror ()
304 returns a string something like "Error nnn occurred" and sets
305 .I errno
307 .B EINVAL
308 if the error number is unknown.
309 C99 and POSIX.1-2008 require the return value to be non-NULL.
310 .SH NOTES
311 The GNU C Library uses a buffer of 1024 characters for
312 .BR strerror ().
313 This buffer size therefore should be sufficient to avoid an
314 .B ERANGE
315 error when calling
316 .BR strerror_r ().
318 .BR strerrorname_np ()
320 .BR strerrordesc_np ()
321 are thread-safe and async-signal-safe.
322 .SH SEE ALSO
323 .BR err (3),
324 .BR errno (3),
325 .BR error (3),
326 .BR perror (3),
327 .BR strsignal (3),
328 .BR locale (7)