1.\" $NetBSD: strerror.3,v 1.21 2020/03/25 18:50:47 kre 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 March 24, 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 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 SEE ALSO 213.Xr intro 2 , 214.Xr err 3 , 215.Xr psignal 3 , 216.Xr warn 3 217.Sh STANDARDS 218The 219.Fn perror 220and 221.Fn strerror 222functions conform to 223.St -isoC-99 . 224The 225.Fn strerror_r 226function conforms to 227.St -p1003.1-2001 . 228The 229.Fn strerror_l 230function conforms to 231.St -p1003.1-2008 . 232.Sh HISTORY 233The 234.Fn perror 235function first appeared in 236.At v4 . 237The 238.Fn strerror 239function first appeared in 240.Bx 4.3 Reno . 241The 242.Fn strerror_r 243function first appeared in 244.Nx 4.0 . 245The 246.Fn strerror_l 247function was first released in 248.Nx 7.0 . 249.\"The 250.\".Fn strerror_lr 251.\"function first appeared in 252.\".Nx 10.0 . 253.Sh BUGS 254The 255.Fn strerror 256function may return its result in a static buffer which 257will be overwritten by subsequent calls. 258For portable use, this must be assumed to be a subsequent 259call from the current, or any other, thread in the process. 260This implementation limits the issue to calls from the 261current thread. 262The 263.Fn strerror_l 264function has a similar restriction, but even in other 265implementations, is required to use thread local storage, 266so only other calls from the calling thread can overwrite 267the result. 268Both 269.Fn strerror 270and 271.Fn strerror_l 272use the same thread local storage, a call to either will destroy 273the result from an earlier call by the same thread of either of them. 274.Pp 275Programs that use the deprecated 276.Va sys_errlist 277variable often fail to compile because they declare it 278inconsistently. 279