From d7498aebed1f70d1ad66d4cf6b5455a90a2af420 Mon Sep 17 00:00:00 2001
From: Per Cederqvist <ceder@lysator.liu.se>
Date: Tue, 17 Oct 2006 21:06:12 +0000
Subject: [PATCH] Added functions wrapper functions around a few stdio
 functions, and use them when accessing database files.  This is a preparatory
 step for introducing "struct dbfile", which will enable lyskomd to manipulate
 more than one database file format at the same time. That ability is needed
 for solving bug 1616. * src/server/ram-io.c, src/server/ram-io.h: New files.
 (dbfile_ftell): New function. (dbfile_getc): New function. (dbfile_ungetc):
 New function. * src/server/Makefile.am (libcheck_a_SOURCES): Added ram-io.c.
 (lyskomd_SOURCES): Added ram-io.h and ram-io.c. (DBCK): Added ram-io.h and
 ram-io.c. * src/server/simple-cache.c (read_person): Check the return value
 from fparse_set_pos(). (read_conference): Ditto. (read_text_stat): Ditto.
 (write_conf): New static function. (save_one_conf): Use it to simplify code.
 (write_text): New static function. (save_one_text): Use it to simplify code.
 * src/server/ram-parse.h, src/server/ram-parse.c (fparse_set_pos): New
 function. * src/server/ram-output.c: Include ram-io.h. (foutput_space): New
 static function.  Use it where appropriate. (foutput_bool): Ditto.
 (foutput_array_start): Ditto. (foutput_array_end): Ditto.
 (foutput_array_nodata): Ditto. * src/server/ram-output.h,
 src/server/ram-output.c (foutput_atsign): New function. (foutput_newline):
 New function. * src/server/dbck-cache.c: Include ram-io.h.  Use dbfile_getc()
 instead of getc(), dbfile_ungetc() instead of ungetc(), and dbfile_ftell()
 instead of ftell(), when accessing the database file.  Use foutput_atsign and
 foutput_newline when appropriate. * src/server/local-to-global.c: Ditto. *
 src/server/ram-parse.c: Ditto. * src/server/simple-cache.c: Ditto.

---
 ChangeLog                    |  41 ++++
 src/server/Makefile.am       |   7 +-
 src/server/dbck-cache.c      |  93 ++++-----
 src/server/local-to-global.c |  29 +--
 src/server/ram-io.c          |  59 ++++++
 src/server/ram-io.h          |  32 +++
 src/server/ram-output.c      | 212 ++++++++++++--------
 src/server/ram-output.h      |   6 +
 src/server/ram-parse.c       | 366 ++++++++++++++++++-----------------
 src/server/ram-parse.h       |   4 +
 src/server/simple-cache.c    | 101 ++++++----
 11 files changed, 591 insertions(+), 359 deletions(-)
 create mode 100644 src/server/ram-io.c
 create mode 100644 src/server/ram-io.h

diff --git a/ChangeLog b/ChangeLog
index 09ec82e25..1c19aa281 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,44 @@
+2006-10-17  Per Cederqvist  <ceder@lysator.liu.se>
+
+	Added functions wrapper functions around a few stdio functions,
+	and use them when accessing database files.  This is a preparatory
+	step for introducing "struct dbfile", which will enable lyskomd to
+	manipulate more than one database file format at the same time.
+	That ability is needed for solving bug 1616.
+	* src/server/ram-io.c, src/server/ram-io.h: New files.
+	(dbfile_ftell): New function.
+	(dbfile_getc): New function.
+	(dbfile_ungetc): New function.
+	* src/server/Makefile.am (libcheck_a_SOURCES): Added ram-io.c.
+	(lyskomd_SOURCES): Added ram-io.h and ram-io.c.
+	(DBCK): Added ram-io.h and ram-io.c.
+	* src/server/simple-cache.c (read_person): Check the return value
+	from fparse_set_pos().
+	(read_conference): Ditto.
+	(read_text_stat): Ditto.
+	(write_conf): New static function.
+	(save_one_conf): Use it to simplify code.
+	(write_text): New static function.
+	(save_one_text): Use it to simplify code.
+	* src/server/ram-parse.h, src/server/ram-parse.c
+	(fparse_set_pos): New function.
+	* src/server/ram-output.c: Include ram-io.h.
+	(foutput_space): New static function.  Use it where appropriate.
+	(foutput_bool): Ditto.
+	(foutput_array_start): Ditto.
+	(foutput_array_end): Ditto.
+	(foutput_array_nodata): Ditto.
+	* src/server/ram-output.h, src/server/ram-output.c
+	(foutput_atsign): New function.
+	(foutput_newline): New function.
+	* src/server/dbck-cache.c: Include ram-io.h.  Use dbfile_getc()
+	instead of getc(), dbfile_ungetc() instead of ungetc(), and
+	dbfile_ftell() instead of ftell(), when accessing the database
+	file.  Use foutput_atsign and foutput_newline when appropriate.
+	* src/server/local-to-global.c: Ditto.
+	* src/server/ram-parse.c: Ditto.
+	* src/server/simple-cache.c: Ditto.
+
 2006-10-01  Per Cederqvist  <ceder@lysator.liu.se>
 
 	Check that conferences read from the database are the expected
diff --git a/src/server/Makefile.am b/src/server/Makefile.am
index ec20a2253..cdc0f830d 100644
--- a/src/server/Makefile.am
+++ b/src/server/Makefile.am
@@ -64,7 +64,7 @@ endif
 check_LIBRARIES = libcheck.a
 libcheck_a_SOURCES = local-to-global.c ram-parse.c ram-output.c \
 	log.c ram-smalloc.c memory.c getopt.c getopt1.c \
-	misc-types.c
+	misc-types.c ram-io.c
 
 lyskomd_SOURCES = $(DISKOBJS) $(GENOBJS) \
         aux-item-def-parse.y \
@@ -75,7 +75,8 @@ lyskomd_SOURCES = $(DISKOBJS) $(GENOBJS) \
 	text.h isc-interface.h isc-malloc.h isc-parse.h \
 	kom-memory.h local-to-global.h log.h lyskomd.h manipulate.h \
 	minmax.h param.h prot-a-output.h prot-a-parse.h \
-	prot-a-send-async.h prot-a.h ram-output.h ram-parse.h \
+	prot-a-send-async.h prot-a.h \
+	ram-io.h ram-io.c ram-output.h ram-parse.h \
 	rfc931.h send-async.h server-config.h string-malloc.h \
 	text-garb.h version-info.h sigflags.h trace-alloc.h \
 	lockdb.h lockdb.c server-time.h oop-malloc.h oop-malloc.c \
@@ -153,7 +154,7 @@ DISKOBJS = ramkomd.c ram-smalloc.c simple-cache.c ram-parse.c ram-output.c \
 GETOPT = getopt.c getopt1.c
 NODIST_DBCK = version-info.c
 DBCK = dbck.c dbck-cache.c ram-smalloc.c ram-parse.c server-config.c \
-	ram-output.c memory.c conf-file.c $(GETOPT) \
+	ram-io.c ram-output.c memory.c conf-file.c $(GETOPT) \
 	local-to-global.c lockdb.h lockdb.c
 
 # Files for encrypt (a program to transform the database from unencrypted
diff --git a/src/server/dbck-cache.c b/src/server/dbck-cache.c
index a2f3bd50c..2bf5f2b66 100644
--- a/src/server/dbck-cache.c
+++ b/src/server/dbck-cache.c
@@ -69,6 +69,7 @@
 #include "log.h"
 #include "lyskomd.h"
 #include "kom-memory.h"
+#include "ram-io.h"
 #include "ram-parse.h"
 #include "ram-output.h"
 #include "conf-file.h"
@@ -512,7 +513,7 @@ get_version(const char *fn)
 	return -1;
 
     fseek(fp, 5, SEEK_SET);
