00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #include <stdio.h>
00031 #include <errno.h>
00032 #include <unistd.h>
00033 #include <netdb.h>
00034
00035
00036
00037
00038 static char* id="$Id$";
00039 static char* version="gethostbyname 0.1";
00040 static char* help_msg="\
00041 Usage: gethostbyname [-hV] -n host\n\
00042 Options:\n\
00043 -n host host name\n\
00044 -V version number\n\
00045 -h this help message\n\
00046 ";
00047
00048
00049 int main(int argc, char** argv)
00050 {
00051 char c;
00052 char* name;
00053 struct hostent* he;
00054 unsigned char** h;
00055
00056 name=0;
00057
00058 opterr=0;
00059 while ((c=getopt(argc, argv, "n:hV"))!=-1){
00060 switch(c){
00061 case 'n':
00062 name=optarg;
00063 break;
00064 case 'V':
00065 printf("version: %s\n", version);
00066 printf("%s\n", id);
00067 exit(0);
00068 break;
00069 case 'h':
00070 printf("version: %s\n", version);
00071 printf("%s", help_msg);
00072 exit(0);
00073 break;
00074 case '?':
00075 if (isprint(optopt))
00076 fprintf(stderr, "Unknown option `-%c´\n", optopt);
00077 else
00078 fprintf(stderr, "Unknown character `\\x%x´\n", optopt);
00079 goto error;
00080 case ':':
00081 fprintf(stderr, "Option `-%c´ requires an argument.\n",
00082 optopt);
00083 goto error;
00084 break;
00085 default:
00086 abort();
00087 }
00088 }
00089
00090 if (name==0){
00091 fprintf(stderr, "Missing domain name (-n name)\n");
00092 goto error;
00093 }
00094
00095 he=gethostbyname(name);
00096 if (he==0) printf("no answer\n");
00097 else{
00098 printf("h_name=%s\n", he->h_name);
00099 for(h=he->h_aliases;*h;h++)
00100 printf(" alias=%s\n", *h);
00101 for(h=he->h_addr_list;*h;h++)
00102 printf(" ip=%d.%d.%d.%d\n", (*h)[0],(*h)[1],(*h)[2],(*h)[3] );
00103 }
00104 printf("done\n");
00105 exit(0);
00106 error:
00107 exit(-1);
00108 }