strerror_r
System error messages
Interface
#include <stdio.h>
void | perror (const char *string) |
externint | 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. The strerror function accepts an error number argumenterrnum
and returns a pointer to the corresponding message string.
The strerror_r function renders the same result into strerrbuf
for a maximum of buflen
characters and returns 0 upon success.
The perror function finds the error message corresponding to the current value of the global variable <span class="Va">errno</span> and writes it, followed by a newline, to the standard error file descriptor. If the argument \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; otherwise, only the error message string is printed.
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 < errnum
< sys_nerr
.
If insufficient storage is provided in strerrbuf
(as specified in buflen
) to contain the error string, strerror_r returns <span class="Er">ERANGE</span> and strerrbuf
will contain an error message that has been truncated and <span class="Dv">NUL</span> terminated to fit the length specified by buflen
.
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.Example 1
#include <stdio.h> int main() { char error_message[100] = {0}; perror(error_message); return 0; }
Output:No error