123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114 |
- #define WANT_ERRORLIST 1
- #include <stdio.h>
- #include <string.h>
- #include <errno.h>
- #include <limits.h>
- #if (INT_MAX >> 31)
- #error need to check size allocation for static buffer 'retbuf'
- #endif
- extern char *__ltostr(char *buf, long uval, int base, int uppercase);
- #if WANT_ERRORLIST
- static char retbuf[48];
- #else
- static char retbuf[33];
- #endif
- static const char unknown_error[] = "Unknown Error: errno";
- char *strerror(int err)
- {
- char *pos;
- #if WANT_ERRORLIST
- if ((err >= 0) && (err < sys_nerr)) {
- strcpy(retbuf, sys_errlist[err]);
- return retbuf;
- }
- #endif
-
- pos = __ltostr(retbuf + sizeof(retbuf) + 1, err, 10, 0)
- - sizeof(unknown_error);
- strcpy(pos, unknown_error);
- *(pos + sizeof(unknown_error) - 1) = '=';
- return pos;
- }
- #ifdef CHECK_BUF
- #include <stdio.h>
- #include <stdlib.h>
- int main(void)
- {
- int max = 0;
- int j, retcode;
- char *p;
- #if WANT_ERRORLIST
- int i;
- #endif
- retcode = EXIT_SUCCESS;
- #if WANT_ERRORLIST
- for ( i=0 ; i < sys_nerr ; i++ ) {
- j = strlen(sys_errlist[i])+1;
- if (j > max) max = j;
- }
- #endif
- p = strerror(INT_MIN);
- j = strlen(p)+1;
- if (j > max) {
- max = j;
- printf("strerror.c - Test of INT_MIN: <%s> %d\n", p, j);
- }
- if (sizeof(retbuf) != max) {
- printf("Error: strerror.c - dimension of retbuf should be = %d\n", max);
- retcode = EXIT_FAILURE;
- }
- printf("Passed.\n");
- return retcode;
- }
- #endif
|