errmsg.c 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /*
  2. * errmsg.c - implementation of the elf_errmsg(3) function.
  3. * Copyright (C) 1995 - 1999, 2004 Michael Riepe
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Library General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Library General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Library General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  18. */
  19. #include <private.h>
  20. #ifndef lint
  21. static const char rcsid[] = "@(#) $Id: errmsg.c,v 1.11 2008/05/23 08:15:34 michael Exp $";
  22. #endif /* lint */
  23. #if HAVE_DGETTEXT
  24. # undef HAVE_CATGETS
  25. # include <libintl.h>
  26. #else /* HAVE_DGETTEXT */
  27. # define dgettext(dom, str) str
  28. #endif /* HAVE_DGETTEXT */
  29. #if HAVE_CATGETS
  30. # include <nl_types.h>
  31. static nl_catd _libelf_cat = (nl_catd)0;
  32. #endif /* HAVE_CATGETS */
  33. #if HAVE_DGETTEXT || HAVE_CATGETS
  34. static const char domain[] = "libelf";
  35. #endif /* HAVE_DGETTEXT || HAVE_CATGETS */
  36. #if PIC
  37. static const char *_messages[] = {
  38. #else /* PIC */
  39. static const char *const _messages[] = {
  40. #endif /* PIC */
  41. #define __err__(a,b) b,
  42. #include <errors.h> /* include string tables from errors.h */
  43. #undef __err__
  44. };
  45. const char*
  46. elf_errmsg(int err) {
  47. if (err == 0) {
  48. err = _elf_errno;
  49. if (err == 0) {
  50. return NULL;
  51. }
  52. }
  53. else if (err == -1) {
  54. err = _elf_errno;
  55. }
  56. if (err < 0 || err >= ERROR_NUM || _messages[err] == NULL) {
  57. err = ERROR_UNKNOWN;
  58. }
  59. #if HAVE_CATGETS
  60. if (_libelf_cat == (nl_catd)0) {
  61. _libelf_cat = catopen(domain, 0);
  62. }
  63. if (_libelf_cat != (nl_catd)-1) {
  64. return catgets(_libelf_cat, 1, err + 1, _messages[err]);
  65. }
  66. #endif /* HAVE_CATGETS */
  67. return dgettext(domain, _messages[err]);
  68. }