xref: /illumos-gate/usr/src/cmd/getent/dogetethers.c (revision 012e6ce759c490003aed29439cc47d3d73a99ad3)
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 /*
23  * Copyright 1994,2002 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <netdb.h>
30 #include <sys/types.h>
31 #include <sys/socket.h>
32 #include <net/if.h>
33 #include <netinet/in.h>
34 #include <netinet/if_ether.h>
35 #include "getent.h"
36 
37 static int
38 putethers(const char *hostname, const struct ether_addr *e, FILE *fp)
39 {
40 	if (hostname == NULL || e == NULL)
41 		return (EXC_SYNTAX);
42 
43 	if (fprintf(fp, "%-20s %s\n", hostname, ether_ntoa(e)) == EOF)
44 		return (EXC_SYNTAX); /* for lack of a better error code */
45 	return (EXC_SUCCESS);
46 }
47 
48 /*
49  * ether_ntohost/hostton - get entries from ethers database
50  */
51 int
52 dogetethers(const char **list)
53 {
54 	int rc = EXC_SUCCESS;
55 
56 	if (list == NULL || *list == NULL) {
57 		rc = EXC_ENUM_NOT_SUPPORTED;
58 	} else {
59 		for (; *list != NULL; list++) {
60 			struct ether_addr ea;
61 			struct ether_addr *e;
62 			char hostname[MAXHOSTNAMELEN + 1];
63 			char *hp;
64 			int	retval;
65 
66 			if ((e = ether_aton(*list)) != NULL) {
67 				hp = hostname;
68 				retval = ether_ntohost(hp, e);
69 			} else {
70 				hp = (char *)*list;
71 				e = &ea;
72 				retval = ether_hostton(hp, e);
73 			}
74 			if (retval != 0)
75 				rc = EXC_NAME_NOT_FOUND;
76 			else
77 				rc = putethers(hp, e, stdout);
78 		}
79 	}
80 
81 	return (rc);
82 }
83