date and time created 88/03/30 15:59:46 by bostic
[unix-history] / usr / src / old / mkhosts / mkhosts.c
CommitLineData
cbf32a5d
DF
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
8char copyright[] =
9"@(#) Copyright (c) 1980 Regents of the University of California.\n\
10 All rights reserved.\n";
11#endif not lint
12
6ffc4188 13#ifndef lint
9bd38ba8 14static char sccsid[] = "@(#)mkhosts.c 5.2 (Berkeley) %G%";
cbf32a5d 15#endif not lint
6ffc4188
RC
16
17#include <sys/file.h>
18#include <stdio.h>
19#include <netdb.h>
20#include <ndbm.h>
21
22char buf[BUFSIZ];
23
24main(argc, argv)
25 char *argv[];
26{
27 DBM *dp;
28 register struct hostent *hp;
29 datum key, content;
30 register char *cp, *tp, **sp;
0d4ad104
JB
31 register int *nap;
32 int naliases;
2037425d
RC
33 int verbose = 0, entries = 0, maxlen = 0, error = 0;
34 char tempname[BUFSIZ], newname[BUFSIZ];
6ffc4188
RC
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 }
0cc97e50
RC
44 if (access(argv[1], R_OK) < 0) {
45 perror(argv[1]);
46 exit(1);
47 }
6ffc4188 48 umask(0);
2037425d 49
9bd38ba8 50 (void)sprintf(tempname, "%s.new", argv[1]);
2037425d 51 dp = dbm_open(tempname, O_WRONLY|O_CREAT|O_EXCL, 0644);
6ffc4188 52 if (dp == NULL) {
2037425d 53 fprintf(stderr, "dbm_open failed: ");
6ffc4188
RC
54 perror(argv[1]);
55 exit(1);
56 }
0cc97e50 57 sethostfile(argv[1]);
6ffc4188
RC
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 }
0d4ad104 73 bcopy((char *)&naliases, (char *)nap, sizeof(int));
6ffc4188
RC
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);
2037425d
RC
86 if (dbm_store(dp, key, content, DBM_INSERT) < 0) {
87 perror(hp->h_name);
88 goto err;
89 }
6ffc4188
RC
90 for (sp = hp->h_aliases; *sp; sp++) {
91 key.dptr = *sp;
92 key.dsize = strlen(*sp);
2037425d
RC
93 if (dbm_store(dp, key, content, DBM_INSERT) < 0) {
94 perror(*sp);
95 goto err;
96 }
6ffc4188
RC
97 }
98 key.dptr = hp->h_addr;
99 key.dsize = hp->h_length;
2037425d
RC
100 if (dbm_store(dp, key, content, DBM_INSERT) < 0) {
101 perror("dbm_store host address");
102 goto err;
103 }
6ffc4188
RC
104 entries++;
105 if (cp - buf > maxlen)
106 maxlen = cp - buf;
107 }
108 endhostent();
2037425d
RC
109 dbm_close(dp);
110
9bd38ba8
KB
111 (void)sprintf(tempname, "%s.new.pag", argv[1]);
112 (void)sprintf(newname, "%s.pag", argv[1]);
2037425d
RC
113 if (rename(tempname, newname) < 0) {
114 perror("rename .pag");
115 exit(1);
116 }
9bd38ba8
KB
117 (void)sprintf(tempname, "%s.new.dir", argv[1]);
118 (void)sprintf(newname, "%s.dir", argv[1]);
2037425d
RC
119 if (rename(tempname, newname) < 0) {
120 perror("rename .dir");
121 exit(1);
122 }
6ffc4188
RC
123 printf("%d host entries, maximum length %d\n", entries, maxlen);
124 exit(0);
2037425d 125err:
9bd38ba8 126 (void)sprintf(tempname, "%s.new.pag", argv[1]);
2037425d 127 unlink(tempname);
9bd38ba8 128 (void)sprintf(tempname, "%s.new.dir", argv[1]);
2037425d
RC
129 unlink(tempname);
130 exit(1);
6ffc4188 131}