1/*
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * "Portions Copyright (c) 1999 Apple Computer, Inc.  All Rights
7 * Reserved.  This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.0 (the 'License').  You may not use this file
10 * except in compliance with the License.  Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
12 * this file.
13 *
14 * The Original Code and all software distributed under the License are
15 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT.  Please see the
19 * License for the specific language governing rights and limitations
20 * under the License."
21 *
22 * @APPLE_LICENSE_HEADER_END@
23 */
24/*	$OpenBSD: stdethers.c,v 1.3 1997/09/11 19:47:33 deraadt Exp $ */
25
26/*
27 * Copyright (c) 1995 Mats O Jansson <moj@stacken.kth.se>
28 * All rights reserved.
29 *
30 * Redistribution and use in source and binary forms, with or without
31 * modification, are permitted provided that the following conditions
32 * are met:
33 * 1. Redistributions of source code must retain the above copyright
34 *    notice, this list of conditions and the following disclaimer.
35 * 2. Redistributions in binary form must reproduce the above copyright
36 *    notice, this list of conditions and the following disclaimer in the
37 *    documentation and/or other materials provided with the distribution.
38 * 3. All advertising materials mentioning features or use of this software
39 *    must display the following acknowledgement:
40 *	This product includes software developed by Mats O Jansson
41 * 4. The name of the author may not be used to endorse or promote products
42 *    derived from this software without specific prior written permission.
43 *
44 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
45 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
46 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
47 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
48 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
49 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
50 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
51 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
52 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
53 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
54 * SUCH DAMAGE.
55 */
56
57#include <sys/cdefs.h>
58#ifndef LINT
59__unused static char rcsid[] = "$OpenBSD: stdethers.c,v 1.3 1997/09/11 19:47:33 deraadt Exp $";
60#endif
61
62#include <sys/types.h>
63#include <sys/socket.h>
64#include <net/if.h>
65#include <netinet/in.h>
66#include <netinet/if_ether.h>
67#include <stdio.h>
68#include <stdlib.h>
69#include <string.h>
70#include <ctype.h>
71
72char *ProgramName = "stdethers";
73
74#ifndef NTOA_FIX
75#define	NTOA(x) (char *)ether_ntoa(x)
76#else
77#define NTOA(x) (char *) working_ntoa((u_char *) x)
78
79/* As of 1995-12-02 NetBSD and OpenBSD has an SunOS 4 incompatible ether_ntoa.
80   The code in usr/lib/libc/net/ethers seems to do the correct thing
81   when asking YP but not when returning string from ether_ntoa.
82 */
83
84char *
85working_ntoa(e)
86	u_char	*e;
87{
88	static char a[] = "xx:xx:xx:xx:xx:xx";
89
90	sprintf(a, "%x:%x:%x:%x:%x:%x",
91		e[0], e[1], e[2], e[3], e[4], e[5]);
92	return a;
93}
94#endif
95
96static int read_line(fp, buf, size)
97FILE *fp;
98char *buf;
99int size;
100{
101	int done = 0;
102
103	do {
104		while (fgets(buf, size, fp)) {
105			int len = strlen(buf);
106			done += len;
107			if (len > 1 && buf[len-2] == '\\' &&
108					buf[len-1] == '\n') {
109				int ch;
110				buf += len - 2;
111				size -= len - 2;
112				*buf = '\n'; buf[1] = '\0';
113				/*
114				 * Skip leading white space on next line
115				 */
116				while ((ch = getc(fp)) != EOF &&
117					isascii(ch) && isspace(ch))
118						;
119				(void) ungetc(ch, fp);
120			} else {
121				return done;
122			}
123		}
124	} while (size > 0 && !feof(fp));
125
126	return done;
127}
128
129int
130main (argc,argv)
131int argc;
132char *argv[];
133{
134	FILE	*data_file;
135	char	 data_line[1024];
136	int	 usage = 0;
137	int	 line_no = 0;
138	int	 len;
139	char	*p,*k,*v;
140	struct ether_addr eth_addr;
141	char	 hostname[256];
142
143	if (argc > 2) {
144		usage++;
145	}
146
147	if (usage) {
148		fprintf(stderr,
149			"usage: %s [file]\n",
150			ProgramName);
151		exit(1);
152	}
153
154	if (argc == 2) {
155		data_file = fopen(argv[1], "r");
156		if (data_file == NULL) {
157			fprintf(stderr,
158				"%s: can't open %s\n",
159				ProgramName,
160				argv[1]);
161			exit(1);
162		}
163	} else {
164		data_file = stdin;
165	}
166
167	while (read_line(data_file,data_line,sizeof(data_line))) {
168
169		line_no++;
170		len = strlen(data_line);
171
172		if (len > 0) {
173			if (data_line[0] == '#')
174				continue;
175		}
176
177		/*
178		 * Check if we have the whole line
179		 */
180
181		if (data_line[len-1] != '\n') {
182			if (argc == 2) {
183				fprintf(stderr,
184					"line %d in \"%s\" is too long",
185					line_no, argv[1]);
186			} else {
187				fprintf(stderr,
188					"line %d in \"stdin\" is too long",
189					line_no);
190			}
191		} else {
192			data_line[len-1] = '\0';
193		}
194
195		p = (char *) &data_line;
196
197		k  = p;				/* save start of key */
198		while (!isspace(*p)) { p++; };	/* find first "space" */
199		while (isspace(*p)) { p++; };	/* move over "space" */
200
201		v = p;				/* save start of value */
202		while(*p != '\0') { p++; };	/* find end of string */
203
204		if (ether_line(data_line, &eth_addr, hostname) == 0) {
205			fprintf(stdout, "%s\t%s\n",
206				NTOA(&eth_addr),
207				hostname);
208		} else {
209			fprintf(stderr,
210				"%s: ignoring line %d: \"%s\"\n",
211				ProgramName,
212				line_no,
213				data_line);
214		}
215	}
216
217	return(0);
218
219}
220