*** /home/scott/bbrun-1.1/bbrun/bbrun.c	Tue Dec 12 14:05:15 2000
--- ./bbrun.c	Sat Dec 22 23:56:35 2001
***************
*** 211,220 ****
            strncpy(history[n], ptr, 256);
            n++;
  	}
  
! 	if (n == MAXHISTLEN) 
  	{
    	  maxed = 1;
  	}
      }
      fclose(fp);
--- 211,230 ----
            strncpy(history[n], ptr, 256);
            n++;
  	}
+ 	
+ 	// scott@furt.com
+ 	// This is a NULL line, which should NEVER happen.
+ 	// Stop any further processing, becuase chances are very good
+ 	// that the rest of the file is corrupt too.
+ 	else {
+ 		break;
+ 	}
  
! 	// Dont process more than MAXHISTLEN lines
! 	if (n >= MAXHISTLEN) 
  	{
    	  maxed = 1;
+ 	  break;
  	}
      }
      fclose(fp);
***************
*** 227,251 ****
  
  void writeHistory(char *historyItem)
  {
      int dup = 0;
!     int c = 0;
!     char buf[256];
      char *ptr;
      FILE *fp;
  
!    fp = fopen(filename, "r");
  
!     while( fgets(buf, 256, fp) != 0)
      {
  	ptr = strtok(buf, "\n");
!         if (strcmp(historyItem, ptr) == 0)
!  	{
!   	  dup = 1;
!   	}
      }
      fclose(fp);
  
  
      // Remove first (oldest) item and append the most recent one.
      if (maxed == 1 && dup == 0) 
      {
--- 237,338 ----
  
  void writeHistory(char *historyItem)
  {
+     int MAXCHARS = 256;
      int dup = 0;
!     // c is not used in writeHistory anymore.
!     //int c = 0;
!     char buf[MAXCHARS];
      char *ptr;
      FILE *fp;
+     // scott@furt.com
+     // hist is an array of MAXHISTLEN lines
+     char hist[MAXHISTLEN][MAXCHARS];
+     // for reading file in
+     int i = 0;
+     // for writing file back
+     int n = 0;
  
!     fp = fopen(filename, "r");
  
!     while( fgets(buf, MAXCHARS, fp) != 0)
      {
  	ptr = strtok(buf, "\n");
! 	// scott@furt.com
! 	// Check for NULL ptr, to help fix a segfault bug when a blank
! 	// line got into ~/.bbrun_history and made ptr==NULL
! 	if (ptr != NULL) 
! 	{
! 		// this is a duplicate command
!         	if (strcmp(historyItem, ptr) == 0)
!  		{
!   		  dup = 1;
!   		}
! 		// not duplicate, store it in the hist[] array
! 		else {
! 		  strncpy(hist[i++], ptr, MAXCHARS);
! 		}
! 
! 		// dont read more than MAXHISTLEN lines.
! 		if (i == MAXHISTLEN) {
! 			break;
! 		}
! 	}
! 	else {
! 		break;
! 	}
      }
      fclose(fp);
+    
+     // scott@furt.com
+     //
+     // Here's where i reworked history file writing to be
+     // (IMO) less prone to corruption and errors and baddies.
+     // 
+     // History file has most recent commands last in the file,
+     // and most ancient commands first.  Duplicate commands are
+     // moved to be first in the file.  This is exactly the same
+     // functionality that the original authour intended.
+  
+     // Open up the history file for writing
+     // "w" mode will blank the original, which is intentional,
+     // i want to write a fresh history file each time, rather
+     // than edit the current one.
+     if ((fp = fopen(filename, "w")) == 0) 
+     {
+ 	    printf("Cannot open %s for writing!", filename);
+ 	    exit(0);
+     }
  
+     // If this is a duplicate command, write it at the beginning of the file.
+     if (dup == 1) 
+     {
+ 	    fprintf(fp, "%s\n", historyItem);
+     }
  
+     // Dont write the most ancient command if history file is maxed
+     // and there is are duplicates.  (so that the history file never
+     // exceeds MAXHISTLEN lines)
+     if (!dup && maxed) {
+ 	n = 1;
+     }
+     
+     // Write the history file.
+     for (; n < i; n++) 
+     {
+ 	    fprintf(fp, "%s\n", hist[n]);
+     }
+ 
+     // Now, if it's not a duplicate, write the current command
+     // at the end of the file.
+     if (dup == 0) 
+     {
+         fprintf(fp, "%s\n", historyItem);
+     }
+     
+     fclose(fp);
+     
+     // Below is the original writeHistory() code:    
+     /* 
      // Remove first (oldest) item and append the most recent one.
      if (maxed == 1 && dup == 0) 
      {
***************
*** 279,285 ****
        fprintf(fp,"%s\n", historyItem);
        fclose(fp);
      }
! 
  }
  
  
--- 366,373 ----
        fprintf(fp,"%s\n", historyItem);
        fclose(fp);
      }
!   */
!     
  }
  
  
