--- freem/src/global_bltin.c 2025/03/09 19:14:25 1.4
+++ freem/src/global_bltin.c 2025/04/08 14:39:21 1.8
@@ -1,5 +1,5 @@
/*
- * $Id: global_bltin.c,v 1.4 2025/03/09 19:14:25 snw Exp $
+ * $Id: global_bltin.c,v 1.8 2025/04/08 14:39:21 snw Exp $
* freem database engine
*
*
@@ -24,6 +24,18 @@
* along with FreeM. If not, see .
*
* $Log: global_bltin.c,v $
+ * Revision 1.8 2025/04/08 14:39:21 snw
+ * Initial work on global handler refactor
+ *
+ * Revision 1.7 2025/03/24 04:13:11 snw
+ * Replace action macro dat with fra_dat to avoid symbol conflict on OS/2
+ *
+ * Revision 1.6 2025/03/24 01:33:30 snw
+ * Guard declaration of time function in global_bltin.c for portability
+ *
+ * Revision 1.5 2025/03/22 22:52:24 snw
+ * Add STRLEN_GBL macro to manage global string length
+ *
* Revision 1.4 2025/03/09 19:14:25 snw
* First phase of REUSE compliance and header reformat
*
@@ -37,11 +49,13 @@
#include
#include
#include
-
+#include
#include
-
#include "mpsdef.h"
+#include "global_bltin.h"
+
+global_handle *global_handles_head;
static void b_free (short filedes, unsigned long blknbr);
static void splitp (short filedes, char *block, long *addr, long *offs, unsigned long *blknbr);
@@ -95,10 +109,257 @@ static void panic (void);
#define BOTTOM 6
#define DATA 8
-#if !defined(__OpenBSD__) && !defined(_AIX) && !defined(__osf__) && !defined(MSDOS) && !defined(__vax__)
+#if !defined(__OpenBSD__) && !defined(_AIX) && !defined(__osf__) && !defined(MSDOS) && !defined(__vax__) && !defined(__OS2__)
long time ();
#endif
+inline long gbl_path(char *key, char *buf)
+{
+ 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 */
+
+ /* construct full UNIX filename */
+ savj = 0;
+ savch = ch = EOL;
+ pathscan = TRUE;
+nextpath:
+ k = 0;
+ j = savj;
+
+ if (key[1] == '%' || key[1] == '$') { /* %-globals and SSVN backing storage, no explicit path */
+
+ if (gloplib[0] != EOL) {
+
+ /* append % global access path */
+ while ((ch = buf[k++] = gloplib[j++]) != ':' && ch != EOL);
+
+ }
+
+ }
+ else if (key[1] != '/') { /* no explicit path specified */
+
+ if (glopath[0] != EOL) {
+
+ /* append global access path */
+ while ((ch = buf[k++] = glopath[j++]) != ':' && ch != EOL);
+
+ }
+
+ }
+
+ if (savj == 0 && ch == EOL) pathscan = FALSE; /* one path only: inhibit search */
+
+ if (k > 0) {
+
+ if (k == 1 || (k == 2 && buf[0] == '.')) {
+ k = 0;
+ }
+ else {
+ buf[k - 1] = '/';
+ }
+
+ }
+
+ savch = ch;
+ savj = j;
+ i = 0;
+ j = 0;
+
+ while (key[i] != EOL) {
+
+ if ((buf[k] = key[i]) == DELIM) break;
+
+ if (buf[k] == '/') {
+
+ j = i;
+
+ if (k > i) {
+ i = 0;
+ j = 0;
+ k = 0;
+
+ continue;
+ }
+
+ }
+
+ i++;
+ k++;
+
+ }
+
+ buf[k] = NUL; /* NUL not EOL !!! */
+
+ return i;
+} /* global_file */
+
+int gbl_lock(global_handle *g, int type)
+{
+ if (g->locked == TRUE || lonelyflag == TRUE) {
+ return TRUE;
+ }
+
+ locking (g->fd, type, 0L);
+ g->locked = TRUE;
+}
+
+int gbl_unlock(global_handle *g)
+{
+ if (g->locked == FALSE || lonelyflag == TRUE) {
+ return TRUE;
+ }
+
+ locking (g->fd, 0, 0L);
+ g->locked = FALSE;
+}
+
+void gbl_close(global_handle *g)
+{
+ if (g->opened == TRUE) {
+ close (g->fd);
+
+ g->use_count = 0;
+ g->age = 0;
+ g->last_block = 0;
+ g->locked = FALSE;
+ g->opened = FALSE;
+ }
+}
+
+void gbl_close_all(void)
+{
+ global_handle *g;
+
+ for (g = global_handles_head; g != NULL; g = g->next) {
+ gbl_close (g);
+ }
+}
+
+int gbl_create(global_handle *g)
+{
+ while (1) {
+ errno = 0;
+
+ if ((g->fd = creat (g->global_path, 0666)) != -1) break;
+
+ if (errno == EMFILE || errno == ENFILE) {
+ gbl_close_all ();
+ continue;
+ }
+
+ return PROTECT;
+ }
+
+ g->opened = TRUE;
+ g->age = time (0L);
+ g->last_block = 0;
+ g->use_count = 1;
+
+ return OK;
+}
+
+short gbl_open(global_handle *g, short action)
+{
+ if (g->opened == FALSE) {
+ while (1) {
+ errno = 0;
+ g->fd = open (g->global_path, 2);
+
+ if (g->fd != -1) break;
+
+ switch (errno) {
+ case EINTR:
+ continue;
+
+ case EMFILE:
+ case ENFILE:
+ close_all_globals ();
+ continue;
+ }
+
+ break;
+ }
+
+ if (g->fd == -1) {
+ g->use_count = 0;
+ g->age = 0;
+ g->last_block = 0;
+ g->locked = FALSE;
+ g->opened = FALSE;
+ }
+ else {
+ g->opened = TRUE;
+ }
+ }
+
+ return g->opened;
+
+} /* gbl_open */
+
+global_handle *gbl_handle(char *key)
+{
+ global_handle *g;
+ char global_name[256];
+ int i;
+ long path_len;
+ char block[BLOCKLEN];
+ struct stat dinf;
+
+ i = 0;
+ while (key[i] != EOL) {
+ if ((global_name[i] = key[i]) == DELIM) break;
+
+ i++;
+ }
+ global_name[i] = NUL;
+
+
+ for (g = global_handles_head; g != NULL; g = g->next) {
+ if (strncmp (g->global_name, global_name, 256) == 0) {
+ g->use_count++;
+ if (!lonelyflag) {
+ g->fast_path = 0;
+ }
+
+ fstat (g->fd, &dinf);
+ if (g->age > dinf.st_mtime) {
+ g->fast_path = 2;
+ return g;
+ }
+
+ g->age = time (0L);
+ g->fast_path = 0;
+
+ return g;
+ }
+ }
+ g = (global_handle *) malloc (sizeof (global_handle));
+ NULLPTRCHK(g,"gbl_open");
+
+ g->use_count = 1;
+ g->locked = FALSE;
+ g->age = time (0L);
+ g->last_block = 0;
+ g->opened = FALSE;
+ g->fd = 0;
+ g->fast_path = -1;
+
+ strcpy (g->global_name, global_name);
+ gbl_path (key, g->global_path);
+
+ g->next = global_handles_head;
+ global_handles_head = g;
+
+ return g;
+}
+
+
/* globals management */
/* 0 = set_sym 1 = get_sym */
@@ -173,10 +434,12 @@ static void panic (void);
* the file is *not* closed on return. since access is regulated by the
* locking mechanism, that will not spell trouble.
*/
-
+
void global_bltin (short action, char *key, char *data)
{
+ global_handle *g;
+
/* these must be static variables */
static short filedes; /* filedescr for global access */
@@ -186,7 +449,7 @@ void global_bltin (short action, char *k
/* static or dynamic */
static unsigned long blknbr; /* block number */
- static unsigned long oldblk;
+// static unsigned long oldblk;
static unsigned long newblk;
static unsigned long other;
static long j1;
@@ -208,6 +471,8 @@ void global_bltin (short action, char *k
static int tryfast; /* try fast access if get_sym on */
/* previous global */
+ int iresult;
+
struct stat dinf; /* get modification date */
long savj,
@@ -218,7 +483,6 @@ void global_bltin (short action, char *k
ch;
long pathscan; /* flag for repeated scan of pathlist setting an undef global */
-
/* process optional limitations */
if (glvnflag.all && key[0] >= '%' && key[0] <= 'z') {
@@ -295,6 +559,7 @@ void global_bltin (short action, char *k
}
}
+
if (action == getnext) {
getnflag = TRUE;
@@ -370,90 +635,9 @@ void global_bltin (short action, char *k
}
}
- /* construct full UNIX filename */
- savj = 0;
- savch = ch = EOL;
- pathscan = TRUE;
- nextpath:
- k = 0;
- j = savj;
-
- if (key[1] == '%' || key[1] == '$') { /* %-globals and SSVN backing storage, no explicit path */
-
- if (gloplib[0] != EOL) {
-
- /* append % global access path */
- while ((ch = filnam[k++] = gloplib[j++]) != ':' && ch != EOL);
-
- }
-
- }
- else if (key[1] != '/') { /* no explicit path specified */
-
- if (glopath[0] != EOL) {
-
- /* append global access path */
- while ((ch = filnam[k++] = glopath[j++]) != ':' && ch != EOL);
-
- }
-
- }
-
- if (savj == 0 && ch == EOL) pathscan = FALSE; /* one path only: inhibit search */
-
- if (k > 0) {
-
- if (k == 1 || (k == 2 && filnam[0] == '.')) {
- k = 0;
- }
- else {
- filnam[k - 1] = '/';
- }
-
- }
-
- savch = ch;
- savj = j;
- i = 0;
- j = 0;
-
- while (key[i] != EOL) {
-
- if ((filnam[k] = key[i]) == DELIM) break;
+ g = gbl_handle (key);
+ i = gbl_path (key, filnam);
- if (filnam[k] == '/') {
-
- j = i;
-
- if (k > i) {
- i = 0;
- j = 0;
- k = 0;
-
- continue;
- }
-
- }
-
- i++;
- k++;
-
- }
-
- filnam[k] = NUL; /* NUL not EOL !!! */
-
- /* if a unix directory is specified, reposition '^' */
- /* '^/usr/test' becomes '/usr/^test' */
- if (j > 0) {
-
- for (k = 0; k < j; k++) {
- filnam[k] = filnam[k + 1];
- }
-
- filnam[j] = '^';
-
- }
-
/* compact key to internal format: characters are shifted left */
/* delimiters become LSB of previous character */
/* test subscripts for being numeric or not */
@@ -589,93 +773,11 @@ void global_bltin (short action, char *k
compactkey[k] = g_EOL;
- /* look whether file is already open */
- tryfast = FALSE;
- ch = usage[i = j = inuse];
-
- while (i < NO_GLOBLS) {
-
- k = 0;
-
- while (filnam[k] == oldfil[i][k]) {
-
- if (filnam[k++] == NUL) {
-
- filedes = olddes[i];
-
- if (inuse == i && action == get_sym) {
-
- tryfast = TRUE;
-
- if (usage[i] != (-1)) usage[i]++;
- if (lonelyflag) goto tfast2;
-
- fstat (filedes, &dinf);
-
- if (g_ages[i] > dinf.st_mtime) goto tfast2;
-
- g_ages[i] = time (0L);
-
- goto tfast0;
-
- }
-
- inuse = i;
-
- if (usage[i] != (-1)) usage[i]++;
-
- goto lock;
- }
-
- }
-
- if (ch < 0 || (usage[i] >= 0 && usage[i] < ch)) ch = usage[j = i];
-
- if (i++ == inuse) {
- inuse = (-1);
- i = 0;
- }
-
- }
-
- inuse = j;
- usage[j] = 1;
-
- /* close previous file */
- if ((filedes = olddes[j]) > 0) {
- close (filedes);
- olddes[j] = 0;
- }
-
- strcpy (oldfil[j], filnam); /* save current filename */
-
reopen:
- for (;;) {
+ gbl_open (g, action);
+ if (g->fd == -1) {
- errno = 0;
-
- if ((filedes = open (filnam, 2)) != -1) break;
- if (errno == EINTR) continue;
-
- if (errno == EMFILE || errno == ENFILE) { /* too many open files now */
- close_all_globals ();
-
- continue;
- }
-
- break;
- }
-
- if (filedes == -1) {
-
- usage[inuse] = 0;
- oldfil[inuse][0] = NUL;
- olddes[inuse] = 0;
- g_ages[inuse] = 0;
-
- if ((pathscan || errno != ENOENT) && (savch != EOL)) goto nextpath; /* try next access path */
-
/* file not found */
if (action != set_sym) {
@@ -684,7 +786,7 @@ reopen:
return;
}
- if (action == dat || action == zdata) {
+ if (action == fra_dat || action == zdata) {
data[0] = '0';
data[1] = EOL1;
@@ -708,15 +810,7 @@ reopen:
merr_raise (PROTECT);
return;
}
-
- if (pathscan) {
- savj = 0;
- savch = ch = EOL;
- pathscan = FALSE;
-
- goto nextpath;
- }
-
+
if (setop) {
tmp1[0] = EOL;
@@ -746,29 +840,19 @@ reopen:
block[NRBLK + 2] = ROOT + 1; /* nr. of blocks */
/* create file, write_lock it and initialize root block */
- for (;;) {
-
- errno = 0;
-
- if ((filedes = creat (filnam, 0666)) != -1) break;
+ gbl_lock (g, 1);
- if (errno == EMFILE || errno == ENFILE) {
- close_all_globals ();
- continue;
- }
-
- merr_raise (PROTECT);
+ if ((iresult = gbl_create (g)) != OK) {
+ merr_raise (iresult);
return;
- }
-
- if (lonelyflag == FALSE) locking (filedes, 1, 0L);
+ }
for (;;) {
errno = 0;
- lseek (filedes, ROOT * BLOCKLEN, 0);
- write (filedes, block, BLOCKLEN);
+ lseek (g->fd, ROOT * BLOCKLEN, 0);
+ write (g->fd, block, BLOCKLEN);
if (errno == 0) break;
@@ -789,25 +873,23 @@ reopen:
for (;;) {
errno = 0;
- write (filedes, block, BLOCKLEN);
+ write (g->fd, block, BLOCKLEN);
if (errno == 0) break;
- lseek (filedes, (ROOT + 1L) * BLOCKLEN, 0);
+ lseek (g->fd, (ROOT + 1L) * BLOCKLEN, 0);
panic ();
}
- close (filedes);
-
- if (lonelyflag == FALSE) locking (filedes, 0, 0L); /* unlock */
-
+ gbl_close (g);
+ gbl_unlock (g);
+ gbl_open (g, action);
+
/* close new file, so other users can find it */
return;
}
- olddes[inuse] = filedes; /* save current filedescriptor */
-
/* request global for exclusive use */
/* odd numbered actions get read access (get_sym,data,fra_order) 3 */
/* even ones read/write access (set_sym,kill_sym) 1 */
@@ -817,19 +899,18 @@ lock:
if (action == get_sym) {
tfast0:
-
- if (lonelyflag == FALSE) locking (filedes, 3, 0L);
+ gbl_lock (g, 3);
if (tryfast) goto tfast1; /* try again last block */
- blknbr = oldblk = ROOT; /* start with ROOT block */
+ blknbr = g->last_block = ROOT; /* start with ROOT block */
for (;;) {
tfast1:
- lseek (filedes, (long) blknbr * (long) (BLOCKLEN), 0);
- read (filedes, block, BLOCKLEN);
+ lseek (g->fd, (long) blknbr * (long) (BLOCKLEN), 0);
+ read (g->fd, block, BLOCKLEN);
tfast2:
@@ -873,7 +954,6 @@ tfast2:
}
- /* key1[j]=g_EOL; */
j = ch;
do {
@@ -917,7 +997,8 @@ tfast2:
if (typ == EMPTY) {
if (blknbr == ROOT) {
- close (filedes);
+ //close (filedes);
+ gbl_close (g);
goto reopen;
}
@@ -932,41 +1013,35 @@ tfast2:
addr += UNSIGN (block[addr]) + 2; /* skip key */
- if ((blknbr = UNSIGN (block[addr]) * 65536 + UNSIGN (block[addr + 1]) * 256 + UNSIGN (block[addr + 2])) == oldblk) {
+ if ((blknbr = UNSIGN (block[addr]) * 65536 + UNSIGN (block[addr + 1]) * 256 + UNSIGN (block[addr + 2])) == g->last_block) {
merr_raise (DBDGD);
goto quit;
}
addr += PLEN; /* skip data */
- oldblk = blknbr;
+ g->last_block = blknbr;
if (merr () == INRPT) goto quit;
}
} /* end of get_sym */
-
- if (lonelyflag == FALSE) locking (filedes, (action & 01 ? 3 : 1), 0L);
-
+ gbl_lock (g, action & 01 ? 3 : 1);
+
/* a KILL on an unsubscripted global deletes the entire file */
if (action == kill_sym && compactkey[0] == g_EOL) {
- lseek (filedes, ROOT, 0);
+ lseek (g->fd, ROOT, 0);
/* note : UNIX does not tell other */
block[BTYP] = EMPTY; /* jobs that a file has been unlinked */
/* as long as they keep it open. */
/* so we mark this global as EMPTY */
- write (filedes, block, BLOCKLEN);
-
- if (lonelyflag == FALSE) locking (filedes, 0, 0L); /* unlock */
-
- close (filedes);
-
- olddes[inuse] = 0;
- oldfil[inuse][0] = NUL;
- usage[inuse] = 0;
+ write (g->fd, block, BLOCKLEN);
+
+ gbl_unlock (g);
+ gbl_close (g);
unlink (filnam);
@@ -976,7 +1051,7 @@ tfast2:
k_again: /* entry point for repeated kill operations */
/* scan tree for the proper position of key */
- blknbr = oldblk = ROOT; /* start with ROOT block */
+ blknbr = g->last_block = ROOT; /* start with ROOT block */
trx = (-1);
for (;;) {
@@ -989,8 +1064,8 @@ k_again: /* entry point for repeated
traceblk[trx] = blknbr;
traceadr[trx] = 0;
- lseek (filedes, (long) blknbr * (long) (BLOCKLEN), 0);
- read (filedes, block, BLOCKLEN);
+ lseek (g->fd, (long) blknbr * (long) (BLOCKLEN), 0);
+ read (g->fd, block, BLOCKLEN);
typ = block[BTYP];
@@ -1002,7 +1077,7 @@ k_again: /* entry point for repeated
if (typ == EMPTY) {
if (blknbr == ROOT) {
- close (filedes);
+ gbl_close (g);
goto reopen;
}
@@ -1016,13 +1091,13 @@ k_again: /* entry point for repeated
addr += UNSIGN (block[addr]);
addr += 2; /* skip key */
- if ((blknbr = UNSIGN (block[addr]) * 65536 + UNSIGN (block[addr + 1]) * 256 + UNSIGN (block[addr + 2])) == oldblk) {
+ if ((blknbr = UNSIGN (block[addr]) * 65536 + UNSIGN (block[addr + 1]) * 256 + UNSIGN (block[addr + 2])) == g->last_block) {
merr_raise (DBDGD);
goto quit;
}
addr += PLEN; /* skip data */
- oldblk = blknbr;
+ g->last_block = blknbr;
}
traceadr[trx] = addr;
@@ -1157,10 +1232,10 @@ s10: {
}
- lseek (filedes, (long) blknbr * (long) (BLOCKLEN), 0);
- write (filedes, block, BLOCKLEN);
+ lseek (g->fd, (long) blknbr * (long) (BLOCKLEN), 0);
+ write (g->fd, block, BLOCKLEN);
- if (traceadr[trx] == 0) update (filedes, compactkey, keyl);
+ if (traceadr[trx] == 0) update (g->fd, compactkey, keyl);
break;
}
@@ -1202,7 +1277,6 @@ s10: {
}
else {
- /* if (j1<0) */
/* we need more space */
if ((offset - j1) > DATALIM) {
@@ -1245,12 +1319,12 @@ s20:
}
stcpy0 (&block[++addr], data, (long) datal);
- lseek (filedes, (long) blknbr * (long) (BLOCKLEN), 0);
- write (filedes, block, BLOCKLEN);
+ lseek (g->fd, (long) blknbr * (long) (BLOCKLEN), 0);
+ write (g->fd, block, BLOCKLEN);
break;
- case dat:
+ case fra_dat:
data[0] = '0';
data[1] = EOL1;
@@ -1277,8 +1351,8 @@ s20:
if ((blknbr = UNSIGN (block[RLPTR]) * 65536 + UNSIGN (block[RLPTR + 1]) * 256 + UNSIGN (block[RLPTR + 2]))) {
- lseek (filedes, (long) blknbr * (long) (BLOCKLEN), 0);
- read (filedes, block, BLOCKLEN);
+ lseek (g->fd, (long) blknbr * (long) (BLOCKLEN), 0);
+ read (g->fd, block, BLOCKLEN);
j1 = UNSIGN (block[0]);
i = 0;
@@ -1360,8 +1434,8 @@ s20:
goto quit;
} /* no next block */
- lseek (filedes, (long) blknbr * (long) (BLOCKLEN), 0);
- read (filedes, block, BLOCKLEN);
+ lseek (g->fd, (long) blknbr * (long) (BLOCKLEN), 0);
+ read (g->fd, block, BLOCKLEN);
scandblk (block, &addr, &found);
}
@@ -1517,8 +1591,8 @@ s20:
goto quit; /* no next block */
}
- lseek (filedes, (long) blknbr * (long) (BLOCKLEN), 0);
- read (filedes, block, BLOCKLEN);
+ lseek (g->fd, (long) blknbr * (long) (BLOCKLEN), 0);
+ read (g->fd, block, BLOCKLEN);
addr = 0;
offset = UNSIGN (block[OFFS]) * 256 +
@@ -1541,8 +1615,8 @@ s20:
goto quit; /* no next block */
}
- lseek (filedes, (long) blknbr * (long) (BLOCKLEN), 0);
- read (filedes, block, BLOCKLEN);
+ lseek (g->fd, (long) blknbr * (long) (BLOCKLEN), 0);
+ read (g->fd, block, BLOCKLEN);
addr = 0;
}
@@ -1606,6 +1680,7 @@ s20:
}
zref[j++] = ch0;
+
if (j >= 252) {
zref[j] = EOL;
@@ -1751,8 +1826,8 @@ killo: /* entry from killone section
other = traceblk[--trx];
addr = traceadr[trx];
- lseek (filedes, (long) other * (long) (BLOCKLEN), 0);
- read (filedes, block, BLOCKLEN);
+ lseek (g->fd, (long) other * (long) (BLOCKLEN), 0);
+ read (g->fd, block, BLOCKLEN);
addr += UNSIGN (block[addr]);
addr += (2 + PLEN); /* skip previous entry */
@@ -1771,8 +1846,8 @@ killo: /* entry from killone section
trx = trxsav;
- lseek (filedes, (long) blknbr * (long) (BLOCKLEN), 0);
- read (filedes, block, BLOCKLEN);
+ lseek (g->fd, (long) blknbr * (long) (BLOCKLEN), 0);
+ read (g->fd, block, BLOCKLEN);
offset = UNSIGN (block[OFFS]) * 256 +
UNSIGN (block[OFFS + 1]);
@@ -1878,33 +1953,33 @@ p_empty: /* entry if pointer block goes
if (left) {
- lseek (filedes, (long) left * (long) (BLOCKLEN), 0);
- read (filedes, block0, BLOCKLEN);
+ lseek (g->fd, (long) left * (long) (BLOCKLEN), 0);
+ read (g->fd, block0, BLOCKLEN);
block0[RLPTR] = block[RLPTR];
block0[RLPTR + 1] = block[RLPTR + 1];
block0[RLPTR + 2] = block[RLPTR + 2];
- lseek (filedes, (long) left * (long) (BLOCKLEN), 0);
- write (filedes, block0, BLOCKLEN);
+ lseek (g->fd, (long) left * (long) (BLOCKLEN), 0);
+ write (g->fd, block0, BLOCKLEN);
}
if (right) {
- lseek (filedes, (long) right * (long) (BLOCKLEN), 0);
- read (filedes, block0, BLOCKLEN);
+ lseek (g->fd, (long) right * (long) (BLOCKLEN), 0);
+ read (g->fd, block0, BLOCKLEN);
block0[LLPTR] = block[LLPTR];
block0[LLPTR + 1] = block[LLPTR + 1];
block0[LLPTR + 2] = block[LLPTR + 2];
- lseek (filedes, (long) right * (long) (BLOCKLEN), 0);
- write (filedes, block0, BLOCKLEN);
+ lseek (g->fd, (long) right * (long) (BLOCKLEN), 0);
+ write (g->fd, block0, BLOCKLEN);
}
- b_free (filedes, blknbr); /* modify free list */
+ b_free (g->fd, blknbr); /* modify free list */
/* delete pointer */
/**************************/
@@ -1917,8 +1992,8 @@ p_empty: /* entry if pointer block goes
blknbr = traceblk[--trx];
addr = traceadr[trx];
- lseek (filedes, (long) (blknbr) * (long) (BLOCKLEN), 0);
- read (filedes, block, BLOCKLEN);
+ lseek (g->fd, (long) (blknbr) * (long) (BLOCKLEN), 0);
+ read (g->fd, block, BLOCKLEN);
offset = UNSIGN (block[OFFS]) * 256 +
UNSIGN (block[OFFS + 1]);
freecnt = UNSIGN (block[addr]) + 2 + PLEN;
@@ -1930,18 +2005,18 @@ p_empty: /* entry if pointer block goes
if (blknbr == ROOT) { /* global went empty */
- lseek (filedes, 0L, 0);
+ lseek (g->fd, 0L, 0);
/* note : UNIX does not tell other */
block[BTYP] = EMPTY; /* jobs that a file has been unlinked */
/* as long as they keep it open. */
/* so we mark this global as EMPTY */
- write (filedes, block, BLOCKLEN);
- close (filedes);
+ write (g->fd, block, BLOCKLEN);
+ gbl_close (g);
unlink (filnam);
-
- if (lonelyflag == FALSE) locking (filedes, 0, 0L); /* unlock */
+
+ gbl_unlock (g);
olddes[inuse] = 0;
oldfil[inuse][0] = NUL;
@@ -1962,13 +2037,13 @@ p_empty: /* entry if pointer block goes
for (i = offset; i < offset + freecnt; block[i++] = 0);
- lseek (filedes, (long) (blknbr) * (long) (BLOCKLEN), 0);
- write (filedes, block, BLOCKLEN);
+ lseek (g->fd, (long) (blknbr) * (long) (BLOCKLEN), 0);
+ write (g->fd, block, BLOCKLEN);
if (addr == 0) { /* update of pointer */
traceadr[trx] = 0;
- update (filedes, &block[2], (long) UNSIGN (block[0]));
+ update (g->fd, &block[2], (long) UNSIGN (block[0]));
}
trx = trxsav;
@@ -2010,11 +2085,11 @@ p_empty: /* entry if pointer block goes
}
block[OFFS] = offset / 256;
block[OFFS + 1] = offset % 256;
- lseek (filedes, (long) blknbr * (long) (BLOCKLEN), 0);
- write (filedes, block, BLOCKLEN);
+ lseek (g->fd, (long) blknbr * (long) (BLOCKLEN), 0);
+ write (g->fd, block, BLOCKLEN);
if (addr < 3) { /* update of pointer */
traceadr[trx] = 0;
- update (filedes, &block[2], (long) UNSIGN (block[0]));
+ update (g->fd, &block[2], (long) UNSIGN (block[0]));
}
}
@@ -2037,8 +2112,8 @@ zinv:
goto quit;
} /* no previous block */
- lseek (filedes, (long) blknbr * (long) (BLOCKLEN), 0);
- read (filedes, block, BLOCKLEN);
+ lseek (g->fd, (long) blknbr * (long) (BLOCKLEN), 0);
+ read (g->fd, block, BLOCKLEN);
addr = UNSIGN (block[OFFS]) * 256 +
UNSIGN (block[OFFS + 1]);
@@ -2217,8 +2292,8 @@ zinv:
break; /* no next block */
}
- lseek (filedes, (long) blknbr * (long) (BLOCKLEN), 0);
- read (filedes, block, BLOCKLEN);
+ lseek (g->fd, (long) blknbr * (long) (BLOCKLEN), 0);
+ read (g->fd, block, BLOCKLEN);
addr = 0;
offset = UNSIGN (block[OFFS]) * 256 +
@@ -2333,10 +2408,9 @@ quit:
/* clean things up */
- lseek (filedes, ROOT, 0);
-
- if (lonelyflag == FALSE) locking (filedes, 0, 0L); /* unlock */
-
+ lseek (g->fd, ROOT, 0);
+ gbl_unlock (g);
+
return;
@@ -2347,7 +2421,7 @@ splitd: /* split data block in two se
/* 'addr' there I would like to insert, if possible (which is not) */
/* 'offset' filled up to this limit */
- getnewblk (filedes, &newblk); /* get a new block */
+ getnewblk (g->fd, &newblk); /* get a new block */
/* if we have to insert at the begin or end of a block */
/* we don't split - we just start a new block */
@@ -2365,8 +2439,8 @@ splitd: /* split data block in two se
block[RLPTR + 1] = newblk % 65536 / 256;
block[RLPTR + 2] = newblk % 256;
- lseek (filedes, (long) blknbr * (long) (BLOCKLEN), 0);
- write (filedes, block, BLOCKLEN);
+ lseek (g->fd, (long) blknbr * (long) (BLOCKLEN), 0);
+ write (g->fd, block, BLOCKLEN);
block[RLPTR] = right;
block[RLPTR + 1] = right1;
@@ -2378,22 +2452,22 @@ splitd: /* split data block in two se
addr = 0;
blknbr = newblk;
- insert (filedes, compactkey, keyl, newblk);
+ insert (g->fd, compactkey, keyl, newblk);
/* up-date LL-PTR of RL-block */
if ((other = right * 65536 + right1 * 256 + right2)) {
char block0[BLOCKLEN];
- lseek (filedes, (long) other * (long) (BLOCKLEN), 0);
- read (filedes, block0, BLOCKLEN);
+ lseek (g->fd, (long) other * (long) (BLOCKLEN), 0);
+ read (g->fd, block0, BLOCKLEN);
block0[LLPTR] = blknbr / 65536;
block0[LLPTR + 1] = blknbr % 65536 / 256;
block0[LLPTR + 2] = blknbr % 256;
- lseek (filedes, (long) other * (long) (BLOCKLEN), 0);
- write (filedes, block0, BLOCKLEN);
+ lseek (g->fd, (long) other * (long) (BLOCKLEN), 0);
+ write (g->fd, block0, BLOCKLEN);
}
@@ -2411,8 +2485,8 @@ splitd: /* split data block in two se
block[LLPTR + 1] = newblk % 65536 / 256;
block[LLPTR + 2] = newblk % 256;
- lseek (filedes, (long) blknbr * (long) (BLOCKLEN), 0);
- write (filedes, block, BLOCKLEN);
+ lseek (g->fd, (long) blknbr * (long) (BLOCKLEN), 0);
+ write (g->fd, block, BLOCKLEN);
block[LLPTR] = left;
block[LLPTR + 1] = left1;
@@ -2424,12 +2498,12 @@ splitd: /* split data block in two se
blknbr = newblk;
traceadr[trx] = (-1); /* inhibit second update of pointers */
- insert (filedes, compactkey, keyl, newblk);
+ insert (g->fd, compactkey, keyl, newblk);
if (addr < 3) { /* update of pointer */
traceadr[trx] = 0;
- update (filedes, compactkey, keyl);
+ update (g->fd, compactkey, keyl);
}
/* other is ***always*** zero !!!
@@ -2537,26 +2611,26 @@ splitd: /* split data block in two se
block0[LLPTR + 1] = blknbr % 65536 / 256;
block0[LLPTR + 2] = blknbr % 256;
- lseek (filedes, (long) (newblk) * (long) (BLOCKLEN), 0);
- write (filedes, block0, BLOCKLEN);
+ lseek (g->fd, (long) (newblk) * (long) (BLOCKLEN), 0);
+ write (g->fd, block0, BLOCKLEN);
offset = limit;
/* insert new block in pointer structure */
- insert (filedes, &block0[2], (long) UNSIGN (block0[0]), newblk);
+ insert (g->fd, &block0[2], (long) UNSIGN (block0[0]), newblk);
/* up-date LL-PTR of RL-block */
if (other != 0) {
- lseek (filedes, (long) other * (long) (BLOCKLEN), 0);
- read (filedes, block0, BLOCKLEN);
+ lseek (g->fd, (long) other * (long) (BLOCKLEN), 0);
+ read (g->fd, block0, BLOCKLEN);
block0[LLPTR] = newblk / 65536;
block0[LLPTR + 1] = newblk % 65536 / 256;
block0[LLPTR + 2] = newblk % 256;
- lseek (filedes, (long) other * (long) (BLOCKLEN), 0);
- write (filedes, block0, BLOCKLEN);
+ lseek (g->fd, (long) other * (long) (BLOCKLEN), 0);
+ write (g->fd, block0, BLOCKLEN);
}
@@ -2577,28 +2651,28 @@ splitd: /* split data block in two se
block0[LLPTR + 1] = blknbr % 65536 / 256;
block0[LLPTR + 2] = blknbr % 256;
- lseek (filedes, (long) blknbr * (long) (BLOCKLEN), 0);
- write (filedes, block, BLOCKLEN);
+ lseek (g->fd, (long) blknbr * (long) (BLOCKLEN), 0);
+ write (g->fd, block, BLOCKLEN);
stcpy0 (block, block0, (long) BLOCKLEN);
traceadr[trx] = (addr -= limit);
traceblk[trx] = (blknbr = newblk);
/* insert new block in pointer structure */
- insert (filedes, &block0[2], (long) UNSIGN (block0[0]), newblk);
+ insert (g->fd, &block0[2], (long) UNSIGN (block0[0]), newblk);
/* up-date LL-PTR of RL-block */
if (other != 0) {
- lseek (filedes, (long) other * (long) (BLOCKLEN), 0);
- read (filedes, block0, BLOCKLEN);
+ lseek (g->fd, (long) other * (long) (BLOCKLEN), 0);
+ read (g->fd, block0, BLOCKLEN);
block0[LLPTR] = newblk / 65536;
block0[LLPTR + 1] = newblk % 65536 / 256;
block0[LLPTR + 2] = newblk % 256;
- lseek (filedes, (long) other * (long) (BLOCKLEN), 0);
- write (filedes, block0, BLOCKLEN);
+ lseek (g->fd, (long) other * (long) (BLOCKLEN), 0);
+ write (g->fd, block0, BLOCKLEN);
}
@@ -3478,7 +3552,9 @@ void close_all_globals (void)
} /* end close_all_globals() */
static void panic (void)
-{
+{
+ printf ("write failed\r\n");
+
printf ("\033[s\033[25H\033[5;7mwrite needs more disk space immediately\007");
sleep (1);
printf ("\033[m\007\033[2K\033[u");
@@ -3498,15 +3574,15 @@ static void panic (void)
void gbl_dump_stat(void)
{
- register int i;
+ global_handle *g;
printf ("FreeM Global Statistics [PID %d]\r\n", pid);
- printf ("%-10s%-20s%s\r\n", "USECT", "AGE", "FILE");
- printf ("%-10s%-20s%s\r\n", "=====", "===", "====");
+ printf ("%-20s%-10s%-20s%-10s%s\r\n", "GLOBAL", "USECT", "AGE", "LAST BLK", "FILE");
+ printf ("%-20s%-10s%-20s%-10s%s\r\n", "======", "=====", "===", "========", "====");
- for (i = 0; i < NO_GLOBLS; i++) {
- printf ("%-10d%-20ld%s\r\n", usage[i], g_ages[i], oldfil[i]);
+ for (g = global_handles_head; g != NULL; g = g->next) {
+ printf ("%-20s%-10d%-20d%-10d%s\r\n", g->global_name, g->use_count, g->age, g->last_block, g->global_path);
}
}