Annotation of researchv10no/cmd/lcomp/lsub.c, revision 1.1

1.1     ! root        1: #include "stdio.h"
        !             2: typedef unsigned long ul;
        !             3: typedef struct {
        !             4:        char *fname;
        !             5:        int len;        /* how many counts have been seen */
        !             6:        int quot;       /* how much has been allocated for cnt */
        !             7:        unsigned long *cnt;
        !             8: } stab;
        !             9: stab *tab;
        !            10: int ntab, ltab;
        !            11: FILE *fd, *sfd, *cfd;
        !            12: char buf[256];
        !            13: char fname[512] = "/";
        !            14: extern char *malloc();
        !            15: char flg[128];
        !            16: unsigned long val;
        !            17: 
        !            18: main(argc, argv)
        !            19: char **argv;
        !            20: {      int i, j;
        !            21:        if(argc <= 1) {
        !            22:                fprintf(stderr, "subtract from where?\n");
        !            23:                exit(1);
        !            24:        }
        !            25:        if((fd = fopen("prof.out", "r")) == 0) {
        !            26:                perror("prof.out");
        !            27:                exit(1);
        !            28:        }
        !            29:        readall(fd);
        !            30:        fclose(fd);
        !            31:        sfd = fopen(argv[1], "r");
        !            32:        if(sfd == 0) {
        !            33:                perror(argv[1]);
        !            34:                exit(1);
        !            35:        }
        !            36:        suball(sfd);
        !            37:        fclose(sfd);
        !            38:        if((fd = fopen("prof.out", "w")) == 0) {
        !            39:                perror(argv[1]);
        !            40:                exit(1);
        !            41:        }
        !            42:        for(i = 0; i < ntab; i++)
        !            43:                rewrite(tab + i);
        !            44:        exit(0);
        !            45: }
        !            46: 
        !            47: readall(fd)
        !            48: FILE *fd;
        !            49: {      int c, i, index;
        !            50:        stab *curtab = 0;
        !            51: sawnl:
        !            52:        if((c = getc(fd)) == EOF)
        !            53:                return;
        !            54:        if(c == '\n')
        !            55:                goto sawnl;
        !            56:        if(c == '/') {
        !            57:                fscanf(fd, "%s", fname+1);
        !            58:                for(i = 0; i < ntab; i++)
        !            59:                        if(strcmp(fname, tab[i].fname) == 0)
        !            60:                                break;
        !            61:                if(i >= ntab) { /* new file */
        !            62:                        if(ltab == 0) {
        !            63:                                tab = (stab *)malloc(20 * sizeof(stab));
        !            64:                                ltab = 20;
        !            65:                        }
        !            66:                        else if(ntab >= ltab)
        !            67:                                tab = (stab *)realloc((char *)tab,
        !            68:                                        (ltab += 20) * sizeof(stab));
        !            69:                        tab[ntab].fname = malloc(sizeof(fname) + 1);
        !            70:                        strcpy(tab[ntab].fname, fname);
        !            71:                        tab[ntab].len = tab[ntab].quot = 0;
        !            72:                        ntab++;
        !            73:                }
        !            74:                curtab = tab + i;
        !            75:                index = 0;
        !            76:        }
        !            77:        else if(c < '0' || c > '9') {
        !            78:                fprintf(stderr, "prof.out has weird format\n");
        !            79:                abort();
        !            80:        }
        !            81:        else {
        !            82:                ungetc(c, fd);
        !            83:                fscanf(fd, "%d", &val);
        !            84:                if(curtab->len <= index) {
        !            85:                        if(curtab->quot == 0) {
        !            86:                                curtab->cnt = (ul *)malloc(100*sizeof(long));
        !            87:                                curtab->quot = 100;
        !            88:                                for(i = 0; i < 100; i++)
        !            89:                                        curtab->cnt[i] = 0;
        !            90:                        }
        !            91:                        else if(curtab->len >= curtab->quot) {
        !            92:                                curtab->cnt = (ul *)realloc(curtab->cnt,
        !            93:                                        (curtab->quot += 200) * sizeof(long));
        !            94:                                for(i = curtab->quot-200; i < curtab->quot; i++)
        !            95:                                        curtab->cnt[i] = 0;
        !            96:                        }
        !            97:                        curtab->len++;
        !            98:                }
        !            99:                curtab->cnt[index++] += val;
        !           100:        }
        !           101:        goto sawnl;
        !           102: }
        !           103: 
        !           104: rewrite(x)
        !           105: stab *x;
        !           106: {      int i;
        !           107:        for(i = 0; i < x->len; i++)
        !           108:                if(x->cnt[i])
        !           109:                        break;
        !           110:        if(i >= x->len)
        !           111:                return;
        !           112:        fprintf(fd, "%s\n", x->fname);
        !           113:        for(i = 0; i < x->len; i++)
        !           114:                fprintf(fd, "%u\n", x->cnt[i]);
        !           115: }
        !           116: suball(fd)
        !           117: FILE *fd;
        !           118: {      int c, i, index;
        !           119:        stab *curtab = 0;
        !           120: sawnl:
        !           121:        if((c = getc(fd)) == EOF)
        !           122:                return;
        !           123:        if(c == '\n')
        !           124:                goto sawnl;
        !           125:        if(c == '/') {
        !           126:                fscanf(fd, "%s", fname+1);
        !           127:                for(i = 0; i < ntab; i++)
        !           128:                        if(strcmp(fname, tab[i].fname) == 0)
        !           129:                                break;
        !           130:                if(i >= ntab) { /* new file */
        !           131:                        fprintf(stderr, "new file %s ignored\n", fname);
        !           132:                        goto sawnl;
        !           133:                }
        !           134:                curtab = tab + i;
        !           135:                index = 0;
        !           136:        }
        !           137:        else if(c < '0' || c > '9') {
        !           138:                fprintf(stderr, "prof.out has weird format\n");
        !           139:                abort();
        !           140:        }
        !           141:        else {
        !           142:                ungetc(c, fd);
        !           143:                fscanf(fd, "%d", &val);
        !           144:                curtab->cnt[index++] -= val;
        !           145:        }
        !           146:        goto sawnl;
        !           147: }

unix.superglobalmegacorp.com

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