strerror.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. * Erik Andersen June 2002
  33. *
  34. * Added strerror_r (per SuSv3 which differs from glibc) and adapted
  35. * strerror to match.
  36. */
  37. #define WANT_ERRORLIST 1
  38. #define _STDIO_UTILITY /* For _int10tostr. */
  39. #include <stdio.h>
  40. #include <string.h>
  41. #include <errno.h>
  42. int strerror_r(int err, char *retbuf, size_t buflen)
  43. {
  44. #if WANT_ERRORLIST
  45. if (err < 0 || err >= sys_nerr || sys_errlist[err] == NULL) {
  46. return -EINVAL;
  47. }
  48. if (retbuf==NULL || buflen < 1) {
  49. return -ERANGE;
  50. }
  51. strncpy(retbuf, sys_errlist[err], buflen);
  52. retbuf[buflen-1] = '\0';
  53. return 0;
  54. #else
  55. char *pos;
  56. static const char unknown_error[] = "Unknown Error: errno"; /* = */
  57. if (err < 0 || err >= sys_nerr || sys_errlist[err] == NULL) {
  58. return -EINVAL;
  59. }
  60. /* unknown error -- leave space for the '=' */
  61. pos = _int10tostr(retbuf+sizeof(retbuf)-1, err) - sizeof(unknown_error);
  62. strcpy(pos, unknown_error);
  63. *(pos + sizeof(unknown_error) - 1) = '=';
  64. return 0;
  65. #endif
  66. }
  67. /* Return a string descibing the errno code in ERRNUM.
  68. The storage is good only until the next call to strerror.
  69. Writing to the storage causes undefined behavior. */
  70. char *strerror(int err)
  71. {
  72. #if WANT_ERRORLIST
  73. static char retbuf[48];
  74. #else
  75. #if __BUFLEN_INT10TOSTR > 12
  76. #error currently set up for 32 bit ints max!
  77. #endif
  78. static char retbuf[33]; /* 33 is sufficient for 32 bit ints */
  79. #endif
  80. if (strerror_r(err, retbuf, sizeof(retbuf)) != 0) {
  81. return NULL;
  82. }
  83. return(retbuf);
  84. }
  85. #ifdef CHECK_BUF
  86. /* quick way to check buffer length */
  87. #include <stdio.h>
  88. #include <stdlib.h>
  89. int main(void)
  90. {
  91. int max = 0;
  92. int j, retcode;
  93. char *p;
  94. #if WANT_ERRORLIST
  95. int i;
  96. #endif
  97. retcode = EXIT_SUCCESS;
  98. #if WANT_ERRORLIST
  99. for ( i=0 ; i < sys_nerr ; i++ ) {
  100. j = strlen(sys_errlist[i])+1;
  101. if (j > max) max = j;
  102. }
  103. #endif
  104. p = strerror(INT_MIN);
  105. j = retbuf+sizeof(retbuf) - p;
  106. if ( > max) {
  107. max = j;
  108. printf("strerror.c - Test of INT_MIN: <%s> %d\n", p, j);
  109. }
  110. if (sizeof(retbuf) != max) {
  111. printf("Error: strerror.c - dimension of retbuf should be = %d\n", max);
  112. retcode = EXIT_FAILURE;
  113. }
  114. printf("Passed.\n");
  115. return retcode;
  116. }
  117. #endif