xref: /titanic_44/usr/src/cmd/getent/dogetproto.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 #ident	"%Z%%M%	%I%	%E% SMI"
23 
24 /*
25  * Copyright (c) 1994, by Sun Microsystems, Inc.
26  */
27 
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <sys/types.h>
31 #include <sys/socket.h>
32 #include <netinet/in.h>
33 #include <netdb.h>
34 #include "getent.h"
35 
36 static int
putprotoent(const struct protoent * pp,FILE * fp)37 putprotoent(const struct protoent *pp, FILE *fp)
38 {
39 	char **p;
40 	int rc = 0;
41 
42 	if (pp == NULL) {
43 		return (1);
44 	}
45 
46 	if (fprintf(fp, "%-20s %d",
47 		    pp->p_name, pp->p_proto) == EOF)
48 		rc = 1;
49 	for (p = pp->p_aliases; *p != 0; p++) {
50 		if (fprintf(fp, " %s", *p) == EOF)
51 			rc = 1;
52 	}
53 	if (putc('\n', fp) == EOF)
54 		rc = 1;
55 	return (rc);
56 }
57 
58 /*
59  * getprotobyname/addr - get entries from protocols database
60  */
61 int
dogetproto(const char ** list)62 dogetproto(const char **list)
63 {
64 	struct protoent *pp;
65 	int rc = EXC_SUCCESS;
66 
67 	if (list == NULL || *list == NULL) {
68 		while ((pp = getprotoent()) != NULL)
69 			(void) putprotoent(pp, stdout);
70 	} else {
71 		for (; *list != NULL; list++) {
72 			int protocol = atoi(*list);
73 			if (protocol != 0)
74 				pp = getprotobynumber(protocol);
75 			else
76 				pp = getprotobyname(*list);
77 			if (pp == NULL)
78 				rc = EXC_NAME_NOT_FOUND;
79 			else
80 				(void) putprotoent(pp, stdout);
81 		}
82 	}
83 
84 	return (rc);
85 }
86