/contrib/bind9/lib/isc/unix/strerror.c

https://bitbucket.org/freebsd/freebsd-head/ · C · 74 lines · 41 code · 12 blank · 21 comment · 8 complexity · 0ec8dbdb1a19688836083042023fa484 MD5 · raw file

  1. /*
  2. * Copyright (C) 2004, 2005, 2007, 2009 Internet Systems Consortium, Inc. ("ISC")
  3. * Copyright (C) 2001 Internet Software Consortium.
  4. *
  5. * Permission to use, copy, modify, and/or distribute this software for any
  6. * purpose with or without fee is hereby granted, provided that the above
  7. * copyright notice and this permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  10. * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  11. * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  12. * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  13. * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  14. * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  15. * PERFORMANCE OF THIS SOFTWARE.
  16. */
  17. /* $Id: strerror.c,v 1.10 2009/02/16 23:48:04 tbox Exp $ */
  18. /*! \file */
  19. #include <config.h>
  20. #include <stdio.h>
  21. #include <string.h>
  22. #include <isc/mutex.h>
  23. #include <isc/once.h>
  24. #include <isc/print.h>
  25. #include <isc/strerror.h>
  26. #include <isc/util.h>
  27. #ifdef HAVE_STRERROR
  28. /*%
  29. * We need to do this this way for profiled locks.
  30. */
  31. static isc_mutex_t isc_strerror_lock;
  32. static void init_lock(void) {
  33. RUNTIME_CHECK(isc_mutex_init(&isc_strerror_lock) == ISC_R_SUCCESS);
  34. }
  35. #else
  36. extern const char * const sys_errlist[];
  37. extern const int sys_nerr;
  38. #endif
  39. void
  40. isc__strerror(int num, char *buf, size_t size) {
  41. #ifdef HAVE_STRERROR
  42. char *msg;
  43. unsigned int unum = (unsigned int)num;
  44. static isc_once_t once = ISC_ONCE_INIT;
  45. REQUIRE(buf != NULL);
  46. RUNTIME_CHECK(isc_once_do(&once, init_lock) == ISC_R_SUCCESS);
  47. LOCK(&isc_strerror_lock);
  48. msg = strerror(num);
  49. if (msg != NULL)
  50. snprintf(buf, size, "%s", msg);
  51. else
  52. snprintf(buf, size, "Unknown error: %u", unum);
  53. UNLOCK(&isc_strerror_lock);
  54. #else
  55. unsigned int unum = (unsigned int)num;
  56. REQUIRE(buf != NULL);
  57. if (num >= 0 && num < sys_nerr)
  58. snprintf(buf, size, "%s", sys_errlist[num]);
  59. else
  60. snprintf(buf, size, "Unknown error: %u", unum);
  61. #endif
  62. }