commit: d749352609d337968c846bd9056462bebf7331a0
parent ae890c1c83e975c49a4f4194d342a000667b3097
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date: Sat, 11 Oct 2025 00:47:22 +0200
cmd/gai_strerror: new
Diffstat:
A | cmd/gai_strerror.c | 73 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1 file changed, 73 insertions(+), 0 deletions(-)
diff --git a/cmd/gai_strerror.c b/cmd/gai_strerror.c
@@ -0,0 +1,73 @@
+// 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;
+ }
+ }
+}