Annotation of researchv10no/libcbt/bt.c, revision 1.1

1.1     ! root        1: #include "cbt.h"
        !             2: #include "pr.h"
        !             3: 
        !             4: bfile          *curbf;
        !             5: extern bfile   *newtran();
        !             6: extern char    *malloc(), *strcpy();
        !             7: extern long    lseek();
        !             8: 
        !             9: bfile *bopen(s, typ) char *s;  /* typ is 0 or 2 */
        !            10: {      bfile *p;
        !            11:        int n, i;
        !            12: 
        !            13:        p = alloc(bfile);
        !            14:        if(p == NULL)
        !            15:                goto nomem;
        !            16:        n = strlen(s);
        !            17:        p->fname = malloc((unsigned)n + 3);
        !            18:        if(p->fname == NULL)
        !            19:                goto nomem;
        !            20:        (void) strcpy(p->fname, s);
        !            21:        strcpy(p->fname + n, ".T");
        !            22:        if((p->tfd = open(p->fname, typ)) == -1) {
        !            23:                free(p->fname);
        !            24:                free((char *)p);
        !            25:                return(NULL);
        !            26:        }
        !            27:        p->rdwrt = typ;
        !            28:        p->fatal = p->advnc = 0;
        !            29:        p->altname = NULL;
        !            30:        for(i = 0; i <= MXHT; i++) {
        !            31:                p->path[i] = NULL;
        !            32:                p->flag[i] = 0;
        !            33:                p->loc[i] = 0;
        !            34:        }
        !            35:        p->path[0] = (hdr *)malloc(NDSZ);
        !            36:        if(p->path[0] == NULL)
        !            37:                goto nomem;
        !            38:        curbf = p;
        !            39:        if(ndrd(0, (ndaddr)0) == EOF)
        !            40:                return(NULL);
        !            41:        strcpy(p->fname + n, ".F");
        !            42:        if(!treeonly(p) && (p->dfd = open(p->fname, typ)) == -1) {
        !            43:                (void) close(p->tfd);
        !            44:                free(p->fname);
        !            45:                free((char *) p->path[0]);
        !            46:                free((char *)p);
        !            47:                return(NULL);
        !            48:        }
        !            49:        else if(treeonly(p))
        !            50:                p->dfd = -1;
        !            51:        p->fname[n] = 0;
        !            52:        if(shared(p))
        !            53:                return(newtran(p));
        !            54:        else if(tranid == 0)
        !            55:                tranid = getlpid();
        !            56:        p->height = p->path[0]->hlev;
        !            57:        for(n = 1; n <= p->height; n++)
        !            58:                if((p->path[n] = (hdr *)malloc(NDSZ)) == NULL)
        !            59:                        goto nomem;
        !            60:        if(p->height > 0)
        !            61:                mvgbt((char *)p->path[p->height], (char *)p->path[0], NDSZ);
        !            62:        (void) bfirst(p);
        !            63:        return(p);
        !            64: nomem:
        !            65:        errno = BNOMEM;
        !            66:        return(NULL);
        !            67: }
        !            68: 
        !            69: bseek(bf, key) bfile *bf; mbuf key;
        !            70: {      private m;
        !            71:        int i;
        !            72:        if(bf == NULL)
        !            73:                return(EOF);
        !            74:        if(notran(bf))
        !            75:                return(EOF);
        !            76:        if(!readonly(bf))
        !            77:                for(i = 0; i < bf->height; i++)
        !            78:                        if(mustwrite(bf, i))
        !            79:                                if(fixpath(bf) == EOF)
        !            80:                                        return(EOF);
        !            81:        bf->rdptr.rnum = desce(bf, key, &m);
        !            82:        if(bf->rdptr.rnum == EOF)
        !            83:                return(EOF);
        !            84:        bf->advnc = 0;
        !            85:        bf->rdptr.rptr = m.d;
        !            86:        if(m.match == EOF) {
        !            87:                if(advance() == EOF)
        !            88:                        return(EOF);
        !            89:                if(bf->rdptr.rptr != NULL)
        !            90:                        m.match = NOTFOUND;
        !            91:        }
        !            92:        if(m.match != EOF)
        !            93:                mvgbt(bf->rdptr.rpref, key.mdata, bf->rdptr.rptr->dcom);
        !            94:                /* maybe use rptr instead of m.d */
        !            95:        return(m.match);
        !            96: }
        !            97: 
        !            98: bfirst(bf) bfile *bf;
        !            99: {      mbuf key;
        !           100:        key.mlen = 0;
        !           101:        return(bseek(bf, key));
        !           102: }
        !           103: 
        !           104: bclose(bf) bfile *bf;
        !           105: {      int i;
        !           106:        if(bf == NULL)
        !           107:                return;
        !           108:        if(shared(bf)) {
        !           109:                if(intran())
        !           110:                        trabort();
        !           111:                rmtran(bf);
        !           112:        }
        !           113:        else
        !           114:                bflush(bf);
        !           115:        free(bf->fname);
        !           116:        for(i = 0; i <= MXHT; i++)
        !           117:                if(bf->path[i] != NULL)
        !           118:                        free((char *)bf->path[i]);
        !           119:        if(bf->tfd != -1)
        !           120:                (void) close(bf->tfd);
        !           121:        if(bf->dfd != -1)
        !           122:                (void) close(bf->dfd);
        !           123:        free((char *)bf);
        !           124: }
        !           125: 
        !           126: bflush(bf) bfile *bf;
        !           127: {      int i;
        !           128:        if(bf == NULL)
        !           129:                return(0);
        !           130:        if(!bf->rdwrt) {
        !           131:                errno = BNOWRITE;
        !           132:                return(EOF);
        !           133:        }
        !           134:        curbf = bf;
        !           135:        if(shared(bf))
        !           136:                if(intran()) {
        !           137:                        trabort();
        !           138:                        return(EOF);
        !           139:                }
        !           140:                else return(0);
        !           141:        for(i = 0; i <= bf->height; i++) {
        !           142:                if(!mustwrite(bf, i))
        !           143:                        continue;
        !           144:                if(readonly(bf) || i == bf->height)
        !           145:                        if(ndwrt(bf->path[i], bf->loc[i]) == EOF)
        !           146:                                return(EOF);
        !           147:                else
        !           148:                        if(fixpath(bf) == EOF)
        !           149:                                return(EOF);
        !           150:                bf->flag[i] = 0;
        !           151:        }
        !           152:        return(0);
        !           153: }
        !           154: 
        !           155: breclen(bf) bfile *bf;
        !           156: {
        !           157:        if(bf == NULL)
        !           158:                return(EOF);
        !           159:        if(notran(bf))
        !           160:                return(EOF);
        !           161:        if(bf->advnc)
        !           162:                if(advance() == EOF)
        !           163:                        return(EOF);
        !           164:        if(bf->rdptr.rnum >= bf->path[0]->kcnt)
        !           165:                return(EOF);
        !           166:        if(treeonly(bf))
        !           167:                return(0);
        !           168:        return(lfadr(bf->path[0], bf->rdptr.rnum)->llen);
        !           169: }
        !           170: 
        !           171: mbuf bkey(bf) bfile *bf;
        !           172: {      mbuf x;
        !           173:        dkey *d;
        !           174:        if(bf == NULL)
        !           175:                goto eof;
        !           176:        if(notran(bf))
        !           177:                goto eof;
        !           178:        if(bf->advnc)
        !           179:                if(advance() == EOF)
        !           180:                        goto eof;
        !           181:        if(bf->rdptr.rnum >= bf->path[0]->kcnt)
        !           182:                goto eof;
        !           183:        d = bf->rdptr.rptr;
        !           184:        x.mlen = d->dlen - DKEYSZ + d->dcom;
        !           185:        mvgbt(bf->rdptr.rpref + d->dcom, d->dkey, d->dlen-DKEYSZ);
        !           186:        x.mdata = bf->rdptr.rpref;
        !           187:        return(x);
        !           188: eof:
        !           189:        x.mlen = 0;
        !           190:        x.mdata = NULL;
        !           191:        return(x);
        !           192: }
        !           193: 
        !           194: bread(bf, key, rec) bfile *bf; mbuf *key, *rec;
        !           195: {
        !           196:        dkey *d;
        !           197:        lfaddr *x;
        !           198:        int n;
        !           199:        if(bf == NULL)
        !           200:                return(NULL);
        !           201:        if(notran(bf))
        !           202:                return(EOF);
        !           203:        if(bf->advnc)
        !           204:                if(advance() == EOF)
        !           205:                        return(EOF);
        !           206:        if(bf->rdptr.rnum >= bf->path[0]->kcnt)
        !           207:                return(EOF);
        !           208:        if(key != NULL) {
        !           209:                d = bf->rdptr.rptr;
        !           210:                key->mlen = d->dlen - DKEYSZ + d->dcom;
        !           211:                mvgbt(key->mdata, bf->rdptr.rpref, d->dcom);
        !           212:                mvgbt(key->mdata + d->dcom, d->dkey, d->dlen - DKEYSZ);
        !           213:        }
        !           214:        if(rec != NULL && !treeonly(bf)) {
        !           215:                x = lfadr(bf->path[0], bf->rdptr.rnum);
        !           216:                rec->mlen = x->llen;
        !           217:                if(rec->mlen != 0) {
        !           218:                        (void) lseek(bf->dfd, x->lloc, 0);
        !           219:                        if((n = read(bf->dfd, rec->mdata, (int)rec->mlen))
        !           220:                                != rec->mlen) {
        !           221:                                if(n >= 0)
        !           222:                                        errno = BRDERR;
        !           223:                                return(EOF);
        !           224:                        }
        !           225:                }
        !           226:        }
        !           227:        bf->advnc = 1;
        !           228:        return(0);
        !           229: }
        !           230: static struct D { struct D *a; char *b;} VER = {&VER,"\n82/10/9:bt.c\n"};
        !           231: /*0001011110110101*/

unix.superglobalmegacorp.com

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