xref: /netbsd-src/lib/libc/string/strerror.3 (revision 7ec84f335ad5fb4672305723762810836fa080e1)
1.\" $NetBSD: strerror.3,v 1.24 2020/04/04 21:29:54 dholland Exp $
2.\"
3.\" Copyright (c) 1980, 1991, 1993
4.\"	The Regents of the University of California.  All rights reserved.
5.\"
6.\" This code is derived from software contributed to Berkeley by
7.\" the American National Standards Committee X3, on Information
8.\" Processing Systems.
9.\"
10.\" Redistribution and use in source and binary forms, with or without
11.\" modification, are permitted provided that the following conditions
12.\" are met:
13.\" 1. Redistributions of source code must retain the above copyright
14.\"    notice, this list of conditions and the following disclaimer.
15.\" 2. Redistributions in binary form must reproduce the above copyright
16.\"    notice, this list of conditions and the following disclaimer in the
17.\"    documentation and/or other materials provided with the distribution.
18.\" 3. Neither the name of the University nor the names of its contributors
19.\"    may be used to endorse or promote products derived from this software
20.\"    without specific prior written permission.
21.\"
22.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32.\" SUCH DAMAGE.
33.\"
34.\"     @(#)strerror.3	8.1 (Berkeley) 6/9/93
35.Dd April 4, 2020
36.Dt STRERROR 3
37.Os
38.Sh NAME
39.Nm perror ,
40.Nm strerror ,
41.Nm strerror_l ,
42.\" .Nm strerror_lr ,
43.Nm strerror_r ,
44.Nm sys_errlist ,
45.Nm sys_nerr
46.Nd system error messages
47.Sh LIBRARY
48.Lb libc
49.Sh SYNOPSIS
50.In stdio.h
51.Ft void
52.Fn perror "const char *string"
53.In errno.h
54.Vt extern const char * const sys_errlist[] ;
55.Vt extern const int sys_nerr ;
56.In string.h
57.Ft "char *"
58.Fn strerror "int errnum"
59.Ft int
60.Fn strerror_r "int errnum" "char *strerrbuf" "size_t buflen"
61.Ft "char *"
62.Fn strerror_l "int errnum" "locale_t loc"
63.\".Ft int
64.\".Fn strerror_lr "int errnum" "char *strerrbuf" "size_t buflen" "locale_t loc"
65.Sh DESCRIPTION
66The
67.Fn strerror ,
68.Fn strerror_l ,
69.\".Fn strerror_lr ,
70.Fn strerror_r ,
71and
72.Fn perror
73functions look up the language-dependent error message
74string corresponding to an error number.
75.Pp
76The
77.Fn strerror
78function accepts an error number argument
79.Fa errnum
80and returns a pointer to the corresponding
81message string.
82The application should not attempt to modify the
83returned string, it may be shared, or read only.
84.Pp
85The
86.Fn strerror_r
87function renders the same result into
88.Fa strerrbuf
89for a maximum of
90.Fa buflen
91characters (including terminator) and returns 0 upon success.
92.Pp
93The
94.Fn strerror_l
95function is like
96.Fn strerror
97but provides in
98.Fa loc
99the locale to be used to obtain the language for the message,
100rather than using the application's current locale.
101.\".Pp
102.\"The
103.\".Fn strerror_lr
104.\"function is to
105.\".Fn strerror_l
106.\"as
107.\".Fn strerror_r
108.\"is to
109.\".Fn strerror .
110.Pp
111The
112.Fn perror
113function finds the error message corresponding to the current
114value of the global variable
115.Va errno
116.Pq Xr intro 2
117and writes it, followed by a newline, to the
118standard error file descriptor.
119If the argument
120.Fa string
121is
122.Pf non- Dv NULL
123and does not point to the nul character,
124this string is prepended to the message
125string and separated from it by
126a colon and space
127.Pq Dq Li ":\ " ;
128otherwise, only the error message string is printed.
129Note that in most cases the
130.Xr err 3
131and
132.Xr warn 3
133family of functions is preferable to
134.Fn perror ;
135they are more flexible and also print the program name.
136.Pp
137If the error number is not recognized, these functions return an error message
138string containing
139.\" , in the appropriate language,
140.Dq Li "Unknown error:\ "
141followed by the error number in decimal.
142To warn about this,
143.Fn strerror
144and
145.Fn strerror_l
146set
147.Dv errno
148to
149.Er EINVAL ,
150and
151.Fn strerror_r
152.\"and
153.\".Fn strerror_lr
154returns
155.Er EINVAL .
156In other cases, except where noted below,
157.Dv errno
158is not altered, so applications should set it to a known value
159(usually zero) before calling either
160.Fn strerror
161or
162.Fn strerror_l
163if the resulting value in
164.Dv errno
165is to be tested for this condition.
166Error numbers recognized by this implementation fall in
167the range 0 <
168.Fa errnum
169<
170.Fa sys_nerr .
171.Pp
172If insufficient storage is provided in
173.Fa strerrbuf
174(as specified in
175.Fa buflen )
176to contain the error string,
177.Fn strerror_r
178.\" and
179.\" .Fn strerror_lr
180returns
181.Er ERANGE
182and
183.Fa strerrbuf
184will contain an error message that has been truncated and
185.Dv NUL
186terminated to fit the length specified by
187.Fa buflen .
188In extraordinary cases, it is possible that the internal
189buffer used by
190.Fn strerror
191and
192.Fn strerror_l
193may be too small for a translated message,
194in which case it will be truncated as indicated for
195.Fn strerror_r
196and
197.Dv errno
198will be set to
199.Er ERANGE .
200.Pp
201The POSIX locale message strings can be accessed directly using the external
202array
203.Va sys_errlist .
204The external value
205.Va sys_nerr
206contains a count of the messages in
207.Va sys_errlist .
208The use of these variables is deprecated;
209one of the
210.Fn strerror
211family of functions should be used instead.
212.Sh COMPATIBILITY
213Programs that attempt to use the deprecated
214.Va sys_errlist
215variable often fail to compile because they provide their own,
216inconsistent, declaration of it.
217Such programs should be updated to use
218.Fn strerror .
219.Sh ERRORS
220These functions may fail if:
221.Bl -tag -width Er
222.It Bq Er EINVAL
223The error number was out of range.
224.It Bq Er ERANGE
225The string buffer supplied was not large enough to hold the complete
226error message.
227.El
228.Sh SEE ALSO
229.Xr intro 2 ,
230.Xr err 3 ,
231.Xr psignal 3 ,
232.Xr warn 3
233.Sh STANDARDS
234The
235.Fn perror
236and
237.Fn strerror
238functions conform to
239.St -isoC-99 .
240The
241.Fn strerror_r
242function conforms to
243.St -p1003.1-2001 .
244The
245.Fn strerror_l
246function conforms to
247.St -p1003.1-2008 .
248.Sh HISTORY
249The
250.Fn perror
251function first appeared in
252.At v4 .
253The
254.Fn strerror
255function first appeared in
256.Bx 4.3 Reno .
257The
258.Fn strerror_r
259function first appeared in
260.Nx 4.0 .
261The
262.Fn strerror_l
263function was first released in
264.Nx 7.0 .
265.\"The
266.\".Fn strerror_lr
267.\"function first appeared in
268.\".Nx 10.0 .
269.Sh BUGS
270The
271.Fn strerror
272function may return its result in a static buffer which
273will be overwritten by subsequent calls.
274For portable use, this must be assumed to be a subsequent
275call from the current, or any other, thread in the process.
276This implementation limits the issue to calls from the
277current thread.
278The
279.Fn strerror_l
280function has a similar restriction, but even in other
281implementations, is required to use thread local storage,
282so only other calls from the calling thread can overwrite
283the result.
284Both
285.Fn strerror
286and
287.Fn strerror_l
288use the same thread local storage; a call to either will destroy
289the result from an earlier call by the same thread of either of them.
290