0

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;
  • You pretty much re-implemented what `strstr()` does, I would use that: http://www.cplusplus.com/reference/cstring/strstr/ – Scotty Bauer Aug 29 '13 at 19:58
  • 4
    You need to look up the MX record, not the regular A record for the domain part. Many domains do not set up a host with the domain's root name, or, if it exists, it may not at all be equipped to handle SMTP. – tripleee Aug 29 '13 at 20:03
  • 1
    @Scotty: you mean `strchr`, right? The OP is looking for the position of a single character. – Jongware Aug 29 '13 at 20:28
  • @Jongware Yeah, that would actually be a better function to use, thanks. – Scotty Bauer Aug 29 '13 at 20:30

1 Answers1

0

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]);
}
dcaswell
  • 3,137
  • 2
  • 26
  • 25