1#include "loc.h" 2 3/* $Id: query-loc.c,v 1.1 2008/02/15 01:47:15 marka Exp $ */ 4 5/* Global variables */ 6char *progname; 7short debug; 8 9int 10main (argc, argv) 11 int argc; 12 char *argv[]; 13{ 14 extern char *optarg; 15 extern int optind; 16 17 short verbose = FALSE; 18 char *host; 19 20 char ch; 21 22 char *loc = NULL; 23 struct in_addr addr; 24 struct hostent *hp; 25 26 progname = argv[0]; 27 while ((ch = getopt (argc, argv, "vd:")) != EOF) 28 { 29 switch (ch) 30 { 31 case 'v': 32 verbose = TRUE; 33 break; 34 case 'd': 35 debug = atoi (optarg); 36 if (debug <= 0) 37 { 38 (void) fprintf (stderr, 39 "%s: illegal debug value.\n", progname); 40 exit (2); 41 } 42 break; 43 default: 44 usage (); 45 } 46 } 47 argc -= optind; 48 argv += optind; 49 if (argc != 1) 50 { 51 usage (); 52 } 53 if (verbose || debug) 54 { 55 printf ("\nThis is %s, version %s.\n\n", progname, VERSION); 56 } 57 host = argv[0]; 58 (void) res_init (); 59 60 if ((addr.s_addr = inet_addr (host)) == INADDR_NONE) 61 { 62 if (debug >= 1) 63 printf ("%s is a name\n", host); 64 loc = getlocbyname (host, FALSE); 65 } 66 else 67 { 68 if (debug >= 1) 69 printf ("%s is an IP address ", host); 70 hp = (struct hostent *) gethostbyaddr 71 ((char *) &addr, sizeof (addr), AF_INET); 72 if (hp) 73 { 74 if (debug >= 1) 75 printf ("and %s is its official name\n", 76 hp->h_name); 77 loc = getlocbyname (hp->h_name, FALSE); 78 } 79 else 80 { 81 if (debug >= 1) 82 printf ("which has no name\n"); 83 loc = getlocbyaddr (addr, NULL); 84 } 85 } 86 if (loc == NULL) 87 { 88 printf ("No LOCation found for %s\n", host); 89 exit (1); 90 } 91 else 92 { 93 if (verbose || debug) 94 printf ("LOCation for %s is ", host); 95 printf ("%s\n", loc); 96 exit (0); 97 } 98} 99