home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / bsd_srcs / contrib / usr.x25 / lib / getx25hostent.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-09-03  |  2.8 KB  |  154 lines

  1. /*    getx25hostent.c    2.0    86-9-29    */
  2.  
  3. /*
  4.  * Get next entry from /etc/x25hosts table
  5.  * Adapted from 4.2bsd network support code.
  6.  *
  7.  * Frank Pronk
  8.  * The University of British Columbia
  9.  * Laboratory for Computational Vision
  10.  * Copyright (c)
  11.  */
  12.  
  13. #include <sys/types.h>
  14. #include <sys/socket.h>
  15. #include <sys/stat.h>
  16. #include <netccitt/x25.h>
  17. #include <netdb.h>
  18. #include <ctype.h>
  19.  
  20. /*
  21.  * ccitt version.
  22.  */
  23.  
  24. #define    MAXALIASES    8
  25. #define    MAXADDRSIZE    64
  26. #define LINESIZE    256
  27.  
  28. static char HOSTDB[] = "/etc/x25hosts";
  29. static char line[LINESIZE+1];
  30. static char hostaddr[MAXADDRSIZE];
  31. static struct hostent host;
  32. #ifdef h_addr        /* for 4.3bsd; see <netdb.h> */
  33. static char *host_addrs[2];
  34. #endif
  35. static char *host_aliases[MAXALIASES];
  36. static char *FileStart, *FilePos, *FileEnd;
  37. static short stayopen;
  38. static char *any();
  39. char *malloc ();
  40.  
  41. setx25hostent(f)
  42.     int f;
  43. {
  44.     register int fd = -1;
  45.     struct stat st;
  46.  
  47.     if (FileStart == 0) {
  48.         if ((fd = open (HOSTDB, 0)) < 0)
  49.             return;
  50.         fstat (fd, &st);
  51.         if ((FileStart = malloc (st.st_size)) == 0)
  52.             goto fail;
  53.         if (read (fd, FileStart, st.st_size) != st.st_size)
  54.             goto fail;
  55.         FileEnd = FileStart + st.st_size;
  56.         close (fd);
  57.         stayopen |= f;
  58.     }
  59.     FilePos = FileStart;
  60.     return;
  61. fail:
  62.     if (FileStart) {
  63.         free (FileStart);
  64.         FileStart = 0;
  65.     }
  66.     if (fd >= 0)
  67.         close (fd);
  68. }
  69.  
  70. endx25hostent()
  71. {
  72.     if (FileStart && !stayopen) {
  73.         free (FileStart);
  74.         FileStart = 0;
  75.     }
  76. }
  77.  
  78. struct hostent *
  79. getx25hostent()
  80. {
  81.     register char *p, *cp, **q, *end;
  82.     long iaddr;
  83.  
  84.     if (FileStart == 0) {
  85.         setx25hostent (0);
  86.         if (FileStart == 0)
  87.             return (0);
  88.     }
  89. #ifdef h_addr
  90.     host.h_addr_list = host_addrs;
  91.     host_addrs[0] = hostaddr;
  92. #else
  93.     host.h_addr = hostaddr;
  94. #endif
  95.     p = FilePos;
  96.     end = any (p, FileEnd, "\n");
  97.     for (; end; p = end+1, end = any (end+1, FileEnd, "\n")) {
  98.         if (*p == '#')
  99.             continue;
  100.         bcopy (p, line, (end + 1) - p);
  101.         p = line;
  102.         if (cp = any (p, line+LINESIZE, "\n#"))
  103.             *cp = '\0';
  104.         if ((cp = any (p, line+LINESIZE, " \t")) == 0)
  105.             continue;
  106.         *cp++ = '\0';
  107.  
  108.         if (ccitt_addr(p, (struct sockaddr_x25 *)hostaddr) == 0)
  109.             continue;
  110.         host.h_length = sizeof (struct sockaddr_x25);
  111.         host.h_addrtype = AF_CCITT;
  112.  
  113.         while (*cp == ' ' || *cp == '\t')
  114.             cp++;
  115.         host.h_name = cp;
  116.         q = host.h_aliases = host_aliases;
  117.         cp = any(cp, line+LINESIZE, " \t");
  118.         if (cp != 0) 
  119.             *cp++ = '\0';
  120.         while (cp && *cp) {
  121.             if (*cp == ' ' || *cp == '\t') {
  122.                 cp++;
  123.                 continue;
  124.             }
  125.             if (q < &host_aliases[MAXALIASES - 1])
  126.                 *q++ = cp;
  127.             cp = any(cp, line+LINESIZE, " \t");
  128.             if (cp != 0)
  129.                 *cp++ = '\0';
  130.         }
  131.         *q = 0;
  132.         FilePos = end + 1;
  133.         return (&host);
  134.     }
  135.     return (0);
  136. }
  137.  
  138. static char *
  139. any(cp, limit, match)
  140.     register char *cp;
  141.     char *limit, *match;
  142. {
  143.     register char *mp, c;
  144.  
  145.     while (cp < limit) {
  146.         c = *cp;
  147.         for (mp = match; *mp; mp++)
  148.             if (*mp == c)
  149.                 return (cp);
  150.         cp++;
  151.     }
  152.     return ((char *)0);
  153. }
  154.