--- freem/src/global_bltin.c 2025/04/08 14:39:21 1.8 +++ freem/src/global_bltin.c 2025/04/08 20:00:56 1.10 @@ -1,5 +1,5 @@ /* - * $Id: global_bltin.c,v 1.8 2025/04/08 14:39:21 snw Exp $ + * $Id: global_bltin.c,v 1.10 2025/04/08 20:00:56 snw Exp $ * freem database engine * * @@ -24,6 +24,12 @@ * along with FreeM. If not, see . * * $Log: global_bltin.c,v $ + * Revision 1.10 2025/04/08 20:00:56 snw + * Global handler now uses a header file and maintains the last journaling transaction ID + * + * Revision 1.9 2025/04/08 16:46:11 snw + * Add global file header and offsets + * * Revision 1.8 2025/04/08 14:39:21 snw * Initial work on global handler refactor * @@ -53,6 +59,7 @@ #include #include "mpsdef.h" +#include "journal.h" #include "global_bltin.h" global_handle *global_handles_head; @@ -197,7 +204,7 @@ nextpath: buf[k] = NUL; /* NUL not EOL !!! */ return i; -} /* global_file */ +} /* gbl_path() */ int gbl_lock(global_handle *g, int type) { @@ -207,7 +214,7 @@ int gbl_lock(global_handle *g, int type) locking (g->fd, type, 0L); g->locked = TRUE; -} +} /* gbl_lock() */ int gbl_unlock(global_handle *g) { @@ -217,7 +224,7 @@ int gbl_unlock(global_handle *g) locking (g->fd, 0, 0L); g->locked = FALSE; -} +} /* gbl_unlock() */ void gbl_close(global_handle *g) { @@ -230,7 +237,7 @@ void gbl_close(global_handle *g) g->locked = FALSE; g->opened = FALSE; } -} +} /* gbl_close() */ void gbl_close_all(void) { @@ -239,7 +246,111 @@ void gbl_close_all(void) for (g = global_handles_head; g != NULL; g = g->next) { gbl_close (g); } -} +} /* gbl_close_all() */ + +int gbl_write_initial_header(global_handle *g) +{ + global_header hdr; + unsigned long old_position; + char m[5] = GBL_MAGIC; + char msg[256]; + + if (g->opened == FALSE) { + return FALSE; + } + + memcpy (hdr.magic, m, 5); + hdr.format_version = GBL_FORMAT_VERSION; + strncpy (hdr.host_triplet, HOST, 40); + hdr.block_size = BLOCKLEN; + hdr.last_transaction_id = 0; + hdr.created = time (0L); + hdr.last_backup = -1; + + gbl_lock (g, 1); + old_position = lseek (g->fd, 0, SEEK_CUR); + lseek (g->fd, 0, SEEK_SET); + + if (write (g->fd, &hdr, sizeof (global_header)) == -1) { + snprintf (msg, sizeof (msg), "error %d writing global header for %s", errno, g->global_name); + m_fatal (msg); + } + + lseek (g->fd, old_position, SEEK_SET); + gbl_unlock (g); + + return TRUE; +} /* gbl_write_initial_header() */ + + +int gbl_write_header(global_handle *g, global_header *hdr) +{ + unsigned long old_position; + char msg[256]; + + if (g->opened == FALSE) { + return FALSE; + } + + gbl_lock (g, 1); + old_position = lseek (g->fd, 0, SEEK_CUR); + lseek (g->fd, 0, SEEK_SET); + + if (write (g->fd, hdr, sizeof (global_header)) == -1) { + snprintf (msg, sizeof (msg), "error %d writing global header for %s", errno, g->global_name); + m_fatal (msg); + } + + lseek (g->fd, old_position, SEEK_SET); + gbl_unlock (g); + + return TRUE; +} /* gbl_write_header() */ + +int gbl_read_header(global_handle *g, global_header *h) +{ + unsigned long old_position; + char m[5] = GBL_MAGIC; + + + if (g->opened == FALSE) { + return GBL_HDR_NOTOPEN; + } + + gbl_lock (g, 1); + old_position = lseek (g->fd, 0, SEEK_CUR); + lseek (g->fd, 0, SEEK_SET); + + read (g->fd, h, sizeof (global_header)); + + lseek (g->fd, old_position, SEEK_SET); + gbl_unlock (g); + + if (strncmp (h->magic, m, 5) != 0) { + return GBL_HDR_BADMAGIC; + } + if (h->format_version != GBL_FORMAT_VERSION) { + return GBL_HDR_BADVERSION; + } + if (h->block_size != BLOCKLEN) { + return GBL_HDR_BADBLOCKSIZE; + } + + return GBL_HDR_OK; +} /* gbl_read_header() */ + +int gbl_update_tid(global_handle *g) +{ + global_header h; + + if (gbl_read_header (g, &h) != GBL_HDR_OK) { + return FALSE; + } + + h.last_transaction_id = jnl_tran_id; + + return gbl_write_header (g, &h); +} /* gbl_update_tid() */ int gbl_create(global_handle *g) { @@ -260,12 +371,17 @@ int gbl_create(global_handle *g) g->age = time (0L); g->last_block = 0; g->use_count = 1; + + gbl_write_initial_header (g); return OK; -} +} /* gbl_create() */ short gbl_open(global_handle *g, short action) { + int result; + global_header h; + if (g->opened == FALSE) { while (1) { errno = 0; @@ -279,7 +395,7 @@ short gbl_open(global_handle *g, short a case EMFILE: case ENFILE: - close_all_globals (); + gbl_close_all (); continue; } @@ -295,12 +411,21 @@ short gbl_open(global_handle *g, short a } else { g->opened = TRUE; + result = gbl_read_header (g, &h); + + if (result == GBL_HDR_OK) { + g->opened = TRUE; + } + else { + gbl_close (g); + return FALSE; + } } } return g->opened; -} /* gbl_open */ +} /* gbl_open() */ global_handle *gbl_handle(char *key) { @@ -357,7 +482,7 @@ global_handle *gbl_handle(char *key) global_handles_head = g; return g; -} +} /* gbl_handle() */ /* globals management */ @@ -439,31 +564,30 @@ void global_bltin (short action, char *k { global_handle *g; + + unsigned long hdr_offset; /* these must be static variables */ - static short filedes; /* filedescr for global access */ static char filnam[256]; /* name of global/unix file */ /* the following vars may be */ /* static or dynamic */ - static unsigned long blknbr; /* block number */ -// static unsigned long oldblk; static unsigned long newblk; static unsigned long other; static long j1; static long limit; static short typ; /* block type */ - static long keyl, /* length of compacted key */ - datal, /* length of data */ - olddatal, - offset, - found, - addr, /* address of key in 'block' */ - needed, /* new bytes needed to ins. stuff */ - ret_to, /* return code */ - kill_again; + static long keyl; /* length of compacted key */ + static long datal; /* length of data */ + static long olddatal; + static long offset; + static long found; + static long addr; /* address of key in 'block' */ + static long needed; /* new bytes needed to ins. stuff */ + static long ret_to; /* return code */ + static long kill_again; static char key1[256]; static char tmp1[256]; /* intermediate storage for op= */ static char block[BLOCKLEN]; @@ -475,14 +599,16 @@ void global_bltin (short action, char *k struct stat dinf; /* get modification date */ - long savj, - savch; /* saved j and ch for multiple pathes */ - register long int i, - j, - k, - ch; - long pathscan; /* flag for repeated scan of pathlist setting an undef global */ + long savj; + long savch; /* saved j and ch for multiple pathes */ + register long int i; + register long int j; + register long int k; + register long int ch; + long pathscan; /* flag for repeated scan of pathlist setting an undef global */ + hdr_offset = sizeof (global_header); + /* process optional limitations */ if (glvnflag.all && key[0] >= '%' && key[0] <= 'z') { @@ -561,7 +687,6 @@ void global_bltin (short action, char *k if (action == getnext) { - getnflag = TRUE; varnam[0] = EOL; @@ -851,7 +976,7 @@ reopen: errno = 0; - lseek (g->fd, ROOT * BLOCKLEN, 0); + lseek (g->fd, hdr_offset + (ROOT * BLOCKLEN), SEEK_SET); write (g->fd, block, BLOCKLEN); if (errno == 0) break; @@ -877,7 +1002,7 @@ reopen: if (errno == 0) break; - lseek (g->fd, (ROOT + 1L) * BLOCKLEN, 0); + lseek (g->fd, hdr_offset + ((ROOT + 1L) * BLOCKLEN), SEEK_SET); panic (); } @@ -909,7 +1034,7 @@ tfast0: tfast1: - lseek (g->fd, (long) blknbr * (long) (BLOCKLEN), 0); + lseek (g->fd, hdr_offset + ((long) blknbr * (long) (BLOCKLEN)), SEEK_SET); read (g->fd, block, BLOCKLEN); @@ -1031,7 +1156,7 @@ tfast2: /* a KILL on an unsubscripted global deletes the entire file */ if (action == kill_sym && compactkey[0] == g_EOL) { - lseek (g->fd, ROOT, 0); + lseek (g->fd, hdr_offset + ROOT, SEEK_SET); /* note : UNIX does not tell other */ block[BTYP] = EMPTY; /* jobs that a file has been unlinked */ @@ -1064,7 +1189,7 @@ k_again: /* entry point for repeated traceblk[trx] = blknbr; traceadr[trx] = 0; - lseek (g->fd, (long) blknbr * (long) (BLOCKLEN), 0); + lseek (g->fd, hdr_offset + ((long) blknbr * (long) (BLOCKLEN)), SEEK_SET); read (g->fd, block, BLOCKLEN); typ = block[BTYP]; @@ -1232,7 +1357,7 @@ s10: { } - lseek (g->fd, (long) blknbr * (long) (BLOCKLEN), 0); + lseek (g->fd, hdr_offset + ((long) blknbr * (long) (BLOCKLEN)), SEEK_SET); write (g->fd, block, BLOCKLEN); if (traceadr[trx] == 0) update (g->fd, compactkey, keyl); @@ -1319,8 +1444,11 @@ s20: } stcpy0 (&block[++addr], data, (long) datal); - lseek (g->fd, (long) blknbr * (long) (BLOCKLEN), 0); + lseek (g->fd, hdr_offset + ((long) blknbr * (long) (BLOCKLEN)), SEEK_SET); write (g->fd, block, BLOCKLEN); + + gbl_update_tid (g); + break; @@ -1351,7 +1479,7 @@ s20: if ((blknbr = UNSIGN (block[RLPTR]) * 65536 + UNSIGN (block[RLPTR + 1]) * 256 + UNSIGN (block[RLPTR + 2]))) { - lseek (g->fd, (long) blknbr * (long) (BLOCKLEN), 0); + lseek (g->fd, hdr_offset + ((long) blknbr * (long) (BLOCKLEN)), SEEK_SET); read (g->fd, block, BLOCKLEN); j1 = UNSIGN (block[0]); @@ -1434,7 +1562,7 @@ s20: goto quit; } /* no next block */ - lseek (g->fd, (long) blknbr * (long) (BLOCKLEN), 0); + lseek (g->fd, hdr_offset + ((long) blknbr * (long) (BLOCKLEN)), SEEK_SET); read (g->fd, block, BLOCKLEN); scandblk (block, &addr, &found); @@ -1591,7 +1719,7 @@ s20: goto quit; /* no next block */ } - lseek (g->fd, (long) blknbr * (long) (BLOCKLEN), 0); + lseek (g->fd, hdr_offset + ((long) blknbr * (long) (BLOCKLEN)), SEEK_SET); read (g->fd, block, BLOCKLEN); addr = 0; @@ -1615,7 +1743,7 @@ s20: goto quit; /* no next block */ } - lseek (g->fd, (long) blknbr * (long) (BLOCKLEN), 0); + lseek (g->fd, hdr_offset + ((long) blknbr * (long) (BLOCKLEN)), SEEK_SET); read (g->fd, block, BLOCKLEN); addr = 0; @@ -1826,7 +1954,7 @@ killo: /* entry from killone section other = traceblk[--trx]; addr = traceadr[trx]; - lseek (g->fd, (long) other * (long) (BLOCKLEN), 0); + lseek (g->fd, hdr_offset + ((long) other * (long) (BLOCKLEN)), SEEK_SET); read (g->fd, block, BLOCKLEN); addr += UNSIGN (block[addr]); @@ -1846,7 +1974,7 @@ killo: /* entry from killone section trx = trxsav; - lseek (g->fd, (long) blknbr * (long) (BLOCKLEN), 0); + lseek (g->fd, hdr_offset + ((long) blknbr * (long) (BLOCKLEN)), SEEK_SET); read (g->fd, block, BLOCKLEN); offset = UNSIGN (block[OFFS]) * 256 + @@ -1953,28 +2081,28 @@ p_empty: /* entry if pointer block goes if (left) { - lseek (g->fd, (long) left * (long) (BLOCKLEN), 0); + lseek (g->fd, hdr_offset + ((long) left * (long) (BLOCKLEN)), SEEK_SET); read (g->fd, block0, BLOCKLEN); block0[RLPTR] = block[RLPTR]; block0[RLPTR + 1] = block[RLPTR + 1]; block0[RLPTR + 2] = block[RLPTR + 2]; - lseek (g->fd, (long) left * (long) (BLOCKLEN), 0); + lseek (g->fd, hdr_offset + ((long) left * (long) (BLOCKLEN)), SEEK_SET); write (g->fd, block0, BLOCKLEN); } if (right) { - lseek (g->fd, (long) right * (long) (BLOCKLEN), 0); + lseek (g->fd, hdr_offset + ((long) right * (long) (BLOCKLEN)), SEEK_SET); read (g->fd, block0, BLOCKLEN); block0[LLPTR] = block[LLPTR]; block0[LLPTR + 1] = block[LLPTR + 1]; block0[LLPTR + 2] = block[LLPTR + 2]; - lseek (g->fd, (long) right * (long) (BLOCKLEN), 0); + lseek (g->fd, hdr_offset + ((long) right * (long) (BLOCKLEN)), SEEK_SET); write (g->fd, block0, BLOCKLEN); } @@ -1992,7 +2120,7 @@ p_empty: /* entry if pointer block goes blknbr = traceblk[--trx]; addr = traceadr[trx]; - lseek (g->fd, (long) (blknbr) * (long) (BLOCKLEN), 0); + lseek (g->fd, hdr_offset + ((long) (blknbr) * (long) (BLOCKLEN)), SEEK_SET); read (g->fd, block, BLOCKLEN); offset = UNSIGN (block[OFFS]) * 256 + UNSIGN (block[OFFS + 1]); @@ -2005,7 +2133,7 @@ p_empty: /* entry if pointer block goes if (blknbr == ROOT) { /* global went empty */ - lseek (g->fd, 0L, 0); + lseek (g->fd, hdr_offset + 0L, SEEK_SET); /* note : UNIX does not tell other */ block[BTYP] = EMPTY; /* jobs that a file has been unlinked */ @@ -2037,7 +2165,7 @@ p_empty: /* entry if pointer block goes for (i = offset; i < offset + freecnt; block[i++] = 0); - lseek (g->fd, (long) (blknbr) * (long) (BLOCKLEN), 0); + lseek (g->fd, hdr_offset + ((long) (blknbr) * (long) (BLOCKLEN)), SEEK_SET); write (g->fd, block, BLOCKLEN); if (addr == 0) { /* update of pointer */ @@ -2085,7 +2213,7 @@ p_empty: /* entry if pointer block goes } block[OFFS] = offset / 256; block[OFFS + 1] = offset % 256; - lseek (g->fd, (long) blknbr * (long) (BLOCKLEN), 0); + lseek (g->fd, hdr_offset + ((long) blknbr * (long) (BLOCKLEN)), SEEK_SET); write (g->fd, block, BLOCKLEN); if (addr < 3) { /* update of pointer */ traceadr[trx] = 0; @@ -2112,7 +2240,7 @@ zinv: goto quit; } /* no previous block */ - lseek (g->fd, (long) blknbr * (long) (BLOCKLEN), 0); + lseek (g->fd, hdr_offset + ((long) blknbr * (long) (BLOCKLEN)), SEEK_SET); read (g->fd, block, BLOCKLEN); addr = UNSIGN (block[OFFS]) * 256 + @@ -2292,7 +2420,7 @@ zinv: break; /* no next block */ } - lseek (g->fd, (long) blknbr * (long) (BLOCKLEN), 0); + lseek (g->fd, hdr_offset + ((long) blknbr * (long) (BLOCKLEN)), SEEK_SET); read (g->fd, block, BLOCKLEN); addr = 0; @@ -2408,7 +2536,7 @@ quit: /* clean things up */ - lseek (g->fd, ROOT, 0); + lseek (g->fd, hdr_offset + ROOT, SEEK_SET); gbl_unlock (g); return; @@ -2439,7 +2567,7 @@ splitd: /* split data block in two se block[RLPTR + 1] = newblk % 65536 / 256; block[RLPTR + 2] = newblk % 256; - lseek (g->fd, (long) blknbr * (long) (BLOCKLEN), 0); + lseek (g->fd, hdr_offset + ((long) blknbr * (long) (BLOCKLEN)), SEEK_SET); write (g->fd, block, BLOCKLEN); block[RLPTR] = right; @@ -2459,14 +2587,14 @@ splitd: /* split data block in two se char block0[BLOCKLEN]; - lseek (g->fd, (long) other * (long) (BLOCKLEN), 0); + lseek (g->fd, hdr_offset + ((long) other * (long) (BLOCKLEN)), SEEK_SET); read (g->fd, block0, BLOCKLEN); block0[LLPTR] = blknbr / 65536; block0[LLPTR + 1] = blknbr % 65536 / 256; block0[LLPTR + 2] = blknbr % 256; - lseek (g->fd, (long) other * (long) (BLOCKLEN), 0); + lseek (g->fd, hdr_offset + ((long) other * (long) (BLOCKLEN)), SEEK_SET); write (g->fd, block0, BLOCKLEN); } @@ -2485,7 +2613,7 @@ splitd: /* split data block in two se block[LLPTR + 1] = newblk % 65536 / 256; block[LLPTR + 2] = newblk % 256; - lseek (g->fd, (long) blknbr * (long) (BLOCKLEN), 0); + lseek (g->fd, hdr_offset + ((long) blknbr * (long) (BLOCKLEN)), SEEK_SET); write (g->fd, block, BLOCKLEN); block[LLPTR] = left; @@ -2611,7 +2739,7 @@ splitd: /* split data block in two se block0[LLPTR + 1] = blknbr % 65536 / 256; block0[LLPTR + 2] = blknbr % 256; - lseek (g->fd, (long) (newblk) * (long) (BLOCKLEN), 0); + lseek (g->fd, hdr_offset + ((long) (newblk) * (long) (BLOCKLEN)), SEEK_SET); write (g->fd, block0, BLOCKLEN); offset = limit; @@ -2622,14 +2750,14 @@ splitd: /* split data block in two se /* up-date LL-PTR of RL-block */ if (other != 0) { - lseek (g->fd, (long) other * (long) (BLOCKLEN), 0); + lseek (g->fd, hdr_offset + ((long) other * (long) (BLOCKLEN)), SEEK_SET); read (g->fd, block0, BLOCKLEN); block0[LLPTR] = newblk / 65536; block0[LLPTR + 1] = newblk % 65536 / 256; block0[LLPTR + 2] = newblk % 256; - lseek (g->fd, (long) other * (long) (BLOCKLEN), 0); + lseek (g->fd, hdr_offset + ((long) other * (long) (BLOCKLEN)), SEEK_SET); write (g->fd, block0, BLOCKLEN); } @@ -2651,7 +2779,7 @@ splitd: /* split data block in two se block0[LLPTR + 1] = blknbr % 65536 / 256; block0[LLPTR + 2] = blknbr % 256; - lseek (g->fd, (long) blknbr * (long) (BLOCKLEN), 0); + lseek (g->fd, hdr_offset + ((long) blknbr * (long) (BLOCKLEN)), SEEK_SET); write (g->fd, block, BLOCKLEN); stcpy0 (block, block0, (long) BLOCKLEN); @@ -2664,14 +2792,14 @@ splitd: /* split data block in two se /* up-date LL-PTR of RL-block */ if (other != 0) { - lseek (g->fd, (long) other * (long) (BLOCKLEN), 0); + lseek (g->fd, hdr_offset + ((long) other * (long) (BLOCKLEN)), SEEK_SET); read (g->fd, block0, BLOCKLEN); block0[LLPTR] = newblk / 65536; block0[LLPTR + 1] = newblk % 65536 / 256; block0[LLPTR + 2] = newblk % 256; - lseek (g->fd, (long) other * (long) (BLOCKLEN), 0); + lseek (g->fd, hdr_offset + ((long) other * (long) (BLOCKLEN)), SEEK_SET); write (g->fd, block0, BLOCKLEN); } @@ -2708,6 +2836,10 @@ static void splitp (short filedes, char unsigned long other; register int i, j; + long hdr_offset; + + hdr_offset = sizeof (global_header); + getnewblk (filedes, &newblk); /* get a new block */ if (*blknbr == ROOT) { /* ROOT overflow is special */ @@ -2743,7 +2875,7 @@ static void splitp (short filedes, char block0[NRBLK + 2] = i % 256; block0[BTYP] = POINTER; - lseek (filedes, ROOT, 0); + lseek (filedes, hdr_offset + ROOT, SEEK_SET); write (filedes, block0, BLOCKLEN); /* shift trace_stack */ @@ -2815,7 +2947,7 @@ static void splitp (short filedes, char block0[LLPTR + 1] = (*blknbr) % 65536 / 256; block0[LLPTR + 2] = (*blknbr) % 256; - lseek (filedes, (long) (newblk) * (long) (BLOCKLEN), 0); + lseek (filedes, hdr_offset + ((long) (newblk) * (long) (BLOCKLEN)), SEEK_SET); write (filedes, block0, BLOCKLEN); (*offs) = limit; @@ -2825,14 +2957,14 @@ static void splitp (short filedes, char /* up-date LL-PTR of RL-block */ if (other != 0) { - lseek (filedes, (long) other * (long) (BLOCKLEN), 0); + lseek (filedes, hdr_offset + ((long) other * (long) (BLOCKLEN)), SEEK_SET); read (filedes, block0, BLOCKLEN); block0[LLPTR] = newblk / 65536; block0[LLPTR + 1] = newblk % 65536 / 256; block0[LLPTR + 2] = newblk % 256; - lseek (filedes, (long) other * (long) (BLOCKLEN), 0); + lseek (filedes, hdr_offset + ((long) other * (long) (BLOCKLEN)), SEEK_SET); write (filedes, block0, BLOCKLEN); } @@ -2858,7 +2990,7 @@ static void splitp (short filedes, char (*addr) -= limit; (*offs) -= limit; - lseek (filedes, (long) (*blknbr) * (long) (BLOCKLEN), 0); + lseek (filedes, hdr_offset + ((long) (*blknbr) * (long) (BLOCKLEN)), SEEK_SET); write (filedes, block, BLOCKLEN); stcpy0 (block, block0, (long) BLOCKLEN); @@ -2869,14 +3001,14 @@ static void splitp (short filedes, char /* up-date LL-PTR of RL-block */ if (other != 0) { - lseek (filedes, (long) other * (long) (BLOCKLEN), 0); + lseek (filedes, hdr_offset + ((long) other * (long) (BLOCKLEN)), SEEK_SET); read (filedes, block0, BLOCKLEN); block0[LLPTR] = newblk / 65536; block0[LLPTR + 1] = newblk % 65536 / 256; block0[LLPTR + 2] = newblk % 256; - lseek (filedes, (long) other * (long) (BLOCKLEN), 0); + lseek (filedes, hdr_offset + ((long) other * (long) (BLOCKLEN)), SEEK_SET); write (filedes, block0, BLOCKLEN); } @@ -2899,7 +3031,10 @@ static void update (short filedes, char unsigned long blknbr; char block[BLOCKLEN]; long i, j, k; + long hdr_offset; + hdr_offset = sizeof (global_header); + while (traceadr[trx] == 0) { /* update of pointer blocks necessary */ if (--trx < 0) break; @@ -2907,7 +3042,7 @@ static void update (short filedes, char blknbr = traceblk[trx]; addr = traceadr[trx]; - lseek (filedes, (long) blknbr * (long) (BLOCKLEN), 0); + lseek (filedes, hdr_offset + ((long) blknbr * (long) (BLOCKLEN)), SEEK_SET); read (filedes, block, BLOCKLEN); { @@ -2958,12 +3093,12 @@ static void update (short filedes, char while (i < keyl) block[j++] = ins_key[i++]; /* block pointed to remains the same */ - lseek (filedes, (long) blknbr * (long) (BLOCKLEN), 0); + lseek (filedes, hdr_offset + ((long) blknbr * (long) (BLOCKLEN)), SEEK_SET); write (filedes, block, BLOCKLEN); } - lseek (filedes, (long) blknbr * (long) (BLOCKLEN), 0); + lseek (filedes, hdr_offset + ((long) blknbr * (long) (BLOCKLEN)), SEEK_SET); read (filedes, block, BLOCKLEN); } @@ -2988,12 +3123,15 @@ static void insert (int filedes, char *i long needed; long addr; register int i, k; + long hdr_offset; + hdr_offset = sizeof (global_header); + trxsav = trx--; blk = traceblk[trx]; addr = traceadr[trx]; - lseek (filedes, (long) (blk) * (long) (BLOCKLEN), 0); + lseek (filedes, hdr_offset + ((long) (blk) * (long) (BLOCKLEN)), SEEK_SET); read (filedes, block, BLOCKLEN); offset = UNSIGN (block[OFFS]) * 256 + @@ -3030,7 +3168,7 @@ static void insert (int filedes, char *i block[i++] = blknbr % 65536 / 256; block[i] = blknbr % 256; - lseek (filedes, (long) (blk) * (long) (BLOCKLEN), 0); + lseek (filedes, hdr_offset + ((long) (blk) * (long) (BLOCKLEN)), SEEK_SET); write (filedes, block, BLOCKLEN); trx = trxsav; @@ -3050,25 +3188,28 @@ static void b_free (short filedes, unsig unsigned long other; long i; long offset; - + long hdr_offset; + + hdr_offset = sizeof (global_header); + /* mark block as empty */ - lseek (filedes, (long) (blknbr) * BLOCKLEN, 0); + lseek (filedes, hdr_offset + ((long) (blknbr) * BLOCKLEN), SEEK_SET); read (filedes, block0, BLOCKLEN); block0[BTYP] = EMPTY; - lseek (filedes, (long) (blknbr) * BLOCKLEN, 0); + lseek (filedes, hdr_offset + ((long) (blknbr) * BLOCKLEN), SEEK_SET); write (filedes, block0, BLOCKLEN); /* do we have a list of free blocks? */ - lseek (filedes, ROOT, 0); + lseek (filedes, hdr_offset + ROOT, SEEK_SET); read (filedes, block0, BLOCKLEN); if ((free = UNSIGN (block0[FREE]) * 65536 + UNSIGN (block0[FREE + 1]) * 256 + UNSIGN (block0[FREE + 2]))) { for (;;) { - lseek (filedes, (long) free * (long) BLOCKLEN, 0); + lseek (filedes, hdr_offset + ((long) free * (long) BLOCKLEN), SEEK_SET); read (filedes, block0, BLOCKLEN); other = UNSIGN (block0[RLPTR]) * 65536 + @@ -3101,7 +3242,7 @@ static void b_free (short filedes, unsig block0[RLPTR + 1] = other % 65536 / 256; block0[RLPTR + 2] = other % 256; - lseek (filedes, (long) free * (long) BLOCKLEN, 0); + lseek (filedes, hdr_offset + ((long) free * (long) BLOCKLEN), SEEK_SET); write (filedes, block0, BLOCKLEN); for (i = 0; i < BLOCKLEN; block0[i++] = 0); /* clear block */ @@ -3122,14 +3263,14 @@ static void b_free (short filedes, unsig getnewblk (filedes, &free); /* set FBLK free blocks pointer */ - lseek (filedes, ROOT, 0); + lseek (filedes, hdr_offset + ROOT, SEEK_SET); read (filedes, block0, BLOCKLEN); block0[FREE] = free / 65536; block0[FREE + 1] = free % 65536 / 256; block0[FREE + 2] = free % 256; - lseek (filedes, ROOT, 0); + lseek (filedes, hdr_offset + ROOT, SEEK_SET); write (filedes, block0, BLOCKLEN); for (i = 0; i < BLOCKLEN; block0[i++] = 0); /* clear block */ @@ -3145,7 +3286,7 @@ static void b_free (short filedes, unsig block0[OFFS] = offset / 256; block0[OFFS + 1] = offset % 256; - lseek (filedes, (long) free * (long) BLOCKLEN, 0); + lseek (filedes, hdr_offset + ((long) free * (long) BLOCKLEN), SEEK_SET); write (filedes, block0, BLOCKLEN); return; @@ -3272,8 +3413,11 @@ static void getnewblk (int filedes, unsi unsigned long freeblks, no_of_blks; long other; long offset; + long hdr_offset; - lseek (filedes, ROOT, 0); + hdr_offset = sizeof (global_header); + + lseek (filedes, hdr_offset + ROOT, SEEK_SET); read (filedes, nblock, BLOCKLEN); freeblks = UNSIGN (nblock[FREE]) * 65536 + UNSIGN (nblock[FREE + 1]) * 256 + UNSIGN (nblock[FREE + 2]); @@ -3281,7 +3425,7 @@ static void getnewblk (int filedes, unsi if (freeblks) { - lseek (filedes, (long) (freeblks) * BLOCKLEN, 0); + lseek (filedes, hdr_offset + ((long) (freeblks) * BLOCKLEN), SEEK_SET); read (filedes, nblock, BLOCKLEN); offset = UNSIGN (nblock[OFFS]) * 256 + UNSIGN (nblock[OFFS + 1]); @@ -3294,27 +3438,27 @@ static void getnewblk (int filedes, unsi /* update RL-block, if any */ if (other) { - lseek (filedes, (long) (other) * BLOCKLEN, 0); + lseek (filedes, hdr_offset + ((long) (other) * BLOCKLEN), SEEK_SET); read (filedes, nblock, BLOCKLEN); nblock[LLPTR] = 0; nblock[LLPTR + 1] = 0; nblock[LLPTR + 2] = 0; - lseek (filedes, (long) (other) * BLOCKLEN, 0); + lseek (filedes, hdr_offset + ((long) (other) * BLOCKLEN), SEEK_SET); write (filedes, nblock, BLOCKLEN); } /* update ROOT block */ - lseek (filedes, ROOT, 0); + lseek (filedes, hdr_offset + ROOT, SEEK_SET); read (filedes, nblock, BLOCKLEN); nblock[FREE] = other / 65536; nblock[FREE + 1] = other % 65536 / 256; nblock[FREE + 2] = other % 256; - lseek (filedes, ROOT, 0); + lseek (filedes, hdr_offset + ROOT, SEEK_SET); write (filedes, nblock, BLOCKLEN); return; @@ -3328,7 +3472,7 @@ static void getnewblk (int filedes, unsi nblock[OFFS] = offset / 256; nblock[OFFS + 1] = offset % 256; - lseek (filedes, (long) (freeblks) * BLOCKLEN, 0); + lseek (filedes, hdr_offset + ((long) (freeblks) * BLOCKLEN), SEEK_SET); write (filedes, nblock, BLOCKLEN); return; @@ -3341,7 +3485,7 @@ static void getnewblk (int filedes, unsi nblock[NRBLK + 1] = no_of_blks % 65536 / 256; nblock[NRBLK + 2] = no_of_blks % 256; - lseek (filedes, ROOT, 0); + lseek (filedes, hdr_offset + ROOT, SEEK_SET); write (filedes, nblock, BLOCKLEN); *blknbr = no_of_blks; @@ -3350,7 +3494,7 @@ static void getnewblk (int filedes, unsi errno = 0; - lseek (filedes, (long) (no_of_blks) * BLOCKLEN, 0); + lseek (filedes, hdr_offset + ((long) (no_of_blks) * BLOCKLEN), SEEK_SET); write (filedes, nblock, BLOCKLEN); if (errno == 0) break; @@ -3529,26 +3673,13 @@ short g_numeric (char *str) } /* end g_numeric() */ + +/* DEPRECATED: use gbl_close_all() instead */ void close_all_globals (void) { - register int i; - - for (i = 0; i < NO_GLOBLS; i++) { - - if (oldfil[i][0] != NUL) { - - close (olddes[i]); - - usage[i] = 0; - olddes[i] = 0; - oldfil[i][0] = NUL; - - } - - } - + gbl_close_all (); + return; - } /* end close_all_globals() */ static void panic (void)