1*f14fb602SLionel Sambuc /* $NetBSD: getnetent.c,v 1.21 2012/03/20 17:44:18 matt Exp $ */
22fe8fb19SBen Gras
32fe8fb19SBen Gras /*
42fe8fb19SBen Gras * Copyright (c) 1983, 1993
52fe8fb19SBen Gras * The Regents of the University of California. All rights reserved.
62fe8fb19SBen Gras *
72fe8fb19SBen Gras * Redistribution and use in source and binary forms, with or without
82fe8fb19SBen Gras * modification, are permitted provided that the following conditions
92fe8fb19SBen Gras * are met:
102fe8fb19SBen Gras * 1. Redistributions of source code must retain the above copyright
112fe8fb19SBen Gras * notice, this list of conditions and the following disclaimer.
122fe8fb19SBen Gras * 2. Redistributions in binary form must reproduce the above copyright
132fe8fb19SBen Gras * notice, this list of conditions and the following disclaimer in the
142fe8fb19SBen Gras * documentation and/or other materials provided with the distribution.
152fe8fb19SBen Gras * 3. Neither the name of the University nor the names of its contributors
162fe8fb19SBen Gras * may be used to endorse or promote products derived from this software
172fe8fb19SBen Gras * without specific prior written permission.
182fe8fb19SBen Gras *
192fe8fb19SBen Gras * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
202fe8fb19SBen Gras * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
212fe8fb19SBen Gras * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
222fe8fb19SBen Gras * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
232fe8fb19SBen Gras * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
242fe8fb19SBen Gras * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
252fe8fb19SBen Gras * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
262fe8fb19SBen Gras * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
272fe8fb19SBen Gras * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
282fe8fb19SBen Gras * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
292fe8fb19SBen Gras * SUCH DAMAGE.
302fe8fb19SBen Gras *
312fe8fb19SBen Gras * Portions Copyright (c) 1993 Carlos Leandro and Rui Salgueiro
322fe8fb19SBen Gras * Dep. Matematica Universidade de Coimbra, Portugal, Europe
332fe8fb19SBen Gras *
342fe8fb19SBen Gras * Permission to use, copy, modify, and distribute this software for any
352fe8fb19SBen Gras * purpose with or without fee is hereby granted, provided that the above
362fe8fb19SBen Gras * copyright notice and this permission notice appear in all copies.
372fe8fb19SBen Gras *
382fe8fb19SBen Gras * from getnetent.c 1.1 (Coimbra) 93/06/02
392fe8fb19SBen Gras */
402fe8fb19SBen Gras
412fe8fb19SBen Gras #include <sys/cdefs.h>
422fe8fb19SBen Gras #if defined(LIBC_SCCS) && !defined(lint)
432fe8fb19SBen Gras #if 0
442fe8fb19SBen Gras static char sccsid[] = "@(#)getnetent.c 8.1 (Berkeley) 6/4/93";
452fe8fb19SBen Gras static char rcsid[] = "Id: getnetent.c,v 8.4 1997/06/01 20:34:37 vixie Exp ";
462fe8fb19SBen Gras #else
47*f14fb602SLionel Sambuc __RCSID("$NetBSD: getnetent.c,v 1.21 2012/03/20 17:44:18 matt Exp $");
482fe8fb19SBen Gras #endif
492fe8fb19SBen Gras #endif /* LIBC_SCCS and not lint */
502fe8fb19SBen Gras
512fe8fb19SBen Gras #include "namespace.h"
522fe8fb19SBen Gras #include <sys/types.h>
532fe8fb19SBen Gras #include <sys/socket.h>
542fe8fb19SBen Gras #include <netinet/in.h>
552fe8fb19SBen Gras #include <arpa/inet.h>
562fe8fb19SBen Gras #include <netdb.h>
572fe8fb19SBen Gras #include <stdio.h>
582fe8fb19SBen Gras #include <string.h>
592fe8fb19SBen Gras
602fe8fb19SBen Gras #ifdef __weak_alias
612fe8fb19SBen Gras __weak_alias(endnetent,_endnetent)
622fe8fb19SBen Gras __weak_alias(getnetent,_getnetent)
632fe8fb19SBen Gras __weak_alias(setnetent,_setnetent)
642fe8fb19SBen Gras #endif
652fe8fb19SBen Gras
662fe8fb19SBen Gras #define MAXALIASES 35
672fe8fb19SBen Gras
682fe8fb19SBen Gras static FILE *netf;
692fe8fb19SBen Gras static char line[BUFSIZ+1];
702fe8fb19SBen Gras static struct netent net;
712fe8fb19SBen Gras static char *net_aliases[MAXALIASES];
722fe8fb19SBen Gras int _net_stayopen;
732fe8fb19SBen Gras
74*f14fb602SLionel Sambuc static void __setnetent(int);
75*f14fb602SLionel Sambuc static void __endnetent(void);
762fe8fb19SBen Gras
772fe8fb19SBen Gras void
setnetent(int stayopen)78*f14fb602SLionel Sambuc setnetent(int stayopen)
792fe8fb19SBen Gras {
802fe8fb19SBen Gras
812fe8fb19SBen Gras sethostent(stayopen);
822fe8fb19SBen Gras __setnetent(stayopen);
832fe8fb19SBen Gras }
842fe8fb19SBen Gras
852fe8fb19SBen Gras void
endnetent(void)86*f14fb602SLionel Sambuc endnetent(void)
872fe8fb19SBen Gras {
882fe8fb19SBen Gras
892fe8fb19SBen Gras endhostent();
902fe8fb19SBen Gras __endnetent();
912fe8fb19SBen Gras }
922fe8fb19SBen Gras
932fe8fb19SBen Gras static void
__setnetent(int f)94*f14fb602SLionel Sambuc __setnetent(int f)
952fe8fb19SBen Gras {
962fe8fb19SBen Gras
972fe8fb19SBen Gras if (netf == NULL)
98*f14fb602SLionel Sambuc netf = fopen(_PATH_NETWORKS, "re");
992fe8fb19SBen Gras else
1002fe8fb19SBen Gras rewind(netf);
1012fe8fb19SBen Gras _net_stayopen |= f;
1022fe8fb19SBen Gras }
1032fe8fb19SBen Gras
1042fe8fb19SBen Gras static void
__endnetent(void)105*f14fb602SLionel Sambuc __endnetent(void)
1062fe8fb19SBen Gras {
1072fe8fb19SBen Gras
1082fe8fb19SBen Gras if (netf) {
1092fe8fb19SBen Gras fclose(netf);
1102fe8fb19SBen Gras netf = NULL;
1112fe8fb19SBen Gras }
1122fe8fb19SBen Gras _net_stayopen = 0;
1132fe8fb19SBen Gras }
1142fe8fb19SBen Gras
1152fe8fb19SBen Gras struct netent *
getnetent(void)116*f14fb602SLionel Sambuc getnetent(void)
1172fe8fb19SBen Gras {
1182fe8fb19SBen Gras char *p;
1192fe8fb19SBen Gras register char *cp, **q;
1202fe8fb19SBen Gras
121*f14fb602SLionel Sambuc if (netf == NULL && (netf = fopen(_PATH_NETWORKS, "re")) == NULL)
1222fe8fb19SBen Gras return (NULL);
1232fe8fb19SBen Gras #if (defined(__sparc__) && defined(_LP64)) || \
1242fe8fb19SBen Gras defined(__alpha__) || \
1252fe8fb19SBen Gras (defined(__i386__) && defined(_LP64)) || \
1262fe8fb19SBen Gras (defined(__sh__) && defined(_LP64))
1272fe8fb19SBen Gras net.__n_pad0 = 0;
1282fe8fb19SBen Gras #endif
1292fe8fb19SBen Gras again:
130*f14fb602SLionel Sambuc p = fgets(line, (int)sizeof line, netf);
1312fe8fb19SBen Gras if (p == NULL)
1322fe8fb19SBen Gras return (NULL);
1332fe8fb19SBen Gras if (*p == '#')
1342fe8fb19SBen Gras goto again;
1352fe8fb19SBen Gras cp = strpbrk(p, "#\n");
1362fe8fb19SBen Gras if (cp == NULL)
1372fe8fb19SBen Gras goto again;
1382fe8fb19SBen Gras *cp = '\0';
1392fe8fb19SBen Gras net.n_name = p;
1402fe8fb19SBen Gras cp = strpbrk(p, " \t");
1412fe8fb19SBen Gras if (cp == NULL)
1422fe8fb19SBen Gras goto again;
1432fe8fb19SBen Gras *cp++ = '\0';
1442fe8fb19SBen Gras while (*cp == ' ' || *cp == '\t')
1452fe8fb19SBen Gras cp++;
1462fe8fb19SBen Gras p = strpbrk(cp, " \t");
1472fe8fb19SBen Gras if (p != NULL)
1482fe8fb19SBen Gras *p++ = '\0';
1492fe8fb19SBen Gras net.n_net = inet_network(cp);
1502fe8fb19SBen Gras net.n_addrtype = AF_INET;
1512fe8fb19SBen Gras q = net.n_aliases = net_aliases;
1522fe8fb19SBen Gras if (p != NULL) {
1532fe8fb19SBen Gras cp = p;
1542fe8fb19SBen Gras while (cp && *cp) {
1552fe8fb19SBen Gras if (*cp == ' ' || *cp == '\t') {
1562fe8fb19SBen Gras cp++;
1572fe8fb19SBen Gras continue;
1582fe8fb19SBen Gras }
1592fe8fb19SBen Gras if (q < &net_aliases[MAXALIASES - 1])
1602fe8fb19SBen Gras *q++ = cp;
1612fe8fb19SBen Gras cp = strpbrk(cp, " \t");
1622fe8fb19SBen Gras if (cp != NULL)
1632fe8fb19SBen Gras *cp++ = '\0';
1642fe8fb19SBen Gras }
1652fe8fb19SBen Gras }
1662fe8fb19SBen Gras *q = NULL;
1672fe8fb19SBen Gras return (&net);
1682fe8fb19SBen Gras }
169