Try this:
Code:
itsme@itsme:~/C$ cat domainname.c
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
int main(int argc, char **argv)
{
char *str, *p;
int ctr = 0, alldigits = 1;
if(argc != 2)
{
puts("Usage: domainname <url>");
exit(1);
}
if((str = strstr(argv[1], "://")))
str += 3;
else
str = argv[1];
if((p = strchr(str, '/')))
*p = '\0';
else
p = str + strlen(str);
p--;
while(p > str)
{
if(*p == '.')
{
if(ctr && !alldigits)
{
p++;
break;
}
ctr++;
}
else
{
if(!isdigit(*p))
alldigits = 0;
}
p--;
}
puts(p);
return 0;
}
Code:
itsme@itsme:~/C$ ./domainname www.mail.yahoo.com
yahoo.com
itsme@itsme:~/C$ ./domainname www.mail.yahoo.com/india
yahoo.com
itsme@itsme:~/C$ ./domainname 192.54.89.90/india/myhome.htm
192.54.89.90
It will also work if you leave the protocol part of the url there too:
Code:
itsme@itsme:~/C$ ./domainname http://192.54.89.90/india/myhome.htm
192.54.89.90