home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / unix / volume25 / hostcvt / gethostent.c < prev    next >
C/C++ Source or Header  |  1992-01-03  |  3KB  |  132 lines

  1. /*
  2.  * Copyright (c) 1983 Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms are permitted
  6.  * provided that the above copyright notice and this paragraph are
  7.  * duplicated in all such forms and that any documentation,
  8.  * advertising materials, and other materials related to such
  9.  * distribution and use acknowledge that the software was developed
  10.  * by the University of California, Berkeley.  The name of the
  11.  * University may not be used to endorse or promote products derived
  12.  * from this software without specific prior written permission.
  13.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  14.  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  15.  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  16.  */
  17.  
  18. #include <stdio.h>
  19. #include <sys/types.h>
  20. #include <sys/socket.h>
  21. #include <netdb.h>
  22. #include <ctype.h>
  23.  
  24. #ifndef lint
  25. static char *RCSid = "$Id: gethostent.c,v 1.2 90/10/03 22:50:29 rogers Release $";
  26. #endif
  27.  
  28. /*
  29.  * Internet version.
  30.  */
  31. #define    MAXALIASES    35
  32. #define    MAXADDRSIZE    14
  33.  
  34. static FILE *hostf = NULL;
  35. static char line[BUFSIZ+1];
  36. static char hostaddr[MAXADDRSIZE];
  37. static struct hostent host;
  38. static char *host_aliases[MAXALIASES];
  39. static char *host_addrs[] = {
  40.     hostaddr,
  41.     NULL
  42. };
  43.  
  44. /*
  45.  * The following is shared with gethostnamadr.c
  46.  */
  47.  
  48. static char *any();
  49.  
  50. #ifdef NOTUSEDINHOSTCVT
  51.  
  52. mysethostent()
  53. {
  54.     if (hostf != NULL)
  55.         rewind(hostf);
  56. }
  57.  
  58. myendhostent()
  59. {
  60.     if (hostf != NULL) {
  61.         (void)fclose(hostf);
  62.         hostf = NULL;
  63.     }
  64. }
  65. #endif  /* NOTUSEDINHOSTCVT */
  66.  
  67. struct hostent *
  68. mygethostent(fname)
  69. char *fname;
  70. {
  71.     u_long inet_addr();
  72.     char *p;
  73.     register char *cp, **q;
  74.  
  75.     if (hostf == NULL && (hostf = fopen(fname, "r" )) == NULL)
  76.         return (NULL);
  77. again:
  78.     if ((p = fgets(line, BUFSIZ, hostf)) == NULL)
  79.         return (NULL);
  80.     if (*p == '#')
  81.         goto again;
  82.     cp = any(p, "#\n");
  83.     if (cp == NULL)
  84.         goto again;
  85.     *cp = '\0';
  86.     cp = any(p, " \t");
  87.     if (cp == NULL)
  88.         goto again;
  89.     *cp++ = '\0';
  90.     /* THIS STUFF IS INTERNET SPECIFIC */
  91.     host.h_addr_list = host_addrs;
  92.     *((u_long *)host.h_addr) = inet_addr(p);
  93.     host.h_length = sizeof (u_long);
  94.     host.h_addrtype = AF_INET;
  95.     while (*cp == ' ' || *cp == '\t')
  96.         cp++;
  97.     host.h_name = cp;
  98.     q = host.h_aliases = host_aliases;
  99.     cp = any(cp, " \t");
  100.     if (cp != NULL) 
  101.         *cp++ = '\0';
  102.     while (cp && *cp) {
  103.         if (*cp == ' ' || *cp == '\t') {
  104.             cp++;
  105.             continue;
  106.         }
  107.         if (q < &host_aliases[MAXALIASES - 1])
  108.             *q++ = cp;
  109.         cp = any(cp, " \t");
  110.         if (cp != NULL)
  111.             *cp++ = '\0';
  112.     }
  113.     *q = NULL;
  114.     return (&host);
  115. }
  116.  
  117. static char *
  118. any(cp, match)
  119.     register char *cp;
  120.     char *match;
  121. {
  122.     register char *mp, c;
  123.  
  124.     while (c = *cp) {
  125.         for (mp = match; *mp; mp++)
  126.             if (*mp == c)
  127.                 return (cp);
  128.         cp++;
  129.     }
  130.     return ((char *)0);
  131. }
  132.