Add in_domain() to check if the argument is a subdomain

This commit is contained in:
Marco d'Itri 2014-10-15 14:30:35 +02:00
parent 257abceab3
commit 3aa160e6a1
2 changed files with 27 additions and 0 deletions

26
whois.c
View File

@ -1049,6 +1049,32 @@ int domcmp(const char *dom, const char *tld)
return 0;
}
/* check if dom is a subdomain of tld */
int in_domain(const char *dom, const char *tld)
{
size_t dom_len, tld_len;
const char *p = NULL;
if ((dom_len = strlen(dom)) == 0)
return 0;
if ((tld_len = strlen(tld)) == 0)
return 0;
/* dom cannot be shorter than what we are looking for */
/* -1 to ignore dom containing just a dot and tld */
if (tld_len >= dom_len - 1)
return 0;
p = dom + dom_len - tld_len;
/* fail if the character before tld is not a dot */
if (*(p - 1) != '.')
return 0;
return strcaseeq(p, tld);
}
const char *is_new_gtld(const char *s)
{
int i;

View File

@ -29,6 +29,7 @@ unsigned long myinet_aton(const char *);
unsigned long asn32_to_long(const char *);
int isasciidigit(const char);
int domcmp(const char *, const char *);
int in_domain(const char *, const char *);
const char *is_new_gtld(const char *);
int domfind(const char *, const char *[]);
char *normalize_domain(const char *);