]> git.saurik.com Git - apple/network_cmds.git/blame - stdethers.tproj/stdethers.c
network_cmds-76.tar.gz
[apple/network_cmds.git] / stdethers.tproj / stdethers.c
CommitLineData
b7080c8e
A
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#ifndef LINT
58static char rcsid[] = "$OpenBSD: stdethers.c,v 1.3 1997/09/11 19:47:33 deraadt Exp $";
59#endif
60
61#include <sys/types.h>
62#include <sys/socket.h>
63#include <net/if.h>
64#include <netinet/in.h>
65#include <netinet/if_ether.h>
66#include <stdio.h>
67#include <string.h>
68#include <ctype.h>
69
70char *ProgramName = "stdethers";
71
72extern int ether_line(char *, struct ether_addr *, char *);
73extern char *ether_ntoa(struct ether_addr *);
74
75#ifndef NTOA_FIX
76#define NTOA(x) (char *)ether_ntoa(x)
77#else
78#define NTOA(x) (char *) working_ntoa((u_char *) x)
79
80/* As of 1995-12-02 NetBSD and OpenBSD has an SunOS 4 incompatible ether_ntoa.
81 The code in usr/lib/libc/net/ethers seems to do the correct thing
82 when asking YP but not when returning string from ether_ntoa.
83 */
84
85char *
86working_ntoa(e)
87 u_char *e;
88{
89 static char a[] = "xx:xx:xx:xx:xx:xx";
90
91 sprintf(a, "%x:%x:%x:%x:%x:%x",
92 e[0], e[1], e[2], e[3], e[4], e[5]);
93 return a;
94}
95#endif
96
97static int read_line(fp, buf, size)
98FILE *fp;
99char *buf;
100int size;
101{
102 int done = 0;
103
104 do {
105 while (fgets(buf, size, fp)) {
106 int len = strlen(buf);
107 done += len;
108 if (len > 1 && buf[len-2] == '\\' &&
109 buf[len-1] == '\n') {
110 int ch;
111 buf += len - 2;
112 size -= len - 2;
113 *buf = '\n'; buf[1] = '\0';
114 /*
115 * Skip leading white space on next line
116 */
117 while ((ch = getc(fp)) != EOF &&
118 isascii(ch) && isspace(ch))
119 ;
120 (void) ungetc(ch, fp);
121 } else {
122 return done;
123 }
124 }
125 } while (size > 0 && !feof(fp));
126
127 return done;
128}
129
130int
131main (argc,argv)
132int argc;
133char *argv[];
134{
135 FILE *data_file;
136 char data_line[1024];
137 int usage = 0;
138 int line_no = 0;
139 int len;
140 char *p,*k,*v;
141 struct ether_addr eth_addr;
142 char hostname[256];
143
144 if (argc > 2) {
145 usage++;
146 }
147
148 if (usage) {
149 fprintf(stderr,
150 "usage: %s [file]\n",
151 ProgramName);
152 exit(1);
153 }
154
155 if (argc == 2) {
156 data_file = fopen(argv[1], "r");
157 if (data_file == NULL) {
158 fprintf(stderr,
159 "%s: can't open %s\n",
160 ProgramName,
161 argv[1]);
162 exit(1);
163 }
164 } else {
165 data_file = stdin;
166 }
167
168 while (read_line(data_file,data_line,sizeof(data_line))) {
169
170 line_no++;
171 len = strlen(data_line);
172
173 if (len > 0) {
174 if (data_line[0] == '#')
175 continue;
176 }
177
178 /*
179 * Check if we have the whole line
180 */
181
182 if (data_line[len-1] != '\n') {
183 if (argc == 2) {
184 fprintf(stderr,
185 "line %d in \"%s\" is too long",
186 line_no, argv[1]);
187 } else {
188 fprintf(stderr,
189 "line %d in \"stdin\" is too long",
190 line_no);
191 }
192 } else {
193 data_line[len-1] = '\0';
194 }
195
196 p = (char *) &data_line;
197
198 k = p; /* save start of key */
199 while (!isspace(*p)) { p++; }; /* find first "space" */
200 while (isspace(*p)) { p++; }; /* move over "space" */
201
202 v = p; /* save start of value */
203 while(*p != '\0') { p++; }; /* find end of string */
204
205 if (ether_line(data_line, &eth_addr, hostname) == 0) {
206 fprintf(stdout, "%s\t%s\n",
207 NTOA(&eth_addr),
208 hostname);
209 } else {
210 fprintf(stderr,
211 "%s: ignoring line %d: \"%s\"\n",
212 ProgramName,
213 line_no,
214 data_line);
215 }
216 }
217
218 return(0);
219
220}