diff -uNr meme-old/bottom.c meme/bottom.c
--- meme-old/bottom.c	Sun Nov 21 12:17:38 1999
+++ meme/bottom.c	Fri Dec  3 18:17:37 1999
@@ -10,6 +10,7 @@
  */
  
 #include <stdio.h>
+#include <stdlib.h>
 #include <sys/mman.h>
 #include "datastru.h"
 
@@ -20,13 +21,12 @@
  * in the input text stream.
  */
  
-read_text(net_type *net, nodelist_type *input, nodelist_type *output,
-          char *filename)
+void read_text(net_type *net, nodelist_type *input, nodelist_type *output,
+	       char *filename)
 {
   FILE *fp_in; int filesize;
   char cha;
   int loop;
-  int result;
   int quote;    /*boolean - whether we are within a quote or not */
   int line, qline;  /*current line being processed... */
   
@@ -34,8 +34,9 @@
   
   if (!(fp_in=fopen(filename,"r")))
   {
-     printf("training file '%s'  not found - bummer!\n",filename);
-     exit(0);
+     fprintf(stderr,"training file '%s'  not found - bummer: ",filename);
+     perror("fopen");
+     exit(1);
   }
   /* firstly, see just how big this training file is... */
   fseek(fp_in,0,2); filesize=ftell(fp_in); fseek(fp_in,0,0);
@@ -52,15 +53,19 @@
       input->size++;
       if (cha>='A' && cha<='Z') cha=cha-'A'+'a'; /* get everything lower-case */
       input->data[input->size]=(int)cha;
-      if (net->mode==69 && cha==34)  /*we hit a quote and are in story mode*/
-        if (quote==0)
-	 {quote=1; input->size--; qline=line; }/*begin quote... */
+      if (net->mode==69 && cha==34) {  /*we hit a quote and are in story mode*/
+        if (quote==0) {
+	  quote=1;
+	  input->size--;
+	  qline=line;
+	}/*begin quote... */
 	else 
 	{
 	   quote=0; input->data[input->size]=10;
 	   if (line-qline>MAX_QUOTED_LINES)
 	       printf("**** POSSIBLE QUOTES-ERROR AT LINE %i\n",qline);
 	}
+      }
       /* only include quoted text, with option 69... */
       if (net->mode==69 && cha!=34 && quote==0) input->size--;
     }
@@ -147,12 +152,10 @@
  * free_chunk(net,start,size)
  * This routine will free up a chunk of net.data at location 'start')
  */
-int free_chunk(net_type *net, int start, int size)
+void free_chunk(net_type *net, int start, int size)
 {
-  int posn;
-  
   /* corner case - ignore requests to free up a 0-sized chunk */
-  if (size==0) return(0);
+  if (size==0);
   /*put a link into previous net->free[size] in net[start], and change
     net->free[size] to be start */
   if(size>MAX_IPOP)
@@ -174,7 +177,7 @@
  * (CURRENTLY UNUSED AND UNTESTED - DEFRAGGING IS PREFERRED OPTION...)
  */
 
-int chop_a_chunk(net_type *net,int size)
+void chop_a_chunk(net_type *net,int size)
 {
    int new_chunk;
    
@@ -192,7 +195,7 @@
      {
        chop_a_chunk(net,size*2);
        chop_a_chunk(net,size);
-       return(0);
+       return;
      }
      
    }
@@ -207,7 +210,7 @@
       {
          chop_a_chunk(net,size+1024);
 	 chop_a_chunk(net,size);
-	 return(0);
+	 return;
       }
    }
    
@@ -218,7 +221,7 @@
  * This routine will dump out stats on chunk-sizes used in a net.
  */
 
-dump_stats(net_type *net)
+void dump_stats(net_type *net)
 {
    int usedcount[MAX_IPOP];
    int level[MAX_LEVELS];
@@ -266,11 +269,14 @@
  * This routine reads in the construct data from a file
  */
 
-int load(net_type *net)
+void load(net_type *net)
 {
   FILE *fp;
 
-  fp=fopen("net","rb");
+  if (!(fp=fopen("net","rb"))) {
+    perror("fopen('net')");
+    exit(1);
+  }
   fread(&(net->ptr),sizeof(int),1,fp);
   fread(&(net->nodespace),sizeof(int),1,fp);
   fread(&(net->actual_num_nodes),sizeof(int),1,fp);
@@ -278,7 +284,10 @@
   fread(&(net->next_dream),sizeof(int),1,fp);
   fread(&(net->free),sizeof(int),MAX_IPOP+1,fp);   
   fread(&(net->data),sizeof(int),MAX_DATA_SIZE,fp);
-  fclose(fp);
+  if (fclose(fp)) {
+    perror("fclose('net')");
+    exit(1);
+  }
 }
 
 /****************************************************************
@@ -287,20 +296,25 @@
  *  of the program
  */
 
-save(net_type *net)
+void save(net_type *net)
 {
   FILE *fp;
 
-  fp=fopen("net","wb");
+  if (!(fp=fopen("net","wb"))) {
+    perror("fopen('net')");
+    exit(1);
+  }
   fwrite(&(net->ptr),sizeof(int),1,fp);
   fwrite(&(net->nodespace),sizeof(int),1,fp);
   fwrite(&(net->actual_num_nodes),sizeof(int),1,fp);
   fwrite(&(net->deleted_node),sizeof(int),1,fp);
   fwrite(&(net->next_dream),sizeof(int),1,fp);
-  fwrite(&(net->free),sizeof(int),MAX_IPOP+1,fp);  
+  fwrite(&(net->free),sizeof(int),MAX_IPOP+1,fp);
   fwrite(&(net->data),sizeof(int),MAX_DATA_SIZE,fp);
- 
-  fclose(fp);
+  if (fclose(fp)) {
+    perror("fclose('net')");
+    exit(1);
+  }
 }                                                     
  
  
diff -uNr meme-old/datastru.h meme/datastru.h
--- meme-old/datastru.h	Sun Nov 21 12:13:42 1999
+++ meme/datastru.h	Fri Dec  3 18:17:39 1999
@@ -188,3 +188,53 @@
    int node[FRAG_SIZE+2];
    int  size;
 } table_type;
