xref: /titanic_41/usr/src/cmd/getent/dogetnet.c (revision 7c2fbfb345896881c631598ee3852ce9ce33fb07)
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  * All rights reserved.
27  */
28 
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <sys/types.h>
33 #include <sys/socket.h>
34 #include <netinet/in.h>
35 #include <arpa/inet.h>
36 #include <netdb.h>
37 #include "getent.h"
38 
39 /*
40  * Print a network number such as 129.144
41  */
42 char *
43 inet_nettoa(struct in_addr in)
44 {
45 	u_long addr = htonl(in.s_addr);
46 	u_char *up = (u_char *)&addr;
47 	static char result[256];
48 
49 	/* Omit leading zeros */
50 	if (up[0]) {
51 		(void) sprintf(result, "%d.%d.%d.%d",
52 		    up[0], up[1], up[2], up[3]);
53 	} else if (up[1]) {
54 		(void) sprintf(result, "%d.%d.%d", up[1], up[2], up[3]);
55 	} else if (up[2]) {
56 		(void) sprintf(result, "%d.%d", up[2], up[3]);
57 	} else {
58 		(void) sprintf(result, "%d", up[3]);
59 	}
60 	return (result);
61 }
62 
63 static int
64 putnetent(const struct netent *np, FILE *fp)
65 {
66 	char **p;
67 	int rc = 0;
68 	struct in_addr in;
69 
70 	if (np == NULL) {
71 		return (1);
72 	}
73 
74 	in.s_addr = np->n_net;
75 	if (fprintf(fp, "%-20s %s",
76 		    np->n_name, inet_nettoa(in)) == EOF)
77 		rc = 1;
78 	for (p = np->n_aliases; *p != 0; p++) {
79 		if (fprintf(fp, " %s", *p) == EOF)
80 			rc = 1;
81 	}
82 	if (putc('\n', fp) == EOF)
83 		rc = 1;
84 	return (rc);
85 }
86 
87 /*
88  * getnetbyname/addr - get entries from network database
89  */
90 int
91 dogetnet(const char **list)
92 {
93 	struct netent *np;
94 	int rc = EXC_SUCCESS;
95 
96 	if (list == NULL || *list == NULL) {
97 		while ((np = getnetent()) != NULL)
98 			(void) putnetent(np, stdout);
99 	} else {
100 		for (; *list != NULL; list++) {
101 			long addr = inet_network(*list);
102 			if (addr != -1)
103 				np = getnetbyaddr(addr, AF_INET);
104 			else
105 				np = getnetbyname(*list);
106 			if (np == NULL)
107 				rc = EXC_NAME_NOT_FOUND;
108 			else
109 				(void) putnetent(np, stdout);
110 		}
111 	}
112 
113 	return (rc);
114 }
115