strerror.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /* Copyright (C) 1991, 1993, 1994 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. The GNU C Library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Library General Public License as
  5. published by the Free Software Foundation; either version 2 of the
  6. License, or (at your option) any later version.
  7. The GNU C Library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Library General Public License for more details.
  11. You should have received a copy of the GNU Library General Public
  12. License along with the GNU C Library; see the file COPYING.LIB. If
  13. not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  14. Cambridge, MA 02139, USA. */
  15. /*
  16. * Manuel Novoa III Dec 2000
  17. *
  18. * Converted to use my new (un)signed long (long) to string routines, which
  19. * are smaller than the previous functions and don't require static buffers.
  20. * Removed dependence on strcat in the process.
  21. *
  22. * Also appended a test routine ( -DCHECK_BUF ) to allow a quick check
  23. * on the buffer length when the sys_errorlist is modified.
  24. *
  25. * Added the option WANT_ERRORLIST for low-memory applications to omit the
  26. * error message strings and only output the error number.
  27. */
  28. #define WANT_ERRORLIST 1
  29. #include <stdio.h>
  30. #include <string.h>
  31. #include <errno.h>
  32. #include <limits.h>
  33. #if (INT_MAX >> 31)
  34. /* We're set up for 32 bit ints */
  35. #error need to check size allocation for static buffer 'retbuf'
  36. #endif
  37. extern char *__ltostr(char *buf, long uval, int base, int uppercase);
  38. #if WANT_ERRORLIST
  39. static char retbuf[48];
  40. #else
  41. static char retbuf[33]; /* 33 is sufficient for 32 bit ints */
  42. #endif
  43. static const char unknown_error[] = "Unknown Error: errno"; /* = */
  44. /* Return a string descibing the errno code in ERRNUM.
  45. The storage is good only until the next call to strerror.
  46. Writing to the storage causes undefined behavior. */
  47. char *strerror(int err)
  48. {
  49. char *pos;
  50. #if WANT_ERRORLIST
  51. if ((err >= 0) && (err < sys_nerr)) {
  52. strcpy(retbuf, sys_errlist[err]);
  53. return retbuf;
  54. }
  55. #endif
  56. /* unknown error */
  57. pos = __ltostr(retbuf + sizeof(retbuf) + 1, err, 10, 0)
  58. - sizeof(unknown_error); /* leave space for the '=' */
  59. strcpy(pos, unknown_error);
  60. *(pos + sizeof(unknown_error) - 1) = '=';
  61. return pos;
  62. }
  63. #ifdef CHECK_BUF
  64. /* quick way to check buffer length */
  65. #include <stdio.h>
  66. #include <stdlib.h>
  67. int main(void)
  68. {
  69. int max = 0;
  70. int j, retcode;
  71. char *p;
  72. #if WANT_ERRORLIST
  73. int i;
  74. #endif
  75. retcode = EXIT_SUCCESS;
  76. #if WANT_ERRORLIST
  77. for ( i=0 ; i < sys_nerr ; i++ ) {
  78. j = strlen(sys_errlist[i])+1;
  79. if (j > max) max = j;
  80. }
  81. #endif
  82. p = strerror(INT_MIN);
  83. j = strlen(p)+1;
  84. if (j > max) max = j;
  85. printf("strerror.c - Test of INT_MIN: <%s> %d\n", p, j);
  86. if (sizeof(retbuf) != max) {
  87. printf("Error: strerror.c - dimension of retbuf should be = %d\n", max);
  88. retcode = EXIT_FAILURE;
  89. }
  90. printf("strerror.c - dimension of retbuf correct at %d\n", max);
  91. return retcode;
  92. }
  93. #endif