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