xref: /freebsd/lib/libc/string/strerror.3 (revision 069ac18495ad8fde2748bc94b0f80a50250bb01d)
1.\" Copyright (c) 1980, 1991, 1993
2.\"	The Regents of the University of California.  All rights reserved.
3.\"
4.\" This code is derived from software contributed to Berkeley by
5.\" the American National Standards Committee X3, on Information
6.\" Processing Systems.
7.\"
8.\" Redistribution and use in source and binary forms, with or without
9.\" modification, are permitted provided that the following conditions
10.\" are met:
11.\" 1. Redistributions of source code must retain the above copyright
12.\"    notice, this list of conditions and the following disclaimer.
13.\" 2. Redistributions in binary form must reproduce the above copyright
14.\"    notice, this list of conditions and the following disclaimer in the
15.\"    documentation and/or other materials provided with the distribution.
16.\" 3. Neither the name of the University nor the names of its contributors
17.\"    may be used to endorse or promote products derived from this software
18.\"    without specific prior written permission.
19.\"
20.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30.\" SUCH DAMAGE.
31.\"
32.Dd December 17, 2020
33.Dt STRERROR 3
34.Os
35.Sh NAME
36.Nm perror ,
37.Nm strerror ,
38.Nm strerror_l ,
39.Nm strerror_r ,
40.Nm sys_errlist ,
41.Nm sys_nerr
42.Nd system error messages
43.Sh LIBRARY
44.Lb libc
45.Sh SYNOPSIS
46.In stdio.h
47.Ft void
48.Fn perror "const char *string"
49.Vt extern const char * const sys_errlist[] ;
50.Vt extern const int sys_nerr ;
51.In string.h
52.Ft "char *"
53.Fn strerror "int errnum"
54.Ft "char *"
55.Fn strerror_l "int errnum" "locale_t"
56.Ft int
57.Fn strerror_r "int errnum" "char *strerrbuf" "size_t buflen"
58.Sh DESCRIPTION
59The
60.Fn strerror ,
61.Fn strerror_l ,
62.Fn strerror_r ,
63and
64.Fn perror
65functions look up the error message string corresponding to an
66error number.
67.Pp
68The
69.Fn strerror
70function accepts an error number argument
71.Fa errnum
72and returns a pointer to the corresponding message string
73in the current locale.
74.Fn strerror
75is not thread-safe.
76It returns a pointer to an internal static buffer that could be
77overwritten by a
78.Fn strerror
79call from another thread.
80.Pp
81The
82.Fn strerror_l
83function accepts
84.Fa errnum
85error number and
86.Fa locale
87locale handle arguments and returns a pointer to a string
88corresponding to the specified error in the given locale.
89.Fn strerror_l
90is thread-safe, its result can be only overwritten by
91another call to
92.Fn strerror_l
93from the current thread.
94.Pp
95The
96.Fn strerror_r
97function renders the same result into
98.Fa strerrbuf
99for a maximum of
100.Fa buflen
101characters and returns 0 upon success.
102.Pp
103The
104.Fn perror
105function finds the error message corresponding to the current
106value of the global variable
107.Va errno
108.Pq Xr intro 2
109and writes it, followed by a newline, to the
110standard error file descriptor.
111If the argument
112.Fa string
113is
114.Pf non- Dv NULL
115and does not point to the null character,
116this string is prepended to the message
117string and separated from it by
118a colon and space
119.Pq Dq Li ":\ " ;
120otherwise, only the error message string is printed.
121.Pp
122If the error number is not recognized, these functions return an error message
123string containing
124.Dq Li "Unknown error:\ "
125followed by the error number in decimal.
126The
127.Fn strerror
128and
129.Fn strerror_r
130functions return
131.Er EINVAL
132as a warning.
133Error numbers recognized by this implementation fall in
134the range 0 <
135.Fa errnum
136<
137.Fa sys_nerr .
138The number 0 is also recognized, although applications that take advantage of
139this are likely to use unspecified values of
140.Va errno .
141.Pp
142If insufficient storage is provided in
143.Fa strerrbuf
144(as specified in
145.Fa buflen )
146to contain the error string,
147.Fn strerror_r
148returns
149.Er ERANGE
150and
151.Fa strerrbuf
152will contain an error message that has been truncated and
153.Dv NUL
154terminated to fit the length specified by
155.Fa buflen .
156.Pp
157The message strings can be accessed directly using the external
158array
159.Va sys_errlist .
160The external value
161.Va sys_nerr
162contains a count of the messages in
163.Va sys_errlist .
164The use of these variables is deprecated;
165.Fn strerror ,
166.Fn strerror_l ,
167or
168.Fn strerror_r
169should be used instead.
170.Sh EXAMPLES
171The following example shows how to use
172.Fn perror
173to report an error.
174.Bd -literal -offset 2n
175#include <fcntl.h>
176#include <stdio.h>
177#include <stdlib.h>
178
179int
180main(void)
181{
182	int fd;
183
184	if ((fd = open("/nonexistent", O_RDONLY)) == -1) {
185		perror("open()");
186		exit(1);
187	}
188	printf("File descriptor: %d\en", fd);
189	return (0);
190}
191.Ed
192.Pp
193When executed, the program will print an error message along the lines of
194.Ql "open(): No such file or directory" .
195.Sh SEE ALSO
196.Xr intro 2 ,
197.Xr err 3 ,
198.Xr psignal 3
199.Sh STANDARDS
200The
201.Fn perror
202and
203.Fn strerror
204functions conform to
205.St -isoC-99 .
206The
207.Fn strerror_r
208function conforms to
209.St -p1003.1-2001 .
210The
211.Fn strerror_l
212function conforms to
213.St -p1003.1-2008 .
214.Sh HISTORY
215The
216.Fn strerror
217and
218.Fn perror
219functions first appeared in
220.Bx 4.4 .
221The
222.Fn strerror_r
223function was implemented in
224.Fx 4.4
225by
226.An Wes Peters Aq Mt wes@FreeBSD.org .
227The
228.Fn strerror_l
229function was added in
230.Fx 13.0 .
231.Sh BUGS
232The
233.Fn strerror
234function returns its result in a static buffer which
235will be overwritten by subsequent calls.
236.Pp
237Programs that use the deprecated
238.Va sys_errlist
239variable often fail to compile because they declare it
240inconsistently.
241Size of the
242.Va sys_errlist
243object might increase during FreeBSD lifetime,
244breaking some ABI stability guarantees.
245