A Better Way to Verify Email Domain?

105 views Asked by At

I have a small piece of C code to pull the email domain from a supplied string, then do a host lookup on it. It seems fairly simple, and works. Could this be simplified further, or made better, though? Thanks for viewing.

struct hostent *host;
int at = 0, ei = 0;
char email_host[MAX_HOSTNAME_LEN];
for ( at = 0; at < strlen(argument); at++ )
{
    if (argument[at] == '@' )
    {
        strcpy(&email_host[ei],&argument[at+1]);
        ei++;
    }
}
host = gethostbyname(email_host);
if ( !host  )
    fail = TRUE;
1

There are 1 answers

0
dcaswell On

This can be simplified:

if (argument[at] == '@' )
{
    strcpy(&email_host[ei],&argument[at+1]);
    ei++;
}

to simply:

if (argument[at] == '@' )
{
    strcpy(email_host,&argument[at+1]);
}