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