{
dbhead_t *dbh;
struct dbf_dhead dbhead;
- dbfield_t *dbf, *cur_f;
- int ret, nfields, offset;
+ dbfield_t *dbf, *cur_f, *tdbf;
+ int ret, nfields, offset, gf_retval;
if ((dbh = (dbhead_t *)malloc(sizeof(dbhead_t))) == NULL)
return NULL;
dbhead.dbh_date[DBH_DATE_MONTH],
dbhead.dbh_date[DBH_DATE_DAY]);
- dbh->db_nfields = nfields = (dbh->db_hlen - sizeof(struct dbf_dhead)) /
- sizeof(struct dbf_dfield);
-
- /* get all the field info */
- dbf = (dbfield_t *)malloc(sizeof(dbfield_t) * nfields);
-
+ /* malloc enough memory for the maximum number of fields:
+ 32 * 254 = 8128 bytes */
+ tdbf = (dbfield_t *)malloc(sizeof(dbfield_t)*254);
+
offset = 1;
- for (cur_f = dbf; cur_f < &dbf[nfields] ; cur_f++) {
- if (get_dbf_field(dbh, cur_f) < 0) {
+ nfields = 0;
+ gf_retval = 0;
+ for (cur_f = tdbf; gf_retval < 2 && nfields < 254; cur_f++) {
+ gf_retval = get_dbf_field(dbh, cur_f);
+
+ if (gf_retval < 0) {
free_dbf_head(dbh);
return NULL;
}
- cur_f->db_foffset = offset;
- offset += cur_f->db_flen;
+ if (gf_retval != 2 ) {
+ cur_f->db_foffset = offset;
+ offset += cur_f->db_flen;
+ nfields++;
+ }
}
+ dbh->db_nfields = nfields;
+
+ /* malloc the right amount of space for records, copy and destroy old */
+ dbf = (dbfield_t *)malloc(sizeof(dbfield_t)*nfields);
+ memcpy(dbf, tdbf, sizeof(dbfield_t)*nfields);
+ free(tdbf);
+
dbh->db_fields = dbf;
return dbh;
return ret;
}
+ /* Check for the '0Dh' field terminator , if found return '2'
+ which will tell the loop we are at the end of fields */
+ if (dbfield.dbf_name[0]==0x0d) {
+ return 2;
+ }
+
/* build the field name */
copy_crimp(dbf->db_fname, dbfield.dbf_name, DBF_NAMELEN);
break;
default:
dbf->db_flen = get_short(dbfield.dbf_flen);
+ break;
}
if ((dbf->db_format = get_dbf_f_fmt(dbf)) == NULL) {