logo

utils-extra

Collection of extra tools for Unixes git clone https://anongit.hacktivis.me/git/utils-extra.git/

gai_strerror.c (1520B)


  1. // utils-extra: Collection of extra tools for Unixes
  2. // SPDX-FileCopyrightText: 2017 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me>
  3. // SPDX-License-Identifier: MPL-2.0
  4. #define _DEFAULT_SOURCE
  5. #include <netdb.h>
  6. #include <stdio.h>
  7. #include <string.h>
  8. struct nameval
  9. {
  10. const char *name;
  11. int val;
  12. };
  13. #define GAI_ERR(ecode) {.name = #ecode, .val = ecode}
  14. static struct nameval ecodes[] = {
  15. GAI_ERR(EAI_AGAIN),
  16. GAI_ERR(EAI_BADFLAGS),
  17. GAI_ERR(EAI_FAIL),
  18. GAI_ERR(EAI_FAMILY),
  19. GAI_ERR(EAI_MEMORY),
  20. #ifdef EAI_NODATA
  21. GAI_ERR(EAI_NODATA),
  22. #endif
  23. GAI_ERR(EAI_NONAME),
  24. GAI_ERR(EAI_OVERFLOW),
  25. GAI_ERR(EAI_SERVICE),
  26. GAI_ERR(EAI_SOCKTYPE),
  27. GAI_ERR(EAI_SYSTEM),
  28. };
  29. static int ecodes_len = sizeof(ecodes) / sizeof(*ecodes);
  30. int
  31. main(int argc, char *argv[])
  32. {
  33. argc -= 1;
  34. argv += 1;
  35. if(argc == 0)
  36. {
  37. for(int i = 0; i < ecodes_len; i++)
  38. printf("%s: %s\n", ecodes[i].name, gai_strerror(ecodes[i].val));
  39. return 0;
  40. }
  41. for(int argi = 0; argi < argc; argi++)
  42. {
  43. if(memcmp("EAI_", argv[argi], 4) != 0)
  44. {
  45. fprintf(stderr,
  46. "gai_strerror: error: ecode name needs to start with EAI_, got '%s'\n",
  47. argv[argi]);
  48. return 1;
  49. }
  50. int ei = 0;
  51. for(; ei < ecodes_len; ei++)
  52. {
  53. if(strcmp(ecodes[ei].name, argv[argi]) == 0)
  54. {
  55. printf("%s: %s\n", ecodes[ei].name, gai_strerror(ecodes[ei].val));
  56. break;
  57. }
  58. }
  59. if(ei == ecodes_len)
  60. {
  61. fprintf(stderr, "gai_strerror: error: Unknown ecode \"%s\"\n", argv[argi]);
  62. return 1;
  63. }
  64. }
  65. }