system error messages

You're viewing an older version of this page (#3288). View the current version.

View versions (4)

NAME

<span class="Nm" id="perror">perror</span>, <span class="Nm" id="strerror">strerror</span>, <span class="Nm" id="strerror_r">strerror_r</span>, <span class="Nm" id="sys_errlist">sys_errlist</span>, <span class="Nm" id="sys_nerr">sys_nerr</span>

INTERFACE

#include <stdio.h> void perror ( const char *string ) extern const char * const sys_errlist[]; extern const int sys_nerr;

#include <string.h> char * strerror ( int errnum ) int strerror_r ( int errnum, char *strerrbuf, size_t buflen )

DESCRIPTION

The strerror , strerror_r and perror functions look up the error message string corresponding to an error number. &#92;n &#92;n The strerror function accepts an error number argument &#92;c errnum and returns a pointer to the corresponding message string. &#92;n &#92;n The strerror_r function renders the same result into &#92;c strerrbuf for a maximum of &#92;c buflen characters and returns 0 upon success. &#92;n &#92;n The perror function finds the error message corresponding to the current value of the global variable <span class="Va">errno</span> (<span class="Pq">reference:intro (2)</span>) and writes it, followed by a newline, to the standard error file descriptor. If the argument &#92;c string is non- <span class="Dv">NULL</span> and does not point to the null character, this string is prepended to the message string and separated from it by a colon and space (<span class="Pq">"<span class="Dq"><span class="Li">: </span>;</span>"</span>) otherwise, only the error message string is printed. &#92;n &#92;n If the error number is not recognized, these functions return an error message string containing "<span class="Dq"><span class="Li">Unknown error: </span></span>" followed by the error number in decimal. The strerror and strerror_r functions return <span class="Er">EINVAL</span> as a warning. Error numbers recognized by this implementation fall in the range 0 < &#92;c errnum < &#92;c sys_nerr. &#92;n &#92;n If insufficient storage is provided in &#92;c strerrbuf (as specified in &#92;c buflen) to contain the error string, strerror_r returns <span class="Er">ERANGE</span> and &#92;c strerrbuf will contain an error message that has been truncated and <span class="Dv">NUL</span> terminated to fit the length specified by &#92;c buflen. &#92;n &#92;n The message strings can be accessed directly using the external array <span class="Va">sys_errlist</span>. The external value <span class="Va">sys_nerr</span> contains a count of the messages in <span class="Va">sys_errlist</span>. The use of these variables is deprecated; strerror or strerror_r should be used instead.

SEE ALSO

reference:intro (2) , reference:psignal (3)

STANDARDS

The perror and strerror functions conform to The strerror_r function conforms to

HISTORY

The strerror and perror functions first appeared in 4.4BSD The strerror_r function was implemented in FreeBSD 4.4 by <span class="An">Wes</span> <span class="An">Peters</span> <span class="An"><<span class="Aq">wes@FreeBSD.org</span>>.</span>

BUGS

For unknown error numbers, the strerror function will return its result in a static buffer which may be overwritten by subsequent calls. &#92;n &#92;n The return type for strerror is missing a type-qualifier; it should actually be <span class="Vt">const</span> <span class="Vt">char</span> <span class="Vt">*</span>. &#92;n &#92;n Programs that use the deprecated <span class="Va">sys_errlist</span> variable often fail to compile because they declare it inconsistently.