need more resolution on starting time for accounting
[unix-history] / usr / src / old / mkhosts / mkhosts.c
CommitLineData
6ffc4188 1#ifndef lint
0cc97e50 2static char *sccsid = "@(#)mkhosts.c 4.3 (Berkeley) 84/05/17";
6ffc4188
RC
3#endif
4
5#include <sys/file.h>
6#include <stdio.h>
7#include <netdb.h>
8#include <ndbm.h>
9
10char buf[BUFSIZ];
11
12main(argc, argv)
13 char *argv[];
14{
15 DBM *dp;
16 register struct hostent *hp;
17 datum key, content;
18 register char *cp, *tp, **sp;
19 register int naliases, *nap;
20 int verbose = 0, entries = 0, maxlen = 0;
21
22 if (argc > 1 && strcmp(argv[1], "-v") == 0) {
23 verbose++;
24 argv++, argc--;
25 }
26 if (argc != 2) {
27 fprintf(stderr, "usage: mkhosts [ -v ] file\n");
28 exit(1);
29 }
0cc97e50
RC
30 if (access(argv[1], R_OK) < 0) {
31 perror(argv[1]);
32 exit(1);
33 }
6ffc4188
RC
34 umask(0);
35 dp = ndbmopen(argv[1], O_WRONLY|O_CREAT|O_EXCL, 0644);
36 if (dp == NULL) {
37 fprintf(stderr, "dbminit failed: ");
38 perror(argv[1]);
39 exit(1);
40 }
41 dp->db_maxbno = 0;
0cc97e50 42 sethostfile(argv[1]);
6ffc4188
RC
43 sethostent(1);
44 while (hp = gethostent()) {
45 cp = buf;
46 tp = hp->h_name;
47 while (*cp++ = *tp++)
48 ;
49 nap = (int *)cp;
50 cp += sizeof (int);
51 naliases = 0;
52 for (sp = hp->h_aliases; *sp; sp++) {
53 tp = *sp;
54 while (*cp++ = *tp++)
55 ;
56 naliases++;
57 }
58 *nap = naliases;
59 bcopy((char *)&hp->h_addrtype, cp, sizeof (int));
60 cp += sizeof (int);
61 bcopy((char *)&hp->h_length, cp, sizeof (int));
62 cp += sizeof (int);
63 bcopy(hp->h_addr, cp, hp->h_length);
64 cp += hp->h_length;
65 content.dptr = buf;
66 content.dsize = cp - buf;
67 if (verbose)
68 printf("store %s, %d aliases\n", hp->h_name, naliases);
69 key.dptr = hp->h_name;
70 key.dsize = strlen(hp->h_name);
088382d2 71 dbmstore(dp, key, content, DB_INSERT);
6ffc4188
RC
72 for (sp = hp->h_aliases; *sp; sp++) {
73 key.dptr = *sp;
74 key.dsize = strlen(*sp);
088382d2 75 dbmstore(dp, key, content, DB_INSERT);
6ffc4188
RC
76 }
77 key.dptr = hp->h_addr;
78 key.dsize = hp->h_length;
088382d2 79 dbmstore(dp, key, content, DB_INSERT);
6ffc4188
RC
80 entries++;
81 if (cp - buf > maxlen)
82 maxlen = cp - buf;
83 }
84 endhostent();
85 printf("%d host entries, maximum length %d\n", entries, maxlen);
86 exit(0);
87}