Annotation of 43BSDReno/usr.sbin/mkhosts/mkhosts.c, revision 1.1

1.1     ! root        1: /*
        !             2:  * Copyright (c) 1983 The Regents of the University of California.
        !             3:  * All rights reserved.
        !             4:  *
        !             5:  * Redistribution and use in source and binary forms are permitted provided
        !             6:  * that: (1) source distributions retain this entire copyright notice and
        !             7:  * comment, and (2) distributions including binaries display the following
        !             8:  * acknowledgement:  ``This product includes software developed by the
        !             9:  * University of California, Berkeley and its contributors'' in the
        !            10:  * documentation or other materials provided with the distribution and in
        !            11:  * all advertising materials mentioning features or use of this software.
        !            12:  * Neither the name of the University nor the names of its contributors may
        !            13:  * be used to endorse or promote products derived from this software without
        !            14:  * specific prior written permission.
        !            15:  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
        !            16:  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
        !            17:  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
        !            18:  */
        !            19: 
        !            20: #ifndef lint
        !            21: char copyright[] =
        !            22: "@(#) Copyright (c) 1983 The Regents of the University of California.\n\
        !            23:  All rights reserved.\n";
        !            24: #endif /* not lint */
        !            25: 
        !            26: #ifndef lint
        !            27: static char sccsid[] = "@(#)mkhosts.c  5.4 (Berkeley) 6/1/90";
        !            28: #endif /* not lint */
        !            29: 
        !            30: #include <sys/file.h>
        !            31: #include <stdio.h>
        !            32: #include <netdb.h>
        !            33: #include <ndbm.h>
        !            34: 
        !            35: char   buf[BUFSIZ];
        !            36: 
        !            37: main(argc, argv)
        !            38:        char *argv[];
        !            39: {
        !            40:        DBM *dp;
        !            41:        register struct hostent *hp;
        !            42:        datum key, content;
        !            43:        register char *cp, *tp, **sp;
        !            44:        register int *nap;
        !            45:        int naliases;
        !            46:        int verbose = 0, entries = 0, maxlen = 0, error = 0;
        !            47:        char tempname[BUFSIZ], newname[BUFSIZ];
        !            48: 
        !            49:        if (argc > 1 && strcmp(argv[1], "-v") == 0) {
        !            50:                verbose++;
        !            51:                argv++, argc--;
        !            52:        }
        !            53:        if (argc != 2) {
        !            54:                fprintf(stderr, "usage: mkhosts [ -v ] file\n");
        !            55:                exit(1);
        !            56:        }
        !            57:        if (access(argv[1], R_OK) < 0) {
        !            58:                perror(argv[1]);
        !            59:                exit(1);
        !            60:        }
        !            61:        umask(0);
        !            62: 
        !            63:        (void)sprintf(tempname, "%s.new", argv[1]);
        !            64:        dp = dbm_open(tempname, O_WRONLY|O_CREAT|O_EXCL, 0644);
        !            65:        if (dp == NULL) {
        !            66:                fprintf(stderr, "dbm_open failed: ");
        !            67:                perror(argv[1]);
        !            68:                exit(1);
        !            69:        }
        !            70:        sethostfile(argv[1]);
        !            71:        sethostent(1);
        !            72:        while (hp = gethostent()) {
        !            73:                cp = buf;
        !            74:                tp = hp->h_name;
        !            75:                while (*cp++ = *tp++)
        !            76:                        ;
        !            77:                nap = (int *)cp;
        !            78:                cp += sizeof (int);
        !            79:                naliases = 0;
        !            80:                for (sp = hp->h_aliases; *sp; sp++) {
        !            81:                        tp = *sp;
        !            82:                        while (*cp++ = *tp++)
        !            83:                                ;
        !            84:                        naliases++;
        !            85:                }
        !            86:                bcopy((char *)&naliases, (char *)nap, sizeof(int));
        !            87:                bcopy((char *)&hp->h_addrtype, cp, sizeof (int));
        !            88:                cp += sizeof (int);
        !            89:                bcopy((char *)&hp->h_length, cp, sizeof (int));
        !            90:                cp += sizeof (int);
        !            91:                bcopy(hp->h_addr, cp, hp->h_length);
        !            92:                cp += hp->h_length;
        !            93:                content.dptr = buf;
        !            94:                content.dsize = cp - buf;
        !            95:                if (verbose)
        !            96:                        printf("store %s, %d aliases\n", hp->h_name, naliases);
        !            97:                key.dptr = hp->h_name;
        !            98:                key.dsize = strlen(hp->h_name);
        !            99:                if (dbm_store(dp, key, content, DBM_INSERT) < 0) {
        !           100:                        perror(hp->h_name);
        !           101:                        goto err;
        !           102:                }
        !           103:                for (sp = hp->h_aliases; *sp; sp++) {
        !           104:                        key.dptr = *sp;
        !           105:                        key.dsize = strlen(*sp);
        !           106:                        if (dbm_store(dp, key, content, DBM_INSERT) < 0) {
        !           107:                                perror(*sp);
        !           108:                                goto err;
        !           109:                        }
        !           110:                }
        !           111:                key.dptr = hp->h_addr;
        !           112:                key.dsize = hp->h_length;
        !           113:                if (dbm_store(dp, key, content, DBM_INSERT) < 0) {
        !           114:                        perror("dbm_store host address");
        !           115:                        goto err;
        !           116:                }
        !           117:                entries++;
        !           118:                if (cp - buf > maxlen)
        !           119:                        maxlen = cp - buf;
        !           120:        }
        !           121:        endhostent();
        !           122:        dbm_close(dp);
        !           123: 
        !           124:        (void)sprintf(tempname, "%s.new.pag", argv[1]);
        !           125:        (void)sprintf(newname, "%s.pag", argv[1]);
        !           126:        if (rename(tempname, newname) < 0) {
        !           127:                perror("rename .pag");
        !           128:                exit(1);
        !           129:        }
        !           130:        (void)sprintf(tempname, "%s.new.dir", argv[1]);
        !           131:        (void)sprintf(newname, "%s.dir", argv[1]);
        !           132:        if (rename(tempname, newname) < 0) {
        !           133:                perror("rename .dir");
        !           134:                exit(1);
        !           135:        }
        !           136:        printf("%d host entries, maximum length %d\n", entries, maxlen);
        !           137:        exit(0);
        !           138: err:
        !           139:        (void)sprintf(tempname, "%s.new.pag", argv[1]);
        !           140:        unlink(tempname);
        !           141:        (void)sprintf(tempname, "%s.new.dir", argv[1]);
        !           142:        unlink(tempname);
        !           143:        exit(1);
        !           144: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.