Git-Url: http://git.frugalware.org/gitweb/gitweb.cgi?p=fvbe.git;a=commitdiff;h=745a8350c996b988839f2e8c0dca4181372bc8fc
commit 745a8350c996b988839f2e8c0dca4181372bc8fc Author: James Buren <[email protected]> Date: Mon Apr 8 07:50:01 2013 -0500 add function which validates if a string contains an IPv4 address diff --git a/src/local.h b/src/local.h index da52931..0c38dcc 100644 --- a/src/local.h +++ b/src/local.h @@ -127,6 +127,7 @@ struct tool }; extern int charpp_qsort(const void *A,const void *B); +extern bool isipv4(const char *ip,char **end); extern bool ishostname(const char *name); extern void account_free(struct account *account); extern int get_number_padding(int n); diff --git a/src/utility.c b/src/utility.c index f06c2e6..6ba6579 100644 --- a/src/utility.c +++ b/src/utility.c @@ -33,6 +33,31 @@ extern int charpp_qsort(const void *A,const void *B) return strcmp(a,b); } +extern bool isipv4(const char *ip,char **end) +{ + int x[4] = {0}; + int n = 0; + + if(ip == 0) + { + errno = EINVAL; + error(strerror(errno)); + return false; + } + + if(sscanf(ip,"%d.%d.%d.%d%n",&x[0],&x[1],&x[2],&x[3],&n) != 4) + return false; + + for( int i = 0 ; i < 4 ; ++i ) + if(x[i] < 0 || x[i] > UCHAR_MAX) + return false; + + if(end != 0) + *end = (char *) ip + n; + + return true; +} + extern bool ishostname(const char *name) { size_t i = 0; _______________________________________________ Frugalware-git mailing list [email protected] http://frugalware.org/mailman/listinfo/frugalware-git
