1251881Speter/*	$OpenBSD: getprotoname.c,v 1.8 2015/09/14 07:38:38 guenther Exp $ */
2251881Speter/*
3251881Speter * Copyright (c) 1983, 1993
4251881Speter *	The Regents of the University of California.  All rights reserved.
5251881Speter *
6251881Speter * Redistribution and use in source and binary forms, with or without
7251881Speter * modification, are permitted provided that the following conditions
8251881Speter * are met:
9251881Speter * 1. Redistributions of source code must retain the above copyright
10251881Speter *    notice, this list of conditions and the following disclaimer.
11251881Speter * 2. Redistributions in binary form must reproduce the above copyright
12251881Speter *    notice, this list of conditions and the following disclaimer in the
13251881Speter *    documentation and/or other materials provided with the distribution.
14251881Speter * 3. Neither the name of the University nor the names of its contributors
15251881Speter *    may be used to endorse or promote products derived from this software
16251881Speter *    without specific prior written permission.
17251881Speter *
18251881Speter * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19251881Speter * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20251881Speter * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21251881Speter * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22251881Speter * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23251881Speter * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24251881Speter * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25251881Speter * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26251881Speter * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27251881Speter * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28251881Speter * SUCH DAMAGE.
29251881Speter */
30251881Speter
31251881Speter#include <netdb.h>
32251881Speter#include <stdio.h>
33251881Speter#include <string.h>
34251881Speter
35251881Speterint
36251881Spetergetprotobyname_r(const char *name, struct protoent *pe,
37251881Speter    struct protoent_data *pd)
38251881Speter{
39251881Speter	char **cp;
40251881Speter	int error;
41251881Speter
42251881Speter	setprotoent_r(pd->stayopen, pd);
43251881Speter	while ((error = getprotoent_r(pe, pd)) == 0) {
44251881Speter		if (strcmp(pe->p_name, name) == 0)
45251881Speter			break;
46251881Speter		for (cp = pe->p_aliases; *cp != 0; cp++)
47251881Speter			if (strcmp(*cp, name) == 0)
48251881Speter				goto found;
49251881Speter	}
50251881Speterfound:
51251881Speter	if (!pd->stayopen && pd->fp != NULL) {
52251881Speter		fclose(pd->fp);
53251881Speter		pd->fp = NULL;
54251881Speter	}
55251881Speter	return (error);
56251881Speter}
57251881SpeterDEF_WEAK(getprotobyname_r);
58251881Speter
59251881Speterstruct protoent *
60251881Spetergetprotobyname(const char *name)
61251881Speter{
62251881Speter	extern struct protoent_data _protoent_data;
63251881Speter	static struct protoent proto;
64251881Speter
65251881Speter	if (getprotobyname_r(name, &proto, &_protoent_data) != 0)
66251881Speter		return (NULL);
67251881Speter	return (&proto);
68251881Speter}
69251881SpeterDEF_WEAK(getprotobyname);
70251881Speter