121377Sdist /*
2*61150Sbostic * Copyright (c) 1983, 1993
3*61150Sbostic * The Regents of the University of California. All rights reserved.
433679Sbostic *
542625Sbostic * %sccs.include.redist.c%
621377Sdist */
77896Ssam
826618Sdonn #if defined(LIBC_SCCS) && !defined(lint)
9*61150Sbostic static char sccsid[] = "@(#)getnetent.c 8.1 (Berkeley) 06/04/93";
1033679Sbostic #endif /* LIBC_SCCS and not lint */
1121377Sdist
1212691Ssam #include <sys/types.h>
137896Ssam #include <sys/socket.h>
1446604Sbostic #include <netinet/in.h>
1546604Sbostic #include <arpa/inet.h>
167896Ssam #include <netdb.h>
1746604Sbostic #include <stdio.h>
1846604Sbostic #include <string.h>
197896Ssam
207896Ssam #define MAXALIASES 35
217896Ssam
2246604Sbostic static FILE *netf;
237896Ssam static char line[BUFSIZ+1];
247896Ssam static struct netent net;
257896Ssam static char *net_aliases[MAXALIASES];
2628280Slepreau int _net_stayopen;
277896Ssam
2846604Sbostic void
setnetent(f)297896Ssam setnetent(f)
307896Ssam int f;
317896Ssam {
327896Ssam if (netf == NULL)
3342266Sbostic netf = fopen(_PATH_NETWORKS, "r" );
347896Ssam else
357896Ssam rewind(netf);
3628280Slepreau _net_stayopen |= f;
377896Ssam }
387896Ssam
3946604Sbostic void
endnetent()407896Ssam endnetent()
417896Ssam {
4228280Slepreau if (netf) {
437896Ssam fclose(netf);
447896Ssam netf = NULL;
457896Ssam }
4628280Slepreau _net_stayopen = 0;
477896Ssam }
487896Ssam
497896Ssam struct netent *
getnetent()507896Ssam getnetent()
517896Ssam {
527896Ssam char *p;
537896Ssam register char *cp, **q;
547896Ssam
5542266Sbostic if (netf == NULL && (netf = fopen(_PATH_NETWORKS, "r" )) == NULL)
567896Ssam return (NULL);
577896Ssam again:
587896Ssam p = fgets(line, BUFSIZ, netf);
597896Ssam if (p == NULL)
607896Ssam return (NULL);
617896Ssam if (*p == '#')
627896Ssam goto again;
6346604Sbostic cp = strpbrk(p, "#\n");
647896Ssam if (cp == NULL)
657896Ssam goto again;
667896Ssam *cp = '\0';
677896Ssam net.n_name = p;
6846604Sbostic cp = strpbrk(p, " \t");
697896Ssam if (cp == NULL)
707896Ssam goto again;
717896Ssam *cp++ = '\0';
727896Ssam while (*cp == ' ' || *cp == '\t')
737896Ssam cp++;
7446604Sbostic p = strpbrk(cp, " \t");
757896Ssam if (p != NULL)
767896Ssam *p++ = '\0';
778364Ssam net.n_net = inet_network(cp);
788342Ssam net.n_addrtype = AF_INET;
799190Ssam q = net.n_aliases = net_aliases;
8010087Ssam if (p != NULL)
819190Ssam cp = p;
8210087Ssam while (cp && *cp) {
8310087Ssam if (*cp == ' ' || *cp == '\t') {
8410087Ssam cp++;
8510087Ssam continue;
867896Ssam }
8710087Ssam if (q < &net_aliases[MAXALIASES - 1])
8810087Ssam *q++ = cp;
8946604Sbostic cp = strpbrk(cp, " \t");
9010087Ssam if (cp != NULL)
9110087Ssam *cp++ = '\0';
927896Ssam }
937896Ssam *q = NULL;
947896Ssam return (&net);
957896Ssam }
96