Annotation of researchv10no/cmd/PDP11/11ranlib.c, revision 1.1

1.1     ! root        1: /*
        !             2:  * Insert a directory into a character archive file
        !             3:  */
        !             4: #include       "ar.h"          /*god: char fmt archives */
        !             5: #include       "a.out.h"       /*god: pdp11 fmt a.out.h */
        !             6: #include       <stdio.h>
        !             7: #ifndef AR             /*god: name of archiver */
        !             8: #define AR "ar"
        !             9: #endif
        !            10: #define        MAGIC   exp.a_magic
        !            11: #define        BADMAG  MAGIC!=A_MAGIC1 && MAGIC!=A_MAGIC2  \
        !            12:                && MAGIC!=A_MAGIC3 && MAGIC!=A_MAGIC4
        !            13: struct ar_hdr  arp;
        !            14: long   arsize;
        !            15: struct exec    exp;
        !            16: FILE   *fi, *fo;
        !            17: long   off, oldoff;
        !            18: long   atol();
        !            19: long   ftell();
        !            20: #define TABSZ  2000
        !            21: struct tab
        !            22: {      char cname[8];
        !            23:        long cloc;
        !            24: } tab[TABSZ];
        !            25: int tnum;
        !            26: int new;
        !            27: char   tempnm[] = "__.SYMDEF";
        !            28: char   firstname[17];
        !            29: long   offdelta;
        !            30: 
        !            31: char   *progname;      /*god: holds argv[0] */
        !            32: 
        !            33: main(argc, argv)
        !            34: char **argv;
        !            35: {
        !            36:        char buf[256];
        !            37:        char magbuf[SARMAG+1];
        !            38: 
        !            39:        progname = argv[0];     /*god: for error messages */
        !            40:        --argc;
        !            41:        while(argc--) {
        !            42:                fi = fopen(*++argv,"r");
        !            43:                if (fi == NULL) {
        !            44:                        fprintf(stderr, "%s: cannot open %s\n",
        !            45:                                        progname, *argv);
        !            46:                        continue;
        !            47:                }
        !            48:                off = SARMAG;
        !            49:                fread(magbuf, 1, SARMAG, fi);   /* get magic no. */
        !            50:                if (strncmp(magbuf, ARMAG, SARMAG)) {
        !            51:                        fprintf(stderr, "not archive: %s\n", *argv);
        !            52:                        continue;
        !            53:                }
        !            54:                fseek(fi, 0L, 0);
        !            55:                new = tnum = 0;
        !            56:                if(nextel(fi) == 0)
        !            57:                {       fclose(fi);
        !            58:                        continue;
        !            59:                }
        !            60:                do {
        !            61:                        long o;
        !            62:                        register n;
        !            63:                        struct nlist sym;
        !            64: 
        !            65:                        fread((char *)&exp, 1, sizeof(struct exec), fi);
        !            66:                        if (BADMAG)             /* archive element not in  */
        !            67:                                continue;       /* proper format - skip it */
        !            68:                        o = (long)exp.a_text + exp.a_data;
        !            69:                        if ((exp.a_flag & 01) == 0)
        !            70:                                o *= 2;
        !            71:                        fseek(fi, o, 1);
        !            72:                        n = exp.a_syms / sizeof(struct nlist);
        !            73:                        if (n == 0) {
        !            74:                                fprintf(stderr, "%s: %s-- no name list\n",
        !            75:                                                progname, arp.ar_name);
        !            76:                                continue;
        !            77:                        }
        !            78:                        while (--n >= 0) {
        !            79:                                fread((char *)&sym, 1, sizeof(sym), fi);
        !            80:                                if ((sym.n_type&N_EXT)==0)
        !            81:                                        continue;
        !            82:                                switch (sym.n_type&N_TYPE) {
        !            83: 
        !            84:                                case N_UNDF:
        !            85:                                        if (sym.n_value!=0)
        !            86:                                                stash(&sym);
        !            87:                                        continue;
        !            88: 
        !            89:                                default:
        !            90:                                        stash(&sym);
        !            91:                                        continue;
        !            92:                                }
        !            93:                        }
        !            94:                } while(nextel(fi));
        !            95:                new = fixsize();
        !            96:                fclose(fi);
        !            97:                fo = fopen(tempnm, "w");
        !            98:                if(fo == NULL)
        !            99:                {       fprintf(stderr, "can't create temporary\n");
        !           100:                        exit(1);
        !           101:                }
        !           102:                fwrite((char *)tab, tnum, sizeof(struct tab), fo);
        !           103:                fclose(fo);
        !           104:                if(new)
        !           105:                        sprintf(buf, "%s rlb %s %s %s\n",
        !           106:                                     AR, firstname, *argv, tempnm);/*god*/
        !           107:                else    sprintf(buf, "%s rl %s %s\n",
        !           108:                                     AR, *argv, tempnm);/*god*/
        !           109:                if(system(buf))
        !           110:                        fprintf(stderr, "can't execute %s\n", buf);
        !           111:                else fixdate(*argv);
        !           112:                unlink(tempnm);
        !           113:        }
        !           114:        exit(0);
        !           115: }
        !           116: 
        !           117: nextel(af)
        !           118: FILE *af;
        !           119: {
        !           120:        register r;
        !           121:        register char *cp;
        !           122: 
        !           123:        oldoff = off;
        !           124:        fseek(af, off, 0);
        !           125:        r = fread((char *)&arp, 1, sizeof(struct ar_hdr), af);  /* read archive header */
        !           126:        if (r != sizeof(struct ar_hdr))
        !           127:                return(0);
        !           128:        for (cp=arp.ar_name; cp < & arp.ar_name[sizeof(arp.ar_name)]; cp++)
        !           129:                if (*cp == ' ')
        !           130:                        *cp = '\0';
        !           131:        arsize = atol(arp.ar_size);
        !           132:        if (arsize & 1)
        !           133:                arsize++;
        !           134:        off = ftell(af) + arsize;       /* offset to next element */
        !           135:        return(1);
        !           136: }
        !           137: 
        !           138: stash(s) struct nlist *s;
        !           139: {      int i;
        !           140:        if(tnum >= TABSZ)
        !           141:        {       fprintf(stderr, "symbol table overflow\n");
        !           142:                exit(1);
        !           143:        }
        !           144:        for(i=0; i<8; i++)
        !           145:                tab[tnum].cname[i] = s->n_name[i];
        !           146:        tab[tnum].cloc = oldoff;
        !           147:        tnum++;
        !           148: }
        !           149: 
        !           150: fixsize()
        !           151: {      int i;
        !           152:        offdelta = tnum * sizeof(struct tab) + sizeof(arp);
        !           153:        off = SARMAG;
        !           154:        nextel(fi);
        !           155:        if(strncmp(arp.ar_name, tempnm, 14) == 0)
        !           156:        {       new = 0;
        !           157:                offdelta -= sizeof(arp) + arsize;
        !           158:        }
        !           159:        else
        !           160:        {       new = 1;
        !           161:                strncpy(firstname, arp.ar_name, 14);
        !           162:        }
        !           163:        for(i=0; i<tnum; i++)
        !           164:                tab[i].cloc += offdelta;
        !           165:        return(new);
        !           166: }
        !           167: 
        !           168: /* patch time */
        !           169: fixdate(s) char *s;
        !           170: {      long time();
        !           171:        char buf[24];
        !           172:        int fd;
        !           173:        fd = open(s, 1);
        !           174:        if(fd < 0)
        !           175:        {       fprintf(stderr, "can't reopen %s\n", s);
        !           176:                return;
        !           177:        }
        !           178:        sprintf(buf, "%-*ld", sizeof(arp.ar_date), time((long *)NULL)+5);
        !           179:        lseek(fd, (long)SARMAG + ((char *)arp.ar_date-(char *)&arp), 0);
        !           180:        write(fd, buf, sizeof(arp.ar_date));
        !           181:        close(fd);
        !           182: }

unix.superglobalmegacorp.com

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