Home | History | Annotate | Line # | Download | only in stdhosts
stdhosts.c revision 1.4
      1 /*	$NetBSD: stdhosts.c,v 1.4 1997/10/07 14:56:11 lukem Exp $	 */
      2 
      3 /*
      4  * Copyright (c) 1994 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/socket.h>
     36 #include <netinet/in.h>
     37 #include <arpa/inet.h>
     38 #include <ctype.h>
     39 #include <err.h>
     40 #include <limits.h>
     41 #include <stdio.h>
     42 #include <string.h>
     43 
     44 #include "protos.h"
     45 
     46 int	main __P((int, char *[]));
     47 void	usage __P((void));
     48 
     49 extern	char *__progname;	/* from crt0.o */
     50 
     51 int
     52 main(argc, argv)
     53 	int argc;
     54 	char *argv[];
     55 {
     56 	FILE *data_file;
     57 	char data_line[_POSIX2_LINE_MAX];
     58 	int line_no = 0, len;
     59 	char *k, *v, *addr_string, *fname;
     60 	struct in_addr host_addr;
     61 
     62 	addr_string = NULL;		/* XXX gcc -Wuninitialized */
     63 
     64 	if (argc > 2)
     65 		usage();
     66 
     67 	if (argc == 2) {
     68 		fname = argv[1];
     69 		data_file = fopen(fname, "r");
     70 		if (data_file == NULL)
     71 			err(1, "%s", fname);
     72 	} else {
     73 		fname = "<stdin>";
     74 		data_file = stdin;
     75 	}
     76 
     77 	while (read_line(data_file, data_line, sizeof(data_line))) {
     78 		line_no++;
     79 		len = strlen(data_line);
     80 
     81 		if (len <= 1 || data_line[0] == '#')
     82 			continue;
     83 
     84 		/*
     85 		 * Check if we have the whole line
     86 		 */
     87 		if (data_line[len - 1] != '\n') {
     88 			warnx("%s line %d: line to long, skipping",
     89 			    fname, line_no);
     90 			continue;
     91 		} else
     92 			data_line[len - 1] = '\0';
     93 
     94 		v = data_line;
     95 
     96 		/* Find the key, replace trailing whitespace will <NUL> */
     97 		for (k = v; isspace(*v) == 0; v++)
     98 			/*null*/;
     99 		while (isspace(*v))
    100 			*v++ = '\0';
    101 
    102 		if (inet_aton(k, &host_addr) == 0 ||
    103 		    (addr_string = inet_ntoa(host_addr)) == NULL)
    104 			warnx("%s line %d: syntax error", fname, line_no);
    105 
    106 		printf("%s %s\n", addr_string, v);
    107 	}
    108 
    109 	exit(0);
    110 }
    111 
    112 void
    113 usage()
    114 {
    115 
    116 	fprintf(stderr, "usage: %s [file]\n", __progname);
    117 	exit(1);
    118 }
    119