diff -ruN klic-3.003-2001-11-24/compiler/klicdb.c klic-3.003-2001-11-25/compiler/klicdb.c
--- klic-3.003-2001-11-24/compiler/klicdb.c	Sat Nov 24 14:51:10 2001
+++ klic-3.003-2001-11-25/compiler/klicdb.c	Sun Nov 25 22:13:09 2001
@@ -410,7 +410,7 @@
   if ('0' <= c && c <= '9') return c - '0';
   if ('A' <= c && c <= 'F') return c - 'A' + 10;
   if ('a' <= c && c <= 'f') return c - 'a' + 10;
-  error_exit("Invalid atom name: %s", name, 0);
+  error_exit("Invalid atom name: %s", name, NULL);
 }
 
 static void real_atom_name(
@@ -496,6 +496,7 @@
   (void) putc('\"', file);
 }
 
+
 #define TEMPDBNAME  ".klic.db"
 
 static write_db_files(
@@ -506,248 +507,253 @@
   struct functrec* f;
 
   newdb = open_db_out(TEMPDBNAME);
-  (void)fprintf(newdb, "#KLIC DB for system @%lu\n",
+  (void) fprintf(newdb, "#KLIC DB for system @%lu\n",
 		(unsigned long)inittime);
 
   reverse_atomrecs();
   if (!nocfiles) {
     atomh = open_cwd_out("atom.h");
     atomc = open_db_out("atom.c");
-    (void)fprintf(atomh, "#include <klic/atomstuffs.h>\n");
-    (void)fprintf(atomc, "#include <klic/basic.h>\n");
-    (void)fprintf(atomc, "unsigned char * Const init_atomname[] = {\n");
+    (void) fprintf(atomh, "#include <klic/atomstuffs.h>\n");
+    (void) fprintf(atomc,
+      "#include <klic/basic.h>\n\n" \
+      "unsigned char* const init_atomname[] = {\n" );
   }
-  for (a=atomroot; a!=0; a=a->next) {
+
+  for (a=atomroot; a!=NULL; a=a->next) {
     unsigned char realname[BUFSIZE];
-    real_atom_name(a->name,realname);
+    real_atom_name(a->name, realname);
     if (!nocfiles) {
-      (void)fprintf(atomh, "#define atom_%s %dL+ATOMNUMBERBASE\n",
+      (void) fprintf(atomh, "#define atom_%s %dL+ATOMNUMBERBASE\n",
 		    a->name, a->id);
-      (void)putc('\t', atomc);
+      (void) putc('\t', atomc);
       print_c_string(atomc, realname);
-      (void)putc(',', atomc);
-      (void)putc('\n', atomc);
+      (void) putc(',', atomc);
+      (void) putc('\n', atomc);
     }
-    (void)fprintf(newdb, "atom_%s\n", a->name);
+    (void) fprintf(newdb, "atom_%s\n", a->name);
   }
+  assert(a == NULL);
+
   if (!nocfiles) {
-    (void)fprintf(atomc, "};\n");
-    (void)fprintf(atomc,
-		  "Const unsigned long initial_atoms = %d;\n", nextatom);
-    (void)fprintf(atomc, "unsigned char **atomname = (unsigned char **)init_atomname;\n");
-    (void)fclose(atomh);
-    (void)fclose(atomc);
+    (void) fprintf(atomc, "};\n\n" \
+		"const unsigned long initial_atoms = %d;\n" \
+		"unsigned char** atomname =" \
+		" (unsigned char**) init_atomname;\n",
+		nextatom );
+    (void) fclose(atomh);
+    (void) fclose(atomc);
   }
 
   reverse_functrecs();
   if (!nocfiles) {
     functh = open_cwd_out("funct.h");
     functc = open_db_out("funct.c");
-    (void)fprintf(functh, "#include <klic/functorstuffs.h>\n\n");
-    (void)fprintf(functc, "#include <klic/basic.h>\n");
-    (void)fprintf(functc, "#include <klic/atomstuffs.h>\n\n");
-    (void)fprintf(functc, "Const unsigned long init_functors[] = {\n");
+    (void) fprintf(functh, "#include <klic/functorstuffs.h>\n\n");
+    (void) fprintf(functc,
+		"#include <klic/basic.h>\n" \
+		"#include <klic/atomstuffs.h>\n\n" \
+		"const unsigned long init_functors[] = {\n" );
   }
-  for (f=functroot; f!=0; f=f->next) {
+  for (f=functroot; f!=NULL; f=f->next) {
     if (!nocfiles) {
-      (void)fprintf(functh, "#define functor_%s_%d\t%dL+FUNCTORNUMBERBASE\n",
+      (void) fprintf(functh, "#define functor_%s_%d\t%dL+FUNCTORNUMBERBASE\n",
 		    f->principal_functor->name, f->arity,
 		    f->id);
-      (void)fprintf(functc, "\t%dL+ATOMNUMBERBASE,\n",
+      (void) fprintf(functc, "\t%dL+ATOMNUMBERBASE,\n",
 		    f->principal_functor->id);
     }
-    (void)fprintf(newdb, "functor_%s_%d\n",
+    (void) fprintf(newdb, "functor_%s_%d\n",
 		  f->principal_functor->name, f->arity);
   }
-  (void)fclose(newdb);
-
+  assert(f == NULL);
+  (void) fclose(newdb);
 
   if (!nocfiles) {
-    (void)fprintf(functc, "};\n\nConst unsigned long init_arities[] = {\n");
-    for (f=functroot; f!=0; f=f->next) {
+    (void) fprintf(functc, "};\n\n" \
+		"const unsigned long init_arities[] = {\n" );
+    for (f=functroot; f!=NULL; f=f->next) {
       (void)fprintf(functc, "\t%dL,\n", f->arity);
     }
-    (void)fprintf(functc, "};\n");
-    (void)fprintf(functc,
-		  "Const unsigned long initial_functors = %d;\n",
-		  nextfunct);
-    (void)fprintf(functc,
-		  "unsigned long *functors = (unsigned long *)init_functors;\n");
-    (void)fprintf(functc,
-		  "unsigned long *arities = (unsigned long *)init_arities;\n");
-    (void)fclose(functh);
-    (void)fclose(functc);
+    assert(f == NULL);
+    (void) fprintf(functc, "};\n" \
+		"const unsigned long initial_functors = %d;\n" \
+		"unsigned long* functors = (unsigned long*) init_functors;\n" \
+		"unsigned long* arities = (unsigned long*) init_arities;\n",
+		nextfunct );
+    (void) fclose(functh);
+    (void) fclose(functc);
   }
 
   {
-    char *newpath = make_path(dbdir, TEMPDBNAME);
-    char *dbpath = make_path(dbdir, DBFILENAME);
+    char* newpath = make_path(dbdir, TEMPDBNAME);
+    char* dbpath = make_path(dbdir, DBFILENAME);
     if (rename(newpath, dbpath)) {
       error_exit("Can't rename %s to %s", newpath, dbpath);
     }
   }
 }
 
-write_predicate_file()
+static void write_predicate_file(void)
 {
-  FILE *predc;
-  struct modrec *m;
+  FILE* predc;
+  struct modrec* m;
   if (!nolink) {
     predc = open_db_out("predicates.c");
-    (void)fprintf(predc, "#include <klic/basic.h>\n");
-    (void)fprintf(predc, "#include <klic/predinfo.h>\n");
-    (void)fprintf(predc, "\n");
+    (void) fprintf(predc,
+		"#include <klic/basic.h>\n" \
+		"#include <klic/predinfo.h>\n\n" );
   }
 
   if (makelib) {
     char libnamebuf[1024];
-    FILE *libdb;
+    FILE* libdb;
     sprintf(libnamebuf, "lib%s.db", libmade);
     libdb = open_db_out(libnamebuf);
-    for (m=modroot; m!=0; m=m->next) {
-      struct predrec *p;
-      struct refmodrec *r;
-      if (m->morc == Mod) {
-	(void)fprintf(libdb, "module_%s\n", m->name);
-      } else {
-	(void)fprintf(libdb, "class_%s\n", m->name);
-      }
-      for (p=m->defined; p!=0; p=p->next) {
-	(void)fprintf(libdb, "predicate_%s\n", p->name);
+    for (m=modroot; m!=NULL; m=m->next) {
+      struct predrec* p;
+      struct refmodrec* r;
+      (void) fprintf(libdb,
+		(m->morc==Mod ? "module_%s\n" : "class_%s\n"),
+		m->name );
+      for (p=m->defined; p!=NULL; p=p->next) {
+	(void) fprintf(libdb, "predicate_%s\n", p->name);
       }
-      for (r=m->refmod; r!=0; r=r->next) {
-	if (r->morc == Mod) {
-	  (void)fprintf(libdb, "ref_module_%s\n", r->name);
-	} else {
-	  (void)fprintf(libdb, "ref_class_%s\n", r->name);
-	}
+      assert(p == NULL);
+      for (r=m->refmod; r!=NULL; r=r->next) {
+	(void) fprintf(libdb,
+		(r->morc==Mod ? "ref_module_%s\n" : "ref_class_%s\n"),
+		r->name );
       }
+      assert(r == NULL);
     }
-    (void)fclose(libdb);
+    assert(m == NULL);
+    (void) fclose(libdb);
   }
   if (!nocfiles) {
-    struct modrec *linkedmod = 0;
+    struct modrec* linkedmod = NULL;
     struct searched_mod {
-      unsigned char *name;
+      unsigned char* name;
       enum mod_or_class morc;
     };
-#define Push(nam, mc) { smdp->name = nam; smdp->morc = mc; smdp++; }
+#define Push(nam, mc) do{ smdp->name = (nam); smdp->morc = (mc); smdp++; }while(0)
     struct searched_mod searchmod[4096];
-    struct searched_mod *smdp = searchmod;
-    Push((unsigned char *)"main", Mod);
-    Push((unsigned char *)"generic", Mod);
-    Push((unsigned char *)"unify__term__dcode", Mod);
-    Push((unsigned char *)"integer__arithmetics", Mod);
+    struct searched_mod* smdp = searchmod;
+    Push((unsigned char*) "main", Mod);
+    Push((unsigned char*) "generic", Mod);
+    Push((unsigned char*) "unify__term__dcode", Mod);
+    Push((unsigned char*) "integer__arithmetics", Mod);
     while (smdp != searchmod) {
-      struct modrec *searched;
-      struct refmodrec *refmods;
+      struct modrec* searched;
+      struct refmodrec* refmods;
       --smdp;
       searched = find_module(smdp->name, smdp->morc, modroot);
-      if (searched != 0) {
-	struct modrec *newlinked;
-	newlinked = (struct modrec *)malloc(sizeof(struct modrec));
+      if (searched != NULL) {
+	struct modrec* newlinked;
+	newlinked = (struct modrec*) malloc(sizeof(struct modrec));
 	*newlinked = *searched;
 	newlinked->next = linkedmod;
 	linkedmod = newlinked;
 	for (refmods = searched->refmod;
-	     refmods != 0;
+	     refmods != NULL;
 	     refmods = refmods->next) {
-	  if (find_module(refmods->name, refmods->morc, linkedmod) == 0) {
-	    struct searched_mod *pp;
+	  if (find_module(refmods->name, refmods->morc, linkedmod) == NULL) {
+	    struct searched_mod* pp;
 	    Push(refmods->name, refmods->morc);
 	    for (pp=searchmod;
 		 pp->morc != refmods->morc ||
-		 strcmp((char *)(pp->name), (char *)refmods->name) != 0;
+		 strcmp((char*) (pp->name), (char*) refmods->name) != 0;
 		 ++pp)
 	      ;
 	    if (pp != smdp-1) smdp--;
 	  }
 	}
+	assert(refmods == NULL);
       }
     }
+    assert(smdp == searchmod);
     if (!nolink) {
-      for (m=linkedmod; m!=0; m=m->next) {
+      for (m=linkedmod; m!=NULL; m=m->next) {
 	if (m->morc == Mod) {
-	  struct predrec *p;
-	  for (p=m->defined; p!=0; p=p->next) {
-	    (void)fprintf(predc,
-			  "extern Const struct predicate predicate_%s;\n",
-			  p->name);
-	    (void)fprintf(predc,
-			  "static Const struct predinfo predinfo_%s = {\n",
-			  p->name);
-	    (void)fprintf(predc, "  ");
+	  struct predrec* p;
+	  for (p=m->defined; p!=NULL; p=p->next) {
+	    (void) fprintf(predc,
+			"extern const struct predicate predicate_%s;\n" \
+			"static const struct predinfo predinfo_%s = {\n  ",
+			p->name, p->name );
 	    {
 	      unsigned char realname[BUFSIZE];
-	      real_pred_name(p->name,realname);
-	      (void)print_c_string(predc, realname);
+	      real_pred_name(p->name, realname);
+	      (void) print_c_string(predc, realname);
 	    }
-	    (void)fprintf(predc, ",\n");
-	    (void)fprintf(predc, "  &predicate_%s\n", p->name);
-	    (void)fprintf(predc, "};\n");
-	    (void)fprintf(predc, "\n");
+	    (void) fprintf(predc, ",\n  &predicate_%s\n};\n\n", p->name);
 	  }
-	  (void)
-	    fprintf(predc,
-		    "static Const struct predinfo * Const preds_in_%s[] = {\n",
-		    m->name);
-	  for (p=m->defined; p!=0; p=p->next) {
-	    (void)fprintf(predc, "  &predinfo_%s,\n", p->name);
+	  (void) fprintf(predc,
+		"static const struct predinfo* const preds_in_%s[] = {\n",
+		m->name );
+	  for (p=m->defined; p!=NULL; p=p->next) {
+	    (void) fprintf(predc, "  &predinfo_%s,\n", p->name);
 	  }
-	  (void)fprintf(predc, "  0\n");
-	  (void)fprintf(predc, "};\n");
+	  assert(p == NULL);
+	  (void) fprintf(predc, "  0\n};\n");
 	}
       }
-      (void)fprintf(predc, "Const struct modinfo defined_modules[] = {\n");
-      for (m=linkedmod; m!=0; m=m->next) {
+      (void) fprintf(predc, "const struct modinfo defined_modules[] = {\n");
+      for (m=linkedmod; m!=NULL; m=m->next) {
 	if (m->morc == Mod) {
-	  (void)fprintf(predc, "  ");
+	  (void) fprintf(predc, "  ");
 	  {
 	    unsigned char realname[BUFSIZE];
 	    real_atom_name(m->name, realname);
-	    (void)print_c_string(predc, realname);
+	    (void) print_c_string(predc, realname);
 	  }
-	  (void)fprintf(predc, ",\n");
-	  (void)fprintf(predc, "  preds_in_%s,\n", m->name);
+	  (void) fprintf(predc, ",\n  preds_in_%s,\n", m->name);
 	}
       }
-      (void)fprintf(predc, "  0, 0\n");
-      (void)fprintf(predc, "};\n");
-      (void)fclose(predc);
+      (void) fprintf(predc, "  0, 0\n};\n");
+      (void) fclose(predc);
     }
   }
 }
 
-from_same_installation(klicdb, inittime)
-     FILE *klicdb;
-     time_t inittime;
+static int from_same_installation(
+  FILE* klicdb,
+  time_t inittime )
 {
   int c;
   time_t dbtime;
-  if (fgetc(klicdb) != '#') return 0;
+
+  c = fgetc(klicdb);
+  if (c != '#') return 0;
+  assert(c == '#');
+
   do {
     c = fgetc(klicdb);
     if (c == '\n' || c == EOF) {
       return 0;
     }
+    assert(c != '\n' && c != EOF);
   } while (c != '@');
+  assert(c == '@');
+
   {
     unsigned long temp;
     if (fscanf(klicdb, "%lu", &temp) != 1) return 0;
-    dbtime = (time_t)temp;
+    dbtime = (time_t) temp;
   }
   if (dbtime != inittime) return 0;
   rewind(klicdb);
   return 1;
 }
 
+
 #define Optarg() \
-( argv[optind][charind+1] != 0 ? \
+( argv[optind][charind+1] != '\0' ? \
   argv[optind]+charind+1 : \
-  (optind++, argv[optind] ))
+  (optind++, argv[optind]) )
 
-static void unlink_file(name)
-     char *name;
+static void unlink_file(
+  char* name )
 {
   int err = unlink(name);
   if (err == 0) return;
@@ -756,9 +762,9 @@
   }
 }
 
-main(argc,argv)
-     int argc;
-     char **argv;
+extern int main(
+  int argc,
+  char** argv )
 {
   int optind;
   char *dbpath, *initpath, *libdbpath;
@@ -775,7 +781,7 @@
        optind++) {
     int charind;
     for (charind = 1;
-	 argv[optind][charind] != 0;
+	 argv[optind][charind] != '\0';
 	 charind++) {
       switch (c = argv[optind][charind]) {
       case 'X': initdbdir = Optarg(); goto nextarg;
@@ -786,17 +792,19 @@
       default: usage_error(argv[0]);
       }
     }
-  nextarg:;
+    assert(argv[optind][charind] == '\0');
+  nextarg: ;
   }
+  assert(optind==argc || argv[optind][0] != '-');
 
-  if (initdbdir == 0) {
+  if (initdbdir == NULL) {
     fprintf(stderr, "Initial KLIC database directory not specified\n");
     usage_error(argv[0]);
   }
 
-  atomroot = 0; nextatom = 0;
-  functroot = 0; nextfunct = 0;
-  currentmod.name = 0;
+  atomroot = NULL; nextatom = 0;
+  functroot = NULL; nextfunct = 0;
+  currentmod.name = NULL;
 
   initpath = make_path(initdbdir, "klicdb.init");
   libdbpath = make_path(initdbdir, LIBDBNAME);
@@ -812,7 +820,8 @@
 #ifdef USELOCKF
   strcpy(fdbuf, dbpath);
   strcat(fdbuf, ".lock");
-  lockf(fd=open(fdbuf,O_RDWR), F_LOCK, 1);
+  fd = open(fdbuf, O_RDWR);
+  lockf(fd, F_LOCK, 1);
 #endif
 
   if (!makelib) {
@@ -821,14 +830,14 @@
     changes = 0;
   }
   klicdb = fopen(dbpath, "r");
-  if (klicdb != 0 &&
+  if (klicdb != NULL &&
       from_same_installation(klicdb, initstat.st_mtime)) {
     read_db_file(klicdb, dbpath);
     changes = 0;
   } else {
-    FILE *klicinit = fopen(initpath, "r");
-    if (klicinit == 0) {
-      error_exit("Can't open initial database file: %s", initpath, 0);
+    FILE* klicinit = fopen(initpath, "r");
+    if (klicinit == NULL) {
+      error_exit("Can't open initial database file: %s", initpath, NULL);
     }
     unlink_file("atom.h");
     unlink_file(make_path(dbdir, "atom.c"));
@@ -842,6 +851,7 @@
   for (n=optind; n<argc; n++) {
     read_ext_file(argv[n]);
   }
+  assert(n == argc);
 
   {
     struct stat buf;
@@ -859,7 +869,8 @@
 
 #ifdef USELOCKF
   close(fd);
-  lockf(fd,F_ULOCK,1);
+  lockf(fd, F_ULOCK, 1);
 #endif
+
   return 0;
 }
