xref: /original-bsd/old/mkhosts/mkhosts.c (revision 57124d5e)
1 /*
2  * Copyright (c) 1983 Regents of the University of California.
3  * All rights reserved.  The Berkeley software License Agreement
4  * specifies the terms and conditions for redistribution.
5  */
6 
7 #ifndef lint
8 char copyright[] =
9 "@(#) Copyright (c) 1980 Regents of the University of California.\n\
10  All rights reserved.\n";
11 #endif not lint
12 
13 #ifndef lint
14 static char sccsid[] = "@(#)mkhosts.c	5.1 (Berkeley) 05/28/85";
15 #endif not lint
16 
17 #include <sys/file.h>
18 #include <stdio.h>
19 #include <netdb.h>
20 #include <ndbm.h>
21 
22 char	buf[BUFSIZ];
23 
24 main(argc, argv)
25 	char *argv[];
26 {
27 	DBM *dp;
28 	register struct hostent *hp;
29 	datum key, content;
30 	register char *cp, *tp, **sp;
31 	register int *nap;
32 	int naliases;
33 	int verbose = 0, entries = 0, maxlen = 0, error = 0;
34 	char tempname[BUFSIZ], newname[BUFSIZ];
35 
36 	if (argc > 1 && strcmp(argv[1], "-v") == 0) {
37 		verbose++;
38 		argv++, argc--;
39 	}
40 	if (argc != 2) {
41 		fprintf(stderr, "usage: mkhosts [ -v ] file\n");
42 		exit(1);
43 	}
44 	if (access(argv[1], R_OK) < 0) {
45 		perror(argv[1]);
46 		exit(1);
47 	}
48 	umask(0);
49 
50 	sprintf(tempname, "%s.new", argv[1]);
51 	dp = dbm_open(tempname, O_WRONLY|O_CREAT|O_EXCL, 0644);
52 	if (dp == NULL) {
53 		fprintf(stderr, "dbm_open failed: ");
54 		perror(argv[1]);
55 		exit(1);
56 	}
57 	sethostfile(argv[1]);
58 	sethostent(1);
59 	while (hp = gethostent()) {
60 		cp = buf;
61 		tp = hp->h_name;
62 		while (*cp++ = *tp++)
63 			;
64 		nap = (int *)cp;
65 		cp += sizeof (int);
66 		naliases = 0;
67 		for (sp = hp->h_aliases; *sp; sp++) {
68 			tp = *sp;
69 			while (*cp++ = *tp++)
70 				;
71 			naliases++;
72 		}
73 		bcopy((char *)&naliases, (char *)nap, sizeof(int));
74 		bcopy((char *)&hp->h_addrtype, cp, sizeof (int));
75 		cp += sizeof (int);
76 		bcopy((char *)&hp->h_length, cp, sizeof (int));
77 		cp += sizeof (int);
78 		bcopy(hp->h_addr, cp, hp->h_length);
79 		cp += hp->h_length;
80 		content.dptr = buf;
81 		content.dsize = cp - buf;
82 		if (verbose)
83 			printf("store %s, %d aliases\n", hp->h_name, naliases);
84 		key.dptr = hp->h_name;
85 		key.dsize = strlen(hp->h_name);
86 		if (dbm_store(dp, key, content, DBM_INSERT) < 0) {
87 			perror(hp->h_name);
88 			goto err;
89 		}
90 		for (sp = hp->h_aliases; *sp; sp++) {
91 			key.dptr = *sp;
92 			key.dsize = strlen(*sp);
93 			if (dbm_store(dp, key, content, DBM_INSERT) < 0) {
94 				perror(*sp);
95 				goto err;
96 			}
97 		}
98 		key.dptr = hp->h_addr;
99 		key.dsize = hp->h_length;
100 		if (dbm_store(dp, key, content, DBM_INSERT) < 0) {
101 			perror("dbm_store host address");
102 			goto err;
103 		}
104 		entries++;
105 		if (cp - buf > maxlen)
106 			maxlen = cp - buf;
107 	}
108 	endhostent();
109 	dbm_close(dp);
110 
111 	sprintf(tempname, "%s.new.pag", argv[1]);
112 	sprintf(newname, "%s.pag", argv[1]);
113 	if (rename(tempname, newname) < 0) {
114 		perror("rename .pag");
115 		exit(1);
116 	}
117 	sprintf(tempname, "%s.new.dir", argv[1]);
118 	sprintf(newname, "%s.dir", argv[1]);
119 	if (rename(tempname, newname) < 0) {
120 		perror("rename .dir");
121 		exit(1);
122 	}
123 	printf("%d host entries, maximum length %d\n", entries, maxlen);
124 	exit(0);
125 err:
126 	sprintf(tempname, "%s.new.pag", argv[1]);
127 	unlink(tempname);
128 	sprintf(tempname, "%s.new.dir", argv[1]);
129 	unlink(tempname);
130 	exit(1);
131 }
132