gai_strerror.c (1520B)
- // utils-extra: Collection of extra tools for Unixes
- // SPDX-FileCopyrightText: 2017 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me>
- // SPDX-License-Identifier: MPL-2.0
- #define _DEFAULT_SOURCE
- #include <netdb.h>
- #include <stdio.h>
- #include <string.h>
- struct nameval
- {
- const char *name;
- int val;
- };
- #define GAI_ERR(ecode) {.name = #ecode, .val = ecode}
- static struct nameval ecodes[] = {
- GAI_ERR(EAI_AGAIN),
- GAI_ERR(EAI_BADFLAGS),
- GAI_ERR(EAI_FAIL),
- GAI_ERR(EAI_FAMILY),
- GAI_ERR(EAI_MEMORY),
- #ifdef EAI_NODATA
- GAI_ERR(EAI_NODATA),
- #endif
- GAI_ERR(EAI_NONAME),
- GAI_ERR(EAI_OVERFLOW),
- GAI_ERR(EAI_SERVICE),
- GAI_ERR(EAI_SOCKTYPE),
- GAI_ERR(EAI_SYSTEM),
- };
- static int ecodes_len = sizeof(ecodes) / sizeof(*ecodes);
- int
- main(int argc, char *argv[])
- {
- argc -= 1;
- argv += 1;
- if(argc == 0)
- {
- for(int i = 0; i < ecodes_len; i++)
- printf("%s: %s\n", ecodes[i].name, gai_strerror(ecodes[i].val));
- return 0;
- }
- for(int argi = 0; argi < argc; argi++)
- {
- if(memcmp("EAI_", argv[argi], 4) != 0)
- {
- fprintf(stderr,
- "gai_strerror: error: ecode name needs to start with EAI_, got '%s'\n",
- argv[argi]);
- return 1;
- }
- int ei = 0;
- for(; ei < ecodes_len; ei++)
- {
- if(strcmp(ecodes[ei].name, argv[argi]) == 0)
- {
- printf("%s: %s\n", ecodes[ei].name, gai_strerror(ecodes[ei].val));
- break;
- }
- }
- if(ei == ecodes_len)
- {
- fprintf(stderr, "gai_strerror: error: Unknown ecode \"%s\"\n", argv[argi]);
- return 1;
- }
- }
- }