herror.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. #define __FORCE_GLIBC
  20. #include <features.h>
  21. #include <stdio.h>
  22. #include <string.h>
  23. #include <netdb.h>
  24. #ifdef L_herror
  25. static const char *error_msg = "Resolver error";
  26. static const char *const h_errlist[] = {
  27. "Error 0",
  28. "Unknown host", /* 1 HOST_NOT_FOUND */
  29. "Host name lookup failure", /* 2 TRY_AGAIN */
  30. "Unknown server error", /* 3 NO_RECOVERY */
  31. "No address associated with name", /* 4 NO_ADDRESS */
  32. };
  33. static const int h_nerr = { sizeof(h_errlist)/sizeof(h_errlist[0]) };
  34. /*
  35. * herror -- print the error indicated by the h_errno value.
  36. */
  37. void herror(const char *s)
  38. {
  39. static const char colon_space[] = ": ";
  40. const char *p;
  41. const char *c;
  42. c = colon_space;
  43. if (!s || !*s) {
  44. c += 2;
  45. }
  46. p = error_msg;
  47. if ((h_errno >= 0) && (h_errno < h_nerr)) {
  48. p = h_errlist[h_errno];
  49. }
  50. fprintf(stderr, "%s%s%s\n", s, c, p);
  51. }
  52. #endif
  53. #ifdef L_hstrerror
  54. const char *hstrerror(int err)
  55. {
  56. if (err < 0) {
  57. return(error_msg);
  58. } else if (err < h_nerr) {
  59. return(h_errlist[err]);
  60. }
  61. return(error_msg);
  62. }
  63. #endif