xref: /freebsd/lib/libc/string/strerror.3 (revision 2008043f386721d58158e37e0d7e50df8095942d)
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.\"     @(#)strerror.3	8.1 (Berkeley) 6/9/93
33.\"
34.Dd December 17, 2020
35.Dt STRERROR 3
36.Os
37.Sh NAME
38.Nm perror ,
39.Nm strerror ,
40.Nm strerror_l ,
41.Nm strerror_r ,
42.Nm sys_errlist ,
43.Nm sys_nerr
44.Nd system error messages
45.Sh LIBRARY
46.Lb libc
47.Sh SYNOPSIS
48.In stdio.h
49.Ft void
50.Fn perror "const char *string"
51.Vt extern const char * const sys_errlist[] ;
52.Vt extern const int sys_nerr ;
53.In string.h
54.Ft "char *"
55.Fn strerror "int errnum"
56.Ft "char *"
57.Fn strerror_l "int errnum" "locale_t"
58.Ft int
59.Fn strerror_r "int errnum" "char *strerrbuf" "size_t buflen"
60.Sh DESCRIPTION
61The
62.Fn strerror ,
63.Fn strerror_l ,
64.Fn strerror_r ,
65and
66.Fn perror
67functions look up the error message string corresponding to an
68error number.
69.Pp
70The
71.Fn strerror
72function accepts an error number argument
73.Fa errnum
74and returns a pointer to the corresponding message string
75in the current locale.
76.Fn strerror
77is not thread-safe.
78It returns a pointer to an internal static buffer that could be
79overwritten by a
80.Fn strerror
81call from another thread.
82.Pp
83The
84.Fn strerror_l
85function accepts
86.Fa errnum
87error number and
88.Fa locale
89locale handle arguments and returns a pointer to a string
90corresponding to the specified error in the given locale.
91.Fn strerror_l
92is thread-safe, its result can be only overwritten by
93another call to
94.Fn strerror_l
95from the current thread.
96.Pp
97The
98.Fn strerror_r
99function renders the same result into
100.Fa strerrbuf
101for a maximum of
102.Fa buflen
103characters and returns 0 upon success.
104.Pp
105The
106.Fn perror
107function finds the error message corresponding to the current
108value of the global variable
109.Va errno
110.Pq Xr intro 2
111and writes it, followed by a newline, to the
112standard error file descriptor.
113If the argument
114.Fa string
115is
116.Pf non- Dv NULL
117and does not point to the null character,
118this string is prepended to the message
119string and separated from it by
120a colon and space
121.Pq Dq Li ":\ " ;
122otherwise, only the error message string is printed.
123.Pp
124If the error number is not recognized, these functions return an error message
125string containing
126.Dq Li "Unknown error:\ "
127followed by the error number in decimal.
128The
129.Fn strerror
130and
131.Fn strerror_r
132functions return
133.Er EINVAL
134as a warning.
135Error numbers recognized by this implementation fall in
136the range 0 <
137.Fa errnum
138<
139.Fa sys_nerr .
140The number 0 is also recognized, although applications that take advantage of
141this are likely to use unspecified values of
142.Va errno .
143.Pp
144If insufficient storage is provided in
145.Fa strerrbuf
146(as specified in
147.Fa buflen )
148to contain the error string,
149.Fn strerror_r
150returns
151.Er ERANGE
152and
153.Fa strerrbuf
154will contain an error message that has been truncated and
155.Dv NUL
156terminated to fit the length specified by
157.Fa buflen .
158.Pp
159The message strings can be accessed directly using the external
160array
161.Va sys_errlist .
162The external value
163.Va sys_nerr
164contains a count of the messages in
165.Va sys_errlist .
166The use of these variables is deprecated;
167.Fn strerror ,
168.Fn strerror_l ,
169or
170.Fn strerror_r
171should be used instead.
172.Sh EXAMPLES
173The following example shows how to use
174.Fn perror
175to report an error.
176.Bd -literal -offset 2n
177#include <fcntl.h>
178#include <stdio.h>
179#include <stdlib.h>
180
181int
182main(void)
183{
184	int fd;
185
186	if ((fd = open("/nonexistent", O_RDONLY)) == -1) {
187		perror("open()");
188		exit(1);
189	}
190	printf("File descriptor: %d\en", fd);
191	return (0);
192}
193.Ed
194.Pp
195When executed, the program will print an error message along the lines of
196.Ql "open(): No such file or directory" .
197.Sh SEE ALSO
198.Xr intro 2 ,
199.Xr err 3 ,
200.Xr psignal 3
201.Sh STANDARDS
202The
203.Fn perror
204and
205.Fn strerror
206functions conform to
207.St -isoC-99 .
208The
209.Fn strerror_r
210function conforms to
211.St -p1003.1-2001 .
212The
213.Fn strerror_l
214function conforms to
215.St -p1003.1-2008 .
216.Sh HISTORY
217The
218.Fn strerror
219and
220.Fn perror
221functions first appeared in
222.Bx 4.4 .
223The
224.Fn strerror_r
225function was implemented in
226.Fx 4.4
227by
228.An Wes Peters Aq Mt wes@FreeBSD.org .
229The
230.Fn strerror_l
231function was added in
232.Fx 13.0 .
233.Sh BUGS
234The
235.Fn strerror
236function returns its result in a static buffer which
237will be overwritten by subsequent calls.
238.Pp
239Programs that use the deprecated
240.Va sys_errlist
241variable often fail to compile because they declare it
242inconsistently.
243Size of the
244.Va sys_errlist
245object might increase during FreeBSD lifetime,
246breaking some ABI stability guarantees.
247