xref: /netbsd-src/usr.sbin/ypserv/stdethers/stdethers.c (revision 1394f01b4a9e99092957ca5d824d67219565d9b5)
1 /*	$NetBSD: stdethers.c,v 1.2 1997/03/15 18:38:02 is Exp $	*/
2 
3 /*
4  * Copyright (c) 1995 Mats O Jansson <moj@stacken.kth.se>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by Mats O Jansson
18  * 4. The name of the author may not be used to endorse or promote products
19  *    derived from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
22  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
25  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #include <sys/types.h>
35 #include <sys/param.h>
36 #include <sys/socket.h>
37 #include <net/if.h>
38 #include <net/if_ether.h>
39 #include <netinet/in.h>
40 #ifndef NTOA_FIX	/* XXX the below is wrong with old ARP code */
41 #include <net/if_ether.h>
42 #endif
43 #include <ctype.h>
44 #include <err.h>
45 #include <limits.h>
46 #include <stdio.h>
47 #include <string.h>
48 
49 #include "protos.h"
50 
51 void	usage __P((void));
52 
53 extern	char *__progname;		/* from crt0.o */
54 
55 #ifndef NTOA_FIX
56 #define	NTOA(x) ether_ntoa(x)
57 #else
58 char	*working_ntoa __P((struct ether_addr *));
59 
60 #define NTOA(x) working_ntoa(x)
61 
62 /*
63  * As of 1995-12-02 NetBSD has an SunOS 4 incompatible ether_ntoa.
64  * The code in usr/lib/libc/net/ethers seems to do the correct thing
65  * when asking YP but not when returning string from ether_ntoa.
66  */
67 
68 char *
69 working_ntoa(e)
70 	struct ether_addr *e;
71 {
72 	static char a[] = "xx:xx:xx:xx:xx:xx";
73 
74 	sprintf(a, "%x:%x:%x:%x:%x:%x",
75 	    e->ether_addr_octet[0], e->ether_addr_octet[1],
76 	    e->ether_addr_octet[2], e->ether_addr_octet[3],
77 	    e->ether_addr_octet[4], e->ether_addr_octet[5]);
78 	return a;
79 }
80 #endif
81 
82 int
83 main(argc, argv)
84 	int argc;
85 	char **argv;
86 {
87 	FILE *data_file;
88 	char data_line[_POSIX2_LINE_MAX];
89 	int line_no = 0, len;
90 	char *fname;
91 	struct ether_addr eth_addr;
92 	char hostname[MAXHOSTNAMELEN];
93 
94 	if (argc > 2)
95 		usage();
96 
97 	if (argc == 2) {
98 		fname = argv[1];
99 		data_file = fopen(fname, "r");
100 		if (data_file == NULL)
101 			err(1, "%s", fname);
102 	} else {
103 		fname = "<stdin>";
104 		data_file = stdin;
105 	}
106 
107 	while (read_line(data_file, data_line, sizeof(data_line))) {
108 		line_no++;
109 		len = strlen(data_line);
110 
111 		if (len > 1) {
112 			if (data_line[0] == '#') {
113 				continue;
114 			}
115 		} else
116 			continue;
117 
118 		/*
119 		 * Check if we have the whole line
120 		 */
121 		if (data_line[len - 1] != '\n') {
122 			warnx("%s line %d: line to long, skipping",
123 			    fname, line_no);
124 			continue;
125 		} else
126 			data_line[len - 1] = '\0';
127 
128 		if (ether_line(data_line, &eth_addr, hostname) == 0)
129 			printf("%s\t%s\n", NTOA(&eth_addr), hostname);
130 		else
131 			warnx("ignoring line %d: `%s'", line_no, data_line);
132 	}
133 
134 	exit(0);
135 }
136 
137 void
138 usage()
139 {
140 
141 	fprintf(stderr, "usage: %s [file]\n", __progname);
142 	exit(1);
143 }
144