Improve portability / performance
* `strtonum` is unique to OpenBSD - use `strtol` instead. * Call `strnlen` once. * Avoid int cast.
This commit is contained in:
parent
7e8e312e1b
commit
59108cc615
1 changed files with 17 additions and 6 deletions
|
@ -2,24 +2,34 @@
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <isinetaddr.h>
|
#include <isinetaddr.h>
|
||||||
|
#include <errno.h>
|
||||||
|
|
||||||
|
static
|
||||||
|
int inrange(char buf[4])
|
||||||
|
{
|
||||||
|
char *err;
|
||||||
|
long r;
|
||||||
|
errno = 0;
|
||||||
|
r = strtol(buf, &err, 10);
|
||||||
|
return *err == '\0' && errno == 0 && (r >= 0 && r <= 255);
|
||||||
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
isinetaddr(const char *str)
|
isinetaddr(const char *str)
|
||||||
{
|
{
|
||||||
char buf[4];
|
char buf[4];
|
||||||
const char *err;
|
|
||||||
int i = 0, j = 0, k = 0;
|
int i = 0, j = 0, k = 0;
|
||||||
|
size_t len = strnlen(str, 16);
|
||||||
|
|
||||||
for(int l = 0; l < (int)strnlen(str, 16); l++) {
|
for(size_t l = 0; l < len; l++) {
|
||||||
if (str[l] == '.') {
|
if (str[l] == '.') {
|
||||||
strtonum(buf, 0, 255, &err);
|
if (inrange(buf)) {
|
||||||
if (err != NULL) {
|
|
||||||
return 0;
|
|
||||||
} else {
|
|
||||||
k = 0;
|
k = 0;
|
||||||
j++;
|
j++;
|
||||||
bzero(buf, 3);
|
bzero(buf, 3);
|
||||||
buf[3] = '\0';
|
buf[3] = '\0';
|
||||||
|
} else {
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
} else if (isdigit(str[l])) {
|
} else if (isdigit(str[l])) {
|
||||||
buf[k++] = str[l];
|
buf[k++] = str[l];
|
||||||
|
@ -31,3 +41,4 @@ isinetaddr(const char *str)
|
||||||
}
|
}
|
||||||
return j == 3 && i <= 12 ? 1 : 0;
|
return j == 3 && i <= 12 ? 1 : 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue