Corrected Code for TechNotes V3.I1 Evaluating Space Usage in C-ISAM Files
Posted in 1993
The program in the article, "Evaluating Space Usage in C-ISAM Files," printed in Tech Notes, Volume 3, Issue 1, 1993, has been updated. This online version now reflects the correct information, please update accordingly. -----------------------[CUT HERE]-------------------------------- #include<fcntl.h> extern errno; long ldlong(); /* load an INTEGER ISAM format into a long C format */ short ldint(); /* load a SHORTINT ISAM format into a short C format */ struct dict /* structure at beginning of .idx file */ { char d_magic[2]; char d_dummy1[4]; char d_idxsize[2]; char d_nkeys[2]; char d_cflag[1]; char d_dflag[1]; char d_version[1]; char d_recsize[2]; char d_keysnode[4]; char d_dummy2[6]; char d_freedata[4]; char d_freenode[4]; char d_nextdata[4]; char d_nextnode[4]; char d_transaction[4]; char d_uniqid[4]; char d_audit[4]; char d_lkmethod[2]; char d_altcnt[4]; char d_maxsize[2]; }; main(ac, av) char *av[]; { struct dict *dictp; /* pointer to dict structure */ int fpidx, fpdat; /* descriptors for .idx and .dat files */ char idxname[64], datname[64]; /* name of .idx and .dat files */ char *recp; /* pointer to ISAM record */ char *valid; /* pointer to last character of ISAM record used to know if record is valid or deleted */ long cntval=0; /* counter for valid records */ long cntdel=0; /* counter for deleted records */ if(ac<2) { printf("Usage : %s filename\\n", av[0]); exit(1); } strcpy(datname, av[1]); strcpy(idxname, av[1]); strcat(datname, ".dat"); strcat(idxname, ".idx"); /* getting descriptor for .idx and .dat files */ if((fpidx = open(idxname, O_RDONLY)) < 0) { printf("error on IDX file: open() [errno == %d]\\n", errno); exit(1); } if((fpdat = open(datname, O_RDONLY)) < 0) { printf("error on DAT file: open() [errno == %d]\\n", errno); exit(1); } /* dict structure receiving information from .idx file */ dictp = (struct dict *) malloc(sizeof(struct dict)); if(dictp == (struct dict *) 0) { printf("error getting DICT: malloc() [errno == %d]\\n", errno); exit(1); } /* read from .idx file, initialize dict structure */ if((read(fpidx, dictp, sizeof(struct dict))) == 0) { printf("error getting DICT: read() [errno == %d]\\n", errno); exit(1); } /* allocate memory for receiving record */ recp = (char *) malloc(ldint(dictp->d_recsize) + 1); if(recp == (char *) 0) { printf("error reading record: malloc() [errno == %d]\\n", errno); exit(1); } /* valid character is the last of ISAM record */ valid = recp+ldint(dictp->d_recsize); /* read the whole ISAM dat file and count deleted records. and valid ones */ while(read(fpdat, recp, ldint(dictp->d_recsize) + 1) == (ldint(dictp->d_recsize) + 1)) { if(*valid == '\\n') /* this record is a valid one */ { cntval++; } else { cntdel++; } } printf("record size -> %d\\n", ldint(dictp->d_recsize)); printf("nb of valid records -> %ld\\n", cntval); printf("actual rowid is -> %ld\\n", ldlong(dictp->d_nextdata)); printf("nb of unused records slots -> %ld\\n", cntdel); free(dictp); free(recp); close(fpidx); close(fpdat); exit(0); } #define BYTESHFT 8 #define BYTEMASK 0xff long ldlong(p) register char *p; { return((((((((long) p[0] << BYTESHFT) + (p[1] & BYTEMASK)) << BYTESHFT) + (p[2] & BYTEMASK)) << BYTESHFT) + (p[3] & BYTEMASK))); } short ldint(p) register char *p; { return(((short) p[0] << BYTESHFT) + (p[1] & BYTEMASK)); } -- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% William Daul Advanced Support INFORMIX SOFTWARE INC. 4100 Bohannon Dr. (415) 926-6488 - wk Menlo Park, CA. 94025 uunet!infmx!billd or billd@informix.com