--- freem/src/global_bltin.c 2025/04/11 20:55:49 1.21 +++ freem/src/global_bltin.c 2025/04/13 04:22:43 1.22 @@ -1,5 +1,5 @@ /* - * $Id: global_bltin.c,v 1.21 2025/04/11 20:55:49 snw Exp $ + * $Id: global_bltin.c,v 1.22 2025/04/13 04:22:43 snw Exp $ * freem database engine * * @@ -24,6 +24,9 @@ * along with FreeM. If not, see . * * $Log: global_bltin.c,v $ + * Revision 1.22 2025/04/13 04:22:43 snw + * Fix snprintf calls + * * Revision 1.21 2025/04/11 20:55:49 snw * Disable -Wunused-result where possible * @@ -111,45 +114,6 @@ short g_numeric (char *str); void close_all_globals(void); static void panic (void); -#define ROOT 0L - -/* end of line symbol in global module is 30, which is a code not */ -/* otherwise used in subscripts */ -#define g_EOL 30 - -#define EOL1 EOL - -/* numerics (('.'<<1)&037)==28 ; (('-'<<1)&037)==26; */ -#define POINT 28 -#define MINUS 26 - -/* ALPHA and OMEGA are dummy subscripts in $order processing */ -/* ALPHA sorts before all other subscripts */ -/* OMEGA sorts after all other subscripts */ -/* e.g. ("abc") -> "abc",OMEGA ; ("abc","") -> "abc",ALPHA */ -#define OMEGA 29 -#define ALPHA 31 - -/* length of blocks. status bytes defined as offset to blocklength */ -/* BLOCKLEN 1024 is defined in mpsdef0 include file */ -#define DATALIM (BLOCKLEN-11) -#define LLPTR (BLOCKLEN-10) -#define NRBLK LLPTR -#define COLLA (BLOCKLEN- 7) -#define RLPTR (BLOCKLEN- 6) -#define FREE RLPTR -#define BTYP (BLOCKLEN- 3) -#define OFFS (BLOCKLEN- 2) - -/* length of blockpointers in bytes */ -#define PLEN 3 - -#define EMPTY 0 -#define FBLK 1 -#define POINTER 2 -#define BOTTOM 6 -#define DATA 8 - #if !defined(__OpenBSD__) && !defined(_AIX) && !defined(__osf__) && !defined(MSDOS) && !defined(__vax__) && !defined(__OS2__) long time (); #endif @@ -315,7 +279,7 @@ int gbl_write_initial_header(global_hand 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); + snprintf (msg, sizeof (msg) - 1, "error %d writing global header for %s", errno, g->global_name); m_fatal (msg); } @@ -340,7 +304,7 @@ int gbl_write_header(global_handle *g, g 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); + snprintf (msg, sizeof (msg) - 1, "error %d writing global header for %s", errno, g->global_name); m_fatal (msg); } @@ -554,7 +518,7 @@ int gbl_write_block(global_handle *g, un errno = 0; lseek (g->fd, hdr_offset + (blocknum * g->header.block_size), SEEK_SET); - write (g->fd, block, BLOCKLEN); + write (g->fd, block, g->header.block_size); errsav = errno; g->last_block = blocknum; g->use_count++; @@ -1008,8 +972,7 @@ void global_bltin (short action, char *k reopen: - gbl_open (g, action); - if (g->fd == -1) { + if (!gbl_open (g, action)) { /* file not found */ if (action != set_sym) { @@ -1092,19 +1055,9 @@ reopen: block[BTYP] = DATA; /* type */ block[OFFS] = i / 256; block[OFFS + 1] = i % 256; - - for (;;) { - - errno = 0; - write (g->fd, block, BLOCKLEN); - - if (errno == 0) break; - - lseek (g->fd, hdr_offset + ((ROOT + 1L) * BLOCKLEN), SEEK_SET); - panic (); - - } + gbl_write_block (g, ROOT + 1, block); + gbl_close (g); gbl_unlock (g); gbl_open (g, action); @@ -1126,21 +1079,20 @@ reopen: tfast0: gbl_lock (g, 3); - if (g->fast_path > 0) goto tfast1; /* try again last block */ + if (g->fast_path > 0) { + goto tfast1; /* try again last block */ + } blknbr = g->last_block = ROOT; /* start with ROOT block */ for (;;) { - - -tfast1: + tfast1: gbl_read_block (g, blknbr, block); /* temporarily disabled tfast2: */ if ((typ = block[BTYP]) == DATA) { /* scan data block: here we test for equality only */ - offset = UNSIGN (block[OFFS]) * 256 + UNSIGN (block[OFFS + 1]); j = UNSIGN (block[0]); @@ -1149,9 +1101,7 @@ tfast1: stcpy0 (key1, &block[2], j); /* get first key */ ch = keyl; /* ch is a register! */ - - while (i < offset) { - + while (i < offset) { j = UNSIGN (block[i++]); /* length of key - offset */ k = UNSIGN (block[i++]); /* offset into previous entry */ @@ -1159,13 +1109,11 @@ tfast1: while (k < j) key1[k++] = block[i++]; /* get key */ - if (j != ch) { /* keys have different length */ - + if (j != ch) { /* keys have different length */ i += UNSIGN (block[i]); i++; - continue; - + continue; } j = ch; @@ -1173,21 +1121,17 @@ tfast1: do { j--; } while (compactkey[j] == key1[j]); /* compare keys */ - - - if (j < 0) { - + + if (j < 0) { k = UNSIGN (block[i++]); stcpy0 (data, &block[i], k); /* get value */ data[k] = EOL1; /* append EOL */ - goto quit; - + goto quit; } i += UNSIGN (block[i]); - i++; /* skip data */ - + i++; /* skip data */ } /* fast access failed. try normal path */