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