xref: /csrg-svn/lib/libc/net/getnetent.c (revision 42625)
121377Sdist /*
221377Sdist  * Copyright (c) 1983 Regents of the University of California.
333679Sbostic  * All rights reserved.
433679Sbostic  *
5*42625Sbostic  * %sccs.include.redist.c%
621377Sdist  */
77896Ssam 
826618Sdonn #if defined(LIBC_SCCS) && !defined(lint)
9*42625Sbostic static char sccsid[] = "@(#)getnetent.c	5.7 (Berkeley) 06/01/90";
1033679Sbostic #endif /* LIBC_SCCS and not lint */
1121377Sdist 
127896Ssam #include <stdio.h>
1312691Ssam #include <sys/types.h>
147896Ssam #include <sys/socket.h>
157896Ssam #include <netdb.h>
167896Ssam #include <ctype.h>
177896Ssam 
187896Ssam #define	MAXALIASES	35
197896Ssam 
207896Ssam static FILE *netf = NULL;
217896Ssam static char line[BUFSIZ+1];
227896Ssam static struct netent net;
237896Ssam static char *net_aliases[MAXALIASES];
2428280Slepreau int _net_stayopen;
257896Ssam static char *any();
267896Ssam 
277896Ssam setnetent(f)
287896Ssam 	int f;
297896Ssam {
307896Ssam 	if (netf == NULL)
3142266Sbostic 		netf = fopen(_PATH_NETWORKS, "r" );
327896Ssam 	else
337896Ssam 		rewind(netf);
3428280Slepreau 	_net_stayopen |= f;
357896Ssam }
367896Ssam 
377896Ssam endnetent()
387896Ssam {
3928280Slepreau 	if (netf) {
407896Ssam 		fclose(netf);
417896Ssam 		netf = NULL;
427896Ssam 	}
4328280Slepreau 	_net_stayopen = 0;
447896Ssam }
457896Ssam 
467896Ssam struct netent *
477896Ssam getnetent()
487896Ssam {
497896Ssam 	char *p;
507896Ssam 	register char *cp, **q;
517896Ssam 
5242266Sbostic 	if (netf == NULL && (netf = fopen(_PATH_NETWORKS, "r" )) == NULL)
537896Ssam 		return (NULL);
547896Ssam again:
557896Ssam 	p = fgets(line, BUFSIZ, netf);
567896Ssam 	if (p == NULL)
577896Ssam 		return (NULL);
587896Ssam 	if (*p == '#')
597896Ssam 		goto again;
607896Ssam 	cp = any(p, "#\n");
617896Ssam 	if (cp == NULL)
627896Ssam 		goto again;
637896Ssam 	*cp = '\0';
647896Ssam 	net.n_name = p;
657896Ssam 	cp = any(p, " \t");
667896Ssam 	if (cp == NULL)
677896Ssam 		goto again;
687896Ssam 	*cp++ = '\0';
697896Ssam 	while (*cp == ' ' || *cp == '\t')
707896Ssam 		cp++;
717896Ssam 	p = any(cp, " \t");
727896Ssam 	if (p != NULL)
737896Ssam 		*p++ = '\0';
748364Ssam 	net.n_net = inet_network(cp);
758342Ssam 	net.n_addrtype = AF_INET;
769190Ssam 	q = net.n_aliases = net_aliases;
7710087Ssam 	if (p != NULL)
789190Ssam 		cp = p;
7910087Ssam 	while (cp && *cp) {
8010087Ssam 		if (*cp == ' ' || *cp == '\t') {
8110087Ssam 			cp++;
8210087Ssam 			continue;
837896Ssam 		}
8410087Ssam 		if (q < &net_aliases[MAXALIASES - 1])
8510087Ssam 			*q++ = cp;
8610087Ssam 		cp = any(cp, " \t");
8710087Ssam 		if (cp != NULL)
8810087Ssam 			*cp++ = '\0';
897896Ssam 	}
907896Ssam 	*q = NULL;
917896Ssam 	return (&net);
927896Ssam }
937896Ssam 
947896Ssam static char *
957896Ssam any(cp, match)
967896Ssam 	register char *cp;
977896Ssam 	char *match;
987896Ssam {
997896Ssam 	register char *mp, c;
1007896Ssam 
1017896Ssam 	while (c = *cp) {
1027896Ssam 		for (mp = match; *mp; mp++)
1037896Ssam 			if (*mp == c)
1047896Ssam 				return (cp);
1057896Ssam 		cp++;
1067896Ssam 	}
1077896Ssam 	return ((char *)0);
1087896Ssam }
109