diff options
-rw-r--r-- | plugins/utils.c | 84 |
1 files changed, 52 insertions, 32 deletions
diff --git a/plugins/utils.c b/plugins/utils.c index da9ccedd..22020d72 100644 --- a/plugins/utils.c +++ b/plugins/utils.c | |||
@@ -165,6 +165,9 @@ is_dotted_quad (char *address) | |||
165 | int o1, o2, o3, o4; | 165 | int o1, o2, o3, o4; |
166 | char c[1]; | 166 | char c[1]; |
167 | 167 | ||
168 | if (!address) | ||
169 | return FALSE; | ||
170 | |||
168 | if (sscanf (address, "%d.%d.%d.%d%c", &o1, &o2, &o3, &o4, c) != 4) | 171 | if (sscanf (address, "%d.%d.%d.%d%c", &o1, &o2, &o3, &o4, c) != 4) |
169 | return FALSE; | 172 | return FALSE; |
170 | else if (o1 > 255 || o2 > 255 || o3 > 255 || o4 > 255) | 173 | else if (o1 > 255 || o2 > 255 || o3 > 255 || o4 > 255) |
@@ -185,18 +188,18 @@ is_dotted_quad (char *address) | |||
185 | int | 188 | int |
186 | is_hostname (char *s1) | 189 | is_hostname (char *s1) |
187 | { | 190 | { |
188 | if (strlen (s1) > 63) | 191 | if (!s1 || strlen (s1) > 63) { |
192 | return FALSE; | ||
193 | } | ||
194 | if (strcspn (s1, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUWVXYZ0123456789-.") != 0) { | ||
189 | return FALSE; | 195 | return FALSE; |
190 | if (strcspn | 196 | } |
191 | (s1, | 197 | if (strspn (s1, "0123456789-.") == 1) { |
192 | "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUWVXYZ0123456789-.") != | ||
193 | 0) return FALSE; | ||
194 | if (strspn (s1, "0123456789-.") == 1) | ||
195 | return FALSE; | 198 | return FALSE; |
199 | } | ||
196 | while ((s1 = index (s1, '.'))) { | 200 | while ((s1 = index (s1, '.'))) { |
197 | s1++; | 201 | s1++; |
198 | if (strspn (s1, "0123456789-.") == 1) { | 202 | if (strspn (s1, "0123456789-.") == 1) { |
199 | printf ("%s\n", s1); | ||
200 | return FALSE; | 203 | return FALSE; |
201 | } | 204 | } |
202 | } | 205 | } |
@@ -208,33 +211,40 @@ is_numeric (char *number) | |||
208 | { | 211 | { |
209 | char tmp[1]; | 212 | char tmp[1]; |
210 | float x; | 213 | float x; |
211 | if (sscanf (number, "%f%c", &x, tmp) == 1) | 214 | |
212 | return (TRUE); | 215 | if (!number) |
213 | return (FALSE); | 216 | return FALSE; |
217 | else if (sscanf (number, "%f%c", &x, tmp) == 1) | ||
218 | return TRUE; | ||
219 | else | ||
220 | return FALSE; | ||
214 | } | 221 | } |
215 | 222 | ||
216 | int | 223 | int |
217 | is_positive (char *number) | 224 | is_positive (char *number) |
218 | { | 225 | { |
219 | if (is_numeric (number) && atof (number) > 0.0) | 226 | if (is_numeric (number) && atof (number) > 0.0) |
220 | return (TRUE); | 227 | return TRUE; |
221 | return (FALSE); | 228 | else |
229 | return FALSE; | ||
222 | } | 230 | } |
223 | 231 | ||
224 | int | 232 | int |
225 | is_negative (char *number) | 233 | is_negative (char *number) |
226 | { | 234 | { |
227 | if (is_numeric (number) && atof (number) < 0.0) | 235 | if (is_numeric (number) && atof (number) < 0.0) |
228 | return (TRUE); | 236 | return TRUE; |
229 | return (FALSE); | 237 | else |
238 | return FALSE; | ||
230 | } | 239 | } |
231 | 240 | ||
232 | int | 241 | int |
233 | is_nonnegative (char *number) | 242 | is_nonnegative (char *number) |
234 | { | 243 | { |
235 | if (is_numeric (number) && atof (number) >= 0.0) | 244 | if (is_numeric (number) && atof (number) >= 0.0) |
236 | return (TRUE); | 245 | return TRUE; |
237 | return (FALSE); | 246 | else |
247 | return FALSE; | ||
238 | } | 248 | } |
239 | 249 | ||
240 | int | 250 | int |
@@ -242,8 +252,9 @@ is_percentage (char *number) | |||
242 | { | 252 | { |
243 | int x; | 253 | int x; |
244 | if (is_numeric (number) && (x = atof (number)) >= 0 && x <= 100) | 254 | if (is_numeric (number) && (x = atof (number)) >= 0 && x <= 100) |
245 | return (TRUE); | 255 | return TRUE; |
246 | return (FALSE); | 256 | else |
257 | return FALSE; | ||
247 | } | 258 | } |
248 | 259 | ||
249 | int | 260 | int |
@@ -251,37 +262,42 @@ is_integer (char *number) | |||
251 | { | 262 | { |
252 | long int n; | 263 | long int n; |
253 | 264 | ||
254 | if (strspn (number, "-0123456789 ") != strlen (number)) | 265 | if (!number || (strspn (number, "-0123456789 ") != strlen (number))) |
255 | return (FALSE); | 266 | return FALSE; |
256 | 267 | ||
257 | n = strtol (number, NULL, 10); | 268 | n = strtol (number, NULL, 10); |
269 | |||
258 | if (errno != ERANGE && n >= INT_MIN && n <= INT_MAX) | 270 | if (errno != ERANGE && n >= INT_MIN && n <= INT_MAX) |
259 | return (TRUE); | 271 | return TRUE; |
260 | return (FALSE); | 272 | else |
273 | return FALSE; | ||
261 | } | 274 | } |
262 | 275 | ||
263 | int | 276 | int |
264 | is_intpos (char *number) | 277 | is_intpos (char *number) |
265 | { | 278 | { |
266 | if (is_integer (number) && atoi (number) > 0) | 279 | if (is_integer (number) && atoi (number) > 0) |
267 | return (TRUE); | 280 | return TRUE; |
268 | return (FALSE); | 281 | else |
282 | return FALSE; | ||
269 | } | 283 | } |
270 | 284 | ||
271 | int | 285 | int |
272 | is_intneg (char *number) | 286 | is_intneg (char *number) |
273 | { | 287 | { |
274 | if (is_integer (number) && atoi (number) < 0) | 288 | if (is_integer (number) && atoi (number) < 0) |
275 | return (TRUE); | 289 | return TRUE; |
276 | return (FALSE); | 290 | else |
291 | return FALSE; | ||
277 | } | 292 | } |
278 | 293 | ||
279 | int | 294 | int |
280 | is_intnonneg (char *number) | 295 | is_intnonneg (char *number) |
281 | { | 296 | { |
282 | if (is_integer (number) && atoi (number) >= 0) | 297 | if (is_integer (number) && atoi (number) >= 0) |
283 | return (TRUE); | 298 | return TRUE; |
284 | return (FALSE); | 299 | else |
300 | return FALSE; | ||
285 | } | 301 | } |
286 | 302 | ||
287 | int | 303 | int |
@@ -289,16 +305,20 @@ is_intpercent (char *number) | |||
289 | { | 305 | { |
290 | int i; | 306 | int i; |
291 | if (is_integer (number) && (i = atoi (number)) >= 0 && i <= 100) | 307 | if (is_integer (number) && (i = atoi (number)) >= 0 && i <= 100) |
292 | return (TRUE); | 308 | return TRUE; |
293 | return (FALSE); | 309 | else |
310 | return FALSE; | ||
294 | } | 311 | } |
295 | 312 | ||
296 | int | 313 | int |
297 | is_option (char *str) | 314 | is_option (char *str) |
298 | { | 315 | { |
299 | if (strspn (str, "-") == 1 || strspn (str, "-") == 2) | 316 | if (!str) |
317 | return FALSE; | ||
318 | else if (strspn (str, "-") == 1 || strspn (str, "-") == 2) | ||
300 | return TRUE; | 319 | return TRUE; |
301 | return FALSE; | 320 | else |
321 | return FALSE; | ||
302 | } | 322 | } |
303 | 323 | ||
304 | 324 | ||