xref: /csrg-svn/lib/libc/net/getnetent.c (revision 28280)
121377Sdist /*
221377Sdist  * Copyright (c) 1983 Regents of the University of California.
321377Sdist  * All rights reserved.  The Berkeley software License Agreement
421377Sdist  * specifies the terms and conditions for redistribution.
521377Sdist  */
67896Ssam 
726618Sdonn #if defined(LIBC_SCCS) && !defined(lint)
8*28280Slepreau static char sccsid[] = "@(#)getnetent.c	5.3 (Berkeley) 05/19/86";
926618Sdonn #endif LIBC_SCCS and not lint
1021377Sdist 
117896Ssam #include <stdio.h>
1212691Ssam #include <sys/types.h>
137896Ssam #include <sys/socket.h>
147896Ssam #include <netdb.h>
157896Ssam #include <ctype.h>
167896Ssam 
177896Ssam #define	MAXALIASES	35
187896Ssam 
199808Ssam static char NETDB[] = "/etc/networks";
207896Ssam static FILE *netf = NULL;
217896Ssam static char line[BUFSIZ+1];
227896Ssam static struct netent net;
237896Ssam static char *net_aliases[MAXALIASES];
24*28280Slepreau int _net_stayopen;
257896Ssam static char *any();
267896Ssam 
277896Ssam setnetent(f)
287896Ssam 	int f;
297896Ssam {
307896Ssam 	if (netf == NULL)
317896Ssam 		netf = fopen(NETDB, "r" );
327896Ssam 	else
337896Ssam 		rewind(netf);
34*28280Slepreau 	_net_stayopen |= f;
357896Ssam }
367896Ssam 
377896Ssam endnetent()
387896Ssam {
39*28280Slepreau 	if (netf) {
407896Ssam 		fclose(netf);
417896Ssam 		netf = NULL;
427896Ssam 	}
43*28280Slepreau 	_net_stayopen = 0;
447896Ssam }
457896Ssam 
467896Ssam struct netent *
477896Ssam getnetent()
487896Ssam {
497896Ssam 	char *p;
507896Ssam 	register char *cp, **q;
517896Ssam 
527896Ssam 	if (netf == NULL && (netf = fopen(NETDB, "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