Home | History | Annotate | Line # | Download | only in net
getnetent.c revision 1.1.1.6
      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, with or without
      6  * modification, are permitted provided that the following conditions
      7  * are met:
      8  * 1. Redistributions of source code must retain the above copyright
      9  *    notice, this list of conditions and the following disclaimer.
     10  * 2. Redistributions in binary form must reproduce the above copyright
     11  *    notice, this list of conditions and the following disclaimer in the
     12  *    documentation and/or other materials provided with the distribution.
     13  * 3. All advertising materials mentioning features or use of this software
     14  *    must display the following acknowledgement:
     15  *	This product includes software developed by the University of
     16  *	California, Berkeley and its contributors.
     17  * 4. Neither the name of the University nor the names of its contributors
     18  *    may be used to endorse or promote products derived from this software
     19  *    without specific prior written permission.
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31  * SUCH DAMAGE.
     32  */
     33 
     34 /* Portions Copyright (c) 1993 Carlos Leandro and Rui Salgueiro
     35  *	Dep. Matematica Universidade de Coimbra, Portugal, Europe
     36  *
     37  * Permission to use, copy, modify, and distribute this software for any
     38  * purpose with or without fee is hereby granted, provided that the above
     39  * copyright notice and this permission notice appear in all copies.
     40  *
     41  * from getnetent.c	1.1 (Coimbra) 93/06/02
     42  */
     43 
     44 #if defined(LIBC_SCCS) && !defined(lint)
     45 static char sccsid[] = "@(#)getnetent.c	8.1 (Berkeley) 6/4/93";
     46 static char rcsid[] = "Id: getnetent.c,v 8.4 1997/06/01 20:34:37 vixie Exp ";
     47 #endif /* LIBC_SCCS and not lint */
     48 
     49 #include <sys/types.h>
     50 #include <sys/param.h>
     51 #include <sys/socket.h>
     52 #include <netinet/in.h>
     53 #include <arpa/inet.h>
     54 #include <arpa/nameser.h>
     55 
     56 #include <stdio.h>
     57 #include <resolv.h>
     58 #include <netdb.h>
     59 #include <string.h>
     60 
     61 #ifndef _PATH_NETWORKS
     62 #define _PATH_NETWORKS  "/etc/networks"
     63 #endif
     64 
     65 #define	MAXALIASES	35
     66 
     67 static FILE *netf;
     68 static char line[BUFSIZ+1];
     69 static struct netent net;
     70 static char *net_aliases[MAXALIASES];
     71 int _net_stayopen;
     72 
     73 void _setnetent __P((int));
     74 void _endnetent __P((void));
     75 
     76 void
     77 setnetent(stayopen)
     78 	int stayopen;
     79 {
     80 
     81 	sethostent(stayopen);
     82 	_setnetent(stayopen);
     83 }
     84 
     85 void
     86 endnetent()
     87 {
     88 
     89 	endhostent();
     90 	_endnetent();
     91 }
     92 
     93 void
     94 _setnetent(f)
     95 	int f;
     96 {
     97 
     98 	if (netf == NULL)
     99 		netf = fopen(_PATH_NETWORKS, "r" );
    100 	else
    101 		rewind(netf);
    102 	_net_stayopen |= f;
    103 }
    104 
    105 void
    106 _endnetent()
    107 {
    108 
    109 	if (netf) {
    110 		fclose(netf);
    111 		netf = NULL;
    112 	}
    113 	_net_stayopen = 0;
    114 }
    115 
    116 struct netent *
    117 getnetent()
    118 {
    119 	char *p;
    120 	register char *cp, **q;
    121 
    122 	if (netf == NULL && (netf = fopen(_PATH_NETWORKS, "r" )) == NULL)
    123 		return (NULL);
    124 again:
    125 	p = fgets(line, sizeof line, netf);
    126 	if (p == NULL)
    127 		return (NULL);
    128 	if (*p == '#')
    129 		goto again;
    130 	cp = strpbrk(p, "#\n");
    131 	if (cp == NULL)
    132 		goto again;
    133 	*cp = '\0';
    134 	net.n_name = p;
    135 	cp = strpbrk(p, " \t");
    136 	if (cp == NULL)
    137 		goto again;
    138 	*cp++ = '\0';
    139 	while (*cp == ' ' || *cp == '\t')
    140 		cp++;
    141 	p = strpbrk(cp, " \t");
    142 	if (p != NULL)
    143 		*p++ = '\0';
    144 	net.n_net = inet_network(cp);
    145 	net.n_addrtype = AF_INET;
    146 	q = net.n_aliases = net_aliases;
    147 	if (p != NULL) {
    148 		cp = p;
    149 		while (cp && *cp) {
    150 			if (*cp == ' ' || *cp == '\t') {
    151 				cp++;
    152 				continue;
    153 			}
    154 			if (q < &net_aliases[MAXALIASES - 1])
    155 				*q++ = cp;
    156 			cp = strpbrk(cp, " \t");
    157 			if (cp != NULL)
    158 				*cp++ = '\0';
    159 		}
    160 	}
    161 	*q = NULL;
    162 	return (&net);
    163 }
    164