riscv: Add support for XTheadBb in string-fz[a,i].h
[glibc.git] / support / support_format_hostent.c
blob147cf7957d2e0b0b2e66e62254f56992f2b583e4
1 /* Convert a struct hostent object to a string.
2 Copyright (C) 2016-2023 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
19 #include <support/format_nss.h>
21 #include <arpa/inet.h>
22 #include <errno.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <support/support.h>
26 #include <support/xmemstream.h>
28 static int
29 address_length (int family)
31 switch (family)
33 case AF_INET:
34 return 4;
35 case AF_INET6:
36 return 16;
38 return -1;
41 char *
42 support_format_hostent (struct hostent *h)
44 if (h == NULL)
46 if (h_errno == NETDB_INTERNAL)
47 return xasprintf ("error: NETDB_INTERNAL (errno %d, %m)\n", errno);
48 else
50 char *value = support_format_herrno (h_errno);
51 char *result = xasprintf ("error: %s\n", value);
52 free (value);
53 return result;
57 struct xmemstream mem;
58 xopen_memstream (&mem);
60 fprintf (mem.out, "name: %s\n", h->h_name);
61 for (char **alias = h->h_aliases; *alias != NULL; ++alias)
62 fprintf (mem.out, "alias: %s\n", *alias);
63 for (unsigned i = 0; h->h_addr_list[i] != NULL; ++i)
65 char buf[128];
66 if (inet_ntop (h->h_addrtype, h->h_addr_list[i],
67 buf, sizeof (buf)) == NULL)
68 fprintf (mem.out, "error: inet_ntop failed: %m\n");
69 else
70 fprintf (mem.out, "address: %s\n", buf);
72 if (h->h_length != address_length (h->h_addrtype))
74 char *family = support_format_address_family (h->h_addrtype);
75 fprintf (mem.out, "error: invalid address length %d for %s\n",
76 h->h_length, family);
77 free (family);
80 xfclose_memstream (&mem);
81 return mem.buffer;