-
Notifications
You must be signed in to change notification settings - Fork 144
/
Copy pathutil.c
273 lines (231 loc) · 6.15 KB
/
util.c
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/*
*
* @APPLE_LICENSE_HEADER_START@
*
* Copyright (c) 1999-2008 Apple Inc. All Rights Reserved.
*
* This file contains Original Code and/or Modifications of Original Code
* as defined in and that are subject to the Apple Public Source License
* Version 2.0 (the 'License'). You may not use this file except in
* compliance with the License. Please obtain a copy of the License at
* http://www.opensource.apple.com/apsl/ and read it before using this
* file.
*
* The Original Code and all software distributed under the License are
* distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
* EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
* INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
* Please see the License for the specific language governing rights and
* limitations under the License.
*
* @APPLE_LICENSE_HEADER_END@
*
*/
/*
* util.c
*
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <fcntl.h>
#include <assert.h>
#include "util.h"
static char to_lower(int c);
/**********************************************/
static char ip_string_buffer[20];
char *ip_to_string(int ip) {
sprintf(ip_string_buffer, "%d.%d.%d.%d",
(ip & 0xff000000) >> 24, (ip & 0x00ff0000) >> 16,
(ip & 0x0000ff00) >> 8, (ip & 0x000000ff));
return ip_string_buffer;
}
/**********************************************/
static char to_lower(int c)
{
if (c >= 'A' && c <= 'Z')
return ((c - 'A') + 'a');
return c;
}
/**********************************************/
char *str_dup(char *str)
{
char *ret;
ret = (char*)malloc(strlen(str)+1);
strcpy(ret, str);
return ret;
}
/**********************************************/
int str_casecmp(char *str1, char *str2)
{
int ret;
ret = *str1 - *str2;
while (*str1 && *str2 && ((ret = *str1++ - *str2++) == 0))
;
return ret;
}
/**********************************************/
int strn_casecmp(char *str1, char *str2, int l)
{
int ret;
ret = to_lower( (char) *str1) - to_lower((char) *str2);
while (l-- && to_lower(*str1) && to_lower(*str2) && ((ret = to_lower(*str1++) - to_lower(*str2++)) == 0))
;
return ret;
}
/*
grab a full line from input
put into strBuff as 0 terminated
must maintain the exact eol that the
input string has.
return next position.
*/
char* get_line_str( char* strBuff, char *input, int buffSize)
{
char *p = input;
int sawEOLChar = 0;
assert( strBuff != NULL && input != NULL && buffSize > 0);
memset(strBuff, 0, (size_t) buffSize);
while( *p )
{
assert( buffSize > 0 );
if ( *p == '\r' || *p == '\n' )
{ // grab all eol chars
sawEOLChar = 1;
*strBuff = *p;
strBuff++;
buffSize--;
}
else if ( sawEOLChar )
{
// we saw eol char(s) and now this is not one
// that means we're done
break;
}
else
{
// grab all line chars
*strBuff = *p;
strBuff++;
buffSize--;
}
p++;
}
*strBuff = 0;
// we're not going to change the contents
// of input, but the caller may have the right too.
return (char*)p;
}
/**********************************************
subdivide a string along the delimeter
points in delim
return the next start point in stringp
return the current string start;
*/
char *str_sep(char **stringp, char *delim)
{
int j, dl, i, sl;
char *newstring, *ret;
if (*stringp == NULL)
return NULL;
dl = strlen(delim);
sl = strlen(*stringp);
newstring = NULL;
ret = *stringp;
for (i=0; i<sl; i++)
{
for (j=0; j<dl; j++)
{
if ((*stringp)[i] == delim[j])
{
(*stringp)[i] = '\0';
newstring = &((*stringp)[i+1]);
i = sl; j = dl;
}
}
}
*stringp = newstring;
return ret;
}
/**********************************************/
typedef struct t_ip_cache {
struct t_ip_cache *next;
char *name;
int ip;
} t_ip_cache;
static t_ip_cache *gIPcache = NULL;
int check_IP_cache(char *name, int *ip)
{
t_ip_cache *cur = gIPcache;
while (cur) {
if (str_casecmp(name, cur->name) == 0) {
*ip = cur->ip;
return 0;
}
cur = cur->next;
}
return -1;
}
/**********************************************/
int add_to_IP_cache(char *name, int ip)
{
t_ip_cache *cur;
cur = (t_ip_cache*)malloc(sizeof(t_ip_cache));
if (cur == NULL)
return -1;
cur->ip = ip;
cur->name = malloc(strlen(name) + 1);
strcpy(cur->name, name);
cur->next = gIPcache;
gIPcache = cur;
return 0;
}
/**********************************************/
int inet_aton_(char *s, int *retval)
{
int i, l, x, el[4], ret, good = 1;
x = 0, ret = 0;
l = strlen(s);
el[0] = 0;
for (i=0; i<l; i++) {
if (s[i] == '.') {
x++;
if (x > 3) {
good = 0;
break;
}
el[x] = 0;
}
else if (s[i] >= '0' && s[i] <= '9') {
el[x] *= 10;
el[x] += s[i] - '0';
}
else
good = 0;
}
switch (x) {
case 3:
ret = ( ((el[0] << 24) & 0xff000000) |
((el[1] << 16) & 0x00ff0000) |
((el[2] << 8 ) & 0x0000ff00) |
(el[3] & 0x000000ff) );
break;
case 2:
ret = ( ((el[0] << 24) & 0xff000000) |
((el[1] << 16) & 0x00ff0000) |
(el[2] & 0x0000ffff) );
break;
case 1:
ret = (((el[0] << 24) & 0xff000000) |
(el[1] & 0x00ffffff) );
break;
case 0:
ret = el[0];
break;
}
*retval = ret;
return good;
}