strerror.c 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 ( -DSTRERROR_TEST ) to allow a quick check
  23. * on the buffer length when the sys_errorlist is modified.
  24. */
  25. #include <stdio.h>
  26. #include <string.h>
  27. #include <errno.h>
  28. #include <limits.h>
  29. #if (INT_MAX >> 31)
  30. /* We're set up for 32 bit ints */
  31. #error need to check size allocation for static buffer 'retbuf'
  32. #endif
  33. extern char *__ltostr(char *buf, long uval, int base, int uppercase);
  34. static char retbuf[33]; /* 33 is sufficient for 32 bit ints */
  35. static const char unknown_error[] = "Unknown Error: errno"; /* = */
  36. /* Return a string descibing the errno code in ERRNUM.
  37. The storage is good only until the next call to strerror.
  38. Writing to the storage causes undefined behavior. */
  39. char *strerror(int err)
  40. {
  41. char *pos;
  42. if ((err >= 0) && (err < sys_nerr)) {
  43. strcpy(retbuf, sys_errlist[err]);
  44. return retbuf;
  45. }
  46. /* unknown error */
  47. pos = __ltostr(retbuf + sizeof(retbuf) + 1, err, 10, 0)
  48. - sizeof(unknown_error); /* leave space for the '=' */
  49. strcpy(pos, unknown_error);
  50. *(pos + sizeof(unknown_error) - 1) = '=';
  51. return pos;
  52. }
  53. #if STRERROR_TEST
  54. /* quick way to check for sufficient buffer length */
  55. #include <stdio.h>
  56. #include <stdlib.h>
  57. int main(void)
  58. {
  59. int max = 0;
  60. int i, j;
  61. char *p;
  62. for ( i=0 ; i < sys_nerr ; i++ ) {
  63. j = strlen(sys_errlist[i])+1;
  64. if (j > max) max = j;
  65. }
  66. printf("max len = %i\n", j);
  67. p = strerror(INT_MIN);
  68. printf("<%s> %d\n", p, strlen(p)+1);
  69. printf("current buffer length is %d\n", sizeof(retbuf));
  70. return EXIT_SUCCESS;
  71. }
  72. #endif