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. * Manuel Novoa III Feb 2002
  29. *
  30. * Change to _int10tostr and fix a bug in end-of-buf arg.
  31. */
  32. #define WANT_ERRORLIST 1
  33. #define _STDIO_UTILITY /* For _int10tostr. */
  34. #include <stdio.h>
  35. #include <string.h>
  36. #include <errno.h>
  37. #if WANT_ERRORLIST
  38. static char retbuf[48];
  39. #else
  40. #if __BUFLEN_INT10TOSTR > 12
  41. #error currently set up for 32 bit ints max!
  42. #endif
  43. static char retbuf[33]; /* 33 is sufficient for 32 bit ints */
  44. #endif
  45. static const char unknown_error[] = "Unknown Error: errno"; /* = */
  46. /* Return a string descibing the errno code in ERRNUM.
  47. The storage is good only until the next call to strerror.
  48. Writing to the storage causes undefined behavior. */
  49. char *strerror(int err)
  50. {
  51. char *pos;
  52. #if WANT_ERRORLIST
  53. if ((err >= 0) && (err < sys_nerr)) {
  54. strcpy(retbuf, sys_errlist[err]);
  55. return retbuf;
  56. }
  57. #endif
  58. /* unknown error -- leave space for the '=' */
  59. pos = _int10tostr(retbuf+sizeof(retbuf)-1, err) - sizeof(unknown_error);
  60. strcpy(pos, unknown_error);
  61. *(pos + sizeof(unknown_error) - 1) = '=';
  62. return pos;
  63. }
  64. #ifdef CHECK_BUF
  65. /* quick way to check buffer length */
  66. #include <stdio.h>
  67. #include <stdlib.h>
  68. int main(void)
  69. {
  70. int max = 0;
  71. int j, retcode;
  72. char *p;
  73. #if WANT_ERRORLIST
  74. int i;
  75. #endif
  76. retcode = EXIT_SUCCESS;
  77. #if WANT_ERRORLIST
  78. for ( i=0 ; i < sys_nerr ; i++ ) {
  79. j = strlen(sys_errlist[i])+1;
  80. if (j > max) max = j;
  81. }
  82. #endif
  83. p = strerror(INT_MIN);
  84. j = retbuf+sizeof(retbuf) - p;
  85. if ( > max) {
  86. max = j;
  87. printf("strerror.c - Test of INT_MIN: <%s> %d\n", p, j);
  88. }
  89. if (sizeof(retbuf) != max) {
  90. printf("Error: strerror.c - dimension of retbuf should be = %d\n", max);
  91. retcode = EXIT_FAILURE;
  92. }
  93. printf("Passed.\n");
  94. return retcode;
  95. }
  96. #endif