xref: /netbsd-src/lib/libc/string/strerror.3 (revision 7ec84f335ad5fb4672305723762810836fa080e1)
1*7ec84f33Sdholland.\" $NetBSD: strerror.3,v 1.24 2020/04/04 21:29:54 dholland Exp $
23ecc9e2eSwiz.\"
31cf13731Schristos.\" Copyright (c) 1980, 1991, 1993
41cf13731Schristos.\"	The Regents of the University of California.  All rights reserved.
5473c152aSjtc.\"
6473c152aSjtc.\" This code is derived from software contributed to Berkeley by
7473c152aSjtc.\" the American National Standards Committee X3, on Information
8473c152aSjtc.\" Processing Systems.
9473c152aSjtc.\"
10473c152aSjtc.\" Redistribution and use in source and binary forms, with or without
11473c152aSjtc.\" modification, are permitted provided that the following conditions
12473c152aSjtc.\" are met:
13473c152aSjtc.\" 1. Redistributions of source code must retain the above copyright
14473c152aSjtc.\"    notice, this list of conditions and the following disclaimer.
15473c152aSjtc.\" 2. Redistributions in binary form must reproduce the above copyright
16473c152aSjtc.\"    notice, this list of conditions and the following disclaimer in the
17473c152aSjtc.\"    documentation and/or other materials provided with the distribution.
183ecc9e2eSwiz.\" 3. Neither the name of the University nor the names of its contributors
19473c152aSjtc.\"    may be used to endorse or promote products derived from this software
20473c152aSjtc.\"    without specific prior written permission.
21473c152aSjtc.\"
22473c152aSjtc.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23473c152aSjtc.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24473c152aSjtc.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25473c152aSjtc.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26473c152aSjtc.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27473c152aSjtc.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28473c152aSjtc.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29473c152aSjtc.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30473c152aSjtc.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31473c152aSjtc.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32473c152aSjtc.\" SUCH DAMAGE.
33473c152aSjtc.\"
341cf13731Schristos.\"     @(#)strerror.3	8.1 (Berkeley) 6/9/93
354aee32ddSdholland.Dd April 4, 2020
36473c152aSjtc.Dt STRERROR 3
3738c96853Sgarbled.Os
38473c152aSjtc.Sh NAME
391cf13731Schristos.Nm perror ,
401cf13731Schristos.Nm strerror ,
415388a009Skre.Nm strerror_l ,
425388a009Skre.\" .Nm strerror_lr ,
431cf13731Schristos.Nm strerror_r ,
441cf13731Schristos.Nm sys_errlist ,
451cf13731Schristos.Nm sys_nerr
461cf13731Schristos.Nd system error messages
47312aca53Sperry.Sh LIBRARY
48312aca53Sperry.Lb libc
49473c152aSjtc.Sh SYNOPSIS
501cf13731Schristos.In stdio.h
511cf13731Schristos.Ft void
521cf13731Schristos.Fn perror "const char *string"
531cf13731Schristos.In errno.h
541cf13731Schristos.Vt extern const char * const sys_errlist[] ;
551cf13731Schristos.Vt extern const int sys_nerr ;
56472351e1Swiz.In string.h
571cf13731Schristos.Ft "char *"
58473c152aSjtc.Fn strerror "int errnum"
591cf13731Schristos.Ft int
601cf13731Schristos.Fn strerror_r "int errnum" "char *strerrbuf" "size_t buflen"
615388a009Skre.Ft "char *"
625388a009Skre.Fn strerror_l "int errnum" "locale_t loc"
635388a009Skre.\".Ft int
645388a009Skre.\".Fn strerror_lr "int errnum" "char *strerrbuf" "size_t buflen" "locale_t loc"
65473c152aSjtc.Sh DESCRIPTION
66473c152aSjtcThe
671cf13731Schristos.Fn strerror ,
685388a009Skre.Fn strerror_l ,
695388a009Skre.\".Fn strerror_lr ,
703ecc9e2eSwiz.Fn strerror_r ,
711cf13731Schristosand
721cf13731Schristos.Fn perror
731cf13731Schristosfunctions look up the language-dependent error message
743ecc9e2eSwizstring corresponding to an error number.
75473c152aSjtc.Pp
76473c152aSjtcThe
77473c152aSjtc.Fn strerror
781cf13731Schristosfunction accepts an error number argument
791cf13731Schristos.Fa errnum
801cf13731Schristosand returns a pointer to the corresponding
811cf13731Schristosmessage string.
825388a009SkreThe application should not attempt to modify the
835388a009Skrereturned string, it may be shared, or read only.
841cf13731Schristos.Pp
851cf13731SchristosThe
861cf13731Schristos.Fn strerror_r
871cf13731Schristosfunction renders the same result into
881cf13731Schristos.Fa strerrbuf
891cf13731Schristosfor a maximum of
901cf13731Schristos.Fa buflen
914aee32ddSdhollandcharacters (including terminator) and returns 0 upon success.
921cf13731Schristos.Pp
931cf13731SchristosThe
945388a009Skre.Fn strerror_l
955388a009Skrefunction is like
965388a009Skre.Fn strerror
975388a009Skrebut provides in
985388a009Skre.Fa loc
995388a009Skrethe locale to be used to obtain the language for the message,
1005388a009Skrerather than using the application's current locale.
1015388a009Skre.\".Pp
1025388a009Skre.\"The
1035388a009Skre.\".Fn strerror_lr
1045388a009Skre.\"function is to
1055388a009Skre.\".Fn strerror_l
1065388a009Skre.\"as
1075388a009Skre.\".Fn strerror_r
1085388a009Skre.\"is to
1095388a009Skre.\".Fn strerror .
1105388a009Skre.Pp
1115388a009SkreThe
1121cf13731Schristos.Fn perror
1131cf13731Schristosfunction finds the error message corresponding to the current
1141cf13731Schristosvalue of the global variable
1151cf13731Schristos.Va errno
1161cf13731Schristos.Pq Xr intro 2
1171cf13731Schristosand writes it, followed by a newline, to the
1181cf13731Schristosstandard error file descriptor.
1191cf13731SchristosIf the argument
1201cf13731Schristos.Fa string
1211cf13731Schristosis
1221cf13731Schristos.Pf non- Dv NULL
1231a64d82dSwizand does not point to the nul character,
1241cf13731Schristosthis string is prepended to the message
1251cf13731Schristosstring and separated from it by
1261cf13731Schristosa colon and space
1271cf13731Schristos.Pq Dq Li ":\ " ;
1281cf13731Schristosotherwise, only the error message string is printed.
1296d14a316SdhollandNote that in most cases the
1306d14a316Sdholland.Xr err 3
1316d14a316Sdhollandand
1326d14a316Sdholland.Xr warn 3
1336d14a316Sdhollandfamily of functions is preferable to
1346d14a316Sdholland.Fn perror ;
1356d14a316Sdhollandthey are more flexible and also print the program name.
1361cf13731Schristos.Pp
1375388a009SkreIf the error number is not recognized, these functions return an error message
1381cf13731Schristosstring containing
1395388a009Skre.\" , in the appropriate language,
1401cf13731Schristos.Dq Li "Unknown error:\ "
1411cf13731Schristosfollowed by the error number in decimal.
142bf44a819SkleinkTo warn about this,
1431cf13731Schristos.Fn strerror
1445388a009Skreand
1455388a009Skre.Fn strerror_l
1465388a009Skreset
147bf44a819Skleink.Dv errno
148bf44a819Skleinkto
149bf44a819Skleink.Er EINVAL ,
1501cf13731Schristosand
1511cf13731Schristos.Fn strerror_r
1525388a009Skre.\"and
1535388a009Skre.\".Fn strerror_lr
154bf44a819Skleinkreturns
155bf44a819Skleink.Er EINVAL .
1565388a009SkreIn other cases, except where noted below,
1575388a009Skre.Dv errno
1585388a009Skreis not altered, so applications should set it to a known value
1595388a009Skre(usually zero) before calling either
1605388a009Skre.Fn strerror
1615388a009Skreor
1625388a009Skre.Fn strerror_l
1635388a009Skreif the resulting value in
1645388a009Skre.Dv errno
1655388a009Skreis to be tested for this condition.
1661cf13731SchristosError numbers recognized by this implementation fall in
16701869ca4Swizthe range 0 <
1681cf13731Schristos.Fa errnum
16901869ca4Swiz<
1701cf13731Schristos.Fa sys_nerr .
1711cf13731Schristos.Pp
1721cf13731SchristosIf insufficient storage is provided in
1731cf13731Schristos.Fa strerrbuf
1741cf13731Schristos(as specified in
1751cf13731Schristos.Fa buflen )
1761cf13731Schristosto contain the error string,
1771cf13731Schristos.Fn strerror_r
1785388a009Skre.\" and
1795388a009Skre.\" .Fn strerror_lr
1801cf13731Schristosreturns
1811cf13731Schristos.Er ERANGE
1821cf13731Schristosand
1831cf13731Schristos.Fa strerrbuf
1841cf13731Schristoswill contain an error message that has been truncated and
1851cf13731Schristos.Dv NUL
1861cf13731Schristosterminated to fit the length specified by
1871cf13731Schristos.Fa buflen .
1885388a009SkreIn extraordinary cases, it is possible that the internal
1895388a009Skrebuffer used by
1905388a009Skre.Fn strerror
1915388a009Skreand
1925388a009Skre.Fn strerror_l
1935388a009Skremay be too small for a translated message,
1945388a009Skrein which case it will be truncated as indicated for
1955388a009Skre.Fn strerror_r
1965388a009Skreand
1975388a009Skre.Dv errno
1985388a009Skrewill be set to
1995388a009Skre.Er ERANGE .
2001cf13731Schristos.Pp
2015388a009SkreThe POSIX locale message strings can be accessed directly using the external
2021cf13731Schristosarray
2031cf13731Schristos.Va sys_errlist .
2041cf13731SchristosThe external value
2051cf13731Schristos.Va sys_nerr
2061cf13731Schristoscontains a count of the messages in
2071cf13731Schristos.Va sys_errlist .
2081cf13731SchristosThe use of these variables is deprecated;
2095388a009Skreone of the
2101cf13731Schristos.Fn strerror
2115388a009Skrefamily of functions should be used instead.
212*7ec84f33Sdholland.Sh COMPATIBILITY
213*7ec84f33SdhollandPrograms that attempt to use the deprecated
214*7ec84f33Sdholland.Va sys_errlist
215*7ec84f33Sdhollandvariable often fail to compile because they provide their own,
216*7ec84f33Sdhollandinconsistent, declaration of it.
217*7ec84f33SdhollandSuch programs should be updated to use
218*7ec84f33Sdholland.Fn strerror .
219903a8c16Sdholland.Sh ERRORS
220903a8c16SdhollandThese functions may fail if:
221903a8c16Sdholland.Bl -tag -width Er
222903a8c16Sdholland.It Bq Er EINVAL
223903a8c16SdhollandThe error number was out of range.
224903a8c16Sdholland.It Bq Er ERANGE
225903a8c16SdhollandThe string buffer supplied was not large enough to hold the complete
226903a8c16Sdhollanderror message.
227903a8c16Sdholland.El
2281cf13731Schristos.Sh SEE ALSO
2291cf13731Schristos.Xr intro 2 ,
2306d14a316Sdholland.Xr err 3 ,
2316d14a316Sdholland.Xr psignal 3 ,
2326d14a316Sdholland.Xr warn 3
2331cf13731Schristos.Sh STANDARDS
2341cf13731SchristosThe
2351cf13731Schristos.Fn perror
2361cf13731Schristosand
2371cf13731Schristos.Fn strerror
2381cf13731Schristosfunctions conform to
2391cf13731Schristos.St -isoC-99 .
2401cf13731SchristosThe
2411cf13731Schristos.Fn strerror_r
242473c152aSjtcfunction conforms to
2431cf13731Schristos.St -p1003.1-2001 .
2445388a009SkreThe
2455388a009Skre.Fn strerror_l
2465388a009Skrefunction conforms to
2475388a009Skre.St -p1003.1-2008 .
2481cf13731Schristos.Sh HISTORY
2491cf13731SchristosThe
2501cf13731Schristos.Fn perror
251116f085aSdhollandfunction first appeared in
252116f085aSdholland.At v4 .
253116f085aSdhollandThe
254116f085aSdholland.Fn strerror
255116f085aSdhollandfunction first appeared in
256116f085aSdholland.Bx 4.3 Reno .
2571cf13731SchristosThe
2581cf13731Schristos.Fn strerror_r
2591cf13731Schristosfunction first appeared in
2603ecc9e2eSwiz.Nx 4.0 .
2615388a009SkreThe
2625388a009Skre.Fn strerror_l
2635388a009Skrefunction was first released in
2645388a009Skre.Nx 7.0 .
2655388a009Skre.\"The
2665388a009Skre.\".Fn strerror_lr
2675388a009Skre.\"function first appeared in
2685388a009Skre.\".Nx 10.0 .
2691cf13731Schristos.Sh BUGS
2705388a009SkreThe
2711cf13731Schristos.Fn strerror
2725388a009Skrefunction may return its result in a static buffer which
2735388a009Skrewill be overwritten by subsequent calls.
2745388a009SkreFor portable use, this must be assumed to be a subsequent
2755388a009Skrecall from the current, or any other, thread in the process.
2765388a009SkreThis implementation limits the issue to calls from the
2775388a009Skrecurrent thread.
2785388a009SkreThe
2795388a009Skre.Fn strerror_l
2805388a009Skrefunction has a similar restriction, but even in other
2815388a009Skreimplementations, is required to use thread local storage,
2825388a009Skreso only other calls from the calling thread can overwrite
2835388a009Skrethe result.
2845388a009SkreBoth
2855388a009Skre.Fn strerror
2865388a009Skreand
2875388a009Skre.Fn strerror_l
2884aee32ddSdhollanduse the same thread local storage; a call to either will destroy
2895388a009Skrethe result from an earlier call by the same thread of either of them.
290