Update copyright notices with scripts/update-copyrights
[glibc.git] / sysdeps / unix / sysv / linux / ifreq.c
blob58f51913a8374d7754af48683dea840b37959692
1 /* Copyright (C) 1999-2014 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Andreas Jaeger <aj@suse.de>.
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 <http://www.gnu.org/licenses/>. */
19 #include "ifreq.h"
20 #include <kernel-features.h>
23 void
24 __ifreq (struct ifreq **ifreqs, int *num_ifs, int sockfd)
26 int fd = sockfd;
27 struct ifconf ifc;
28 int rq_len;
29 int nifs;
30 # define RQ_IFS 4
32 if (fd < 0)
33 fd = __opensock ();
34 if (fd < 0)
36 *num_ifs = 0;
37 *ifreqs = NULL;
38 return;
41 ifc.ifc_buf = NULL;
43 /* We may be able to get the needed buffer size directly, rather than
44 guessing. */
45 ifc.ifc_buf = NULL;
46 ifc.ifc_len = 0;
47 if (__ioctl (fd, SIOCGIFCONF, &ifc) < 0 || ifc.ifc_len == 0)
48 rq_len = RQ_IFS * sizeof (struct ifreq);
49 else
50 rq_len = ifc.ifc_len;
52 /* Read all the interfaces out of the kernel. */
53 ifc.ifc_len = rq_len;
54 void *newp = realloc (ifc.ifc_buf, ifc.ifc_len);
55 if (newp == NULL
56 || (ifc.ifc_buf = newp, __ioctl (fd, SIOCGIFCONF, &ifc)) < 0)
58 free (ifc.ifc_buf);
60 if (fd != sockfd)
61 __close (fd);
63 *num_ifs = 0;
64 *ifreqs = NULL;
65 return;
68 nifs = ifc.ifc_len / sizeof (struct ifreq);
70 if (fd != sockfd)
71 __close (fd);
73 *num_ifs = nifs;
74 *ifreqs = realloc (ifc.ifc_buf, nifs * sizeof (struct ifreq));