xref: /titanic_53/usr/src/cmd/ypcmd/stdhosts.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright (c) 1985-1999 by Sun Microsystems, Inc.
24*7c478bd9Sstevel@tonic-gate  * All rights reserved.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"	/* SMI4.1 1.7  */
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate #include <stdio.h>
30*7c478bd9Sstevel@tonic-gate #include <ndbm.h>
31*7c478bd9Sstevel@tonic-gate #include <netdb.h>
32*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
33*7c478bd9Sstevel@tonic-gate #include <sys/socket.h>
34*7c478bd9Sstevel@tonic-gate #include <netinet/in.h>
35*7c478bd9Sstevel@tonic-gate #include <arpa/inet.h>
36*7c478bd9Sstevel@tonic-gate #include <string.h>
37*7c478bd9Sstevel@tonic-gate #include <ctype.h>
38*7c478bd9Sstevel@tonic-gate #include <errno.h>
39*7c478bd9Sstevel@tonic-gate 
40*7c478bd9Sstevel@tonic-gate /*
41*7c478bd9Sstevel@tonic-gate  * Filter to convert both IPv4 and IPv6 addresses from /etc/hosts or
42*7c478bd9Sstevel@tonic-gate  * /etc/inet/ipnodes files.
43*7c478bd9Sstevel@tonic-gate  */
44*7c478bd9Sstevel@tonic-gate 
45*7c478bd9Sstevel@tonic-gate /*
46*7c478bd9Sstevel@tonic-gate  * Size of buffer for input lines. Add two bytes on input for newline
47*7c478bd9Sstevel@tonic-gate  * and terminating NULL. Note that the practical limit for data
48*7c478bd9Sstevel@tonic-gate  * storage in ndbm is (PBLKSIZ - 3 * sizeof (short)). Though this
49*7c478bd9Sstevel@tonic-gate  * differs from spec 1170 the common industry implementation does
50*7c478bd9Sstevel@tonic-gate  * conform to this slightly lower limit.
51*7c478bd9Sstevel@tonic-gate  */
52*7c478bd9Sstevel@tonic-gate 
53*7c478bd9Sstevel@tonic-gate #define	OUTPUTSIZ (PBLKSIZ - 3 * sizeof (short))
54*7c478bd9Sstevel@tonic-gate #define	INPUTSIZ (OUTPUTSIZ + 2)
55*7c478bd9Sstevel@tonic-gate 
56*7c478bd9Sstevel@tonic-gate static int ipv4 = -1;
57*7c478bd9Sstevel@tonic-gate static char *cmd;
58*7c478bd9Sstevel@tonic-gate int warn = 0;
59*7c478bd9Sstevel@tonic-gate 
60*7c478bd9Sstevel@tonic-gate static void verify_and_output(const char *key, char *value, int lineno);
61*7c478bd9Sstevel@tonic-gate 
62*7c478bd9Sstevel@tonic-gate void
63*7c478bd9Sstevel@tonic-gate usage()
64*7c478bd9Sstevel@tonic-gate {
65*7c478bd9Sstevel@tonic-gate 	fprintf(stderr, "stdhosts [-w] [-n] [in-file]\n");
66*7c478bd9Sstevel@tonic-gate 	fprintf(stderr, "\t-w\tprint malformed warning messages.\n");
67*7c478bd9Sstevel@tonic-gate 	exit(1);
68*7c478bd9Sstevel@tonic-gate }
69*7c478bd9Sstevel@tonic-gate 
70*7c478bd9Sstevel@tonic-gate main(argc, argv)
71*7c478bd9Sstevel@tonic-gate 	char **argv;
72*7c478bd9Sstevel@tonic-gate {
73*7c478bd9Sstevel@tonic-gate 	char line[INPUTSIZ];
74*7c478bd9Sstevel@tonic-gate 	char adr[INPUTSIZ];
75*7c478bd9Sstevel@tonic-gate 	char nadr[INET6_ADDRSTRLEN]; /* Contains normalised address */
76*7c478bd9Sstevel@tonic-gate 	const char *nadrp;	/* Pointer to the normalised address */
77*7c478bd9Sstevel@tonic-gate 	char *trailer;
78*7c478bd9Sstevel@tonic-gate 	char *commentp;		/* Pointer to comment character '#' */
79*7c478bd9Sstevel@tonic-gate 	int c;
80*7c478bd9Sstevel@tonic-gate 	FILE *fp;
81*7c478bd9Sstevel@tonic-gate 	int lineno = 0;		/* Input line counter */
82*7c478bd9Sstevel@tonic-gate 	struct in_addr in;	/* Used for normalising the IPv4 address */
83*7c478bd9Sstevel@tonic-gate 	struct in6_addr in6;	/* Used for normalising the IPv6 address */
84*7c478bd9Sstevel@tonic-gate 	char *fgetsp;		/* Holds return value for fgets() calls */
85*7c478bd9Sstevel@tonic-gate 	int endoffile = 0;	/* Set when end of file reached */
86*7c478bd9Sstevel@tonic-gate 
87*7c478bd9Sstevel@tonic-gate 	if (cmd = strrchr(argv[0], '/'))
88*7c478bd9Sstevel@tonic-gate 		++cmd;
89*7c478bd9Sstevel@tonic-gate 	else
90*7c478bd9Sstevel@tonic-gate 		cmd = argv[0];
91*7c478bd9Sstevel@tonic-gate 
92*7c478bd9Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "v:wn")) != -1) {
93*7c478bd9Sstevel@tonic-gate 		switch (c) {
94*7c478bd9Sstevel@tonic-gate 		case 'w':	/* Send warning messages to stderr */
95*7c478bd9Sstevel@tonic-gate 			warn = 1;
96*7c478bd9Sstevel@tonic-gate 			break;
97*7c478bd9Sstevel@tonic-gate 		case 'n':
98*7c478bd9Sstevel@tonic-gate 			ipv4 = 0;
99*7c478bd9Sstevel@tonic-gate 			break;
100*7c478bd9Sstevel@tonic-gate 		default:
101*7c478bd9Sstevel@tonic-gate 			usage();
102*7c478bd9Sstevel@tonic-gate 			exit(1);
103*7c478bd9Sstevel@tonic-gate 		}
104*7c478bd9Sstevel@tonic-gate 	}
105*7c478bd9Sstevel@tonic-gate 
106*7c478bd9Sstevel@tonic-gate 	if (optind < argc) {
107*7c478bd9Sstevel@tonic-gate 		fp = fopen(argv[optind], "r");
108*7c478bd9Sstevel@tonic-gate 		if (fp == NULL) {
109*7c478bd9Sstevel@tonic-gate 			fprintf(stderr, "%s: can't open %s\n",
110*7c478bd9Sstevel@tonic-gate 			    cmd, argv[optind]);
111*7c478bd9Sstevel@tonic-gate 			exit(1);
112*7c478bd9Sstevel@tonic-gate 		}
113*7c478bd9Sstevel@tonic-gate 	} else
114*7c478bd9Sstevel@tonic-gate 		fp = stdin;
115*7c478bd9Sstevel@tonic-gate 
116*7c478bd9Sstevel@tonic-gate 	while (!endoffile &&
117*7c478bd9Sstevel@tonic-gate 	    (fgetsp = fgets(line, sizeof (line), fp)) != NULL) {
118*7c478bd9Sstevel@tonic-gate 		lineno++;
119*7c478bd9Sstevel@tonic-gate 
120*7c478bd9Sstevel@tonic-gate 		/* Check for comments */
121*7c478bd9Sstevel@tonic-gate 		if ((commentp = strchr(line, '#')) != NULL) {
122*7c478bd9Sstevel@tonic-gate 			if ((line[strlen(line) - 1] != '\n') &&
123*7c478bd9Sstevel@tonic-gate 			    (strlen(line) >= (sizeof (line) - 1))) {
124*7c478bd9Sstevel@tonic-gate 				/*
125*7c478bd9Sstevel@tonic-gate 				 * Discard the remainder of the line
126*7c478bd9Sstevel@tonic-gate 				 * until the newline or EOF, then
127*7c478bd9Sstevel@tonic-gate 				 * continue to parse the line. Use
128*7c478bd9Sstevel@tonic-gate 				 * adr[] rather then line[] to
129*7c478bd9Sstevel@tonic-gate 				 * preserve the contents of line[].
130*7c478bd9Sstevel@tonic-gate 				 */
131*7c478bd9Sstevel@tonic-gate 				while ((fgetsp = fgets(adr, sizeof (adr),
132*7c478bd9Sstevel@tonic-gate 				    fp)) != NULL) {
133*7c478bd9Sstevel@tonic-gate 					if (adr[strlen(adr) - 1] == '\n')
134*7c478bd9Sstevel@tonic-gate 						break;
135*7c478bd9Sstevel@tonic-gate 				}
136*7c478bd9Sstevel@tonic-gate 				if (fgetsp == NULL)
137*7c478bd9Sstevel@tonic-gate 					endoffile = 1;
138*7c478bd9Sstevel@tonic-gate 			}
139*7c478bd9Sstevel@tonic-gate 			/* Terminate line[] at the comment character */
140*7c478bd9Sstevel@tonic-gate 			*commentp = '\0';
141*7c478bd9Sstevel@tonic-gate 		} else if ((line[strlen(line) - 1] != '\n') &&
142*7c478bd9Sstevel@tonic-gate 		    (strlen(line) >= (sizeof (line) - 1))) {
143*7c478bd9Sstevel@tonic-gate 			/*
144*7c478bd9Sstevel@tonic-gate 			 * Catch long lines but not if this is a short
145*7c478bd9Sstevel@tonic-gate 			 * line with no '\n' at the end of the input.
146*7c478bd9Sstevel@tonic-gate 			 */
147*7c478bd9Sstevel@tonic-gate 			if (warn)
148*7c478bd9Sstevel@tonic-gate 				fprintf(stderr,
149*7c478bd9Sstevel@tonic-gate 				    "%s: Warning: more than %d "
150*7c478bd9Sstevel@tonic-gate 				    "bytes on line %d, ignored\n",
151*7c478bd9Sstevel@tonic-gate 				    cmd, sizeof (line) - 2, lineno);
152*7c478bd9Sstevel@tonic-gate 			/*
153*7c478bd9Sstevel@tonic-gate 			 * Discard the remaining lines until the
154*7c478bd9Sstevel@tonic-gate 			 * newline or EOF.
155*7c478bd9Sstevel@tonic-gate 			 */
156*7c478bd9Sstevel@tonic-gate 			while ((fgetsp = fgets(line, sizeof (line),
157*7c478bd9Sstevel@tonic-gate 			    fp)) != NULL)
158*7c478bd9Sstevel@tonic-gate 				if (line[strlen(line) - 1] == '\n')
159*7c478bd9Sstevel@tonic-gate 					break;
160*7c478bd9Sstevel@tonic-gate 			if (fgetsp == NULL)
161*7c478bd9Sstevel@tonic-gate 				endoffile = 1;
162*7c478bd9Sstevel@tonic-gate 			continue;
163*7c478bd9Sstevel@tonic-gate 		}
164*7c478bd9Sstevel@tonic-gate 
165*7c478bd9Sstevel@tonic-gate 		if (sscanf(line, "%s", adr) != 1) { /* Blank line, ignore */
166*7c478bd9Sstevel@tonic-gate 			continue;
167*7c478bd9Sstevel@tonic-gate 		}
168*7c478bd9Sstevel@tonic-gate 
169*7c478bd9Sstevel@tonic-gate 		if ((trailer = strpbrk(line, " \t")) == NULL) {
170*7c478bd9Sstevel@tonic-gate 			if (warn)
171*7c478bd9Sstevel@tonic-gate 				fprintf(stderr,
172*7c478bd9Sstevel@tonic-gate 				    "%s: Warning: no host names on line %d, "
173*7c478bd9Sstevel@tonic-gate 				    "ignored\n", cmd, lineno);
174*7c478bd9Sstevel@tonic-gate 			continue;
175*7c478bd9Sstevel@tonic-gate 		}
176*7c478bd9Sstevel@tonic-gate 
177*7c478bd9Sstevel@tonic-gate 		/*
178*7c478bd9Sstevel@tonic-gate 		 * check for valid addresses
179*7c478bd9Sstevel@tonic-gate 		 *
180*7c478bd9Sstevel@tonic-gate 		 * Attempt an ipv4 conversion, this accepts all valid
181*7c478bd9Sstevel@tonic-gate 		 * ipv4 addresses including:
182*7c478bd9Sstevel@tonic-gate 		 *	d
183*7c478bd9Sstevel@tonic-gate 		 *	d.d
184*7c478bd9Sstevel@tonic-gate 		 *	d.d.d
185*7c478bd9Sstevel@tonic-gate 		 * Unfortunately inet_pton() doesn't recognise these.
186*7c478bd9Sstevel@tonic-gate 		 */
187*7c478bd9Sstevel@tonic-gate 
188*7c478bd9Sstevel@tonic-gate 		in.s_addr = inet_addr(adr);
189*7c478bd9Sstevel@tonic-gate 		if (-1 != (int)in.s_addr) {
190*7c478bd9Sstevel@tonic-gate 			/*
191*7c478bd9Sstevel@tonic-gate 			 * It's safe not to check return of NULL as
192*7c478bd9Sstevel@tonic-gate 			 * nadrp is checked for validity later.
193*7c478bd9Sstevel@tonic-gate 			 */
194*7c478bd9Sstevel@tonic-gate 			nadrp = inet_ntop(AF_INET, &in, nadr, sizeof (nadr));
195*7c478bd9Sstevel@tonic-gate 		} else {
196*7c478bd9Sstevel@tonic-gate 			nadrp = NULL; /* Not a valid IPv4 address */
197*7c478bd9Sstevel@tonic-gate 		}
198*7c478bd9Sstevel@tonic-gate 
199*7c478bd9Sstevel@tonic-gate 		if (ipv4) {
200*7c478bd9Sstevel@tonic-gate 			if (nadrp == NULL) {
201*7c478bd9Sstevel@tonic-gate 				if (warn)
202*7c478bd9Sstevel@tonic-gate 					fprintf(stderr,
203*7c478bd9Sstevel@tonic-gate 					    "%s: Warning: malformed address on"
204*7c478bd9Sstevel@tonic-gate 					    " line %d, ignored\n",
205*7c478bd9Sstevel@tonic-gate 					    cmd, lineno);
206*7c478bd9Sstevel@tonic-gate 				continue;
207*7c478bd9Sstevel@tonic-gate 			}
208*7c478bd9Sstevel@tonic-gate 		} else { /* v4 or v6 for ipnodes */
209*7c478bd9Sstevel@tonic-gate 			if (nadrp == NULL) {
210*7c478bd9Sstevel@tonic-gate 				if (inet_pton(AF_INET6, adr, &in6) == 1) {
211*7c478bd9Sstevel@tonic-gate 					nadrp = inet_ntop(AF_INET6, &in6,
212*7c478bd9Sstevel@tonic-gate 					    nadr, sizeof (nadr));
213*7c478bd9Sstevel@tonic-gate 				}
214*7c478bd9Sstevel@tonic-gate 				if (nadrp == NULL) { /* Invalid IPv6 too */
215*7c478bd9Sstevel@tonic-gate 					if (warn)
216*7c478bd9Sstevel@tonic-gate 						fprintf(stderr,
217*7c478bd9Sstevel@tonic-gate 						    "%s: Warning: malformed"
218*7c478bd9Sstevel@tonic-gate 						    " address on"
219*7c478bd9Sstevel@tonic-gate 						    " line %d, ignored\n",
220*7c478bd9Sstevel@tonic-gate 						    cmd, lineno);
221*7c478bd9Sstevel@tonic-gate 					continue;
222*7c478bd9Sstevel@tonic-gate 				}
223*7c478bd9Sstevel@tonic-gate 			}
224*7c478bd9Sstevel@tonic-gate 		}
225*7c478bd9Sstevel@tonic-gate 
226*7c478bd9Sstevel@tonic-gate 		verify_and_output(nadrp, trailer, lineno);
227*7c478bd9Sstevel@tonic-gate 
228*7c478bd9Sstevel@tonic-gate 	}	/* while */
229*7c478bd9Sstevel@tonic-gate 	exit(0);
230*7c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
231*7c478bd9Sstevel@tonic-gate }
232*7c478bd9Sstevel@tonic-gate 
233*7c478bd9Sstevel@tonic-gate /*
234*7c478bd9Sstevel@tonic-gate  * verify_and_output
235*7c478bd9Sstevel@tonic-gate  *
236*7c478bd9Sstevel@tonic-gate  * Builds and verifies the output key and value string
237*7c478bd9Sstevel@tonic-gate  *
238*7c478bd9Sstevel@tonic-gate  * It makes sure these rules are followed:
239*7c478bd9Sstevel@tonic-gate  *	key + separator + value <= OUTPUTSIZ (for ndbm)
240*7c478bd9Sstevel@tonic-gate  *	names <= MAXALIASES + 1, ie one canonical name + MAXALIASES aliases
241*7c478bd9Sstevel@tonic-gate  * It will also ignore everything after a '#' comment character
242*7c478bd9Sstevel@tonic-gate  */
243*7c478bd9Sstevel@tonic-gate static void
244*7c478bd9Sstevel@tonic-gate verify_and_output(const char *key, char *value, int lineno)
245*7c478bd9Sstevel@tonic-gate {
246*7c478bd9Sstevel@tonic-gate 	char *p;			/* General char pointer */
247*7c478bd9Sstevel@tonic-gate 	char *endp;			/* Points to the NULL at the end */
248*7c478bd9Sstevel@tonic-gate 	char *namep;			/* First character of a name */
249*7c478bd9Sstevel@tonic-gate 	char tmpbuf[OUTPUTSIZ+1];	/* Buffer before writing out */
250*7c478bd9Sstevel@tonic-gate 	char *tmpbufp = tmpbuf;		/* Current point in output string */
251*7c478bd9Sstevel@tonic-gate 	int n = 0;			/* Length of output */
252*7c478bd9Sstevel@tonic-gate 	int names = 0;			/* Number of names found */
253*7c478bd9Sstevel@tonic-gate 	int namelen;			/* Length of the name */
254*7c478bd9Sstevel@tonic-gate 
255*7c478bd9Sstevel@tonic-gate 	if (key) {		/* Just in case key is NULL */
256*7c478bd9Sstevel@tonic-gate 		n = strlen(key);
257*7c478bd9Sstevel@tonic-gate 		if (n > OUTPUTSIZ) {
258*7c478bd9Sstevel@tonic-gate 			if (warn)
259*7c478bd9Sstevel@tonic-gate 				fprintf(stderr,
260*7c478bd9Sstevel@tonic-gate 				    "%s: address too long on "
261*7c478bd9Sstevel@tonic-gate 				    "line %d, line discarded\n",
262*7c478bd9Sstevel@tonic-gate 				    cmd, lineno);
263*7c478bd9Sstevel@tonic-gate 			return;
264*7c478bd9Sstevel@tonic-gate 		}
265*7c478bd9Sstevel@tonic-gate 		memcpy(tmpbufp, key, n+1); /* Plus the '\0' */
266*7c478bd9Sstevel@tonic-gate 		tmpbufp += n;
267*7c478bd9Sstevel@tonic-gate 	}
268*7c478bd9Sstevel@tonic-gate 
269*7c478bd9Sstevel@tonic-gate 	if (value) {		/* Just in case value is NULL */
270*7c478bd9Sstevel@tonic-gate 		p = value;
271*7c478bd9Sstevel@tonic-gate 		if ((endp = strchr(value, '#')) == 0)	/* Ignore # comments */
272*7c478bd9Sstevel@tonic-gate 			endp = p + strlen(p);		/* Or endp = EOL */
273*7c478bd9Sstevel@tonic-gate 		do {
274*7c478bd9Sstevel@tonic-gate 			/*
275*7c478bd9Sstevel@tonic-gate 			 * Skip white space. Type conversion is
276*7c478bd9Sstevel@tonic-gate 			 * necessary to avoid unfortunate effects of
277*7c478bd9Sstevel@tonic-gate 			 * 8-bit characters appearing negative.
278*7c478bd9Sstevel@tonic-gate 			 */
279*7c478bd9Sstevel@tonic-gate 			while ((p < endp) && isspace((unsigned char)*p))
280*7c478bd9Sstevel@tonic-gate 				p++;
281*7c478bd9Sstevel@tonic-gate 
282*7c478bd9Sstevel@tonic-gate 			if (p == endp)	/* End of the string */
283*7c478bd9Sstevel@tonic-gate 				break;
284*7c478bd9Sstevel@tonic-gate 
285*7c478bd9Sstevel@tonic-gate 			names++;
286*7c478bd9Sstevel@tonic-gate 			if (names > (MAXALIASES+1)) { /* cname + MAXALIASES */
287*7c478bd9Sstevel@tonic-gate 				if (warn)
288*7c478bd9Sstevel@tonic-gate 					fprintf(stderr,
289*7c478bd9Sstevel@tonic-gate 					    "%s: Warning: too many "
290*7c478bd9Sstevel@tonic-gate 					    "host names on line %d, "
291*7c478bd9Sstevel@tonic-gate 					    "truncating\n",
292*7c478bd9Sstevel@tonic-gate 					    cmd, lineno);
293*7c478bd9Sstevel@tonic-gate 				break;
294*7c478bd9Sstevel@tonic-gate 			}
295*7c478bd9Sstevel@tonic-gate 
296*7c478bd9Sstevel@tonic-gate 			namep = p;
297*7c478bd9Sstevel@tonic-gate 			while ((p < endp) && !isspace((unsigned char)*p))
298*7c478bd9Sstevel@tonic-gate 				p++;
299*7c478bd9Sstevel@tonic-gate 
300*7c478bd9Sstevel@tonic-gate 			namelen = p - namep;
301*7c478bd9Sstevel@tonic-gate 			n += namelen + 1; /* single white space + name */
302*7c478bd9Sstevel@tonic-gate 			*p = '\0';	   /* Terminate the name string */
303*7c478bd9Sstevel@tonic-gate 			if (n > OUTPUTSIZ) {
304*7c478bd9Sstevel@tonic-gate 				if (warn)
305*7c478bd9Sstevel@tonic-gate 					fprintf(stderr,
306*7c478bd9Sstevel@tonic-gate 					    "%s: Warning: %d byte ndbm limit "
307*7c478bd9Sstevel@tonic-gate 					    "reached on line %d, truncating\n",
308*7c478bd9Sstevel@tonic-gate 					    cmd, OUTPUTSIZ, lineno);
309*7c478bd9Sstevel@tonic-gate 				break;
310*7c478bd9Sstevel@tonic-gate 			}
311*7c478bd9Sstevel@tonic-gate 
312*7c478bd9Sstevel@tonic-gate 			if (names == 1) /* First space is a '\t' */
313*7c478bd9Sstevel@tonic-gate 				*tmpbufp++ = '\t';
314*7c478bd9Sstevel@tonic-gate 			else
315*7c478bd9Sstevel@tonic-gate 				*tmpbufp++ = ' ';
316*7c478bd9Sstevel@tonic-gate 
317*7c478bd9Sstevel@tonic-gate 			memcpy(tmpbufp, namep, namelen+1); /* Plus the '\0' */
318*7c478bd9Sstevel@tonic-gate 			tmpbufp += namelen;
319*7c478bd9Sstevel@tonic-gate 
320*7c478bd9Sstevel@tonic-gate 			if (p < endp)
321*7c478bd9Sstevel@tonic-gate 				p++;	/* Skip the added NULL */
322*7c478bd9Sstevel@tonic-gate 
323*7c478bd9Sstevel@tonic-gate 		} while (p < endp);
324*7c478bd9Sstevel@tonic-gate 	}
325*7c478bd9Sstevel@tonic-gate 
326*7c478bd9Sstevel@tonic-gate 	if (names > 0) {
327*7c478bd9Sstevel@tonic-gate 		fputs(tmpbuf, stdout);
328*7c478bd9Sstevel@tonic-gate 		fputc('\n', stdout);
329*7c478bd9Sstevel@tonic-gate 	} else {
330*7c478bd9Sstevel@tonic-gate 		if (warn)
331*7c478bd9Sstevel@tonic-gate 			fprintf(stderr,
332*7c478bd9Sstevel@tonic-gate 			    "%s: Warning: no host names on line %d, "
333*7c478bd9Sstevel@tonic-gate 			    "ignored\n", cmd, lineno);
334*7c478bd9Sstevel@tonic-gate 	}
335*7c478bd9Sstevel@tonic-gate }
336