xref: /csrg-svn/lib/libc/net/getnetent.c (revision 34816)
121377Sdist /*
221377Sdist  * Copyright (c) 1983 Regents of the University of California.
333679Sbostic  * All rights reserved.
433679Sbostic  *
533679Sbostic  * Redistribution and use in source and binary forms are permitted
6*34816Sbostic  * provided that the above copyright notice and this paragraph are
7*34816Sbostic  * duplicated in all such forms and that any documentation,
8*34816Sbostic  * advertising materials, and other materials related to such
9*34816Sbostic  * distribution and use acknowledge that the software was developed
10*34816Sbostic  * by the University of California, Berkeley.  The name of the
11*34816Sbostic  * University may not be used to endorse or promote products derived
12*34816Sbostic  * from this software without specific prior written permission.
13*34816Sbostic  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14*34816Sbostic  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15*34816Sbostic  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1621377Sdist  */
177896Ssam 
1826618Sdonn #if defined(LIBC_SCCS) && !defined(lint)
19*34816Sbostic static char sccsid[] = "@(#)getnetent.c	5.5 (Berkeley) 06/27/88";
2033679Sbostic #endif /* LIBC_SCCS and not lint */
2121377Sdist 
227896Ssam #include <stdio.h>
2312691Ssam #include <sys/types.h>
247896Ssam #include <sys/socket.h>
257896Ssam #include <netdb.h>
267896Ssam #include <ctype.h>
277896Ssam 
287896Ssam #define	MAXALIASES	35
297896Ssam 
309808Ssam static char NETDB[] = "/etc/networks";
317896Ssam static FILE *netf = NULL;
327896Ssam static char line[BUFSIZ+1];
337896Ssam static struct netent net;
347896Ssam static char *net_aliases[MAXALIASES];
3528280Slepreau int _net_stayopen;
367896Ssam static char *any();
377896Ssam 
387896Ssam setnetent(f)
397896Ssam 	int f;
407896Ssam {
417896Ssam 	if (netf == NULL)
427896Ssam 		netf = fopen(NETDB, "r" );
437896Ssam 	else
447896Ssam 		rewind(netf);
4528280Slepreau 	_net_stayopen |= f;
467896Ssam }
477896Ssam 
487896Ssam endnetent()
497896Ssam {
5028280Slepreau 	if (netf) {
517896Ssam 		fclose(netf);
527896Ssam 		netf = NULL;
537896Ssam 	}
5428280Slepreau 	_net_stayopen = 0;
557896Ssam }
567896Ssam 
577896Ssam struct netent *
587896Ssam getnetent()
597896Ssam {
607896Ssam 	char *p;
617896Ssam 	register char *cp, **q;
627896Ssam 
637896Ssam 	if (netf == NULL && (netf = fopen(NETDB, "r" )) == NULL)
647896Ssam 		return (NULL);
657896Ssam again:
667896Ssam 	p = fgets(line, BUFSIZ, netf);
677896Ssam 	if (p == NULL)
687896Ssam 		return (NULL);
697896Ssam 	if (*p == '#')
707896Ssam 		goto again;
717896Ssam 	cp = any(p, "#\n");
727896Ssam 	if (cp == NULL)
737896Ssam 		goto again;
747896Ssam 	*cp = '\0';
757896Ssam 	net.n_name = p;
767896Ssam 	cp = any(p, " \t");
777896Ssam 	if (cp == NULL)
787896Ssam 		goto again;
797896Ssam 	*cp++ = '\0';
807896Ssam 	while (*cp == ' ' || *cp == '\t')
817896Ssam 		cp++;
827896Ssam 	p = any(cp, " \t");
837896Ssam 	if (p != NULL)
847896Ssam 		*p++ = '\0';
858364Ssam 	net.n_net = inet_network(cp);
868342Ssam 	net.n_addrtype = AF_INET;
879190Ssam 	q = net.n_aliases = net_aliases;
8810087Ssam 	if (p != NULL)
899190Ssam 		cp = p;
9010087Ssam 	while (cp && *cp) {
9110087Ssam 		if (*cp == ' ' || *cp == '\t') {
9210087Ssam 			cp++;
9310087Ssam 			continue;
947896Ssam 		}
9510087Ssam 		if (q < &net_aliases[MAXALIASES - 1])
9610087Ssam 			*q++ = cp;
9710087Ssam 		cp = any(cp, " \t");
9810087Ssam 		if (cp != NULL)
9910087Ssam 			*cp++ = '\0';
1007896Ssam 	}
1017896Ssam 	*q = NULL;
1027896Ssam 	return (&net);
1037896Ssam }
1047896Ssam 
1057896Ssam static char *
1067896Ssam any(cp, match)
1077896Ssam 	register char *cp;
1087896Ssam 	char *match;
1097896Ssam {
1107896Ssam 	register char *mp, c;
1117896Ssam 
1127896Ssam 	while (c = *cp) {
1137896Ssam 		for (mp = match; *mp; mp++)
1147896Ssam 			if (*mp == c)
1157896Ssam 				return (cp);
1167896Ssam 		cp++;
1177896Ssam 	}
1187896Ssam 	return ((char *)0);
1197896Ssam }
120