8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / cmd / getent / dogetnet.c
blob4758d6db2758e55815a30ffc6c074eabf454213e
1 /*
2 * CDDL HEADER START
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.
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.
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]
20 * CDDL HEADER END
22 #ident "%Z%%M% %I% %E% SMI"
25 * Copyright (c) 1994, by Sun Microsystems, Inc.
26 * All rights reserved.
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"
40 * Print a network number such as 129.144
42 char *
43 inet_nettoa(struct in_addr in)
45 u_long addr = htonl(in.s_addr);
46 u_char *up = (u_char *)&addr;
47 static char result[256];
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]);
60 return (result);
63 static int
64 putnetent(const struct netent *np, FILE *fp)
66 char **p;
67 int rc = 0;
68 struct in_addr in;
70 if (np == NULL) {
71 return (1);
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;
82 if (putc('\n', fp) == EOF)
83 rc = 1;
84 return (rc);
88 * getnetbyname/addr - get entries from network database
90 int
91 dogetnet(const char **list)
93 struct netent *np;
94 int rc = EXC_SUCCESS;
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);
113 return (rc);