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