Updated to fedora-glibc-20080103T1958
[glibc.git] / inet / ifreq.c
blob55e833bdb37591c113df0eea34b3aa5af9c477b0
1 /* Copyright (C) 1999, 2002, 2003, 2004 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, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #include "ifreq.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;
42 rq_len = RQ_IFS * sizeof (struct ifreq) / 2; /* Doubled in the loop. */
45 ifc.ifc_len = rq_len *= 2;
46 void *newp = realloc (ifc.ifc_buf, ifc.ifc_len);
47 if (newp == NULL || __ioctl (fd, SIOCGIFCONF, &ifc) < 0)
49 free (ifc.ifc_buf);
51 if (fd != sockfd)
52 __close (fd);
53 *num_ifs = 0;
54 *ifreqs = NULL;
55 return;
57 ifc.ifc_buf = newp;
59 while (rq_len < sizeof (struct ifreq) + ifc.ifc_len);
61 if (fd != sockfd)
62 __close (fd);
64 #ifdef _HAVE_SA_LEN
65 struct ifreq *ifr = *ifreqs;
66 nifs = 0;
67 while ((char *) ifr < ifc.ifc_buf + ifc.ifc_len)
69 ++nifs;
70 ifr = __if_nextreq (ifr);
71 if (ifr == NULL)
72 break;
74 #else
75 nifs = ifc.ifc_len / sizeof (struct ifreq);
76 #endif
78 *num_ifs = nifs;
79 *ifreqs = realloc (ifc.ifc_buf, nifs * sizeof (struct ifreq));