home *** CD-ROM | disk | FTP | other *** search
/ Serving the Web / ServingTheWeb1995.disc1of1.iso / linux / slacksrce / d / libc / libc-4.6 / libc-4 / libc-linux / inet / getnetent.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-17  |  2.4 KB  |  119 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. #if defined(LIBC_SCCS) && !defined(lint)
  19. static char sccsid[] = "@(#)getnetent.c    5.5 (Berkeley) 6/27/88";
  20. #endif /* LIBC_SCCS and not lint */
  21.  
  22. #include "inetprivate.h"
  23.  
  24. #define    MAXALIASES    35
  25.  
  26. static char NETDB[] = _PATH_NETWORKS;
  27. static FILE *netf = NULL;
  28. static char line[BUFSIZ+1];
  29. static struct netent net;
  30. static char *net_aliases[MAXALIASES];
  31. static char *any(char *, char *);
  32.  
  33. int _net_stayopen;
  34.  
  35. void
  36. setnetent(f)
  37.     int f;
  38. {
  39.     if (netf == NULL)
  40.         netf = fopen(NETDB, "r" );
  41.     else
  42.         rewind(netf);
  43.     _net_stayopen |= f;
  44. }
  45.  
  46. void
  47. endnetent()
  48. {
  49.     if (netf) {
  50.         fclose(netf);
  51.         netf = NULL;
  52.     }
  53.     _net_stayopen = 0;
  54. }
  55.  
  56. struct netent *
  57. getnetent()
  58. {
  59.     char *p;
  60.     register char *cp, **q;
  61.  
  62.     if (netf == NULL && (netf = fopen(NETDB, "r" )) == NULL)
  63.         return (NULL);
  64. again:
  65.     p = fgets(line, BUFSIZ, netf);
  66.     if (p == NULL)
  67.         return (NULL);
  68.     if (*p == '#')
  69.         goto again;
  70.     cp = any(p, "#\n");
  71.     if (cp == NULL)
  72.         goto again;
  73.     *cp = '\0';
  74.     net.n_name = p;
  75.     cp = any(p, " \t");
  76.     if (cp == NULL)
  77.         goto again;
  78.     *cp++ = '\0';
  79.     while (*cp == ' ' || *cp == '\t')
  80.         cp++;
  81.     p = any(cp, " \t");
  82.     if (p != NULL)
  83.         *p++ = '\0';
  84.     net.n_net = inet_network(cp);
  85.     net.n_addrtype = AF_INET;
  86.     q = net.n_aliases = net_aliases;
  87.     if (p != NULL) 
  88.         cp = p;
  89.     while (cp && *cp) {
  90.         if (*cp == ' ' || *cp == '\t') {
  91.             cp++;
  92.             continue;
  93.         }
  94.         if (q < &net_aliases[MAXALIASES - 1])
  95.             *q++ = cp;
  96.         cp = any(cp, " \t");
  97.         if (cp != NULL)
  98.             *cp++ = '\0';
  99.     }
  100.     *q = NULL;
  101.     return (&net);
  102. }
  103.  
  104. static char *
  105. any(cp, match)
  106.     register char *cp;
  107.     char *match;
  108. {
  109.     register char *mp, c;
  110.  
  111.     while (c = *cp) {
  112.         for (mp = match; *mp; mp++)
  113.             if (*mp == c)
  114.                 return (cp);
  115.         cp++;
  116.     }
  117.     return ((char *)0);
  118. }
  119.