xref: /csrg-svn/old/mkhosts/mkhosts.c (revision 35881)
121147Sdist /*
2*35881Sbostic  * Copyright (c) 1983 The Regents of the University of California.
3*35881Sbostic  * All rights reserved.
4*35881Sbostic  *
5*35881Sbostic  * Redistribution and use in source and binary forms are permitted
6*35881Sbostic  * provided that the above copyright notice and this paragraph are
7*35881Sbostic  * duplicated in all such forms and that any documentation,
8*35881Sbostic  * advertising materials, and other materials related to such
9*35881Sbostic  * distribution and use acknowledge that the software was developed
10*35881Sbostic  * by the University of California, Berkeley.  The name of the
11*35881Sbostic  * University may not be used to endorse or promote products derived
12*35881Sbostic  * from this software without specific prior written permission.
13*35881Sbostic  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14*35881Sbostic  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15*35881Sbostic  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1621147Sdist  */
1721147Sdist 
1815666Sralph #ifndef lint
1921147Sdist char copyright[] =
20*35881Sbostic "@(#) Copyright (c) 1983 The Regents of the University of California.\n\
2121147Sdist  All rights reserved.\n";
22*35881Sbostic #endif /* not lint */
2315666Sralph 
2421147Sdist #ifndef lint
25*35881Sbostic static char sccsid[] = "@(#)mkhosts.c	5.3 (Berkeley) 10/19/88";
26*35881Sbostic #endif /* not lint */
2721147Sdist 
2815666Sralph #include <sys/file.h>
2915666Sralph #include <stdio.h>
3015666Sralph #include <netdb.h>
3115666Sralph #include <ndbm.h>
3215666Sralph 
3315666Sralph char	buf[BUFSIZ];
3415666Sralph 
3515666Sralph main(argc, argv)
3615666Sralph 	char *argv[];
3715666Sralph {
3815666Sralph 	DBM *dp;
3915666Sralph 	register struct hostent *hp;
4015666Sralph 	datum key, content;
4115666Sralph 	register char *cp, *tp, **sp;
4218004Sbloom 	register int *nap;
4318004Sbloom 	int naliases;
4417026Sralph 	int verbose = 0, entries = 0, maxlen = 0, error = 0;
4517026Sralph 	char tempname[BUFSIZ], newname[BUFSIZ];
4615666Sralph 
4715666Sralph 	if (argc > 1 && strcmp(argv[1], "-v") == 0) {
4815666Sralph 		verbose++;
4915666Sralph 		argv++, argc--;
5015666Sralph 	}
5115666Sralph 	if (argc != 2) {
5215666Sralph 		fprintf(stderr, "usage: mkhosts [ -v ] file\n");
5315666Sralph 		exit(1);
5415666Sralph 	}
5516509Sralph 	if (access(argv[1], R_OK) < 0) {
5616509Sralph 		perror(argv[1]);
5716509Sralph 		exit(1);
5816509Sralph 	}
5915666Sralph 	umask(0);
6017026Sralph 
6132443Sbostic 	(void)sprintf(tempname, "%s.new", argv[1]);
6217026Sralph 	dp = dbm_open(tempname, O_WRONLY|O_CREAT|O_EXCL, 0644);
6315666Sralph 	if (dp == NULL) {
6417026Sralph 		fprintf(stderr, "dbm_open failed: ");
6515666Sralph 		perror(argv[1]);
6615666Sralph 		exit(1);
6715666Sralph 	}
6816509Sralph 	sethostfile(argv[1]);
6915666Sralph 	sethostent(1);
7015666Sralph 	while (hp = gethostent()) {
7115666Sralph 		cp = buf;
7215666Sralph 		tp = hp->h_name;
7315666Sralph 		while (*cp++ = *tp++)
7415666Sralph 			;
7515666Sralph 		nap = (int *)cp;
7615666Sralph 		cp += sizeof (int);
7715666Sralph 		naliases = 0;
7815666Sralph 		for (sp = hp->h_aliases; *sp; sp++) {
7915666Sralph 			tp = *sp;
8015666Sralph 			while (*cp++ = *tp++)
8115666Sralph 				;
8215666Sralph 			naliases++;
8315666Sralph 		}
8418004Sbloom 		bcopy((char *)&naliases, (char *)nap, sizeof(int));
8515666Sralph 		bcopy((char *)&hp->h_addrtype, cp, sizeof (int));
8615666Sralph 		cp += sizeof (int);
8715666Sralph 		bcopy((char *)&hp->h_length, cp, sizeof (int));
8815666Sralph 		cp += sizeof (int);
8915666Sralph 		bcopy(hp->h_addr, cp, hp->h_length);
9015666Sralph 		cp += hp->h_length;
9115666Sralph 		content.dptr = buf;
9215666Sralph 		content.dsize = cp - buf;
9315666Sralph 		if (verbose)
9415666Sralph 			printf("store %s, %d aliases\n", hp->h_name, naliases);
9515666Sralph 		key.dptr = hp->h_name;
9615666Sralph 		key.dsize = strlen(hp->h_name);
9717026Sralph 		if (dbm_store(dp, key, content, DBM_INSERT) < 0) {
9817026Sralph 			perror(hp->h_name);
9917026Sralph 			goto err;
10017026Sralph 		}
10115666Sralph 		for (sp = hp->h_aliases; *sp; sp++) {
10215666Sralph 			key.dptr = *sp;
10315666Sralph 			key.dsize = strlen(*sp);
10417026Sralph 			if (dbm_store(dp, key, content, DBM_INSERT) < 0) {
10517026Sralph 				perror(*sp);
10617026Sralph 				goto err;
10717026Sralph 			}
10815666Sralph 		}
10915666Sralph 		key.dptr = hp->h_addr;
11015666Sralph 		key.dsize = hp->h_length;
11117026Sralph 		if (dbm_store(dp, key, content, DBM_INSERT) < 0) {
11217026Sralph 			perror("dbm_store host address");
11317026Sralph 			goto err;
11417026Sralph 		}
11515666Sralph 		entries++;
11615666Sralph 		if (cp - buf > maxlen)
11715666Sralph 			maxlen = cp - buf;
11815666Sralph 	}
11915666Sralph 	endhostent();
12017026Sralph 	dbm_close(dp);
12117026Sralph 
12232443Sbostic 	(void)sprintf(tempname, "%s.new.pag", argv[1]);
12332443Sbostic 	(void)sprintf(newname, "%s.pag", argv[1]);
12417026Sralph 	if (rename(tempname, newname) < 0) {
12517026Sralph 		perror("rename .pag");
12617026Sralph 		exit(1);
12717026Sralph 	}
12832443Sbostic 	(void)sprintf(tempname, "%s.new.dir", argv[1]);
12932443Sbostic 	(void)sprintf(newname, "%s.dir", argv[1]);
13017026Sralph 	if (rename(tempname, newname) < 0) {
13117026Sralph 		perror("rename .dir");
13217026Sralph 		exit(1);
13317026Sralph 	}
13415666Sralph 	printf("%d host entries, maximum length %d\n", entries, maxlen);
13515666Sralph 	exit(0);
13617026Sralph err:
13732443Sbostic 	(void)sprintf(tempname, "%s.new.pag", argv[1]);
13817026Sralph 	unlink(tempname);
13932443Sbostic 	(void)sprintf(tempname, "%s.new.dir", argv[1]);
14017026Sralph 	unlink(tempname);
14117026Sralph 	exit(1);
14215666Sralph }
143