Updated: April 19, 2023 |
Convert an error number into an error message (reentrant)
#include <string.h> int strerror_r( int errnum, char *strerrbuf, size_t buflen );
libc
Use the -l c option to qcc to link against this library. This library is usually included automatically.
The strerror_r() function maps the error number contained in errnum to an error message, which it stores in the buffer that strerrbuf points to.
#include <stdio.h> #include <string.h> #include <errno.h> #include <stdlib.h> #define MSG_LEN 100 int main( void ) { FILE *fp; char msg_buff[MSG_LEN]; int error_num; fp = fopen( "file.name", "r" ); if( fp == NULL ) { error_num = strerror_r ( errno, msg_buff, MSG_LEN); switch (error_num) { case 0: printf( "Unable to open file: %s\n", msg_buff); break; case EINVAL: printf ( "strerror_r() failed: invalid error code, %d\n", error_num); break; case ERANGE: printf ( "strerror_r() failed: buffer too small: %d\n", MSG_LEN); break; } } return EXIT_SUCCESS; }
Safety: | |
---|---|
Cancellation point | No |
Interrupt handler | No |
Signal handler | Yes |
Thread | Yes |