1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
|
--- libatalk/util/getiface.c.orig Thu Sep 13 00:28:21 2001
+++ libatalk/util/getiface.c Thu Sep 13 00:28:30 2001
@@ -43,13 +43,6 @@
{
/* if we've run out of room, allocate some more. just return
* the present list if we can't. */
- if (*i >= *length) {
- char **new = realloc(list, sizeof(char **)*(*length + IFACE_NUM));
-
- if (!new) /* just break if we can't allocate anything */
- return -1;
- *length += IFACE_NUM;
- }
if ((list[*i] = strdup(name)) == NULL)
return -1;
@@ -60,30 +53,32 @@
}
-static int getifaces(const int sockfd, char **list, int *length)
+static int getifaces(const int sockfd, char ***list, int *length)
{
#ifdef HAVE_IFNAMEINDEX
struct if_nameindex *ifstart, *ifs;
int i = 0;
+ char **new;
- if (!list || *length < 1)
- return 0;
-
ifs = ifstart = if_nameindex();
+
+ new = (char **) malloc((sizeof(ifs)/sizeof(struct if_nameindex) + 1) * sizeof(char *));
while (ifs && ifs->if_name) {
/* just bail if there's a problem */
- if (addname(list, &i, length, ifs->if_name) < 0)
+ if (addname(new, &i, length, ifs->if_name) < 0)
break;
ifs++;
}
if_freenameindex(ifstart);
+ *list = new;
return i;
#else
struct ifconf ifc;
struct ifreq ifrs[ 64 ], *ifr, *nextifr;
int ifrsize, i = 0;
+ char **new;
if (!list || *length < 1)
return 0;
@@ -96,6 +91,7 @@
return 0;
}
+ new = (char **) malloc((ifc.ifc_len/sizeof(struct ifreq) + 1) * sizeof(char *));
for ( ifr = ifc.ifc_req; ifc.ifc_len >= sizeof( struct ifreq );
ifc.ifc_len -= ifrsize, ifr = nextifr ) {
#ifdef BSD4_4
@@ -108,9 +104,10 @@
nextifr = (struct ifreq *)((caddr_t)ifr + ifrsize );
/* just bail if there's a problem */
- if (addname(list, &i, length, ifr->ifr_name) < 0)
+ if (addname(new, &i, length, ifr->ifr_name) < 0)
break;
}
+ *list = new;
return i;
#endif
}
@@ -122,17 +119,14 @@
*/
char **getifacelist()
{
- char **list = (char **) malloc(sizeof(char **)*(IFACE_NUM + 1));
+ char **list;
char **new;
- int length = IFACE_NUM, i, fd;
+ int length, i, fd;
- if (!list)
- return NULL;
-
if ((fd = socket(PF_INET, SOCK_STREAM, 0)) < 0)
return NULL;
- if ((i = getifaces(fd, list, &length)) == 0) {
+ if ((i = getifaces(fd, &list, &length)) == 0) {
free(list);
close(fd);
return NULL;
@@ -140,7 +134,7 @@
close(fd);
if ((i < length) &&
- (new = (char **) realloc(list, sizeof(char **)*(i + 1))))
+ (new = (char **) realloc(list, (i + 1) * sizeof(char *))))
return new;
return list;
|