121147Sdist /*
235881Sbostic * Copyright (c) 1983 The Regents of the University of California.
335881Sbostic * All rights reserved.
435881Sbostic *
542814Sbostic * %sccs.include.redist.c%
621147Sdist */
721147Sdist
815666Sralph #ifndef lint
921147Sdist char copyright[] =
1035881Sbostic "@(#) Copyright (c) 1983 The Regents of the University of California.\n\
1121147Sdist All rights reserved.\n";
1235881Sbostic #endif /* not lint */
1315666Sralph
1421147Sdist #ifndef lint
15*46922Sbostic static char sccsid[] = "@(#)mkhosts.c 5.5 (Berkeley) 03/02/91";
1635881Sbostic #endif /* not lint */
1721147Sdist
1815666Sralph #include <sys/file.h>
1915666Sralph #include <stdio.h>
2015666Sralph #include <netdb.h>
2115666Sralph #include <ndbm.h>
2215666Sralph
2315666Sralph char buf[BUFSIZ];
2415666Sralph
main(argc,argv)2515666Sralph main(argc, argv)
2615666Sralph char *argv[];
2715666Sralph {
2815666Sralph DBM *dp;
2915666Sralph register struct hostent *hp;
3015666Sralph datum key, content;
3115666Sralph register char *cp, *tp, **sp;
3218004Sbloom register int *nap;
3318004Sbloom int naliases;
3417026Sralph int verbose = 0, entries = 0, maxlen = 0, error = 0;
3517026Sralph char tempname[BUFSIZ], newname[BUFSIZ];
3615666Sralph
3715666Sralph if (argc > 1 && strcmp(argv[1], "-v") == 0) {
3815666Sralph verbose++;
3915666Sralph argv++, argc--;
4015666Sralph }
4115666Sralph if (argc != 2) {
4215666Sralph fprintf(stderr, "usage: mkhosts [ -v ] file\n");
4315666Sralph exit(1);
4415666Sralph }
4516509Sralph if (access(argv[1], R_OK) < 0) {
4616509Sralph perror(argv[1]);
4716509Sralph exit(1);
4816509Sralph }
4915666Sralph umask(0);
5017026Sralph
5132443Sbostic (void)sprintf(tempname, "%s.new", argv[1]);
5217026Sralph dp = dbm_open(tempname, O_WRONLY|O_CREAT|O_EXCL, 0644);
5315666Sralph if (dp == NULL) {
5417026Sralph fprintf(stderr, "dbm_open failed: ");
5515666Sralph perror(argv[1]);
5615666Sralph exit(1);
5715666Sralph }
5816509Sralph sethostfile(argv[1]);
5915666Sralph sethostent(1);
6015666Sralph while (hp = gethostent()) {
6115666Sralph cp = buf;
6215666Sralph tp = hp->h_name;
6315666Sralph while (*cp++ = *tp++)
6415666Sralph ;
6515666Sralph nap = (int *)cp;
6615666Sralph cp += sizeof (int);
6715666Sralph naliases = 0;
6815666Sralph for (sp = hp->h_aliases; *sp; sp++) {
6915666Sralph tp = *sp;
7015666Sralph while (*cp++ = *tp++)
7115666Sralph ;
7215666Sralph naliases++;
7315666Sralph }
7418004Sbloom bcopy((char *)&naliases, (char *)nap, sizeof(int));
7515666Sralph bcopy((char *)&hp->h_addrtype, cp, sizeof (int));
7615666Sralph cp += sizeof (int);
7715666Sralph bcopy((char *)&hp->h_length, cp, sizeof (int));
7815666Sralph cp += sizeof (int);
7915666Sralph bcopy(hp->h_addr, cp, hp->h_length);
8015666Sralph cp += hp->h_length;
8115666Sralph content.dptr = buf;
8215666Sralph content.dsize = cp - buf;
8315666Sralph if (verbose)
8415666Sralph printf("store %s, %d aliases\n", hp->h_name, naliases);
8515666Sralph key.dptr = hp->h_name;
8615666Sralph key.dsize = strlen(hp->h_name);
8717026Sralph if (dbm_store(dp, key, content, DBM_INSERT) < 0) {
8817026Sralph perror(hp->h_name);
8917026Sralph goto err;
9017026Sralph }
9115666Sralph for (sp = hp->h_aliases; *sp; sp++) {
9215666Sralph key.dptr = *sp;
9315666Sralph key.dsize = strlen(*sp);
9417026Sralph if (dbm_store(dp, key, content, DBM_INSERT) < 0) {
9517026Sralph perror(*sp);
9617026Sralph goto err;
9717026Sralph }
9815666Sralph }
9915666Sralph key.dptr = hp->h_addr;
10015666Sralph key.dsize = hp->h_length;
10117026Sralph if (dbm_store(dp, key, content, DBM_INSERT) < 0) {
10217026Sralph perror("dbm_store host address");
10317026Sralph goto err;
10417026Sralph }
10515666Sralph entries++;
10615666Sralph if (cp - buf > maxlen)
10715666Sralph maxlen = cp - buf;
10815666Sralph }
10915666Sralph endhostent();
11017026Sralph dbm_close(dp);
11117026Sralph
11232443Sbostic (void)sprintf(tempname, "%s.new.pag", argv[1]);
11332443Sbostic (void)sprintf(newname, "%s.pag", argv[1]);
11417026Sralph if (rename(tempname, newname) < 0) {
11517026Sralph perror("rename .pag");
11617026Sralph exit(1);
11717026Sralph }
11832443Sbostic (void)sprintf(tempname, "%s.new.dir", argv[1]);
11932443Sbostic (void)sprintf(newname, "%s.dir", argv[1]);
12017026Sralph if (rename(tempname, newname) < 0) {
12117026Sralph perror("rename .dir");
12217026Sralph exit(1);
12317026Sralph }
12415666Sralph printf("%d host entries, maximum length %d\n", entries, maxlen);
12515666Sralph exit(0);
12617026Sralph err:
12732443Sbostic (void)sprintf(tempname, "%s.new.pag", argv[1]);
12817026Sralph unlink(tempname);
12932443Sbostic (void)sprintf(tempname, "%s.new.dir", argv[1]);
13017026Sralph unlink(tempname);
13117026Sralph exit(1);
13215666Sralph }
133