-    if ( (c = getc(fp)) == '\n')
+    if ( (c = dbfile_getc(fp)) == '\n')
     {
 	i_fclose(fp);
 	return 0;
@@ -652,13 +653,13 @@ cache_sync_all(void)
         {
         case 0:
             if (conf_arr[ic] == NULL)
-                fprintf(fp, "@");
+                foutput_atsign(fp);
             else
             {
                 fprintf(fp, "+ ");
                 foutput_conference(fp, conf_arr[ic]);
             }
-            putc('\n', fp);
+            foutput_newline(fp);
             break;
         case 1:
         case 2:
@@ -666,7 +667,7 @@ cache_sync_all(void)
             {
                 fprintf(fp, "C %lu", (unsigned long)ic);
                 foutput_conference(fp, conf_arr[ic]);
-                putc('\n', fp);
+                foutput_newline(fp);
             }
             break;
 
@@ -684,7 +685,7 @@ cache_sync_all(void)
         {
         case 0:
             if (pers_arr[ic] == NULL)
-                fprintf(fp, "@");
+                foutput_atsign(fp);
             else
             {
                 fprintf(fp, "+ ");
@@ -692,7 +693,7 @@ cache_sync_all(void)
                 fwrite(pers_arr[ic]->pwd, PASSWD_LEN, 1, fp);
                 foutput_person(fp, pers_arr[ic]);
             }
-            putc('\n', fp);
+            foutput_newline(fp);
             break;
         case 1:
         case 2:
@@ -702,7 +703,7 @@ cache_sync_all(void)
                 fprintf(fp, " %dH", PASSWD_LEN);
                 fwrite(pers_arr[ic]->pwd, PASSWD_LEN, 1, fp);
                 foutput_person(fp, pers_arr[ic]);
-                putc('\n', fp);
+                foutput_newline(fp);
             }
             break;
 
@@ -738,13 +739,13 @@ cache_sync_all(void)
         {
         case 0:
             if (text_arr[it] == NULL)
-                fprintf(fp, "@");
+                foutput_atsign(fp);
             else
             {
                 fprintf(fp, "+ ");
                 foutput_text_stat(fp, text_arr[it]);
             }
-            putc('\n', fp);
+            foutput_newline(fp);
             break;
         case 1:
         case 2:
@@ -752,7 +753,7 @@ cache_sync_all(void)
             {
                 fprintf(fp, "T %lu", it);
                 foutput_text_stat(fp, text_arr[it]);
-                putc('\n', fp);
+                foutput_newline(fp);
             }
             break;
         default:
@@ -924,7 +925,7 @@ init_cache(void)
         {
             kom_log("Data file version is '%ld'\n", data_file_version);
             kom_log("Reading %d conferences, starting at pos %ld.\n",
-                next_free_num-1, (unsigned long)ftell(fp));
+                next_free_num-1, dbfile_ftell(fp));
         }
         break;
     case 1:
@@ -958,7 +959,7 @@ init_cache(void)
 	num = i;
 	fskipwhite(fp);
 
-	switch(c = getc(fp))
+	switch(c = dbfile_getc(fp))
 	{
         case EOF:
             switch (data_file_version)
@@ -988,8 +989,8 @@ init_cache(void)
                 break;
             case 1:
             case 2:
-		restart_kom("@ record in data file version %ld at %lu.\n",
-                            data_file_version, (unsigned long)ftell(fp));
+		restart_kom("@ record in data file version %ld at %ld.\n",
+                            data_file_version, dbfile_ftell(fp));
                 break;
             default:
                 restart_kom("Unknown input file format: %ld\n",
@@ -1001,14 +1002,14 @@ init_cache(void)
             switch (data_file_version)
             {
             case 0:
-                restart_kom("I record in data file version %ld at %lu.\n",
-                            data_file_version, (unsigned long)ftell(fp));
+                restart_kom("I record in data file version %ld at %ld.\n",
+                            data_file_version, dbfile_ftell(fp));
                 break;
             case 1:
             case 2:
                 if (fparse_info(fp, &kom_info) != OK)
-                    restart_kom("Invalid I record in data file version %ld at %lu.\n",
-                                data_file_version, (unsigned long)ftell(fp));
+                    restart_kom("Invalid I record in data file version %ld at %ld.\n",
+                                data_file_version, dbfile_ftell(fp));
                 
                 if (pers_pres_conf != -1)
                 {
@@ -1051,8 +1052,8 @@ init_cache(void)
             switch (data_file_version)
             {
             case 0:
-                restart_kom("C record in data file version %ld at %lu.\n",
-                            data_file_version, (unsigned long)ftell(fp));
+                restart_kom("C record in data file version %ld at %ld.\n",
+                            data_file_version, dbfile_ftell(fp));
                 break;
 
             case 1:
@@ -1078,8 +1079,8 @@ init_cache(void)
             switch (data_file_version)
             {
             case 0:
-                restart_kom("P record in data file version %ld at %lu.\n",
-                            data_file_version, (unsigned long)ftell(fp));
+                restart_kom("P record in data file version %ld at %ld.\n",
+                            data_file_version, dbfile_ftell(fp));
                 break;
 
             case 1:
@@ -1103,8 +1104,8 @@ init_cache(void)
             switch (data_file_version)
             {
             case 0:
-                restart_kom("T record in data file version %ld at %lu\n",
-                            data_file_version, (unsigned long)ftell(fp));
+                restart_kom("T record in data file version %ld at %ld\n",
+                            data_file_version, dbfile_ftell(fp));
                 break;
 
             case 1:
@@ -1147,8 +1148,8 @@ init_cache(void)
                 break;
             case 1:
             case 2:
-                restart_kom("+ record in data file version %ld at %lu.\n",
-                            data_file_version, (unsigned long)ftell(fp));
+                restart_kom("+ record in data file version %ld at %ld.\n",
+                            data_file_version, dbfile_ftell(fp));
                 break;
             default:
                 restart_kom("Unknown input file format: %ld\n",
@@ -1160,13 +1161,13 @@ init_cache(void)
             switch (data_file_version)
             {
             case 0:
-                restart_kom("#record in data file version %ld at %lu.\n",
-                            data_file_version, (unsigned long)ftell(fp));
+                restart_kom("#record in data file version %ld at %ld.\n",
+                            data_file_version, dbfile_ftell(fp));
                 break;
 
             case 1:
             case 2:
-                switch (c = getc(fp))
+                switch (c = dbfile_getc(fp))
                 {
                 case 'C':
                     next_free_num = fparse_long(fp);
@@ -1178,9 +1179,9 @@ init_cache(void)
                     break;
                 default:
                     restart_kom("Unknown # record (#%c) in data "
-                                "file version %ld at %lu.\n",
+                                "file version %ld at %ld.\n",
                                 c, data_file_version,
-				(unsigned long)ftell(fp));
+				dbfile_ftell(fp));
                 }
                 break;
 
@@ -1194,13 +1195,13 @@ init_cache(void)
             switch (data_file_version)
             {
             case 0:
-                restart_kom("- record in data file version %ld at %lu.\n",
-                            data_file_version, (unsigned long)ftell(fp));
+                restart_kom("- record in data file version %ld at %ld.\n",
+                            data_file_version, dbfile_ftell(fp));
                 break;
 
             case 1:
             case 2:
-                switch(c = getc(fp))
+                switch(c = dbfile_getc(fp))
                 {
                 case 'C':
                     num = fparse_long(fp);
@@ -1222,9 +1223,9 @@ init_cache(void)
                     break;
                 default:
                     restart_kom("Unknown - record (%c) in data file "
-                                "version %ld at %lu.\n",
+                                "version %ld at %ld.\n",
                                 c, data_file_version,
-				(unsigned long)ftell(fp));
+				dbfile_ftell(fp));
                 }
                 break;
 
@@ -1236,8 +1237,8 @@ init_cache(void)
 
 	default:
             restart_kom("Unknown record type in data file version "
-			"%ld at %lu.\n",
-                        data_file_version, (unsigned long)ftell(fp));
+			"%ld at %ld.\n",
+                        data_file_version, dbfile_ftell(fp));
 	}
     }
 
@@ -1249,12 +1250,12 @@ init_cache(void)
     if (data_file_version == 0)
     {
         if ( vflag )
-            kom_log("Reading %d persons, starting at pos %lu.\n",
-                next_free_num-1, (unsigned long)ftell(fp));
+            kom_log("Reading %d persons, starting at pos %ld.\n",
+                next_free_num-1, dbfile_ftell(fp));
 	for ( i = 1; i < next_free_num; i++ ) /* PERSONS */
 	{
 	    fskipwhite(fp);
-	    switch(getc(fp))
+	    switch(dbfile_getc(fp))
 	    {
 	    case '@':
 		pers_arr[ i ] = NULL;
@@ -1273,13 +1274,13 @@ init_cache(void)
 	read_text_num = 1;
 
 	if ( vflag )
-	    kom_log("Reading %ld texts, starting at pos %lu.\n",
-		next_text_num-1, (unsigned long)ftell(fp));
+	    kom_log("Reading %ld texts, starting at pos %ld.\n",
+		    next_text_num-1, dbfile_ftell(fp));
 
 	for ( i = 1; i < next_text_num; i++ ) /* TEXT_STATS */
 	{
 	    fskipwhite(fp);
-	    switch(getc(fp))
+	    switch(dbfile_getc(fp))
 	    {
 	    case '@':
 		text_arr[ i ] = NULL;
@@ -1303,8 +1304,8 @@ init_cache(void)
 	}
     } /* if (datafile_version == 0) */
 
-    kom_log("Read %d confs/persons and %ld texts, eof at %lu\n",
-	next_free_num-1, next_text_num-1, (unsigned long)ftell(fp));
+    kom_log("Read %d confs/persons and %ld texts, eof at %ld\n",
+	    next_free_num-1, next_text_num-1, dbfile_ftell(fp));
 
     if (read_text_num == 0 || read_conf_num == 0)
       restart_kom("init_cache(): Failed to read next_conf or text num.\n");
diff --git a/src/server/local-to-global.c b/src/server/local-to-global.c
index d9685e640..9adc8174d 100644
--- a/src/server/local-to-global.c
+++ b/src/server/local-to-global.c
@@ -24,6 +24,7 @@
 #include "s-string.h"
 
 #include "kom-types.h"
+#include "ram-io.h"
 #include "local-to-global.h"
 #include "log.h"
 #include "ram-parse.h"
@@ -1182,10 +1183,10 @@ l2g_read(FILE *fp, Local_to_global *l2g)
 
     /* Read past the start marker */
     fskipwhite(fp);
-    if ( (c = getc(fp)) == EOF || c != '[')
+    if ( (c = dbfile_getc(fp)) == EOF || c != '[')
     {
-	kom_log("l2g_read() failed to find ``['' marker at pos %lu.\n",
-                (unsigned long) ftell(fp));
+	kom_log("l2g_read() failed to find ``['' marker at pos %ld.\n",
+                dbfile_ftell(fp));
 	return FAILURE;
     }
 
@@ -1194,10 +1195,10 @@ l2g_read(FILE *fp, Local_to_global *l2g)
     /* Read numbers until the EOL (end-of-l2g :-) ) marker is read. */
     while (1)
     {
-	if ((c = getc(fp)) == EOF)
+	if ((c = dbfile_getc(fp)) == EOF)
 	{
-	    kom_log("l2g_read(): unexpected EOF at pos %lu.\n",
-		(unsigned long) ftell(fp));
+	    kom_log("l2g_read(): unexpected EOF at pos %ld.\n",
+		    dbfile_ftell(fp));
 	    return FAILURE;
 	}
 
@@ -1207,16 +1208,16 @@ l2g_read(FILE *fp, Local_to_global *l2g)
 	    lno = fparse_long(fp);
 	    if (lno == 0)
 	    {
-		kom_log("l2g_read(): got local number 0 at pos %lu.\n",
-		    (unsigned long) ftell(fp));
+		kom_log("l2g_read(): got local number 0 at pos %ld.\n",
+			dbfile_ftell(fp));
 		return FAILURE;
 	    }
 	    break;
 	case ',':
 	    if (lno == 0)
 	    {
-		kom_log("l2g_read(): missing local number at pos %lu.\n",
-		    (unsigned long)ftell(fp));
+		kom_log("l2g_read(): missing local number at pos %ld.\n",
+			dbfile_ftell(fp));
 		return FAILURE;
 	    }
 	    ++lno;
@@ -1224,8 +1225,8 @@ l2g_read(FILE *fp, Local_to_global *l2g)
 	case ':':
 	    if (lno == 0)
 	    {
-		kom_log("l2g_read(): missing local number at pos %lu.\n",
-		    (unsigned long)ftell(fp));
+		kom_log("l2g_read(): missing local number at pos %ld.\n",
+			dbfile_ftell(fp));
 		return FAILURE;
 	    }
 	    tno = fparse_long(fp);
@@ -1236,8 +1237,8 @@ l2g_read(FILE *fp, Local_to_global *l2g)
 	    l2g->first_unused = first_unused;
 	    return OK;
 	default:
-	    kom_log("l2g_read(): unexpected character ``%c'' at pos %lu.\n",
-		c, (unsigned long) ftell(fp));
+	    kom_log("l2g_read(): unexpected character ``%c'' at pos %ld.\n",
+		    c, dbfile_ftell(fp));
 	    return FAILURE;
 	}
     }
diff --git a/src/server/ram-io.c b/src/server/ram-io.c
new file mode 100644
index 000000000..424568345
--- /dev/null
+++ b/src/server/ram-io.c
@@ -0,0 +1,59 @@
+/* Database file handling.
+ * Copyright (C) 1991, 1993-1999, 2001-2003  Lysator Academic Computer Association.
+ *
+ * This file is part of the LysKOM server.
+ * 
+ * LysKOM is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by 
+ * the Free Software Foundation; either version 1, or (at your option) 
+ * any later version.
+ * 
+ * LysKOM is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * for more details.
+ * 
+ * You should have received a copy of the GNU General Public License
+ * along with LysKOM; see the file COPYING.  If not, write to
+ * Lysator, c/o ISY, Linkoping University, S-581 83 Linkoping, SWEDEN,
+ * or the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, 
+ * MA 02139, USA.
+ *
+ * Please report bugs at http://bugzilla.lysator.liu.se/. 
+ */
+
+#ifdef HAVE_CONFIG_H
+#  include <config.h>
+#endif
+
+#include <errno.h>
+#include <stdio.h>
+#include <assert.h>
+#include "timewrap.h"
+
+#include "server/smalloc.h"
+#include "eintr.h"
+#include "kom-types.h"
+#include "ram-io.h"
+#include "ram-parse.h"
+#include "lyskomd.h"
+#include "log.h"
+
+long
+dbfile_ftell(FILE *fp)
+{
+    return ftell(fp);
+}
+
+int
+dbfile_getc(FILE *fp)
+{
+    return getc(fp);
+}
+
+int
+dbfile_ungetc(FILE *fp,
+	      int c)
+{
+  return ungetc(c, fp);
+}
diff --git a/src/server/ram-io.h b/src/server/ram-io.h
new file mode 100644
index 000000000..f063f87a3
--- /dev/null
+++ b/src/server/ram-io.h
@@ -0,0 +1,32 @@
+/* Store database file state.
+ * Copyright (C) 2006  Lysator Academic Computer Association.
+ *
+ * This file is part of the LysKOM server.
+ * 
+ * LysKOM is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by 
+ * the Free Software Foundation; either version 1, or (at your option) 
+ * any later version.
+ * 
+ * LysKOM is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * for more details.
+ * 
+ * You should have received a copy of the GNU General Public License
+ * along with LysKOM; see the file COPYING.  If not, write to
+ * Lysator, c/o ISY, Linkoping University, S-581 83 Linkoping, SWEDEN,
+ * or the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, 
+ * MA 02139, USA.
+ *
+ * Please report bugs at http://bugzilla.lysator.liu.se/. 
+ */
+
+long
+dbfile_ftell(FILE *fp);
+
+int
+dbfile_getc(FILE *fp);
+
+int
+dbfile_ungetc(FILE *fp, int c);
diff --git a/src/server/ram-output.c b/src/server/ram-output.c
index 680634413..d12420c7a 100644
--- a/src/server/ram-output.c
+++ b/src/server/ram-output.c
@@ -44,6 +44,7 @@
 
 #include "s-string.h"
 #include "kom-types.h"
+#include "ram-io.h"
 #include "ram-output.h"
 #include "lyskomd.h"
 #include "log.h"
@@ -64,6 +65,11 @@ static void foutput_string(FILE *, String);
 static void foutput_text_list(FILE *, const Local_to_global *);
 static void foutput_time(FILE *, time_t);
 static void foutput_ulong(unsigned long, FILE *);
+static void foutput_space(FILE *fp);
+static void foutput_bool(FILE *fp, int val);
+static void foutput_array_start(FILE *fp);
+static void foutput_array_end(FILE *fp);
+static void foutput_array_nodata(FILE *fp);
 
 
 static int output_format = 2;
@@ -163,7 +169,7 @@ foutput_person_2(FILE *fp,
     foutput_priv_bits (fp, person->privileges);
     foutput_personal_flags (fp, person->flags);
 
-    putc(' ', fp);
+    foutput_space(fp);
     l2g_write (fp, &person->created_texts);
     foutput_mark_list (fp, person->marks);
     foutput_membership_list (fp, person->conferences);
@@ -210,7 +216,7 @@ foutput_conference_2(FILE *fp,
 {
     foutput_string(fp, conf_c->name);
     foutput_member_list(fp, conf_c->members);
-    putc(' ', fp);
+    foutput_space(fp);
     l2g_write(fp, &conf_c->texts);
     foutput_conf_type(fp, conf_c->type);
 
@@ -314,13 +320,13 @@ foutput_text_stat_0(FILE *fp,
 
     if ( t_stat->misc_items != NULL && t_stat->no_of_misc > 0 )
     {
-	fputs(" {", fp);
+	foutput_array_start(fp);
 	for ( i = 0; i < t_stat->no_of_misc; i++ )
 	    foutput_misc_info(fp, t_stat->misc_items[ i ]);
-	fputs(" }", fp);
+	foutput_array_end(fp);
     }
     else
-	fputs(" *", fp);
+	foutput_array_nodata(fp);
 }
 
 static void
@@ -340,13 +346,13 @@ foutput_text_stat_2(FILE *fp,
 
     if ( t_stat->misc_items != NULL && t_stat->no_of_misc > 0 )
     {
-	fputs(" {", fp);
+	foutput_array_start(fp);
 	for ( i = 0; i < t_stat->no_of_misc; i++ )
 	    foutput_misc_info(fp, t_stat->misc_items[ i ]);
-	fputs(" }", fp);
+	foutput_array_end(fp);
     }
     else
-	fputs(" *", fp);
+	foutput_array_nodata(fp);
 
     foutput_ulong((unsigned long) t_stat->highest_aux, fp);
     foutput_aux_item_list(fp, &t_stat->aux_item_list);
@@ -377,15 +383,15 @@ static void
 foutput_aux_flags(FILE *fp,
                   Aux_item_flags f)
 {
-    putc(' ', fp);
-    putc(f.deleted + '0', fp);
-    putc(f.inherit + '0', fp);
-    putc(f.secret + '0', fp);
-    putc(f.hide_creator + '0', fp);
-    putc(f.dont_garb + '0', fp);
-    putc(f.reserved3 + '0', fp);
-    putc(f.reserved4 + '0', fp);
-    putc(f.reserved5 + '0', fp);
+    foutput_space(fp);
+    foutput_bool(fp, f.deleted);
+    foutput_bool(fp, f.inherit);
+    foutput_bool(fp, f.secret);
+    foutput_bool(fp, f.hide_creator);
+    foutput_bool(fp, f.dont_garb);
+    foutput_bool(fp, f.reserved3);
+    foutput_bool(fp, f.reserved4);
+    foutput_bool(fp, f.reserved5);
 }
 
 static void
@@ -430,13 +436,13 @@ foutput_aux_item_list(FILE *fp,
     foutput_ulong((unsigned long) aux->length, fp);
     if (aux->items && aux->length > 0)
     {
-        fputs(" {", fp);
+        foutput_array_start(fp);
         for (i = 0; i < aux->length; i++)
             foutput_aux_item(fp, &aux->items[i]);
-        fputs(" }", fp);
+        foutput_array_end(fp);
     }
     else
-        fputs(" *", fp);
+        foutput_array_nodata(fp);
 }
 		      
 
@@ -444,15 +450,15 @@ static void
 foutput_membership_type(FILE *fp,
                         Membership_type type)
 {
-    putc(' ', fp);
-    putc(type.invitation + '0', fp);
-    putc(type.passive    + '0', fp);
-    putc(type.secret     + '0', fp);
-    putc(type.passive_message_invert + '0', fp);
-    putc(type.reserved2  + '0', fp);
-    putc(type.reserved3  + '0', fp);
-    putc(type.reserved4  + '0', fp);
-    putc(type.reserved5  + '0', fp);
+    foutput_space(fp);
+    foutput_bool(fp, type.invitation);
+    foutput_bool(fp, type.passive);
+    foutput_bool(fp, type.secret);
+    foutput_bool(fp, type.passive_message_invert);
+    foutput_bool(fp, type.reserved2);
+    foutput_bool(fp, type.reserved3);
+    foutput_bool(fp, type.reserved4);
+    foutput_bool(fp, type.reserved5);
 }
 
 static void
@@ -573,38 +579,38 @@ static void
 foutput_priv_bits(FILE *fp,
 		  Priv_bits bits)
 {
-    putc(' ', fp);
-    putc(bits.wheel + '0', fp);
-    putc(bits.admin + '0', fp);
-    putc(bits.statistic + '0', fp);
-    putc(bits.create_pers + '0', fp);
-    putc(bits.create_conf + '0', fp);
-    putc(bits.change_name + '0', fp);
-    putc(bits.flg7 + '0', fp);
-    putc(bits.flg8 + '0', fp);
-    putc(bits.flg9 + '0', fp);
-    putc(bits.flg10 + '0', fp);
-    putc(bits.flg11 + '0', fp);
-    putc(bits.flg12 + '0', fp);
-    putc(bits.flg13 + '0', fp);
-    putc(bits.flg14 + '0', fp);
-    putc(bits.flg15 + '0', fp);
-    putc(bits.flg16 + '0', fp);
+    foutput_space(fp);
+    foutput_bool(fp, bits.wheel);
+    foutput_bool(fp, bits.admin);
+    foutput_bool(fp, bits.statistic);
+    foutput_bool(fp, bits.create_pers);
+    foutput_bool(fp, bits.create_conf);
+    foutput_bool(fp, bits.change_name);
+    foutput_bool(fp, bits.flg7);
+    foutput_bool(fp, bits.flg8);
+    foutput_bool(fp, bits.flg9);
+    foutput_bool(fp, bits.flg10);
+    foutput_bool(fp, bits.flg11);
+    foutput_bool(fp, bits.flg12);
+    foutput_bool(fp, bits.flg13);
+    foutput_bool(fp, bits.flg14);
+    foutput_bool(fp, bits.flg15);
+    foutput_bool(fp, bits.flg16);
 }
 
 static void
 foutput_personal_flags(FILE *fp,
 		       Personal_flags flags)
 {
-    putc(' ', fp);
-    putc(flags.unread_is_secret + '0', fp);
-    putc(flags.flg2 + '0', fp);
-    putc(flags.flg3 + '0', fp);
-    putc(flags.flg4 + '0', fp);
-    putc(flags.flg5 + '0', fp);
-    putc(flags.flg6 + '0', fp);
-    putc(flags.flg7 + '0', fp);
-    putc(flags.flg8 + '0', fp);
+    foutput_space(fp);
+    foutput_bool(fp, flags.unread_is_secret);
+    foutput_bool(fp, flags.flg2);
+    foutput_bool(fp, flags.flg3);
+    foutput_bool(fp, flags.flg4);
+    foutput_bool(fp, flags.flg5);
+    foutput_bool(fp, flags.flg6);
+    foutput_bool(fp, flags.flg7);
+    foutput_bool(fp, flags.flg8);
 }	
 
 
@@ -626,13 +632,13 @@ foutput_text_list(FILE *fp,
 
     if (first < end)
     {
-	fputs(" {", fp);
+	foutput_array_start(fp);
 	while (first < end)
 	    foutput_ulong((unsigned long)l2g_lookup(text_list, first++), fp);
-	fputs(" }", fp);
+	foutput_array_end(fp);
     }
     else
-	fprintf(fp, " *");
+	foutput_array_nodata(fp);
 }
 
 
@@ -674,13 +680,13 @@ foutput_membership_list_0 (FILE		* fp,
 
     if ( mlist.confs != NULL && mlist.no_of_confs > 0 )
     {
-	fprintf(fp, " {");
+	foutput_array_start(fp);
 	for ( i = 0; i < mlist.no_of_confs; i++)
 	    foutput_membership_0(fp, mlist.confs + i);
-	fprintf(fp, " }");
+	foutput_array_end(fp);
     }
     else
-	fprintf(fp, " *");
+	foutput_array_nodata(fp);
 }
 
 static void
@@ -693,13 +699,13 @@ foutput_membership_list_2 (FILE		* fp,
 
     if ( mlist.confs != NULL && mlist.no_of_confs > 0 )
     {
-	fprintf(fp, " {");
+	foutput_array_start(fp);
 	for ( i = 0; i < mlist.no_of_confs; i++)
 	    foutput_membership_2(fp, mlist.confs + i);
-	fprintf(fp, " }");
+	foutput_array_end(fp);
     }
     else
-	fprintf(fp, " *");
+	foutput_array_nodata(fp);
 }
 
 static void
@@ -738,13 +744,13 @@ foutput_member_list(FILE *fp,
     fprintf(fp, " %lu", (unsigned long)m_list.no_of_members);
     if ( m_list.members != NULL && m_list.no_of_members > 0 )
     {
-	fprintf(fp, " {");
+	foutput_array_start(fp);
 	for ( i = 0; i < m_list.no_of_members; i++ )
 	    foutput_member(fp, m_list.members[ i ]);
-	fprintf(fp, " }");
+	foutput_array_end(fp);
     }
     else
-	fprintf(fp, " *");
+	foutput_array_nodata(fp);
 }
 
 
@@ -789,26 +795,26 @@ static void
 foutput_conf_type_1(FILE *fp,
                     Conf_type type)
 {
-    putc(' ', fp);
-    putc(type.rd_prot + '0', fp);
-    putc(type.original + '0', fp);
-    putc(type.secret + '0', fp);
-    putc(type.letter_box + '0', fp);
-    putc(type.allow_anon + '0', fp);
-    putc(type.forbid_secret + '0', fp);
-    putc(type.reserved2 + '0', fp);
-    putc(type.reserved3 + '0', fp);
+    foutput_space(fp);
+    foutput_bool(fp, type.rd_prot);
+    foutput_bool(fp, type.original);
+    foutput_bool(fp, type.secret);
+    foutput_bool(fp, type.letter_box);
+    foutput_bool(fp, type.allow_anon);
+    foutput_bool(fp, type.forbid_secret);
+    foutput_bool(fp, type.reserved2);
+    foutput_bool(fp, type.reserved3);
 }
 
 static void
 foutput_conf_type_0(FILE *fp,
                     Conf_type type)
 {
-    putc(' ', fp);
-    putc(type.rd_prot + '0', fp);
-    putc(type.original + '0', fp);
-    putc(type.secret + '0', fp);
-    putc(type.letter_box + '0', fp);
+    foutput_space(fp);
+    foutput_bool(fp, type.rd_prot);
+    foutput_bool(fp, type.original);
+    foutput_bool(fp, type.secret);
+    foutput_bool(fp, type.letter_box);
 }
 
 static void
@@ -889,7 +895,7 @@ foutput_ulong (unsigned long l,
     static char   buf[sizeof(unsigned long) * 3 + 1];
     char         *cp;
 
-    putc(' ', fp);
+    foutput_space(fp);
     if (l < 10)
 	putc("0123456789"[l], fp);
     else
@@ -903,3 +909,45 @@ foutput_ulong (unsigned long l,
 	fwrite(cp, buf + sizeof(buf) - cp, 1, fp);
     }
 }
+
+void
+foutput_atsign(FILE *fp)
+{
+    putc('@', fp);
+}
+
+void
+foutput_newline(FILE *fp)
+{
+    putc('\n', fp);
+}
+
+void
+foutput_space(FILE *fp)
+{
+    putc(' ', fp);
+}
+
+void
+foutput_array_start(FILE *fp)
+{
+    fputs(" {", fp);
+}
+
+void
+foutput_array_end(FILE *fp)
+{
+    fputs(" }", fp);
+}
+
+void
+foutput_array_nodata(FILE *fp)
+{
+    fputs(" *", fp);
+}
+
+void
+foutput_bool(FILE *fp, int val)
+{
+    putc(val + '0', fp);
+}
diff --git a/src/server/ram-output.h b/src/server/ram-output.h
index e21e6031a..508f4eaf8 100644
--- a/src/server/ram-output.h
+++ b/src/server/ram-output.h
@@ -54,5 +54,11 @@ extern void
 foutput_membership(FILE *fp,
                    Membership *mship);
 
+extern void
+foutput_atsign(FILE *fp);
+
+extern void
+foutput_newline(FILE *fp);
+
 extern void
 set_output_format(int fmt);
diff --git a/src/server/ram-parse.c b/src/server/ram-parse.c
index a9884fa84..9862a505c 100644
--- a/src/server/ram-parse.c
+++ b/src/server/ram-parse.c
@@ -39,9 +39,11 @@
 #endif
 #include "timewrap.h"
 #include <sys/types.h>
+#include <errno.h>
 
 #include "s-string.h"
 #include "kom-types.h"
+#include "ram-io.h"
 #include "ram-parse.h"
 #include "log.h"
 #include "lyskomd.h"
@@ -86,10 +88,10 @@ fskipwhite(FILE *fp)
 {
     int c;
 
-    while ( (c = getc(fp)) != EOF && /* isascii(c) && */ isspace(c) )
+    while ( (c = dbfile_getc(fp)) != EOF && /* isascii(c) && */ isspace(c) )
 	;
 
-    ungetc(c, fp);
+    dbfile_ungetc(fp, c);
 }
 
 extern unsigned long
@@ -100,7 +102,7 @@ fparse_long(FILE *fp)
     int c;
 
     fskipwhite(fp);
-    while ( (c = getc(fp)) != EOF && /* isascii(c) && */ isdigit(c))
+    while ( (c = dbfile_getc(fp)) != EOF && /* isascii(c) && */ isdigit(c))
     {
 	foo = 1;
 	res = 10 * res + c - '0';
@@ -108,11 +110,12 @@ fparse_long(FILE *fp)
 
     if ( foo == 0 )
     {
-	kom_log("fparse_long() failed at pos %lu.\n", (unsigned long)ftell(fp));
+	restart_kom("fparse_long() failed at pos %ld.\n",
+		    dbfile_ftell(fp));
 	++fparse_long_errors;
     }
     
-    ungetc(c, fp);
+    dbfile_ungetc(fp, c);
 
     return res;
 }
@@ -448,14 +451,14 @@ fparse_membership_type(FILE *fp,
 {
     fskipwhite(fp);
     
-    result->invitation = getc(fp) != '0';
-    result->passive    = getc(fp) != '0';
-    result->secret     = getc(fp) != '0';
-    result->passive_message_invert = getc(fp) != '0';
-    result->reserved2  = getc(fp) != '0';
-    result->reserved3  = getc(fp) != '0';
-    result->reserved4  = getc(fp) != '0';
-    result->reserved5  = getc(fp) != '0';
+    result->invitation = dbfile_getc(fp) != '0';
+    result->passive    = dbfile_getc(fp) != '0';
+    result->secret     = dbfile_getc(fp) != '0';
+    result->passive_message_invert = dbfile_getc(fp) != '0';
+    result->reserved2  = dbfile_getc(fp) != '0';
+    result->reserved3  = dbfile_getc(fp) != '0';
+    result->reserved4  = dbfile_getc(fp) != '0';
+    result->reserved5  = dbfile_getc(fp) != '0';
 
     return OK;
 }
@@ -495,7 +498,7 @@ fparse_read_range_0(FILE *fp,
     if (no_of_read > 0)
     {
 	fskipwhite(fp);
-	switch(getc(fp))
+	switch(dbfile_getc(fp))
 	{
 	case '{':
 	    n = mship->no_of_read_ranges;
@@ -524,12 +527,12 @@ fparse_read_range_0(FILE *fp,
 	    mship->read_ranges = res;
 
 	    fskipwhite(fp);
-	    if (getc(fp) != '}')
+	    if (dbfile_getc(fp) != '}')
 	    {
                 err_stat = 1;
 		kom_errno = KOM_LDB_ERR;
-		kom_log("fparse_read_range_0(): expected '}' at pos %lu.\n",
-			(unsigned long)ftell(fp));
+		kom_log("fparse_read_range_0(): expected '}' at pos %ld.\n",
+			dbfile_ftell(fp));
 		return FAILURE;
 	    }
 	    break;
@@ -541,8 +544,8 @@ fparse_read_range_0(FILE *fp,
 	    mship->no_of_read_ranges = 0;
 	    break;
 	default:
-	    kom_log("fparse_read_range_0(): expected '*' or '{' at pos %lu.\n",
-		(unsigned long)ftell(fp));
+	    kom_log("fparse_read_range_0(): expected '*' or '{' at pos %ld.\n",
+		    dbfile_ftell(fp));
             err_stat = 2;
 	    kom_errno = KOM_LDB_ERR;
 	    return FAILURE;
@@ -551,10 +554,10 @@ fparse_read_range_0(FILE *fp,
     else
     {
 	fskipwhite(fp);
-	if ( getc(fp) != '*' ) 
+	if ( dbfile_getc(fp) != '*' ) 
 	{
-	    kom_log("fparse_read_range_0(): expected '*' at pos %lu.\n",
-		(unsigned long)ftell(fp));
+	    kom_log("fparse_read_range_0(): expected '*' at pos %ld.\n",
+		    dbfile_ftell(fp));
             err_stat = 3;
 	    kom_errno = KOM_LDB_ERR;
 	    return FAILURE;
@@ -636,7 +639,7 @@ fparse_membership_list_0(FILE *fp,
     if ( result->no_of_confs > 0 )
     {
 	fskipwhite(fp);
-	switch(getc(fp))
+	switch(dbfile_getc(fp))
 	{
 	case '{':
 	    result->confs = REALLOC(result->confs,
@@ -657,10 +660,10 @@ fparse_membership_list_0(FILE *fp,
 	    }
 	    
 	    fskipwhite(fp);
-	    if ( getc(fp) != '}' )
+	    if ( dbfile_getc(fp) != '}' )
 	    {
-		kom_log("fparse_membership_list(): expected '}' at pos %lu.\n",
-		    (unsigned long)ftell(fp));
+		kom_log("fparse_membership_list(): expected '}' at pos %ld.\n",
+			dbfile_ftell(fp));
                 err_stat = 4;
 		kom_errno = KOM_LDB_ERR;
 		return FAILURE;
@@ -680,8 +683,9 @@ fparse_membership_list_0(FILE *fp,
 	    result->no_of_confs = 0;
 	    break;
 	default:
-	    kom_log("fparse_membership_list(): expected '*' or '{' at pos %lu.\n",
-		(unsigned long)ftell(fp));
+	    kom_log("fparse_membership_list(): "
+		    "expected '*' or '{' at pos %ld.\n",
+		    dbfile_ftell(fp));
             err_stat = 5;
 	    kom_errno = KOM_LDB_ERR;
 	    return FAILURE;
@@ -690,10 +694,10 @@ fparse_membership_list_0(FILE *fp,
     else
     {
 	fskipwhite(fp);
-	if ( getc(fp) != '*' ) 
+	if ( dbfile_getc(fp) != '*' ) 
 	{
-	    kom_log("fparse_membership_list(): expected '*' at pos %lu.\n",
-		(unsigned long)ftell(fp));
+	    kom_log("fparse_membership_list(): expected '*' at pos %ld.\n",
+		    dbfile_ftell(fp));
             err_stat = 6;
 	    kom_errno = KOM_LDB_ERR;
 	    return FAILURE;
@@ -726,7 +730,7 @@ fparse_membership_list_2(FILE *fp,
     if ( result->no_of_confs > 0 )
     {
 	fskipwhite(fp);
-	switch(getc(fp))
+	switch(dbfile_getc(fp))
 	{
 	case '{':
 	    result->confs = REALLOC(result->confs,
@@ -747,10 +751,10 @@ fparse_membership_list_2(FILE *fp,
 	    }
 	    
 	    fskipwhite(fp);
-	    if ( getc(fp) != '}' )
+	    if ( dbfile_getc(fp) != '}' )
 	    {
-		kom_log("fparse_membership_list(): expected '}' at pos %lu.\n",
-		    (unsigned long)ftell(fp));
+		kom_log("fparse_membership_list(): expected '}' at pos %ld.\n",
+			dbfile_ftell(fp));
                 err_stat = 4;
 		kom_errno = KOM_LDB_ERR;
 		return FAILURE;
@@ -770,8 +774,9 @@ fparse_membership_list_2(FILE *fp,
 	    result->no_of_confs = 0;
 	    break;
 	default:
-	    kom_log("fparse_membership_list(): expected '*' or '{' at pos %lu.\n",
-		(unsigned long)ftell(fp));
+	    kom_log("fparse_membership_list(): "
+		    "expected '*' or '{' at pos %ld.\n",
+		    dbfile_ftell(fp));
             err_stat = 5;
 	    kom_errno = KOM_LDB_ERR;
 	    return FAILURE;
@@ -780,10 +785,10 @@ fparse_membership_list_2(FILE *fp,
     else
     {
 	fskipwhite(fp);
-	if ( getc(fp) != '*' ) 
+	if ( dbfile_getc(fp) != '*' ) 
 	{
-	    kom_log("fparse_membership_list(): expected '*' at pos %lu.\n",
-		(unsigned long)ftell(fp));
+	    kom_log("fparse_membership_list(): expected '*' at pos %ld.\n",
+		    dbfile_ftell(fp));
             err_stat = 6;
 	    kom_errno = KOM_LDB_ERR;
 	    return FAILURE;
@@ -830,7 +835,7 @@ fparse_conf_list(FILE *fp,
     if ( result->no_of_conf_nos > 0 )
     {
 	fskipwhite(fp);
-	switch(getc(fp))
+	switch(dbfile_getc(fp))
 	{
 	case '{':
 	    result->conf_nos = REALLOC(result->conf_nos,
@@ -847,10 +852,10 @@ fparse_conf_list(FILE *fp,
 		result->conf_nos[ i ] = fparse_long(fp);
 	    
 	    fskipwhite(fp);
-	    if ( getc(fp) != '}' )
+	    if ( dbfile_getc(fp) != '}' )
 	    {
-		kom_log("fparse_conf_list(): expected '}' at pos %lu.\n",
-		    (unsigned long)ftell(fp));
+		kom_log("fparse_conf_list(): expected '}' at pos %ld.\n",
+			dbfile_ftell(fp));
                 err_stat = 7;
 		kom_errno = KOM_LDB_ERR;
 		return FAILURE;
@@ -865,8 +870,8 @@ fparse_conf_list(FILE *fp,
 	    }
 	    break;
 	default:
-	    kom_log("fparse_conf_list(): expected '*' or '{' at pos %lu.\n",
-		(unsigned long)ftell(fp));
+	    kom_log("fparse_conf_list(): expected '*' or '{' at pos %ld.\n",
+		    dbfile_ftell(fp));
             err_stat = 8;
 	    kom_errno = KOM_LDB_ERR;
 	    return FAILURE;
@@ -875,10 +880,10 @@ fparse_conf_list(FILE *fp,
     else
     {
 	fskipwhite(fp);
-	if ( getc(fp) != '*' ) 
+	if ( dbfile_getc(fp) != '*' ) 
 	{
-	    kom_log("fparse_conf_list(): expected '*' at pos %lu.\n",
-		(unsigned long)ftell(fp));
+	    kom_log("fparse_conf_list(): expected '*' at pos %ld.\n",
+		    dbfile_ftell(fp));
             err_stat = 9;
 	    kom_errno = KOM_LDB_ERR;
 	    return FAILURE;
@@ -893,7 +898,7 @@ fparse_conf_list(FILE *fp,
     if ( result->no_of_conf_nos > 0 )
     {
 	fskipwhite(fp);
-	switch(getc(fp))
+	switch(dbfile_getc(fp))
 	{
 	case '{':
 	    result->type_of_conf = REALLOC(result->type_of_conf,
@@ -914,10 +919,10 @@ fparse_conf_list(FILE *fp,
 		}
 	    
 	    fskipwhite(fp);
-	    if ( getc(fp) != '}' )
+	    if ( dbfile_getc(fp) != '}' )
 	    {
-		kom_log("fparse_conf_list(): expected '}' at pos %lu.\n",
-		    (unsigned long)ftell(fp));
+		kom_log("fparse_conf_list(): expected '}' at pos %ld.\n",
+			dbfile_ftell(fp));
                 err_stat = 10;
 		kom_errno = KOM_LDB_ERR;
 		return FAILURE;
@@ -932,8 +937,8 @@ fparse_conf_list(FILE *fp,
 	    }
 	    break;
 	default:
-	    kom_log("fparse_conf_list(): expected '*' or '+' at pos %lu.\n",
-		(unsigned long)ftell(fp));
+	    kom_log("fparse_conf_list(): expected '*' or '+' at pos %ld.\n",
+		    dbfile_ftell(fp));
             err_stat = 11;
 	    kom_errno = KOM_LDB_ERR;
 	    return FAILURE;
@@ -942,10 +947,10 @@ fparse_conf_list(FILE *fp,
     else
     {
 	fskipwhite(fp);
-	if ( getc(fp) != '*' ) 
+	if ( dbfile_getc(fp) != '*' ) 
 	{
-	    kom_log("fparse_conf_list(): expected '*' at pos %lu.\n",
-		(unsigned long)ftell(fp));
+	    kom_log("fparse_conf_list(): expected '*' at pos %ld.\n",
+		    dbfile_ftell(fp));
             err_stat = 12;
 	    kom_errno = KOM_LDB_ERR;
 	    return FAILURE;
@@ -972,7 +977,7 @@ fparse_mark_list(FILE *fp,
     if ( result->no_of_marks > 0 )
     {
 	fskipwhite(fp);
-	switch(getc(fp))
+	switch(dbfile_getc(fp))
 	{
 	case '{':
 	    result->marks = REALLOC(result->marks,
@@ -993,10 +998,10 @@ fparse_mark_list(FILE *fp,
 		}
 	    
 	    fskipwhite(fp);
-	    if ( getc(fp) != '}' )
+	    if ( dbfile_getc(fp) != '}' )
 	    {
-		kom_log("fparse_mark_list(): expected '}' at pos %lu.\n",
-		    (unsigned long)ftell(fp));
+		kom_log("fparse_mark_list(): expected '}' at pos %ld.\n",
+			dbfile_ftell(fp));
                 err_stat = 13;
 		kom_errno = KOM_LDB_ERR;
 		return FAILURE;
@@ -1013,8 +1018,8 @@ fparse_mark_list(FILE *fp,
 	    break;
 
 	default:
-	    kom_log("fparse_mark_list(): expected '*' or '{' at pos %lu.\n",
-		(unsigned long)ftell(fp));
+	    kom_log("fparse_mark_list(): expected '*' or '{' at pos %ld.\n",
+		    dbfile_ftell(fp));
             err_stat = 14;
 	    kom_errno = KOM_LDB_ERR;
 	    return FAILURE;
@@ -1023,10 +1028,10 @@ fparse_mark_list(FILE *fp,
     else
     {
 	fskipwhite(fp);
-	if ( getc(fp) != '*' ) 
+	if ( dbfile_getc(fp) != '*' ) 
 	{
-	    kom_log("fparse_mark_list(): expected '*' at pos %lu.\n",
-		(unsigned long)ftell(fp));
+	    kom_log("fparse_mark_list(): expected '*' at pos %ld.\n",
+		    dbfile_ftell(fp));
             err_stat = 15;
 	    kom_errno = KOM_LDB_ERR;
 	    return FAILURE;
@@ -1075,7 +1080,7 @@ fparse_text_stat_2(FILE *fp,
     if ( result->no_of_misc > 0 )
     {
 	fskipwhite(fp);
-	switch( c = getc(fp) )
+	switch( c = dbfile_getc(fp) )
 	{
 	case '{':
 	    result->misc_items = REALLOC(result->misc_items,
@@ -1094,10 +1099,10 @@ fparse_text_stat_2(FILE *fp,
 		    return FAILURE;
 	    
 	    fskipwhite(fp);
-	    if ( (c = getc(fp)) != '}' )
+	    if ( (c = dbfile_getc(fp)) != '}' )
 	    {
-		kom_log("fparse_text_stat(): expected '}' at pos %lu.\n",
-			(unsigned long)ftell(fp));
+		kom_log("fparse_text_stat(): expected '}' at pos %ld.\n",
+			dbfile_ftell(fp));
 		err_stat = 16;
 		kom_errno = KOM_LDB_ERR;
 		return FAILURE;
@@ -1107,10 +1112,10 @@ fparse_text_stat_2(FILE *fp,
 
 	case '@':
 	case '+':
-	    ungetc(c, fp);
+	    dbfile_ungetc(fp, c);
 	    kom_log("fparse_text_stat(): got '%c'; expected '{' or '*'\n.", c);
-	    kom_log("Character ungetc'd and interpreted as a '*'. (pos %lu).\n",
-		(unsigned long)ftell(fp));
+	    kom_log("Character ungetc'd and interpreted as a '*' at %ld.\n",
+		    dbfile_ftell(fp));
 	    /* Fall through */
 	case '*':
 	    if ( result->misc_items != NULL )
@@ -1121,8 +1126,8 @@ fparse_text_stat_2(FILE *fp,
 	    break;
 
 	default:
-	    kom_log("fparse_text_stat(): expected '*' or '}' at pos %lu.\n",
-		(unsigned long)ftell(fp));
+	    kom_log("fparse_text_stat(): expected '*' or '}' at pos %ld.\n",
+		    dbfile_ftell(fp));
             err_stat = 17;
 	    kom_errno = KOM_LDB_ERR;
 	    return FAILURE;
@@ -1131,10 +1136,10 @@ fparse_text_stat_2(FILE *fp,
     else
     {
 	fskipwhite(fp);
-	if ( (c = getc(fp)) != '*' ) 
+	if ( (c = dbfile_getc(fp)) != '*' ) 
 	{
-	    kom_log("fparse_text_stat(): expected '*' at pos %lu.\n",
-		    (unsigned long)ftell(fp));
+	    kom_log("fparse_text_stat(): expected '*' at pos %ld.\n",
+		    dbfile_ftell(fp));
 	    err_stat = 18;
 	    kom_errno = KOM_LDB_ERR;
 	    return FAILURE;
@@ -1191,7 +1196,7 @@ fparse_text_stat_0(FILE *fp,
     if ( result->no_of_misc > 0 )
     {
 	fskipwhite(fp);
-	switch( c = getc(fp) )
+	switch( c = dbfile_getc(fp) )
 	{
 	case '{':
 	    result->misc_items = REALLOC(result->misc_items,
@@ -1210,10 +1215,10 @@ fparse_text_stat_0(FILE *fp,
 		    return FAILURE;
 	    
 	    fskipwhite(fp);
-	    if ( (c = getc(fp)) != '}' )
+	    if ( (c = dbfile_getc(fp)) != '}' )
 	    {
-		kom_log("fparse_text_stat(): expected '}' at pos %lu.\n",
-			(unsigned long)ftell(fp));
+		kom_log("fparse_text_stat(): expected '}' at pos %ld.\n",
+			dbfile_ftell(fp));
 		err_stat = 19;
 		kom_errno = KOM_LDB_ERR;
 		return FAILURE;
@@ -1225,8 +1230,8 @@ fparse_text_stat_0(FILE *fp,
 	case '+':
 	    ungetc(c, fp);
 	    kom_log("fparse_text_stat(): got '%c'; expected '{' or '*'\n.", c);
-	    kom_log("Character ungetc'd and interpreted as a '*'. (pos %lu).\n",
-		(unsigned long)ftell(fp));
+	    kom_log("Character ungetc'd and interpreted as a '*' at %ld.\n",
+		    dbfile_ftell(fp));
 	    /* Fall through */
 	case '*':
 	    if ( result->misc_items != NULL )
@@ -1237,8 +1242,8 @@ fparse_text_stat_0(FILE *fp,
 	    break;
 
 	default:
-	    kom_log("fparse_text_stat(): expected '*' or '}' at pos %lu.\n",
-		(unsigned long)ftell(fp));
+	    kom_log("fparse_text_stat(): expected '*' or '}' at pos %ld.\n",
+		    dbfile_ftell(fp));
             err_stat = 20;
 	    kom_errno = KOM_LDB_ERR;
 	    return FAILURE;
@@ -1247,10 +1252,10 @@ fparse_text_stat_0(FILE *fp,
     else
     {
 	fskipwhite(fp);
-	if ( (c = getc(fp)) != '*' ) 
+	if ( (c = dbfile_getc(fp)) != '*' ) 
 	{
-	    kom_log("fparse_text_stat(): expected '*' at pos %lu.\n",
-		    (unsigned long)ftell(fp));
+	    kom_log("fparse_text_stat(): expected '*' at pos %ld.\n",
+		    dbfile_ftell(fp));
 	    err_stat = 21;
 	    kom_errno = KOM_LDB_ERR;
 	    return FAILURE;
@@ -1307,17 +1312,17 @@ fparse_text_list(FILE *fp,
     if (no_of_texts > 0)
     {
 	fskipwhite(fp);
-	switch(getc(fp))
+	switch(dbfile_getc(fp))
 	{
 	case '{':
 	    for ( i = 0; i < no_of_texts; i++)
 		l2g_append(result, lno++, fparse_long(fp));
 	    
 	    fskipwhite(fp);
-	    if ( getc(fp) != '}' )
+	    if ( dbfile_getc(fp) != '}' )
 	    {
-		kom_log("fparse_text_list(): expected '}' at pos %lu.\n",
-		    (unsigned long)ftell(fp));
+		kom_log("fparse_text_list(): expected '}' at pos %ld.\n",
+			dbfile_ftell(fp));
                 err_stat = 22;
 		kom_errno = KOM_LDB_ERR;
 		return FAILURE;
@@ -1326,8 +1331,8 @@ fparse_text_list(FILE *fp,
 	    break;
 
 	default:
-	    kom_log("fparse_text_list(): expected '{' at pos %lu.\n",
-		(unsigned long)ftell(fp));
+	    kom_log("fparse_text_list(): expected '{' at pos %ld.\n",
+		    dbfile_ftell(fp));
             err_stat = 23;
 	    kom_errno = KOM_LDB_ERR;
 	    return FAILURE;
@@ -1336,10 +1341,10 @@ fparse_text_list(FILE *fp,
     else
     {
 	fskipwhite(fp);
-	if ( getc(fp) != '*' ) 
+	if ( dbfile_getc(fp) != '*' ) 
 	{
-	    kom_log("fparse_text_list(): expected '*' at pos %lu.\n",
-		(unsigned long)ftell(fp));
+	    kom_log("fparse_text_list(): expected '*' at pos %ld.\n",
+		    dbfile_ftell(fp));
             err_stat = 24;
 	    kom_errno = KOM_LDB_ERR;
 	    return FAILURE;
@@ -1358,10 +1363,10 @@ fparse_string(FILE *fp,
    
     length = fparse_long(fp);
 
-    if ( getc(fp) != 'H' )
+    if ( dbfile_getc(fp) != 'H' )
     {
-	kom_log("fparse_string(): expected 'H' at pos %lu.\n",
-	    (unsigned long)ftell(fp));
+	kom_log("fparse_string(): expected 'H' at pos %ld.\n",
+		dbfile_ftell(fp));
         err_stat = 25;
 	kom_errno = KOM_LDB_ERR;
 	return FAILURE;
@@ -1379,8 +1384,8 @@ fparse_string(FILE *fp,
     if ( fread(result->string, sizeof(char), result->len, fp)
 	!= (size_t)result->len )
     {
-	kom_log("fparse_string(): unexpected eof at pos %lu.\n",
-	    (unsigned long)ftell(fp));
+	kom_log("fparse_string(): unexpected eof at pos %ld.\n",
+		dbfile_ftell(fp));
         err_stat = 26;
 	kom_errno = KOM_LDB_ERR;
 	return FAILURE;
@@ -1399,7 +1404,7 @@ fparse_member_list(FILE *fp,
     if ( result->no_of_members > 0 )
     {
 	fskipwhite(fp);
-	switch(getc(fp))
+	switch(dbfile_getc(fp))
 	{
 	case '{':
 	    result->members = REALLOC(result->members,
@@ -1424,10 +1429,10 @@ fparse_member_list(FILE *fp,
 	    
 	    
 	    fskipwhite(fp);
-	    if ( getc(fp) != '}' )
+	    if ( dbfile_getc(fp) != '}' )
 	    {
-		kom_log("fparse_member_list(): expected '}' at pos %lu.\n",
-		    (unsigned long)ftell(fp));
+		kom_log("fparse_member_list(): expected '}' at pos %ld.\n",
+			dbfile_ftell(fp));
                 err_stat = 27;
 		kom_errno = KOM_LDB_ERR;
 		return FAILURE;
@@ -1444,8 +1449,8 @@ fparse_member_list(FILE *fp,
 	    break;
 
 	default:
-	    kom_log("fparse_member_list(): expected '*' or '{' at pos %lu.\n",
-		(unsigned long)ftell(fp));
+	    kom_log("fparse_member_list(): expected '*' or '{' at pos %ld.\n",
+		    dbfile_ftell(fp));
             err_stat = 28;
 	    kom_errno = KOM_LDB_ERR;
 	    return FAILURE;
@@ -1454,10 +1459,10 @@ fparse_member_list(FILE *fp,
     else
     {
 	fskipwhite(fp);
-	if ( getc(fp) != '*' ) 
+	if ( dbfile_getc(fp) != '*' ) 
 	{
-	    kom_log("fparse_member_list(): expected '*' at pos %lu.\n",
-		(unsigned long)ftell(fp));
+	    kom_log("fparse_member_list(): expected '*' at pos %ld.\n",
+		    dbfile_ftell(fp));
             err_stat = 29;
 	    kom_errno = KOM_LDB_ERR;
 	    return FAILURE;
@@ -1525,22 +1530,22 @@ fparse_priv_bits(FILE *fp,
 {
     fskipwhite(fp);
 
-    result->wheel = getc(fp) != '0';
-    result->admin = getc(fp) != '0';
-    result->statistic = getc(fp) != '0';
-    result->create_pers = getc(fp) != '0';
-    result->create_conf = getc(fp) != '0';
-    result->change_name = getc(fp) != '0';
-    result->flg7 = getc(fp) != '0';
-    result->flg8 = getc(fp) != '0';
-    result->flg9 = getc(fp) != '0';
-    result->flg10 = getc(fp) != '0';
-    result->flg11 = getc(fp) != '0';
-    result->flg12 = getc(fp) != '0';
-    result->flg13 = getc(fp) != '0';
-    result->flg14 = getc(fp) != '0';
-    result->flg15 = getc(fp) != '0';
-    result->flg16 = getc(fp) != '0';
+    result->wheel = dbfile_getc(fp) != '0';
+    result->admin = dbfile_getc(fp) != '0';
+    result->statistic = dbfile_getc(fp) != '0';
+    result->create_pers = dbfile_getc(fp) != '0';
+    result->create_conf = dbfile_getc(fp) != '0';
+    result->change_name = dbfile_getc(fp) != '0';
+    result->flg7 = dbfile_getc(fp) != '0';
+    result->flg8 = dbfile_getc(fp) != '0';
+    result->flg9 = dbfile_getc(fp) != '0';
+    result->flg10 = dbfile_getc(fp) != '0';
+    result->flg11 = dbfile_getc(fp) != '0';
+    result->flg12 = dbfile_getc(fp) != '0';
+    result->flg13 = dbfile_getc(fp) != '0';
+    result->flg14 = dbfile_getc(fp) != '0';
+    result->flg15 = dbfile_getc(fp) != '0';
+    result->flg16 = dbfile_getc(fp) != '0';
 
     return OK;
 }
@@ -1552,14 +1557,14 @@ fparse_personal_flags(FILE *fp,
 {
     fskipwhite(fp);
     
-    result->unread_is_secret = getc(fp) != '0';
-    result->flg2 = getc(fp) != '0';
-    result->flg3 = getc(fp) != '0';
-    result->flg4 = getc(fp) != '0';
-    result->flg5 = getc(fp) != '0';
-    result->flg6 = getc(fp) != '0';
-    result->flg7 = getc(fp) != '0';
-    result->flg8 = getc(fp) != '0';
+    result->unread_is_secret = dbfile_getc(fp) != '0';
+    result->flg2 = dbfile_getc(fp) != '0';
+    result->flg3 = dbfile_getc(fp) != '0';
+    result->flg4 = dbfile_getc(fp) != '0';
+    result->flg5 = dbfile_getc(fp) != '0';
+    result->flg6 = dbfile_getc(fp) != '0';
+    result->flg7 = dbfile_getc(fp) != '0';
+    result->flg8 = dbfile_getc(fp) != '0';
 
     return OK;
 }	
@@ -1572,25 +1577,25 @@ fparse_conf_type(FILE *fp,
     
     fskipwhite(fp);
     
-    result->rd_prot = getc(fp) != '0';
-    result->original = getc(fp) != '0';
-    result->secret = getc(fp) != '0';
-    result->letter_box = getc(fp) != '0';
-    c = getc(fp);
+    result->rd_prot = dbfile_getc(fp) != '0';
+    result->original = dbfile_getc(fp) != '0';
+    result->secret = dbfile_getc(fp) != '0';
+    result->letter_box = dbfile_getc(fp) != '0';
+    c = dbfile_getc(fp);
     if (c != '0' && c != '1')
     {
         result->allow_anon = 1;
 	result->forbid_secret = 0;
 	result->reserved2 = 0;
 	result->reserved3 = 0;
-	ungetc(c, fp);
+	dbfile_ungetc(fp, c);
 	return(OK);
     }
 
     result->allow_anon =   ( c != '0' );
-    result->forbid_secret = ( getc(fp) != '0' );
-    result->reserved2 = ( getc(fp) != '0' );
-    result->reserved3 = ( getc(fp) != '0' );
+    result->forbid_secret = ( dbfile_getc(fp) != '0' );
+    result->reserved2 = ( dbfile_getc(fp) != '0' );
+    result->reserved3 = ( dbfile_getc(fp) != '0' );
 
     return OK;
 }
@@ -1627,7 +1632,7 @@ fparse_who_info_list(FILE *fp,
     if ( result->no_of_persons > 0 )
     {
 	fskipwhite(fp);
-	switch(getc(fp))
+	switch(dbfile_getc(fp))
 	{
 	case '{':
 	    result->info = REALLOC(result->info,
@@ -1650,10 +1655,10 @@ fparse_who_info_list(FILE *fp,
 	    
 	    
 	    fskipwhite(fp);
-	    if ( getc(fp) != '}' )
+	    if ( dbfile_getc(fp) != '}' )
 	    {
-		kom_log("fparse_who_info_list(): expected '}' at pos %lu.\n",
-		    (unsigned long)ftell(fp));
+		kom_log("fparse_who_info_list(): expected '}' at pos %ld.\n",
+			dbfile_ftell(fp));
                 err_stat = 31;
 		kom_errno = KOM_LDB_ERR;
 		return FAILURE;
@@ -1670,8 +1675,8 @@ fparse_who_info_list(FILE *fp,
 	    break;
 
 	default:
-	    kom_log("fparse_who_info_list(): expected '*' or '{' at pos %lu.\n",
-		(unsigned long)ftell(fp));
+	    kom_log("fparse_who_info_list(): expected '*' or '{' at %ld.\n",
+		    dbfile_ftell(fp));
             err_stat = 32;
 	    kom_errno = KOM_LDB_ERR;
 	    return FAILURE;
@@ -1680,10 +1685,10 @@ fparse_who_info_list(FILE *fp,
     else
     {
 	fskipwhite(fp);
-	if ( getc(fp) != '*' ) 
+	if ( dbfile_getc(fp) != '*' ) 
 	{
-	    kom_log("fparse_who_info_list(): expected '*' at pos %lu.\n",
-		(unsigned long)ftell(fp));
+	    kom_log("fparse_who_info_list(): expected '*' at pos %ld.\n",
+		    dbfile_ftell(fp));
             err_stat = 33;
 	    kom_errno = KOM_LDB_ERR;
 	    return FAILURE;
@@ -1704,14 +1709,14 @@ fparse_aux_item_flags(FILE *fp,
 {
     fskipwhite(fp);
 
-    f->deleted = getc(fp) != '0';
-    f->inherit = getc(fp) != '0';
-    f->secret = getc(fp) != '0';
-    f->hide_creator = getc(fp) != '0';
-    f->dont_garb = getc(fp) != '0';
-    f->reserved3 = getc(fp) != '0';
-    f->reserved4 = getc(fp) != '0';
-    f->reserved5 = getc(fp) != '0';
+    f->deleted = dbfile_getc(fp) != '0';
+    f->inherit = dbfile_getc(fp) != '0';
+    f->secret = dbfile_getc(fp) != '0';
+    f->hide_creator = dbfile_getc(fp) != '0';
+    f->dont_garb = dbfile_getc(fp) != '0';
+    f->reserved3 = dbfile_getc(fp) != '0';
+    f->reserved4 = dbfile_getc(fp) != '0';
+    f->reserved5 = dbfile_getc(fp) != '0';
 
     return OK;
 }
@@ -1784,7 +1789,7 @@ fparse_aux_item_list(FILE *fp,
     if ( result->length > 0 )
     {
 	fskipwhite(fp);
-	switch( c = getc(fp) )
+	switch( c = dbfile_getc(fp) )
 	{
 	case '{':
 	    result->items = 
@@ -1803,10 +1808,10 @@ fparse_aux_item_list(FILE *fp,
 		    return FAILURE;
 	    
 	    fskipwhite(fp);
-	    if ( (c = getc(fp)) != '}' )
+	    if ( (c = dbfile_getc(fp)) != '}' )
 	    {
-                kom_log("fparse_aux_item_list(): expected '}' at pos %lu.\n",
-                    (unsigned long)ftell(fp));
+                kom_log("fparse_aux_item_list(): expected '}' at pos %ld.\n",
+			dbfile_ftell(fp));
                 err_stat = 34;
                 kom_errno = KOM_LDB_ERR;
                 return FAILURE;
@@ -1824,8 +1829,8 @@ fparse_aux_item_list(FILE *fp,
 	    break;
 
 	default:
-	    kom_log("fparse_aux_item_list(): expected '*' or '}' at pos %lu.\n",
-		(unsigned long)ftell(fp));
+	    kom_log("fparse_aux_item_list(): expected '*' or '}' at %ld.\n",
+		    dbfile_ftell(fp));
             err_stat = 35;
 	    kom_errno = KOM_LDB_ERR;
 	    return FAILURE;
@@ -1834,10 +1839,10 @@ fparse_aux_item_list(FILE *fp,
     else
     {
 	fskipwhite(fp);
-	if ( (c = getc(fp)) != '*' ) 
+	if ( (c = dbfile_getc(fp)) != '*' ) 
 	{
-            kom_log("fparse_aux_item_list(): expected '*' at pos %lu.\n",
-                (unsigned long)ftell(fp));
+            kom_log("fparse_aux_item_list(): expected '*' at pos %ld.\n",
+		    dbfile_ftell(fp));
             err_stat = 36;
             kom_errno = KOM_LDB_ERR;
             return FAILURE;
@@ -1890,8 +1895,8 @@ fparse_misc_info(FILE *fp,
 	break;
 
     default:
-	kom_log("fparse_misc_info(): illegal info_type %d at pos %lu.\n",
-	    result->type, (unsigned long)ftell(fp));
+	kom_log("fparse_misc_info(): illegal info_type %d at pos %ld.\n",
+		result->type, dbfile_ftell(fp));
         err_stat = 37;
 	kom_errno = KOM_LDB_ERR;
 	return FAILURE;
@@ -1901,3 +1906,16 @@ fparse_misc_info(FILE *fp,
 
     return OK;
 }
+
+extern Success
+fparse_set_pos(FILE *fp,
+	       long offset)
+{
+    if (fseek(fp, offset, SEEK_SET) != 0)
+    {
+	kom_log("fseek failed: %s\n", strerror(errno));
+	return FAILURE;
+    }
+    else
+	return OK;
+}
diff --git a/src/server/ram-parse.h b/src/server/ram-parse.h
index a59932edd..a404e0466 100644
--- a/src/server/ram-parse.h
+++ b/src/server/ram-parse.h
@@ -136,5 +136,9 @@ extern Success
 fparse_misc_info(FILE *fp, 
 		 Misc_info *result);
 
+extern Success
+fparse_set_pos(FILE *fp,
+	       long offset);
+
 extern void
 set_input_format(int fmt);
diff --git a/src/server/simple-cache.c b/src/server/simple-cache.c
index b8ff5ca8e..dff2dbc08 100644
--- a/src/server/simple-cache.c
+++ b/src/server/simple-cache.c
@@ -69,6 +69,7 @@
 #include "cache-node.h"
 #include "cache.h"
 #include "parser.h"
+#include "ram-io.h"
 #include "ram-parse.h"
 #include "ram-output.h"
 #include "server/smalloc.h"
@@ -258,8 +259,10 @@ read_person(FILE *fp,
     long pers_no;
     long new_pos;
 
+    if (fparse_set_pos(fp, pos+1) != OK)	/* Skip 'P' */
+	return NULL;
+
     p = alloc_person();
-    fseek(fp, pos+1, SEEK_SET);	/* Skip 'P' */
     pers_no = fparse_long(fp);
     if ( fparse_person(fp, p) != OK )
     {
@@ -267,7 +270,7 @@ read_person(FILE *fp,
 	return NULL;
     }
 
-    new_pos = ftell(fp);
+    new_pos = dbfile_ftell(fp);
     if (new_pos < 0)
     {
 	kom_log("ftell failed: %s.  Ignored.\n", strerror(errno));
@@ -292,8 +295,10 @@ read_conference(FILE *fp,
     long conf_no;
     long new_pos;
 
+    if (fparse_set_pos(fp, pos+1) != OK)	/* Skip 'C' */
+	return NULL;
+
     c = alloc_conference();
-    fseek(fp, pos+1, SEEK_SET);	/* Skip 'C' */
     conf_no = fparse_long(fp);
     if ( fparse_conference(fp, c) != OK )
     {
@@ -301,7 +306,7 @@ read_conference(FILE *fp,
 	return NULL;
     }
 
-    new_pos = ftell(fp);
+    new_pos = dbfile_ftell(fp);
     if (new_pos < 0)
     {
 	kom_log("ftell failed: %s.  Ignored.\n", strerror(errno));
@@ -324,8 +329,10 @@ read_text_stat(FILE *fp,
     Text_stat *t;
     long dummy;
 
+    if (fparse_set_pos(fp, pos+1) != OK)	/* Skip 'T' */
+	return NULL;
+
     t = alloc_text_stat();
-    fseek(fp, pos+1, SEEK_SET);	/* Skip 'T' */
     dummy = fparse_long(fp);
     if ( fparse_text_stat(fp, t) != OK )
     {
@@ -2085,6 +2092,15 @@ copy_file(FILE *from,
     }
 }    
 
+static void
+write_conf(FILE *fp,
+	   Conference *c,
+	   int conf_no)
+{
+    fprintf(fp, "C %d", conf_no);
+    foutput_conference(fp, c);
+}
+
 static void
 save_one_conf(void)
 {
@@ -2110,24 +2126,22 @@ save_one_conf(void)
 	{
 #ifdef FASTSAVE
             cn->saved_pos = cn->pos;
-            cn->pos = ftell(file_b);
+            cn->pos = dbfile_ftell(file_b);
             cn->s.saved_dirty = cn->s.dirty;
 #else
-	    cn->pos_b = ftell(file_b);
+	    cn->pos_b = dbfile_ftell(file_b);
 #endif
             
 	    if ( cn->snap_shot != NULL )
 	    {
-		fprintf(file_b, "C %lu ", sync_next);
-		foutput_conference(file_b, cn->snap_shot);
+		write_conf(file_b, cn->snap_shot, sync_next);
 #ifdef FASTSAVE
                 free_conference( cn->snap_shot );
 #endif
 	    }
 	    else if ( cn->s.dirty == 0 && cn->ptr != NULL )
 	    {
-		fprintf(file_b, "C %lu", sync_next);
-		foutput_conference(file_b, cn->ptr);
+		write_conf(file_b, cn->ptr, sync_next);
 	    }
 	    else
 	    {
@@ -2137,10 +2151,10 @@ save_one_conf(void)
 	    putc('\n', file_b);
 #ifdef FASTSAVE
             cn->saved_size = cn->size;
-	    cn->size = ftell(file_b) - cn->pos;
+	    cn->size = dbfile_ftell(file_b) - cn->pos;
             cn->s.dirty = 0;
 #else
-	    cn->size_b = ftell(file_b) - cn->pos_b;
+	    cn->size_b = dbfile_ftell(file_b) - cn->pos_b;
 #endif
 	}
 #ifdef FASTSAVE
@@ -2187,7 +2201,7 @@ save_one_pers(void)
 	}
 	else 
 	{
-	    cn->pos_b = ftell(file_b);
+	    cn->pos_b = dbfile_ftell(file_b);
 
 	    if ( cn->snap_shot != NULL )
 	    {
@@ -2203,7 +2217,7 @@ save_one_pers(void)
 	    }
 
 	    putc('\n', file_b);
-	    cn->size_b = ftell(file_b) - cn->pos_b;
+	    cn->size_b = dbfile_ftell(file_b) - cn->pos_b;
 	}
 	sync_next++;
     }
@@ -2275,6 +2289,15 @@ post_sync(void)
 
     
 
+static void
+write_text(FILE *fp,
+	   Text_stat *t,
+	   int text_no)
+{
+    fprintf(fp, "T %d", text_no);
+    foutput_text_stat(fp, t);
+}
+
 static void
 save_one_text(void)
 {
@@ -2303,25 +2326,22 @@ save_one_text(void)
 	{
 #ifdef FASTSAVE
             cn->saved_pos = cn->pos;
-	    cn->pos = ftell(file_b);
+	    cn->pos = dbfile_ftell(file_b);
             cn->s.saved_dirty = cn->s.dirty;
 #else
-	    cn->pos_b = ftell(file_b);
+	    cn->pos_b = dbfile_ftell(file_b);
 #endif
 
 	    if ( cn->snap_shot != NULL )
 	    {
-		fprintf(file_b, "T %lu", sync_next);
-		foutput_text_stat(file_b, cn->snap_shot);
+		write_text(file_b, cn->snap_shot, sync_next);
 #ifdef FASTSAVE
                 free_text_stat( cn->snap_shot );
 #endif
 	    }
 	    else if ( cn->s.dirty == 0 && cn->ptr != NULL )
 	    {
-		fprintf(file_b, "T %lu", sync_next);
-		foutput_text_stat(file_b, cn->ptr);
-
+		write_text(file_b, cn->ptr, sync_next);
 	    }
 	    else
             {
@@ -2330,11 +2350,11 @@ save_one_text(void)
 
 	    putc('\n', file_b);
 #ifdef FASTSAVE
-	    cn->size = ftell(file_b) - cn->pos;
+	    cn->size = dbfile_ftell(file_b) - cn->pos;
 	    next_text_to_sync += 1;
             cn->s.dirty = 0;
 #else
-	    cn->size_b = ftell(file_b) - cn->pos_b;
+	    cn->size_b = dbfile_ftell(file_b) - cn->pos_b;
 	    sync_next++;
 #endif
 
@@ -2358,10 +2378,11 @@ save_one_text(void)
 	    return;
 	}
 
-	offset = ftell(file_b);	/* Make sure that the entire file resides
-				   on disk. This test seems to be necessary.
-				   The data file has been corrupted at least
-				   once. */
+	offset = dbfile_ftell(file_b); /* Make sure that the entire
+					  file resides on disk. This
+					  test seems to be necessary.
+					  The data file has been
+					  corrupted at least once. */
 
 	if (offset == -1)
 	{
@@ -2443,7 +2464,7 @@ save_one_text(void)
 	    return;
 	}
 
-	offset2 = ftell (file_b);
+	offset2 = dbfile_ftell (file_b);
 	if ( offset2 != offset )
 	{
 	    kom_log ("save_one_text(): ftell confused (%ld and %ld).\n",
@@ -2729,7 +2750,7 @@ init_cache(void)
     for (record = 1; !done ; record++)
     {
 	fskipwhite(file_a);
-	switch(c = getc(file_a))
+	switch(c = dbfile_getc(file_a))
 	{
         case EOF:
             done = TRUE;
@@ -2742,7 +2763,7 @@ init_cache(void)
 
 	case '#':
 	    fskipwhite(file_a);
-	    switch(getc(file_a))
+	    switch(dbfile_getc(file_a))
 	    {
 	    case 'C':
 		next_free_num = fparse_long(file_a);
@@ -2759,7 +2780,7 @@ init_cache(void)
 
 	case '-':
 	    fskipwhite(file_a);
-	    switch(getc(file_a))
+	    switch(dbfile_getc(file_a))
 	    {
 	    case 'C':
 		num = fparse_long(file_a);
@@ -2814,7 +2835,7 @@ init_cache(void)
 	    break;
 
 	case 'C':
-	    pos = ftell(file_a) - 1; /* Don't forget the '+' */
+	    pos = dbfile_ftell(file_a) - 1; /* Don't forget the '+' */
 	    num = fparse_long(file_a);
 	    LOGACC(lt_conf_def, num);
 
@@ -2842,13 +2863,13 @@ init_cache(void)
 			    " at record %ld.\n",
 			    record);
 
-	    node->size = ftell(file_a) - node->pos;
+	    node->size = dbfile_ftell(file_a) - node->pos;
 	    setup_small_conf(num, &tmp_conf);
 	    clear_conference(&tmp_conf);
 	    break;
 
 	case 'P':
-	    pos = ftell(file_a) - 1; /* Don't forget the '+' */
+	    pos = dbfile_ftell(file_a) - 1; /* Don't forget the '+' */
 	    num = fparse_long(file_a);
 	    LOGACC(lt_pers_def, num);
 
@@ -2868,13 +2889,13 @@ init_cache(void)
 			    " record %ld.\n",
 			    record);
 	    
-	    node->size = ftell(file_a) - node->pos;
+	    node->size = dbfile_ftell(file_a) - node->pos;
 	    clear_person(&tmp_pers);
 
 	    break;
 
 	case 'T':
-	    pos = ftell(file_a) - 1; /* Don't forget the '+' */
+	    pos = dbfile_ftell(file_a) - 1; /* Don't forget the '+' */
 	    num = fparse_long(file_a);
 	    LOGACC(lt_text_def, num);
 	    
@@ -2895,14 +2916,14 @@ init_cache(void)
 			    " record %ld.\n",
 			    record);
 
-	    node->size = ftell(file_a) - node->pos;
+	    node->size = dbfile_ftell(file_a) - node->pos;
 	    clear_text_stat(&tmp_text);
 	    break;
 
 	default:
 	    restart_kom("init_cache(): Unknown key %c (%d) "
-                        "in data file at %lu\n",
-                        c, c, (unsigned long)ftell(file_a));
+                        "in data file at %ld\n",
+                        c, c, dbfile_ftell(file_a));
 	    break;
 	}
     }
-- 
GitLab