GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: lib/libc/net/getnetent.c Lines: 0 51 0.0 %
Date: 2017-11-13 Branches: 0 42 0.0 %

Line Branch Exec Source
1
/*	$OpenBSD: getnetent.c,v 1.17 2015/01/16 18:20:14 millert Exp $ */
2
/*
3
 * Copyright (c) 1983, 1993
4
 *	The Regents of the University of California.  All rights reserved.
5
 *
6
 * Redistribution and use in source and binary forms, with or without
7
 * modification, are permitted provided that the following conditions
8
 * are met:
9
 * 1. Redistributions of source code must retain the above copyright
10
 *    notice, this list of conditions and the following disclaimer.
11
 * 2. Redistributions in binary form must reproduce the above copyright
12
 *    notice, this list of conditions and the following disclaimer in the
13
 *    documentation and/or other materials provided with the distribution.
14
 * 3. Neither the name of the University nor the names of its contributors
15
 *    may be used to endorse or promote products derived from this software
16
 *    without specific prior written permission.
17
 *
18
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28
 * SUCH DAMAGE.
29
 */
30
31
#include <sys/socket.h>
32
#include <netinet/in.h>
33
#include <arpa/inet.h>
34
#include <netdb.h>
35
#include <stdio.h>
36
#include <string.h>
37
#include <limits.h>
38
39
#define	MAXALIASES	35
40
41
static FILE *netf;
42
static char line[BUFSIZ+1];
43
static struct netent net;
44
static char *net_aliases[MAXALIASES];
45
int _net_stayopen;
46
47
void
48
setnetent(int f)
49
{
50
	if (netf == NULL)
51
		netf = fopen(_PATH_NETWORKS, "re" );
52
	else
53
		rewind(netf);
54
	_net_stayopen |= f;
55
}
56
57
void
58
endnetent(void)
59
{
60
	if (netf) {
61
		fclose(netf);
62
		netf = NULL;
63
	}
64
	_net_stayopen = 0;
65
}
66
67
struct netent *
68
getnetent(void)
69
{
70
	char *p, *cp, **q;
71
	size_t len;
72
73
	if (netf == NULL && (netf = fopen(_PATH_NETWORKS, "re" )) == NULL)
74
		return (NULL);
75
again:
76
	if ((p = fgetln(netf, &len)) == NULL)
77
		return (NULL);
78
	if (p[len-1] == '\n')
79
		len--;
80
	if (len >= sizeof(line) || len == 0)
81
		goto again;
82
	p = memcpy(line, p, len);
83
	line[len] = '\0';
84
	if (*p == '#')
85
		goto again;
86
	if ((cp = strchr(p, '#')) != NULL)
87
		*cp = '\0';
88
	net.n_name = p;
89
	if (strlen(net.n_name) > HOST_NAME_MAX)
90
		net.n_name[HOST_NAME_MAX] = '\0';
91
	cp = strpbrk(p, " \t");
92
	if (cp == NULL)
93
		goto again;
94
	*cp++ = '\0';
95
	while (*cp == ' ' || *cp == '\t')
96
		cp++;
97
	p = strpbrk(cp, " \t");
98
	if (p != NULL)
99
		*p++ = '\0';
100
	net.n_net = inet_network(cp);
101
	net.n_addrtype = AF_INET;
102
	q = net.n_aliases = net_aliases;
103
	cp = p;
104
	while (cp && *cp) {
105
		if (*cp == ' ' || *cp == '\t') {
106
			cp++;
107
			continue;
108
		}
109
		if (q < &net_aliases[MAXALIASES - 1]) {
110
			*q++ = cp;
111
			if (strlen(cp) > HOST_NAME_MAX)
112
				cp[HOST_NAME_MAX] = '\0';
113
		}
114
		cp = strpbrk(cp, " \t");
115
		if (cp != NULL)
116
			*cp++ = '\0';
117
	}
118
	*q = NULL;
119
	return (&net);
120
}