STRERROR(3) | Library Functions Manual | STRERROR(3) |
void
perror(const char *string);
#include <errno.h>
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);
The strerror() function accepts an error number argument errnum 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 errno (intro(2)) and writes it, followed by a newline, to the standard error file descriptor. If the argument string is non-NULL and does not point to the nul 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. Note that in most cases the err(3) and warn(3) family of functions is preferable to perror(); they are more flexible and also print the program name.
If the error number is not recognized, these functions pass an error message string containing “Unknown error:
” followed by the error number in decimal. To warn about this, strerror() sets errno to EINVAL, and strerror_r() returns EINVAL. 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 ERANGE and strerrbuf will contain an error message that has been truncated and NUL terminated to fit the length specified by buflen.
The message strings can be accessed directly using the external array sys_errlist. The external value sys_nerr contains a count of the messages in sys_errlist. The use of these variables is deprecated; strerror() or strerror_r() should be used instead.
The return type for strerror() is missing a type-qualifier; it should actually be const char *.
Programs that use the deprecated sys_errlist variable often fail to compile because they declare it inconsistently.
October 24, 2010 | NetBSD 6.1 |