1 /* Convert a struct netent object to a string.
2 Copyright (C) 2016-2022 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
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.
9
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.
14
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/>. */
18
19 #include <support/format_nss.h>
20
21 #include <arpa/inet.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <support/support.h>
25 #include <support/xmemstream.h>
26
27 char *
support_format_netent(struct netent * e)28 support_format_netent (struct netent *e)
29 {
30 if (e == NULL)
31 {
32 char *value = support_format_herrno (h_errno);
33 char *result = xasprintf ("error: %s\n", value);
34 free (value);
35 return result;
36 }
37
38 struct xmemstream mem;
39 xopen_memstream (&mem);
40
41 if (e->n_name != NULL)
42 fprintf (mem.out, "name: %s\n", e->n_name);
43 for (char **ap = e->n_aliases; *ap != NULL; ++ap)
44 fprintf (mem.out, "alias: %s\n", *ap);
45 if (e->n_addrtype != AF_INET)
46 fprintf (mem.out, "addrtype: %d\n", e->n_addrtype);
47 /* On alpha, e->n_net is an unsigned long. */
48 unsigned int n_net = e->n_net;
49 fprintf (mem.out, "net: 0x%08x\n", n_net);
50
51 xfclose_memstream (&mem);
52 return mem.buffer;
53 }
54