1 /*
2  * Copyright (c) 1990 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that: (1) source code distributions
7  * retain the above copyright notice and this paragraph in its entirety, (2)
8  * distributions including binary code include the above copyright notice and
9  * this paragraph in its entirety in the documentation or other materials
10  * provided with the distribution, and (3) all advertising materials mentioning
11  * features or use of this software display the following acknowledgement:
12  * ``This product includes software developed by the University of California,
13  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
14  * the University nor the names of its contributors may be used to endorse
15  * or promote products derived from this software without specific prior
16  * written permission.
17  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
18  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
19  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20  */
21 #ifndef lint
22 static char rcsid[] =
23     "@(#) $Header: etherent.c,v 1.2 90/09/20 23:16:06 mccanne Exp $ (LBL)";
24 #endif
25 
26 #include <stdio.h>
27 #include <ctype.h>
28 #include <sys/types.h>
29 #include "interface.h"
30 
31 #ifndef ETHER_SERVICE
32 
33 #include "etherent.h"
34 
35 /* Hex digit to integer. */
36 static inline int
37 xdtoi(c)
38 {
39 	if (isdigit(c))
40 		return c - '0';
41 	else if (islower(c))
42 		return c - 'a' + 10;
43 	else
44 		return c - 'A' + 10;
45 }
46 
47 static inline int
48 skip_space(f)
49 	FILE *f;
50 {
51 	int c;
52 
53 	do {
54 		c = getc(f);
55 	} while (isspace(c) && c != '\n');
56 
57 	return c;
58 }
59 
60 static inline int
61 skip_line(f)
62 	FILE *f;
63 {
64 	int c;
65 
66 	do
67 		c = getc(f);
68 	while (c != '\n' && c != EOF);
69 
70 	return c;
71 }
72 
73 struct etherent *
74 next_etherent(fp)
75 	FILE *fp;
76 {
77 	register int c, d, i;
78 	char *bp;
79 	static struct etherent e;
80 	static int nline = 1;
81  top:
82 	while (nline) {
83 		/* Find addr */
84 		c = skip_space(fp);
85 		if (c == '\n')
86 			continue;
87 		/* If this is a comment, or first thing on line
88 		   cannot be etehrnet address, skip the line. */
89 		else if (!isxdigit(c))
90 			c = skip_line(fp);
91 		else {
92 			/* must be the start of an address */
93 			for (i = 0; i < 6; i += 1) {
94 				d = xdtoi(c);
95 				c = getc(fp);
96 				if (c != ':') {
97 					d <<= 4;
98 					d |= xdtoi(c);
99 					c = getc(fp);
100 				}
101 				e.addr[i] = d;
102 				if (c != ':')
103 					break;
104 				c = getc(fp);
105 			}
106 			nline = 0;
107 		}
108 		if (c == EOF)
109 			return 0;
110 	}
111 
112 	/* If we started a new line, 'c' holds the char past the ether addr,
113 	   which we assume is white space.  If we are continuning a line,
114 	   'c' is garbage.  In either case, we can throw it away. */
115 
116 	c = skip_space(fp);
117 	if (c == '\n') {
118 		nline = 1;
119 		goto top;
120 	}
121 	else if (c == '#') {
122 		(void)skip_line(fp);
123 		nline = 1;
124 		goto top;
125 	}
126 	else if (c == EOF)
127 		return 0;
128 
129 	/* Must be a name. */
130 	bp = e.name;
131 	/* Use 'd' to prevent buffer overflow. */
132 	d = sizeof(e.name) - 1;
133 	do {
134 		*bp++ = c;
135 		c = getc(fp);
136 	} while (!isspace(c) && c != EOF && --d > 0);
137 	*bp = '\0';
138 	if (c == '\n')
139 		nline = 1;
140 
141 	return &e;
142 }
143 
144 #endif
145