I\'m interested in mathematics and Daoism. Welcome to talk about these subjects with me.
分类: C/C++
2013-01-31 13:37:01
/*
* return value:
* 1: succ
* 0: not
* */
static int is_valid_ip_addr(const char* ip)
{
const char* str = ip;
int i = 0;
int j = 0;
int number = 0;
int number_count = 0;
if(str[0] == '.')
return 0;
while(ip[i] != '\0')
{
if (number_count == 4)
{
return 0;
}
if((ip[i] != '.') && ((ip[i] < '0') || (ip[i] > '9')))// neither is '.' nor is a number
{
return 0;
}
if(ip[i] == '.')
{
number = atoi(str);
if ((number < 0) || (number > 255))
return 0;
//printf("number = %d\n", number);
str = ip + i + 1;
if(str[0] == '\0')//the last char is '.'
{
return 0;
}
++number_count;
}
i++;
}
if(number_count <= 2)
return 0;
if(number_count == 3)
{
number = atoi(str);
if ((number < 0) || (number > 255))
return 0;
printf("number = %d\n", number);
return 1;
}
return 1;
}