+
+/****************************************************************************
+ * Function declarations (for clean compiles)
+ */
+/* init.c */
+int responce_node(int node);
+
+/* main.c */
+void need_input(net_type *net, nodelist_type *input);
+int off_the_end(net_type *net, int node, nodelist_type *input, int posn);
+void find_match(net_type *net, int node, nodelist_type *input, int posn);
+
+/* high.c */
+void print_node(net_type *net, int node);
+int get_level(net_type *net, int node);
+int find_node(net_type *net, int lhs, int rhs);
+int get_node_size(net_type *net, int node);
+void print_node_to_file(net_type *net, int node, FILE *fp_out);
+void dream(net_type *net);
+
+/* init.c */
+void defrag(net_type *net);
+void initialise();
+
+/* mid.c */
+void delete_node(net_type *net,int node);
+int new_node(net_type *net);
+int make_node(net_type *net, int lhs, int rhs);
+
+/* bottom.c */
+int get_chunk(net_type *net, int size);
+void free_chunk(net_type *net, int start, int size);
+void dump_stats(net_type *net);
+void read_text(net_type *net, nodelist_type *input, nodelist_type *output,
+	       char *filename);
+
+/* low.c */
+void add_lhs_output(net_type *net, int node, int output);
+void add_rhs_output(net_type *net, int node, int output);
+void del_lhs_output(net_type *net, int node, int output);
+void del_rhs_output(net_type *net, int node, int output);
+
+/* tweak.c */
+int compare(net_type *net, int node, nodelist_type *input, int posn);
+int get_meta(net_type *net, nodelist_type *input, int posn, int meta_rhs);
+void find_tweak_match(net_type *net, int node, nodelist_type *input, int posn);
+int tweak_build(net_type *net, int node, nodelist_type *input, int lhs_posn,
+	        tweaks_type *tweaks);
+void multi_tweak_match(net_type *net, int node, nodelist_type *input, int posn,
+                       tweaks_type *tweaks);
diff -uNr meme-old/high.c meme/high.c
--- meme-old/high.c	Sun Nov 21 12:17:38 1999
+++ meme/high.c	Fri Dec  3 18:17:37 1999
@@ -25,7 +25,6 @@
 int find_node(net_type *net, int lhs, int rhs)
 {
    int metanode;
-   int loop;
    int outputs;
    int start, end;
    int num_lhs_op, num_rhs_op;
@@ -64,14 +63,14 @@
 /**************************************************************************/
 /* print_node() - recursive - print out what a node is made up of.
  */
-print_node(net_type *net, int node)
+void print_node(net_type *net, int node)
 {
 
   /* 2 conditions to handle... 1) node<=NUM_BASE_NODES (just print char) and
                                2) node>NUM_BASE_NODES (recurse!) */
 
   /* firstly, sanity check*/
-  if (node==0) return(0); 
+  if (node==0) return; 
 
   if (node<=BASE_NODE_SPACE) printf("%c",node/TOP);
   else
@@ -85,14 +84,14 @@
 /* print_node_to_file() - recursive - print out what a node is made up of.
  * except output to a file.
  */
-print_node_to_file(net_type *net, int node, FILE *fp_out)
+void print_node_to_file(net_type *net, int node, FILE *fp_out)
 {
 
   /* 2 conditions to handle... 1) node<=NUM_BASE_NODES (just print char) and
                                2) node>NUM_BASE_NODES (recurse!) */
 
   /* firstly, sanity check*/
-  if (node==0) return(0); 
+  if (node==0); 
 
   if (node<=BASE_NODE_SPACE)  fprintf(fp_out,"%c",node/TOP);
   else
@@ -107,7 +106,7 @@
 /**************************************************************************/
 /* add_to_input() - recursive - adds a node, char by char, to end of input
  */
-add_to_input(net_type *net, nodelist_type *input, int node)
+void add_to_input(net_type *net, nodelist_type *input, int node)
 {
 
   /* 2 conditions to handle... 1) node<=NUM_BASE_NODES (just print char) and
@@ -156,12 +155,12 @@
 }
 
 /*****************************************************************************
- * int dream(net) - have a clear-out of some nodes with no outputs.
+ *     dream(net) - have a clear-out of some nodes with no outputs.
  *                  This is much like simulated anealing in Neural Nets.
  *		    Called from make_node()
  */
 
-dream(net_type *net)
+void dream(net_type *net)
 {
    int node;
    int total;
@@ -217,3 +216,4 @@
    if (net->next_dream<MAX_WAKE)
    net->next_dream=net->next_dream*100/WAKE_INCREASE; 
 }
+
diff -uNr meme-old/init.c meme/init.c
--- meme-old/init.c	Sun Nov 21 12:17:38 1999
+++ meme/init.c	Fri Dec  3 18:17:37 1999
@@ -8,7 +8,12 @@
 */
 
 #include <stdio.h>
+#include <unistd.h>
+#include <stdlib.h>
 #include <sys/mman.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
 #include "datastru.h"
 
 /**************************************************************************
@@ -35,7 +40,7 @@
  * initialise()  - set up a 'net' file.
  */
  
-initialise()
+void initialise()
 {
   net_type *net;
   int loop;
@@ -44,8 +49,11 @@
 #ifdef DOS
   net=(net_type *)malloc(sizeof(net_type));
 #else   /* We're in UNIX mode... */
-  creat("net",0644);
-  fp=open("net", 2);
+  fp=open("net", O_RDWR | O_CREAT | O_TRUNC, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
+  if (fp<0) {
+    perror("open('net')");
+    exit(1);
+  }
   lseek(fp,sizeof(net_type)-1,SEEK_SET); 
   write(fp, "", 1); 
   net=(net_type *)mmap(0,sizeof(net_type),PROT_READ+PROT_WRITE,MAP_SHARED,fp,0);
@@ -115,16 +123,18 @@
  * NOTE!!!! Table is kept in REVERSE ORDER, highest item first.
  */
  
-int add_to_table(table_type *table,int posn,int node)
+void add_to_table(table_type *table,int posn,int node)
 {
 
   int bot,mid,top, finished;
   int below, above;
   int loop;
 
+  mid=0;
+
   /* printf("add_to_table() -item=%i, tabsize=%i\n",posn,table->size); */
   
-  if (posn==0) return(0);
+  if (posn==0) return;
   /* firstly, work out where in the table that 'posn' fits... */
   if (table->size==0) mid=1;
   else if (posn>table->posn[1]) mid=1;
@@ -159,10 +169,10 @@
  * defragmenting it as it goes...
  */
  
-int defrag(net_type *net)
+void defrag(net_type *net)
 {
   table_type *table;
-  int top,mid,bot,floor,ceiling;
+  int top,floor,ceiling;
   int posn;
   int loop;
   int gap, side, node, size,chunksize;
@@ -217,7 +227,10 @@
       msync((void *)net,sizeof(net_type), MS_ASYNC); /*write changes back*/
 #endif
       free(table);
+      /*
       return(ceiling-net->ptr);
+      */
+      return;
     }
     
     /* OK, now that we've build a table, shuffle every item present to the
diff -uNr meme-old/low.c meme/low.c
--- meme-old/low.c	Sun Nov 21 12:17:38 1999
+++ meme/low.c	Fri Dec  3 18:17:37 1999
@@ -21,7 +21,7 @@
  * 2 nodes with each other.
  */
 
-write_to_scratch(net_type *net, int node)
+void write_to_scratch(net_type *net, int node)
 {
    int lhs,rhs;
    
@@ -80,7 +80,7 @@
 
 int compare_node_node(net_type *net, int node1, int node2)
 {
-  int first,lhs,rhs, result;
+  int first, result;
   
   /* printf("compare '"); print_node(net,node1); printf("' with '");
      print_node(net,node2); printf("'\n"); */
@@ -181,7 +181,7 @@
 {
   int new_chunk,loop;
   int skipped;
-  int old_size;
+  int old_size=0;
 
   /* special case - only one item in chunk */
   if (size==1) {free_chunk(net,chunk,1); return(0);}
@@ -229,7 +229,7 @@
  * called by make_node()
  */
 
-add_lhs_output(net_type *net, int node, int output)
+void add_lhs_output(net_type *net, int node, int output)
 {
   int length;
   int chunk;
@@ -248,7 +248,7 @@
  * called by make_node()
  */
 
-add_rhs_output(net_type *net, int node, int output)
+void add_rhs_output(net_type *net, int node, int output)
 {
   int length;
   int chunk;
@@ -267,7 +267,7 @@
  * called by delete_node()
  */
 
-del_lhs_output(net_type *net, int node, int output)
+void del_lhs_output(net_type *net, int node, int output)
 {
   int length;
   int chunk;
@@ -286,7 +286,7 @@
  * called by delete_node()
  */
 
-del_rhs_output(net_type *net, int node, int output)
+void del_rhs_output(net_type *net, int node, int output)
 {
   int length;
   int chunk;
diff -uNr meme-old/main.c meme/main.c
--- meme-old/main.c	Sun Nov 21 12:17:38 1999
+++ meme/main.c	Fri Dec  3 18:17:37 1999
@@ -1,5 +1,4 @@
-/* meme machine - domain modelling using recursive information
- *  storage.
+/* meme machine - domain modelling using recursive information storage.
  * Routines defined here:
  *
  * off_the_end()
@@ -8,21 +7,32 @@
  * analyse_match()
  * find_match()
  * find_greedy_match()
- * listen()
+ * netlisten()
  * main()
  */
 
 
 #include <stdio.h>
+#include <unistd.h>
+#include <stdlib.h>
 #include <sys/mman.h>
 #include <sys/times.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <signal.h>
+#include <fcntl.h>
+#include <string.h>
 #include "datastru.h"
 
+#define STR_LEN 1024
+char * home=NULL;
+char * chatlog=NULL;
+
+
 /*************************************************************************
  * off_the_end() - routine to handle running out of input
  * (This is a very infrequently called routine, so can be slow as a slug)
  */
- 
 int off_the_end(net_type *net, int node, nodelist_type *input, int posn)
 {
   FILE *fp;
@@ -48,7 +58,7 @@
   { 
      input->size++; input->data[input->size]=node; net->spoken++;
      printf("%c",node/TOP);
-     fp=fopen("/tmp/chatlog","a"); fputc(node/TOP,fp); fclose(fp);
+     fp=fopen(chatlog,"a"); fputc(node/TOP,fp); fclose(fp);
      return(posn+1);
   }
   else if (net->mode==4)  /*we've switched into prompt-mode */
@@ -60,6 +70,9 @@
   }
   if (responce_node(node/TOP) && net->mode==2) net->mode=3;
   /* if (net->mode==3 && !responce_node(node/TOP)) net->mode=2; */
+  /* functionality change! -kees */
+  fprintf(stderr,"\nooh: returning FALSE.  Is that okay? -kees\n");
+  return(FALSE);
 }
 
 /*************************************************************************
@@ -71,108 +84,151 @@
  * further.
  *
  */
-
-int need_input(net_type *net, nodelist_type *input)
+void need_input(net_type *net, nodelist_type *input)
 {
   int start,loop;
-  FILE *fp; char str[20];  int bot_pid; char responce[200];
+  FILE *fp;
+  FILE *botfp;
+  char str[20];
+  int bot_pid;
   int log_posn;
   int received_signal();
+  char botpid[STR_LEN];
+  int fgetcret;
+  static int firsttime=1;
+
+  /* set a clean return value */
+  fgetcret=0;
+
+  /* build our future filenames */
+  snprintf(botpid,STR_LEN,"%s/.meme_bot.pid",home);
+
+  if (!(fp=fopen(chatlog,"a"))) {
+	fprintf(stderr,"fopen('%s')",chatlog);
+	perror("");
+	exit(1);
+  }
  
   /* firstly, send reply out to both input[] and stdout */
   /* add_to_input(net,input,reply); */
-   start=input->size+1;
+  start=input->size+1;
   
   /* sanity check... are we in read mode? */
-  if (net->mode==1) return(0);
+  if (net->mode==1) return;
   /* Deal with when the computer has nothing to say... */
-  if (net->spoken<=1)
-  {
-     printf("hmmmm");
-     fp=fopen("/tmp/chatlog","a"); fprintf(fp,"hmmmm"); fclose(fp);
-  }        
-  /* add return char to end of chat log... */
-  fp=fopen("/tmp/chatlog","a"); fputc(10,fp); fclose(fp);
-  printf("\n---------------------------------------------\n");
+  if (!firsttime) {
+    /* but only after being spoken to */
+    if (net->spoken<=1)
+      {
+	printf("hmmmm");
+	fprintf(fp,"hmmmm");
+      }        
+    /* add return char to end of chat log... */
+    fputc('\n',fp);
+    printf("\n");
+  }
+  firsttime=0;
+  fprintf(stderr,"---------------------------------------------\n");
   
   /* firstly, if we're talking to a bot, tell it that it has input and wait for
      human responce */
-  if (fp=fopen("/tmp/bot_pid","r"))  
-  {    
-     fgets(str,10,fp); fclose(fp); /* read in the bot pid */
+  if ((botfp=fopen(botpid,"r"))) {    
+     fgets(str,10,botfp); fclose(botfp); /* read in the bot pid */
      
      bot_pid=atoi(str);
-     printf("BOT PID IS %i\n",bot_pid);
+     fprintf(stderr,"BOT PID IS %i\n",bot_pid);
      signal(42,(void *)received_signal);
      /* Firstly, save off the current chatlog size.. */
-     fp=fopen("/tmp/chatlog","r"); fseek(fp,0,2);
-     log_posn=ftell(fp); close(fp);
-     printf("OLD LOG_POSN=%i\n",log_posn);
-     printf("SENDING BOT a 42...\n");
+     if (fseek(fp,0,SEEK_END)) {
+	fprintf(stderr,"fseek('%s')",chatlog);
+	perror("");
+	exit(1);
+     }
+     log_posn=ftell(fp);
+     fprintf(stderr,"OLD LOG_POSN=%i\n",log_posn);
+     fprintf(stderr,"SENDING BOT a 42...\n");
      kill(bot_pid,42);  /*tell the bot to seek a reply */
      pause();  /* wait for the reply, then read out the reply*/
  
-     fp=fopen("/tmp/chatlog","r"); fseek(fp,log_posn,0);
+     if (fseek(fp,log_posn,SEEK_SET)) {
+	fprintf(stderr,"fseek('%s')",chatlog);
+	perror("");
+        exit(1);
+     }
      do
      {
        input->size++;
        input->data[input->size]=fgetc(fp);
-       printf("%c",input->data[input->size]);
+       fprintf(stderr,"%c",input->data[input->size]);
        if (responce_node(input->data[input->size-1]) &&
           input->data[input->size]==32)
 	  input->size--;
      } while (input->data[input->size]!=10);
-     fclose(fp); printf("\n");
+     fprintf(stderr,"\n");
   }
   else /* read input from STDIN... */
   {
     do
     {
       input->size++;
-      input->data[input->size]=fgetc(stdin);
-      if (responce_node(input->data[input->size-1]) &&
-          input->data[input->size]==32)
-	  input->size--;
-    } while (input->data[input->size]!=10); 
+      fgetcret=fgetc(stdin);
+      if (fgetcret!=EOF) {
+      	input->data[input->size]=(char)fgetcret;
+      	if (responce_node(input->data[input->size-1]) &&
+            input->data[input->size]==32) input->size--;
+      }
+    } while (fgetcret!=EOF && input->data[input->size]!='\n'); 
   }
-  input->size--;
-  if (!responce_node(input->data[input->size]))
-  {
-    input->size++;
-    input->data[input->size]=(int)'.';
-    input->data[input->size]=input->data[input->size];
+
+  /* close chat log */
+  fclose(fp);
+
+  if (fgetcret!=EOF) {
+     input->size--;
+     if (!responce_node(input->data[input->size]))
+     {
+       input->size++;
+       input->data[input->size]=(int)'.';
+       input->data[input->size]=input->data[input->size];
+     }
   }
 
   /* ugly, but who cares, it works */
-  if (input->data[input->size-3]=='b' &&
-      input->data[input->size-2]=='y' &&
-      input->data[input->size-1]=='e' )
+  if (fgetcret==EOF || (input->size>2 && 
+	 input->data[input->size-3]=='b' &&
+         input->data[input->size-2]=='y' &&
+         input->data[input->size-1]=='e' ))
   {
 #ifdef DOS
      save(net);
 #else
-     msync((void *)net,sizeof(net_type), MS_SYNC);
+     if (msync((void *)net,sizeof(net_type), MS_SYNC)) {
+	perror("msync('net')");
+	exit(1);
+     }
 #endif
-     printf("bye.\n");
+     fprintf(stderr,"bye.\n");
      exit(0);
   }
   if (net->trace>=1)
   {
-    printf("NEW INPUT='");
-    for (loop=start; loop<=input->size; loop++) printf("%c",input->data[loop]);
-    printf("'\n");
+    fprintf(stderr,"NEW INPUT='");
+    for (loop=start; loop<=input->size; loop++) {
+      fprintf(stderr,"%c",input->data[loop]);
+    }
+    fprintf(stderr,"'\n");
   }
   /* need to multiply the input by TOP for consistency now.... */
   for (loop=start; loop<=input->size; loop++)
      input->data[loop]=input->data[loop]*TOP;
-  printf(">>");  /* prepare for the responce */
+  fprintf(stderr,">>");  /* prepare for the responce */
+  fflush(NULL);
 }
 
 /****************************************************************************
  * received_signal() - do nothing 
  */
- 
-received_signal()
+void received_signal(int sig)
 {
 }
 
@@ -249,8 +305,8 @@
  * recurse.
  */
  
-int analyse_match(net_type *net, int metanode,
-                  nodelist_type *input, int result_posn)
+void analyse_match(net_type *net, int metanode,
+		   nodelist_type *input, int result_posn)
 {
    int level_total;
 
@@ -283,13 +339,12 @@
  *
  */
  
-find_match(net_type *net, int node, nodelist_type *input, int posn)
+void find_match(net_type *net, int node, nodelist_type *input, int posn)
 {
-  int metanode,lhs,rhs;
+  int metanode,rhs;
   int result_posn;
-  int bot,mid,top, result, safe_mid;
+  int bot,mid,top, safe_mid;
   int explore;
-  int tracer;
   
   /* printf("*** in find_match, node='"); print_node(net,node); printf("'\n");*/
   
@@ -315,7 +370,7 @@
       /* printf("FOUND MATCH!!!\n"); */
       analyse_match(net, metanode, input, result_posn);
       /* pruning? */
-      if (net->bestlevel>=get_level(net,node)+PRUNE)  return(0);
+      if (net->bestlevel>=get_level(net,node)+PRUNE)  return;
     }
     else if (net->result<0) bot=mid+1;
     else if (net->result>0) top=mid-1;
@@ -341,7 +396,7 @@
         /* printf("FOUND MATCH!!!\n"); */
         analyse_match(net,metanode,input, result_posn);
         /* pruning? */
-        if (net->bestlevel>=get_level(net,node)+PRUNE) return(0);
+        if (net->bestlevel>=get_level(net,node)+PRUNE) return;
       }
       else explore=FALSE;
       if (mid==bot) explore=FALSE;
@@ -364,7 +419,7 @@
         /* printf("FOUND MATCH!!!\n"); */
         analyse_match(net,metanode, input, result_posn);
         /* pruning? */
-        if (net->bestlevel>=get_level(net,node)+PRUNE) return(0);
+        if (net->bestlevel>=get_level(net,node)+PRUNE) return;
       }
       else explore=FALSE;
       if (mid==top) explore=FALSE;
@@ -387,24 +442,32 @@
 }
 
 /**************************************************************************
- * listen() - take in an ascii stream and feed it to the meme machine.
+ * netlisten() - take in an ascii stream and feed it to the meme machine.
  * mode=1 (listen mode) or 2 (chat mode).
  */
- 
-listen(net_type *net, nodelist_type  *input, nodelist_type *output, int tweak)
+void netlisten(net_type *net, nodelist_type  *input, nodelist_type *output,
+	       int tweak)
 {
- 
-  int first, previous, posn;
+  int previous, posn;
   int finished;
-  int loop;
+  /*
+  int first, loop;
   clock_t time,btime, match_time, build_time, dream_time;
   struct tms tbuf;
   int tweak_finished;
+  */
   FILE *fp_out;
   tweaks_type *tweaks;
+  char filename[STR_LEN];
   
   tweaks=(tweaks_type *)malloc(sizeof(tweaks_type));
-  finished=FALSE; fp_out=fopen("output.txt", "w");
+  finished=FALSE;
+  snprintf(filename,STR_LEN,"%s/.meme_output.txt",home);
+  if (!(fp_out=fopen(filename, "w"))) {
+	fprintf(stderr,"fopen('%s')",filename);
+	perror("");
+	exit(1);
+  }
   previous=0; posn=1; net->spoken=0;
   
   /* right, main loop. Chunk the entire input stream. */
@@ -412,7 +475,7 @@
   {
     if (net->trace>=2 && net->mode==1)
       
-      printf("-----------------------------------------------------------\n");
+    printf("-----------------------------------------------------------\n");
     if (posn>input->size && net->mode>1)
     {
       need_input(net,input);
@@ -476,7 +539,7 @@
  * main()
  *  bring net and input text into memory, results of parsing to stdout.
  */
- 
+
 int main(int argc, char *argv[])
 {
   net_type *net;
@@ -484,20 +547,50 @@
   nodelist_type *input, *output;
   int tweak;  /*whether to use tweaking or non-tweaking method for learning*/
   int arg;  /* for parsing program arguments*/
-  char filename[100]="input.txt";
+  char filename[STR_LEN]="input.txt";
   FILE *fp_pid;
-  int cycles,loop;
+  int cycles,loop,fd;
+  char pidfile[STR_LEN];
+  char err_str[STR_LEN];
   
- 
-  printf("\n---------------------------------------------------------------\n");
-  printf("Meme Machine - by Brian Smith (brian@wintermute.demon.co.uk)\n"); 
+  
+  fprintf(stderr,
+	  "\n------------------------------------------------------------\n");
+  fprintf(stderr,
+	  "Meme Machine - by Brian Smith (brian@wintermute.demon.co.uk)\n"); 
+  fprintf(stderr,
+	  "               Additonal code by Kees Cook <cook@cpoint.net>\n");
+
+  /* start up some globals */
+#ifdef DOS
+  chatlog=strdup("/tmp/chatlog");
+#else
+  home=getenv("HOME");
+  snprintf(pidfile,STR_LEN,"%s/.meme_chat.log",home);
+  chatlog=strdup(pidfile);
+#endif
+  if (!chatlog) {
+	perror("strdup");
+	exit(1);
+  }
 
   /* firstly, write the process PID out... */
 #ifdef DOS
 #else
+  snprintf(pidfile,STR_LEN,"%s/.meme_pid",home);
 
-  creat("/tmp/meme_pid",0644);
-  fp_pid=fopen("/tmp/meme_pid","w"); fprintf(fp_pid,"%i\n",getpid());
+  if ((fd=open(pidfile,O_RDWR|O_CREAT|O_TRUNC,
+	       S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH))<0) {
+	snprintf(err_str,STR_LEN,"open('%s')",pidfile);
+	perror(err_str);
+	exit(1);
+  }
+  if (!(fp_pid=fdopen(fd,"w"))) {
+	snprintf(err_str,STR_LEN,"fdopen(%d)",fd);
+	perror(err_str);
+	exit(1);
+  }
+  fprintf(fp_pid,"%i\n",getpid());
   fclose(fp_pid); 
 #endif
 
@@ -514,8 +607,15 @@
    net=(net_type *)malloc(sizeof(net_type));
    load(net);
 #else
- fp=open("net",2);
- net=(net_type *)mmap(0,sizeof(net_type),PROT_READ+PROT_WRITE,MAP_SHARED,fp,0);
+ if ((fp=open("net",O_RDWR))<0) {
+	perror("open('net')");
+	exit(1);
+ }
+ net=(net_type *)mmap(0,sizeof(net_type),PROT_READ|PROT_WRITE,MAP_SHARED,fp,0);
+ if (!net) {
+	perror("mmap('net')");
+	exit(1);
+ }
 #endif
 
   /* sanity check... */
@@ -561,9 +661,10 @@
        printf("-defrag - defragment the 'net' file (can take a long time)\n");
        printf("-stats - show some statistics on the net file\n");
     }
-    else if (argv[arg][0]!='-') strcpy(filename, argv[arg]); /* got filename*/
+    else if (argv[arg][0]!='-') strncpy(filename, argv[arg],STR_LEN); /* got filename*/
     else { printf("unknown argument: '%s'\n",argv[arg]); exit(0); }
   }
+
   if (net->mode==42) { defrag(net); return(0);}
   if (net->mode==1 || net->mode==69)    /* we're in training mode.. */
   {
@@ -571,22 +672,27 @@
     for(loop=1; loop<=cycles; loop++)
     {
       printf("-----------------------\nTRAINING CYCLE %i\n", loop);
-      listen(net,input,output,tweak);
+      netlisten(net,input,output,tweak);
     }
     printf("after training, nodespace=%i, dataspace=%i, free=%i \n",
           net->nodespace, MAX_DATA_SIZE-net->ptr, net->ptr-net->nodespace);
   }
   else /* We're in 'chat' mode... */
   {
-     input->data=(int *)malloc(100000); output->data=(int *)malloc(100000);
-     listen(net,input,output,tweak);
+     input->data=(int *)malloc(100000);
+     output->data=(int *)malloc(100000);
+     netlisten(net,input,output,tweak);
   }
 #ifdef DOS
  printf("writing back to disk...\n");
  save(net);
  printf("finished.\n");
 #else
-  msync((void *)net,sizeof(net_type), MS_ASYNC);  /* write changed data back */
+  /* write changed data back */
+  if (msync((void *)net,sizeof(net_type), MS_SYNC)) {
+	perror("msync('net')");
+	exit(1);
+  }
   printf("finished.\n");
   close(fp); 
 #endif
diff -uNr meme-old/makefile meme/makefile
--- meme-old/makefile	Tue Nov 16 13:47:33 1999
+++ meme/makefile	Fri Dec  3 18:17:44 1999
@@ -1,12 +1,24 @@
-OBJS = bottom.o low.o mid.o high.o \
+OBJS  =bottom.o low.o mid.o high.o \
        init.o tweak.o main.o
 
-CC = gcc 
-CFLAGS	= -I/usr/5include -L/usr/5lib -O 
-LDFLAGS = 
+CC=gcc 
+#CFLAGS=-I/usr/5include -L/usr/5lib -O 
+CFLAGS=-O -Wall
+LDFLAGS= 
 
 all:    $(OBJS)
-	$(CC) -o meme $(OBJS) $(CFLAGS) $(LDFLAGS)  
+	$(CC) -o meme $(OBJS) $(LDFLAGS)  
 
 clean: 
-	/usr/bin/rm *.o *~
+	rm -f *.o *~
+
+.c.o:	$<
+	$(CC) $(CFLAGS) -c $<
+
+bottom.o: datastru.h
+low.o: datastru.h
+mid.o: datastru.h
+high.o: datastru.h
+init.o: datastru.h
+tweak.o: datastru.h
+main.o: datastru.h
\ No newline at end of file
diff -uNr meme-old/mid.c meme/mid.c
--- meme-old/mid.c	Sun Nov 21 12:17:38 1999
+++ meme/mid.c	Fri Dec  3 18:17:37 1999
@@ -23,7 +23,6 @@
 int new_node(net_type *net)
 {
    int node,loop;
-   int marker;
    
    /* get the next node number from the deleted_node stack if possible */
    if (net->deleted_node!=0)
@@ -55,7 +54,7 @@
  * create a new node with inputs lhs and rhs (left-hand and right-hand-side)
  * returns the node created. Returns 0 if the node already exists.
  * 
- * called from listen()
+ * called from netlisten()
  */
 
 int make_node(net_type *net, int lhs, int rhs)
@@ -87,8 +86,8 @@
   {lhs_level++; net->data[node]+=lhs_level*LEVEL_DIV; }
   else
   {rhs_level++; net->data[node]+=rhs_level*LEVEL_DIV; }
-  /* another tracer...\n");
-  if (get_level(net,node)==0) {printf("WOA! levels screwed!\n"); exit(0); }
+  /* another tracer...\n"); 
+     if (get_level(net,node)==0) {printf("WOA! levels screwed!\n"); exit(0); } */
   /* Now see if new node is a responce node or not */
   if (net->data[lhs]>=HIGH_BIT)
           net->data[node]+=HIGH_BIT;
@@ -112,7 +111,7 @@
  * Note - a deleted node is marked by having empty input and output lists
  */
  
-int delete_node(net_type *net,int node)
+void delete_node(net_type *net,int node)
 {
 
   int lhs, rhs;
@@ -132,7 +131,7 @@
      exit(0);
   }
   /* Firstly, if the node is already deleted, forget it. */
-  if (lhs==0 ) return(0);
+  if (lhs==0 ) return;
   
   /* Just need to delete inputs, then add node to the deleted_node stack*/
   
diff -uNr meme-old/tweak.c meme/tweak.c
--- meme-old/tweak.c	Sun Nov 21 12:17:38 1999
+++ meme/tweak.c	Fri Dec  3 18:17:37 1999
@@ -19,6 +19,7 @@
  */
 
 #include <stdio.h>
+#include <string.h>
 #include "datastru.h"
 
 /**************************************************************************
@@ -29,9 +30,9 @@
  */
  
 int find_multi_meta_match(net_type *net, int node,
-                    nodelist_type *input, int posn, int meta_rhs)
+			  nodelist_type *input, int posn, int meta_rhs)
 {
-  int metanode,lhs,rhs;
+  int metanode,rhs;
   int result_posn;
   int bot,mid,top, safe_mid, explore;
   int meta_posn;
@@ -160,7 +161,8 @@
      else
      {
        net->wmsize++; net->wm[net->wmsize]=node;
-       get_meta(net,input,posn,meta_rhs);
+       /* should there be a "RETURN" here? I just added it in. -kees */
+       return get_meta(net,input,posn,meta_rhs);
      }
   }
   else return(FALSE);
@@ -263,7 +265,7 @@
          return(posn+1);
      else /*forget it!!!*/
      {
-       net->tweaked==0;
+       net->tweaked=0; /* this used to be ==0 -kees */
        return(0);
      }
   } 
@@ -419,10 +421,10 @@
  * recurse.
  */
  
-int analyse_tweak_match(net_type *net,int metanode,
-                        nodelist_type *input,int result_posn)
+void analyse_tweak_match(net_type *net,int metanode,
+			 nodelist_type *input,int result_posn)
 {
-  int level;
+  int level=0;
   
   if (net->tweaked!=2)
   /* printf("@@@@ got match '"); print_node(net,metanode); printf("'\n"); */
@@ -455,7 +457,7 @@
     net->best_tweakposn=net->tweak_posn;
     net->best_tweakb4=net->tweak_b4;
     net->best_wmsize=net->wmsize;
-    bcopy(net->wm,net->best_wm,(sizeof(int)*ABS_MAX_WMSIZE));
+    memcpy(net->best_wm,net->wm,(sizeof(int)*ABS_MAX_WMSIZE));
     if (net->mode==2 && net->data[metanode]>=HIGH_BIT) net->mode=3;
   }
   find_tweak_match(net,metanode,input,result_posn);
@@ -468,13 +470,12 @@
  * for exact matches.
  */
   
-find_exact_match(net_type *net, int node,nodelist_type *input,int posn)
+void find_exact_match(net_type *net, int node,nodelist_type *input,int posn)
 {
-  int metanode,lhs,rhs;
+  int metanode,rhs;
   int result_posn;
-  int bot,mid,top, result, safe_mid;
+  int bot,mid,top, safe_mid;
   int explore;
-  int tracer;
   int outputs,total;
  
   if (net->trace>=4) 
@@ -485,7 +486,7 @@
   outputs=net->data[node+LHS_OP];
   total=net->data[node+NUM_LHS_OP];
  /* printf("find_exact_match - outputs=%i\n",total); */
-  if (total==0) return(FALSE); 
+  if (total==0) return; 
   /* set up pointers to start/end of the list of outputs of our LHS node*/
   bot=outputs;
   top=bot+total-1;
@@ -506,7 +507,7 @@
       /* printf("FOUND MATCH!!!\n"); */
       analyse_tweak_match(net,metanode,input, result_posn);
       /* pruning? */
-      if (net->bestlevel>=(get_level(net,node)+PRUNE)) return(0);
+      if (net->bestlevel>=(get_level(net,node)+PRUNE)) return;
     }
     else if (net->result<0) bot=mid+1;
     else if (net->result>0) top=mid-1;
@@ -532,7 +533,7 @@
         /* printf("FOUND MATCH!!!\n"); */
         analyse_tweak_match(net,metanode,input,result_posn);
         /* pruning? */
-        if (net->bestlevel>=get_level(net,node)+PRUNE) return(0);
+        if (net->bestlevel>=get_level(net,node)+PRUNE) return;
       }
       else explore=FALSE;
       if (mid==bot) explore=FALSE;
@@ -555,7 +556,7 @@
         /* printf("FOUND MATCH!!!\n"); */
         analyse_tweak_match(net,metanode, input, result_posn);
         /* pruning? */
-        if (net->bestlevel>=get_level(net,node)+PRUNE) return(0);
+        if (net->bestlevel>=get_level(net,node)+PRUNE) return;
       }
       else explore=FALSE;
       if (mid==top) explore=FALSE;
@@ -573,7 +574,7 @@
  * Basic assumption- the rhs is ALWAYS a base node in all calls to find_match()
  */
 
-find_tweak_match(net_type *net, int node, nodelist_type *input, int posn)
+void find_tweak_match(net_type *net, int node, nodelist_type *input, int posn)
 {
   int metanode,lhs,rhs;
   int result_posn;
@@ -674,8 +675,8 @@
  * wrapper for find_tweak_match() which allows for MULTIPLE tweaks to happen.
  */
 
-multi_tweak_match(net_type *net, int node, nodelist_type *input, int posn,
-		  tweaks_type *tweaks)
+void multi_tweak_match(net_type *net, int node, nodelist_type *input, int posn,
+		       tweaks_type *tweaks)
 {
   int finished;
   int total;
