herror.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /*
  2. * Copyright (c) 1987 Regents of the University of California.
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms are permitted
  6. * provided that: (1) source distributions retain this entire copyright
  7. * notice and comment, and (2) distributions including binaries display
  8. * the following acknowledgement: ``This product includes software
  9. * developed by the University of California, Berkeley and its contributors''
  10. * in the documentation or other materials provided with the distribution
  11. * and in all advertising materials mentioning features or use of this
  12. * software. Neither the name of the University nor the names of its
  13. * contributors may be used to endorse or promote products derived
  14. * from this software without specific prior written permission.
  15. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  16. * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  17. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  18. */
  19. #include <stdio.h>
  20. #include <string.h>
  21. #include <netdb.h>
  22. static const char error_msg[] = "Resolver error";
  23. static const char *const h_errlist[] = {
  24. "Error 0",
  25. "Unknown host", /* 1 HOST_NOT_FOUND */
  26. "Host name lookup failure", /* 2 TRY_AGAIN */
  27. "Unknown server error", /* 3 NO_RECOVERY */
  28. "No address associated with name", /* 4 NO_ADDRESS */
  29. };
  30. static const int h_nerr = { sizeof(h_errlist)/sizeof(h_errlist[0]) };
  31. /*
  32. * herror -- print the error indicated by the h_errno value.
  33. */
  34. void herror(const char *s)
  35. {
  36. static const char colon_space[] = ": ";
  37. const char *p;
  38. const char *c;
  39. c = colon_space;
  40. if (!s || !*s) {
  41. c += 2;
  42. }
  43. p = error_msg;
  44. if ((h_errno >= 0) && (h_errno < h_nerr)) {
  45. p = h_errlist[h_errno];
  46. }
  47. fprintf(stderr, "%s%s%s\n", s, c, p);
  48. }
  49. libc_hidden_def(herror)
  50. const char *hstrerror(int err)
  51. {
  52. if ((unsigned)err < h_nerr)
  53. return(h_errlist[err]);
  54. return error_msg;
  55. }