www.sas.com > Service and Support > Technical Support
 
Technical Support SAS - The power to know(tm)
  TS Home | Intro to Services | News and Info | Contact TS | Site Map | FAQ | Feedback


/*---------------------------------------------------------------------+
|                Copyright (c) 1995, SAS Institute Inc.                |
|                  Unpublished - All Rights Reserved                   |
|                      S A S / C   S A M P L E                         |
|                                                                      |
|         NAME: GETHENT                                                |
|     LANGUAGE: C                                                      |
|      PURPOSE: This program demonstrates the socket calls:            |
|               gethostent(), endhostent(), and sethostent().          |
|               Additional details on these socket functions may be    |
|               found in the SAS/C Library Reference Vol 2, Third      |
|               Edition, Release 6.00.                                 |
|        NOTES: The local host must be configured to use hosts tables  |
|               for name resolution, prefix.ETC.HOSTS. Where "prefix"  |
|               is defined in the SAS/C Library Reference Vol 2,       |
|               Third Edition, Release 6.00, Network Administration    |
|               chapter.                                               |
|   MVS -                                                              |
|      COMPILE, LINK, EXECUTE: SUBMIT prefix.SAMPLE.AUX(LC370CLG)      |
|        NOTES: "prefix" is the SAS/C installation defined high-level  |
|               qualifier.                                             |
|   TSO -                                                              |
|      COMPILE: LC370 CLIST                                            |
|         LINK: CLK370 CLIST                                           |
|      EXECUTE: CALL 'your.load.lib(GETHENT)'                          |
|   CMS -                                                              |
|      COMPILE: LC370                                                  |
|         LINK: CLINK GETHENT (GENMOD                                  |
|      EXECUTE: GETHENT                                                |
+---------------------------------------------------------------------*/
#include <sys/types.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <stdio.h>

#define TRUE 1

static void prthost(struct hostent *h);

main(int argc, char *argv[])
{
struct hostent *h;

/*---------------------------------------------------------------------+
| sethostent() opens the prefix.ETC.HOSTS file, or when using a        |
| nameserver, opens a TCP connection to the nameserver.                |
+---------------------------------------------------------------------*/
sethostent(TRUE);

/*---------------------------------------------------------------------+
| gethostent() reads the next sequential entry in the prefix.ETC.HOSTS |
| file. It returns a pointer to a "hostent" structure.                 |
+---------------------------------------------------------------------*/
while (h=gethostent())
   prthost(h);

/*---------------------------------------------------------------------+
| endhostent() closes the prefix.ETC.HOSTS file, or the connection to  |
| the nameserver.                                                      |
+---------------------------------------------------------------------*/
endhostent();
exit(EXIT_SUCCESS);
}


/*---------------------------------------------------------------------+
| prthost() prints the information returned by gethostent() from the   |
| hostent structure.                                                   |
+---------------------------------------------------------------------*/
static void prthost(struct hostent *h)
{
   char **p;

   /* Print primary name and aliases. */
   printf("\nname: %s\n",h->h_name);
   for (p=h->h_aliases; *p; p++)
      printf("alternate name: %s\n",*p);

   /* Handle unexpected situations gracefully. */
   if (h->h_addrtype != AF_INET)
   {
      printf("Not an internet address.\n");
      return;
   }
   if (h->h_length != sizeof(struct in_addr))
   {
      printf("Invalid length: %d.\n",h->h_length);
      return;
   }

   /* Print the primary address and any alternates.  */
   for (p=h->h_addr_list; *p; p++)
   {
      printf("%s address: %s\n",
             p==h->h_addr_list ? "primary " : "alternate ",
             inet_ntoa((*(struct in_addr *)*p)) );
   }
}

Copyright (c) 2000 SAS Institute Inc. All Rights Reserved.
Terms of Use & Legal Information | Privacy Statement