xref: /csrg-svn/lib/libc/net/getnetent.c (revision 42266)
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
634816Sbostic  * provided that the above copyright notice and this paragraph are
734816Sbostic  * duplicated in all such forms and that any documentation,
834816Sbostic  * advertising materials, and other materials related to such
934816Sbostic  * distribution and use acknowledge that the software was developed
1034816Sbostic  * by the University of California, Berkeley.  The name of the
1134816Sbostic  * University may not be used to endorse or promote products derived
1234816Sbostic  * from this software without specific prior written permission.
1334816Sbostic  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
1434816Sbostic  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
1534816Sbostic  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1621377Sdist  */
177896Ssam 
1826618Sdonn #if defined(LIBC_SCCS) && !defined(lint)
19*42266Sbostic static char sccsid[] = "@(#)getnetent.c	5.6 (Berkeley) 05/21/90";
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 
307896Ssam static FILE *netf = NULL;
317896Ssam static char line[BUFSIZ+1];
327896Ssam static struct netent net;
337896Ssam static char *net_aliases[MAXALIASES];
3428280Slepreau int _net_stayopen;
357896Ssam static char *any();
367896Ssam 
377896Ssam setnetent(f)
387896Ssam 	int f;
397896Ssam {
407896Ssam 	if (netf == NULL)
41*42266Sbostic 		netf = fopen(_PATH_NETWORKS, "r" );
427896Ssam 	else
437896Ssam 		rewind(netf);
4428280Slepreau 	_net_stayopen |= f;
457896Ssam }
467896Ssam 
477896Ssam endnetent()
487896Ssam {
4928280Slepreau 	if (netf) {
507896Ssam 		fclose(netf);
517896Ssam 		netf = NULL;
527896Ssam 	}
5328280Slepreau 	_net_stayopen = 0;
547896Ssam }
557896Ssam 
567896Ssam struct netent *
577896Ssam getnetent()
587896Ssam {
597896Ssam 	char *p;
607896Ssam 	register char *cp, **q;
617896Ssam 
62*42266Sbostic 	if (netf == NULL && (netf = fopen(_PATH_NETWORKS, "r" )) == NULL)
637896Ssam 		return (NULL);
647896Ssam again:
657896Ssam 	p = fgets(line, BUFSIZ, netf);
667896Ssam 	if (p == NULL)
677896Ssam 		return (NULL);
687896Ssam 	if (*p == '#')
697896Ssam 		goto again;
707896Ssam 	cp = any(p, "#\n");
717896Ssam 	if (cp == NULL)
727896Ssam 		goto again;
737896Ssam 	*cp = '\0';
747896Ssam 	net.n_name = p;
757896Ssam 	cp = any(p, " \t");
767896Ssam 	if (cp == NULL)
777896Ssam 		goto again;
787896Ssam 	*cp++ = '\0';
797896Ssam 	while (*cp == ' ' || *cp == '\t')
807896Ssam 		cp++;
817896Ssam 	p = any(cp, " \t");
827896Ssam 	if (p != NULL)
837896Ssam 		*p++ = '\0';
848364Ssam 	net.n_net = inet_network(cp);
858342Ssam 	net.n_addrtype = AF_INET;
869190Ssam 	q = net.n_aliases = net_aliases;
8710087Ssam 	if (p != NULL)
889190Ssam 		cp = p;
8910087Ssam 	while (cp && *cp) {
9010087Ssam 		if (*cp == ' ' || *cp == '\t') {
9110087Ssam 			cp++;
9210087Ssam 			continue;
937896Ssam 		}
9410087Ssam 		if (q < &net_aliases[MAXALIASES - 1])
9510087Ssam 			*q++ = cp;
9610087Ssam 		cp = any(cp, " \t");
9710087Ssam 		if (cp != NULL)
9810087Ssam 			*cp++ = '\0';
997896Ssam 	}
1007896Ssam 	*q = NULL;
1017896Ssam 	return (&net);
1027896Ssam }
1037896Ssam 
1047896Ssam static char *
1057896Ssam any(cp, match)
1067896Ssam 	register char *cp;
1077896Ssam 	char *match;
1087896Ssam {
1097896Ssam 	register char *mp, c;
1107896Ssam 
1117896Ssam 	while (c = *cp) {
1127896Ssam 		for (mp = match; *mp; mp++)
1137896Ssam 			if (*mp == c)
1147896Ssam 				return (cp);
1157896Ssam 		cp++;
1167896Ssam 	}
1177896Ssam 	return ((char *)0);
1187896Ssam }
119