xref: /original-bsd/old/mkhosts/mkhosts.c (revision a141c157)
1 /*
2  * Copyright (c) 1983 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
6  * provided that the above copyright notice and this paragraph are
7  * duplicated in all such forms and that any documentation,
8  * advertising materials, and other materials related to such
9  * distribution and use acknowledge that the software was developed
10  * by the University of California, Berkeley.  The name of the
11  * University may not be used to endorse or promote products derived
12  * from this software without specific prior written permission.
13  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16  */
17 
18 #ifndef lint
19 char copyright[] =
20 "@(#) Copyright (c) 1983 The Regents of the University of California.\n\
21  All rights reserved.\n";
22 #endif /* not lint */
23 
24 #ifndef lint
25 static char sccsid[] = "@(#)mkhosts.c	5.3 (Berkeley) 10/19/88";
26 #endif /* not lint */
27 
28 #include <sys/file.h>
29 #include <stdio.h>
30 #include <netdb.h>
31 #include <ndbm.h>
32 
33 char	buf[BUFSIZ];
34 
35 main(argc, argv)
36 	char *argv[];
37 {
38 	DBM *dp;
39 	register struct hostent *hp;
40 	datum key, content;
41 	register char *cp, *tp, **sp;
42 	register int *nap;
43 	int naliases;
44 	int verbose = 0, entries = 0, maxlen = 0, error = 0;
45 	char tempname[BUFSIZ], newname[BUFSIZ];
46 
47 	if (argc > 1 && strcmp(argv[1], "-v") == 0) {
48 		verbose++;
49 		argv++, argc--;
50 	}
51 	if (argc != 2) {
52 		fprintf(stderr, "usage: mkhosts [ -v ] file\n");
53 		exit(1);
54 	}
55 	if (access(argv[1], R_OK) < 0) {
56 		perror(argv[1]);
57 		exit(1);
58 	}
59 	umask(0);
60 
61 	(void)sprintf(tempname, "%s.new", argv[1]);
62 	dp = dbm_open(tempname, O_WRONLY|O_CREAT|O_EXCL, 0644);
63 	if (dp == NULL) {
64 		fprintf(stderr, "dbm_open failed: ");
65 		perror(argv[1]);
66 		exit(1);
67 	}
68 	sethostfile(argv[1]);
69 	sethostent(1);
70 	while (hp = gethostent()) {
71 		cp = buf;
72 		tp = hp->h_name;
73 		while (*cp++ = *tp++)
74 			;
75 		nap = (int *)cp;
76 		cp += sizeof (int);
77 		naliases = 0;
78 		for (sp = hp->h_aliases; *sp; sp++) {
79 			tp = *sp;
80 			while (*cp++ = *tp++)
81 				;
82 			naliases++;
83 		}
84 		bcopy((char *)&naliases, (char *)nap, sizeof(int));
85 		bcopy((char *)&hp->h_addrtype, cp, sizeof (int));
86 		cp += sizeof (int);
87 		bcopy((char *)&hp->h_length, cp, sizeof (int));
88 		cp += sizeof (int);
89 		bcopy(hp->h_addr, cp, hp->h_length);
90 		cp += hp->h_length;
91 		content.dptr = buf;
92 		content.dsize = cp - buf;
93 		if (verbose)
94 			printf("store %s, %d aliases\n", hp->h_name, naliases);
95 		key.dptr = hp->h_name;
96 		key.dsize = strlen(hp->h_name);
97 		if (dbm_store(dp, key, content, DBM_INSERT) < 0) {
98 			perror(hp->h_name);
99 			goto err;
100 		}
101 		for (sp = hp->h_aliases; *sp; sp++) {
102 			key.dptr = *sp;
103 			key.dsize = strlen(*sp);
104 			if (dbm_store(dp, key, content, DBM_INSERT) < 0) {
105 				perror(*sp);
106 				goto err;
107 			}
108 		}
109 		key.dptr = hp->h_addr;
110 		key.dsize = hp->h_length;
111 		if (dbm_store(dp, key, content, DBM_INSERT) < 0) {
112 			perror("dbm_store host address");
113 			goto err;
114 		}
115 		entries++;
116 		if (cp - buf > maxlen)
117 			maxlen = cp - buf;
118 	}
119 	endhostent();
120 	dbm_close(dp);
121 
122 	(void)sprintf(tempname, "%s.new.pag", argv[1]);
123 	(void)sprintf(newname, "%s.pag", argv[1]);
124 	if (rename(tempname, newname) < 0) {
125 		perror("rename .pag");
126 		exit(1);
127 	}
128 	(void)sprintf(tempname, "%s.new.dir", argv[1]);
129 	(void)sprintf(newname, "%s.dir", argv[1]);
130 	if (rename(tempname, newname) < 0) {
131 		perror("rename .dir");
132 		exit(1);
133 	}
134 	printf("%d host entries, maximum length %d\n", entries, maxlen);
135 	exit(0);
136 err:
137 	(void)sprintf(tempname, "%s.new.pag", argv[1]);
138 	unlink(tempname);
139 	(void)sprintf(tempname, "%s.new.dir", argv[1]);
140 	unlink(tempname);
141 	exit(1);
142 }
143