Home | History | Annotate | Line # | Download | only in stdethers
stdethers.c revision 1.1
      1 /*	$NetBSD: stdethers.c,v 1.1 1996/08/09 10:14:57 thorpej Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1995 Mats O Jansson <moj (at) 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 <netinet/in.h>
     39 #include <netinet/if_ether.h>
     40 #include <ctype.h>
     41 #include <err.h>
     42 #include <limits.h>
     43 #include <stdio.h>
     44 #include <string.h>
     45 
     46 #include "protos.h"
     47 
     48 void	usage __P((void));
     49 
     50 extern	char *__progname;		/* from crt0.o */
     51 
     52 #ifndef NTOA_FIX
     53 #define	NTOA(x) ether_ntoa(x)
     54 #else
     55 char	*working_ntoa __P((struct ether_addr *));
     56 
     57 #define NTOA(x) working_ntoa(x)
     58 
     59 /*
     60  * As of 1995-12-02 NetBSD has an SunOS 4 incompatible ether_ntoa.
     61  * The code in usr/lib/libc/net/ethers seems to do the correct thing
     62  * when asking YP but not when returning string from ether_ntoa.
     63  */
     64 
     65 char *
     66 working_ntoa(e)
     67 	struct ether_addr *e;
     68 {
     69 	static char a[] = "xx:xx:xx:xx:xx:xx";
     70 
     71 	sprintf(a, "%x:%x:%x:%x:%x:%x",
     72 	    e->ether_addr_octet[0], e->ether_addr_octet[1],
     73 	    e->ether_addr_octet[2], e->ether_addr_octet[3],
     74 	    e->ether_addr_octet[4], e->ether_addr_octet[5]);
     75 	return a;
     76 }
     77 #endif
     78 
     79 int
     80 main(argc, argv)
     81 	int argc;
     82 	char **argv;
     83 {
     84 	FILE *data_file;
     85 	char data_line[_POSIX2_LINE_MAX];
     86 	int line_no = 0, len;
     87 	char *fname;
     88 	struct ether_addr eth_addr;
     89 	char hostname[MAXHOSTNAMELEN];
     90 
     91 	if (argc > 2)
     92 		usage();
     93 
     94 	if (argc == 2) {
     95 		fname = argv[1];
     96 		data_file = fopen(fname, "r");
     97 		if (data_file == NULL)
     98 			err(1, "%s", fname);
     99 	} else {
    100 		fname = "<stdin>";
    101 		data_file = stdin;
    102 	}
    103 
    104 	while (read_line(data_file, data_line, sizeof(data_line))) {
    105 		line_no++;
    106 		len = strlen(data_line);
    107 
    108 		if (len > 1) {
    109 			if (data_line[0] == '#') {
    110 				continue;
    111 			}
    112 		} else
    113 			continue;
    114 
    115 		/*
    116 		 * Check if we have the whole line
    117 		 */
    118 		if (data_line[len - 1] != '\n') {
    119 			warnx("%s line %d: line to long, skipping",
    120 			    fname, line_no);
    121 			continue;
    122 		} else
    123 			data_line[len - 1] = '\0';
    124 
    125 		if (ether_line(data_line, &eth_addr, hostname) == 0)
    126 			printf("%s\t%s\n", NTOA(&eth_addr), hostname);
    127 		else
    128 			warnx("ignoring line %d: `%s'", line_no, data_line);
    129 	}
    130 
    131 	exit(0);
    132 }
    133 
    134 void
    135 usage()
    136 {
    137 
    138 	fprintf(stderr, "usage: %s [file]\n", __progname);
    139 	exit(1);
    140 }
    141