|
|
1.1 ! root 1: #include "stdio.h" ! 2: #include "cbt.h" ! 3: extern bfile *bopen(); ! 4: extern mbuf bkey(); ! 5: ! 6: char *index; ! 7: char *dfile; ! 8: char sep = '!'; ! 9: char preflg; ! 10: char nl; ! 11: ! 12: /* get key from index, print all records from dfile. ! 13: * default file names and separator in the index are as given ! 14: * preflg => prefix searching ! 15: */ ! 16: char line[256]; ! 17: char buf1[256], buf2[2048]; ! 18: bfile *bi, *bd; ! 19: ! 20: main(argc, argv) ! 21: char **argv; ! 22: { int i; ! 23: char *p; ! 24: for(i = 1; i < argc && *argv[i] == '-'; i++) ! 25: for(p = argv[i] + 1; *(p-1) && *p; p++) ! 26: switch(*p) { ! 27: default: ! 28: fprintf(stderr, "unk flag %c\n", *p); ! 29: exit(1); ! 30: case 'p': ! 31: preflg = 1; ! 32: break; ! 33: case 'n': ! 34: nl = *++p; ! 35: break; ! 36: case 's': ! 37: sep = *++p; ! 38: break; ! 39: } ! 40: if(i < argc) { ! 41: dfile = argv[i++]; ! 42: bd = bopen(dfile, 0); ! 43: if(bd == NULL) { ! 44: perror(dfile); ! 45: exit(1); ! 46: } ! 47: } ! 48: if(i < argc) { ! 49: index = argv[i++]; ! 50: bi = bopen(index, 0); ! 51: if(bi == NULL) { ! 52: perror(index); ! 53: exit(1); ! 54: } ! 55: } ! 56: for(;;) { ! 57: (void) fgets(line, sizeof(line), stdin); ! 58: if(feof(stdin)) ! 59: exit(1); ! 60: for(p = line; *p && *p != '\n'; p++) ! 61: ; ! 62: if(*p == '\n') ! 63: *p = 0; ! 64: if(index) ! 65: doindex(); ! 66: dodata(); ! 67: } ! 68: } ! 69: ! 70: doindex() ! 71: { mbuf key, found, newkey, rec; ! 72: int cnt; ! 73: char *q; ! 74: key.mlen = strlen(line); ! 75: key.mdata = line; ! 76: if(bseek(bi, key) == EOF) { ! 77: printf("not found\n"); ! 78: return; ! 79: } ! 80: for(cnt = 0; ; cnt++) { ! 81: if(bread(bi, &found, (mbuf *)NULL) == EOF) ! 82: break; ! 83: if(found.mlen < key.mlen || strncmp(found.mdata, line, key.mlen) ! 84: break; ! 85: if(!preflg && found.mdata[key.mlen] != sep) ! 86: break; ! 87: for(q = found.mdata; q < found.mdata + found.mlen; q++) ! 88: if(*q == sep) ! 89: break; ! 90: if(*q != sep) { ! 91: fprintf(stderr, "retrieved bad index:%s\n", ! 92: found.mdata); ! 93: continue; ! 94: } ! 95: newkey.mdata = ++q; ! 96: newkey.mlen = found.mlen - (q - found.mdata); ! 97: if(bseek(bd, newkey) != FOUND) { ! 98: fprintf(stderr, "erro, didn't find %s\n", ! 99: newkey.mdata); ! 100: continue; ! 101: } ! 102: bread(bd, (mbuf *)NULL &rec); ! 103: for(i = 0; i < rec.mlen; i++) ! 104: if(rec.mdata[i] != nl) ! 105: putchar(rec.mdata[i]); ! 106: else ! 107: putchar('\n'); ! 108: putchar('\n'); ! 109: } ! 110: if(cnt == 0) ! 111: printf("not found\n"); ! 112: } ! 113: /*1010001100101110*/
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.