xref: /csrg-svn/old/mkhosts/mkhosts.c (revision 32443)
121147Sdist /*
221147Sdist  * Copyright (c) 1983 Regents of the University of California.
321147Sdist  * All rights reserved.  The Berkeley software License Agreement
421147Sdist  * specifies the terms and conditions for redistribution.
521147Sdist  */
621147Sdist 
715666Sralph #ifndef lint
821147Sdist char copyright[] =
921147Sdist "@(#) Copyright (c) 1980 Regents of the University of California.\n\
1021147Sdist  All rights reserved.\n";
1121147Sdist #endif not lint
1215666Sralph 
1321147Sdist #ifndef lint
14*32443Sbostic static char sccsid[] = "@(#)mkhosts.c	5.2 (Berkeley) 10/22/87";
1521147Sdist #endif not lint
1621147Sdist 
1715666Sralph #include <sys/file.h>
1815666Sralph #include <stdio.h>
1915666Sralph #include <netdb.h>
2015666Sralph #include <ndbm.h>
2115666Sralph 
2215666Sralph char	buf[BUFSIZ];
2315666Sralph 
2415666Sralph main(argc, argv)
2515666Sralph 	char *argv[];
2615666Sralph {
2715666Sralph 	DBM *dp;
2815666Sralph 	register struct hostent *hp;
2915666Sralph 	datum key, content;
3015666Sralph 	register char *cp, *tp, **sp;
3118004Sbloom 	register int *nap;
3218004Sbloom 	int naliases;
3317026Sralph 	int verbose = 0, entries = 0, maxlen = 0, error = 0;
3417026Sralph 	char tempname[BUFSIZ], newname[BUFSIZ];
3515666Sralph 
3615666Sralph 	if (argc > 1 && strcmp(argv[1], "-v") == 0) {
3715666Sralph 		verbose++;
3815666Sralph 		argv++, argc--;
3915666Sralph 	}
4015666Sralph 	if (argc != 2) {
4115666Sralph 		fprintf(stderr, "usage: mkhosts [ -v ] file\n");
4215666Sralph 		exit(1);
4315666Sralph 	}
4416509Sralph 	if (access(argv[1], R_OK) < 0) {
4516509Sralph 		perror(argv[1]);
4616509Sralph 		exit(1);
4716509Sralph 	}
4815666Sralph 	umask(0);
4917026Sralph 
50*32443Sbostic 	(void)sprintf(tempname, "%s.new", argv[1]);
5117026Sralph 	dp = dbm_open(tempname, O_WRONLY|O_CREAT|O_EXCL, 0644);
5215666Sralph 	if (dp == NULL) {
5317026Sralph 		fprintf(stderr, "dbm_open failed: ");
5415666Sralph 		perror(argv[1]);
5515666Sralph 		exit(1);
5615666Sralph 	}
5716509Sralph 	sethostfile(argv[1]);
5815666Sralph 	sethostent(1);
5915666Sralph 	while (hp = gethostent()) {
6015666Sralph 		cp = buf;
6115666Sralph 		tp = hp->h_name;
6215666Sralph 		while (*cp++ = *tp++)
6315666Sralph 			;
6415666Sralph 		nap = (int *)cp;
6515666Sralph 		cp += sizeof (int);
6615666Sralph 		naliases = 0;
6715666Sralph 		for (sp = hp->h_aliases; *sp; sp++) {
6815666Sralph 			tp = *sp;
6915666Sralph 			while (*cp++ = *tp++)
7015666Sralph 				;
7115666Sralph 			naliases++;
7215666Sralph 		}
7318004Sbloom 		bcopy((char *)&naliases, (char *)nap, sizeof(int));
7415666Sralph 		bcopy((char *)&hp->h_addrtype, cp, sizeof (int));
7515666Sralph 		cp += sizeof (int);
7615666Sralph 		bcopy((char *)&hp->h_length, cp, sizeof (int));
7715666Sralph 		cp += sizeof (int);
7815666Sralph 		bcopy(hp->h_addr, cp, hp->h_length);
7915666Sralph 		cp += hp->h_length;
8015666Sralph 		content.dptr = buf;
8115666Sralph 		content.dsize = cp - buf;
8215666Sralph 		if (verbose)
8315666Sralph 			printf("store %s, %d aliases\n", hp->h_name, naliases);
8415666Sralph 		key.dptr = hp->h_name;
8515666Sralph 		key.dsize = strlen(hp->h_name);
8617026Sralph 		if (dbm_store(dp, key, content, DBM_INSERT) < 0) {
8717026Sralph 			perror(hp->h_name);
8817026Sralph 			goto err;
8917026Sralph 		}
9015666Sralph 		for (sp = hp->h_aliases; *sp; sp++) {
9115666Sralph 			key.dptr = *sp;
9215666Sralph 			key.dsize = strlen(*sp);
9317026Sralph 			if (dbm_store(dp, key, content, DBM_INSERT) < 0) {
9417026Sralph 				perror(*sp);
9517026Sralph 				goto err;
9617026Sralph 			}
9715666Sralph 		}
9815666Sralph 		key.dptr = hp->h_addr;
9915666Sralph 		key.dsize = hp->h_length;
10017026Sralph 		if (dbm_store(dp, key, content, DBM_INSERT) < 0) {
10117026Sralph 			perror("dbm_store host address");
10217026Sralph 			goto err;
10317026Sralph 		}
10415666Sralph 		entries++;
10515666Sralph 		if (cp - buf > maxlen)
10615666Sralph 			maxlen = cp - buf;
10715666Sralph 	}
10815666Sralph 	endhostent();
10917026Sralph 	dbm_close(dp);
11017026Sralph 
111*32443Sbostic 	(void)sprintf(tempname, "%s.new.pag", argv[1]);
112*32443Sbostic 	(void)sprintf(newname, "%s.pag", argv[1]);
11317026Sralph 	if (rename(tempname, newname) < 0) {
11417026Sralph 		perror("rename .pag");
11517026Sralph 		exit(1);
11617026Sralph 	}
117*32443Sbostic 	(void)sprintf(tempname, "%s.new.dir", argv[1]);
118*32443Sbostic 	(void)sprintf(newname, "%s.dir", argv[1]);
11917026Sralph 	if (rename(tempname, newname) < 0) {
12017026Sralph 		perror("rename .dir");
12117026Sralph 		exit(1);
12217026Sralph 	}
12315666Sralph 	printf("%d host entries, maximum length %d\n", entries, maxlen);
12415666Sralph 	exit(0);
12517026Sralph err:
126*32443Sbostic 	(void)sprintf(tempname, "%s.new.pag", argv[1]);
12717026Sralph 	unlink(tempname);
128*32443Sbostic 	(void)sprintf(tempname, "%s.new.dir", argv[1]);
12917026Sralph 	unlink(tempname);
13017026Sralph 	exit(1);
13115666Sralph }
132