Index: /trunk/Ohana/src/addstar/src/gimages.c
===================================================================
--- /trunk/Ohana/src/addstar/src/gimages.c	(revision 3648)
+++ /trunk/Ohana/src/addstar/src/gimages.c	(revision 3649)
@@ -17,5 +17,7 @@
   
   /* if image.db is opened successfully, f will not be NULL.
-     if image.db is empty, dbstate will be LCK_EMPTY */
+     if image.db is empty, dbstate will be LCK_EMPTY 
+     if image.db is not locked & opened successfully, we will not reach here!
+  */
   f = GetDB (&dbstate);
   if (dbstate == LCK_EMPTY) {
Index: /trunk/Ohana/src/addstar/src/image-db.c
===================================================================
--- /trunk/Ohana/src/addstar/src/image-db.c	(revision 3648)
+++ /trunk/Ohana/src/addstar/src/image-db.c	(revision 3649)
@@ -36,5 +36,5 @@
   check_permissions (ImageCat);
   f = fsetlockfile (ImageCat, 3600.0, LOCK, &dbstate);
-  if ((f == (FILE *) NULL) && (dbstate != LCK_EMPTY)) {
+  if (f == NULL) {
     fprintf (stderr, "ERROR: can't lock image catalog\n");
     exit (1);
Index: /trunk/Ohana/src/imregister/Makefile
===================================================================
--- /trunk/Ohana/src/imregister/Makefile	(revision 3648)
+++ /trunk/Ohana/src/imregister/Makefile	(revision 3649)
@@ -21,5 +21,4 @@
 INCS	= 	-I$(INC) -I$(LINC) -I$(XINC)
 LIBS	= 	-L$(LLIB) -lohana -lFITS -lm 
-# CFLAGS	=	$(INCS) -Wall -Werror
 CFLAGS	=	$(INCS)
 LFLAGS	=	$(LIBS)
@@ -160,4 +159,8 @@
 	rm -f $(BIN)/$*.$(ARCH)
 
+%.rebuild :
+	rm -f $(BIN)/$*.$(ARCH)
+	make $(DESTBIN)/$*
+
 %.install:
 	make $(DESTBIN)/$*
Index: /trunk/Ohana/src/imregister/detrend/entry.c
===================================================================
--- /trunk/Ohana/src/imregister/detrend/entry.c	(revision 3648)
+++ /trunk/Ohana/src/imregister/detrend/entry.c	(revision 3649)
@@ -77,4 +77,5 @@
     sprintf (fullname, "%s/%s", dBPath, filename);
     f = fsetlockfile (fullname, 2.0, LCK_XCLD, &filestate);
+    /* if there is an error, it may just mean file is locked. */
     if (filestate == LCK_EMPTY) {
 	found = TRUE;
Index: /trunk/Ohana/src/imregister/detrend/output.c
===================================================================
--- /trunk/Ohana/src/imregister/detrend/output.c	(revision 3648)
+++ /trunk/Ohana/src/imregister/detrend/output.c	(revision 3649)
@@ -15,5 +15,4 @@
   PrintSubset (image, match, Nmatch);
   if (output.verbose) fprintf (stderr, "SUCCESS\n");
-
   exit (0);
 }
Index: /trunk/Ohana/src/imregister/src/detregister.c
===================================================================
--- /trunk/Ohana/src/imregister/src/detregister.c	(revision 3648)
+++ /trunk/Ohana/src/imregister/src/detregister.c	(revision 3649)
@@ -1,5 +1,5 @@
 # include "imregister.h"
 # include "detrend.h"
-static char *version = "detregister $Revision: 3.2 $";
+static char *version = "detregister $Revision: 3.3 $";
 
 int main (int argc, char **argv) {
@@ -24,4 +24,5 @@
   dBFile = set_dBFile ();
   if (!fits_db_lock (&db, dBFile)) {
+    fprintf (stderr, "ERROR: failure to lock db\n");
     fits_db_close (&db);
     exit (1);
@@ -32,4 +33,5 @@
   } else {  
     if (!fits_db_load (&db)) {
+      fprintf (stderr, "ERROR: failure to load db\n");
       fits_db_close (&db);
       exit (1);
@@ -37,5 +39,4 @@
   }
 
-  /** we may later want to pull this out and put it elsewhere **/
   fits_convert_DetReg (&newdata, sizeof (DetReg), 1);
   fits_table_to_vtable (&db.ftable, &vtable, 0, 0);
Index: /trunk/Ohana/src/imregister/src/detsearch.c
===================================================================
--- /trunk/Ohana/src/imregister/src/detsearch.c	(revision 3648)
+++ /trunk/Ohana/src/imregister/src/detsearch.c	(revision 3649)
@@ -1,5 +1,5 @@
 # include "imregister.h"
 # include "detrend.h"
-static char *version = "detsearch $Revision: 3.5 $";
+static char *version = "detsearch $Revision: 3.6 $";
 
 int main (int argc, char **argv) {
@@ -23,4 +23,5 @@
   dBFile = set_dBFile ();
   if (!fits_db_lock (&db, dBFile)) {
+    fprintf (stderr, "ERROR: failure to lock db\n");
     fits_db_close (&db);
     exit (1);
@@ -28,4 +29,5 @@
   
   if (!fits_db_load (&db)) {
+    fprintf (stderr, "ERROR: failure to load db\n");
     fits_db_close (&db);
     exit (1);
Index: /trunk/Ohana/src/imregister/src/imregister.c
===================================================================
--- /trunk/Ohana/src/imregister/src/imregister.c	(revision 3648)
+++ /trunk/Ohana/src/imregister/src/imregister.c	(revision 3649)
@@ -1,5 +1,5 @@
 # include "imregister.h"
 # include "imreg.h"
-static char *version = "imregister $Revision: 3.5 $";
+static char *version = "imregister $Revision: 3.6 $";
 
 int main (int argc, char **argv) {
@@ -23,4 +23,5 @@
 
   if (!fits_db_lock (&db, ImageDB)) {
+    fprintf (stderr, "ERROR: failure to lock db\n");
     fits_db_close (&db);
     exit (1);
@@ -31,4 +32,5 @@
   } else {  
     if (!fits_db_load (&db)) {
+      fprintf (stderr, "ERROR: failure to load db\n");
       fits_db_close (&db);
       exit (1);
Index: /trunk/Ohana/src/imregister/src/imregtable.c
===================================================================
--- /trunk/Ohana/src/imregister/src/imregtable.c	(revision 3648)
+++ /trunk/Ohana/src/imregister/src/imregtable.c	(revision 3649)
@@ -1,5 +1,5 @@
 # include "imregister.h"
 # include "imreg.h"
-static char *version = "imregtable $Revision: 3.5 $";
+static char *version = "imregtable $Revision: 3.6 $";
 
 int main (int argc, char **argv) {
@@ -42,4 +42,5 @@
 
   if (!fits_db_lock (&db, ImageDB)) {
+    fprintf (stderr, "ERROR: failure to lock db\n");
     fits_db_close (&db);
     exit (1);
@@ -50,4 +51,5 @@
   } else {  
     if (!fits_db_load (&db)) {
+      fprintf (stderr, "ERROR: failure to load db\n");
       fits_db_close (&db);
       exit (1);
Index: /trunk/Ohana/src/imregister/src/imsearch.c
===================================================================
--- /trunk/Ohana/src/imregister/src/imsearch.c	(revision 3648)
+++ /trunk/Ohana/src/imregister/src/imsearch.c	(revision 3649)
@@ -1,5 +1,5 @@
 # include "imregister.h"
 # include "imreg.h"
-static char *version = "imsearch $Revision: 3.4 $";
+static char *version = "imsearch $Revision: 3.5 $";
 
 int main (int argc, char **argv) {
@@ -17,8 +17,10 @@
 
   if (!fits_db_lock (&db, ImageDB)) {
+    fprintf (stderr, "ERROR: failure to lock db\n");
     fits_db_close (&db);
     exit (1);
   }
   if (!fits_db_load (&db)) {
+    fprintf (stderr, "ERROR: failure to load db\n");
     fits_db_close (&db);
     exit (1);
@@ -36,5 +38,4 @@
 
   OutputSubset (image, Nimage, match, Nmatch);
-
   exit (0);
 }
Index: /trunk/Ohana/src/imregister/src/imstatreg.c
===================================================================
--- /trunk/Ohana/src/imregister/src/imstatreg.c	(revision 3648)
+++ /trunk/Ohana/src/imregister/src/imstatreg.c	(revision 3649)
@@ -1,5 +1,5 @@
 # include "imregister.h"
 # include "imreg.h"
-static char *version = "imstatreg $Revision: 3.11 $";
+static char *version = "imstatreg $Revision: 3.12 $";
 
 int main (int argc, char **argv) {
@@ -58,14 +58,16 @@
     if (temp_db.dbstate == LCK_EMPTY) {
       fprintf (stderr, "temporary database empty\n");
+      fits_db_close (&temp_db);
       goto next;
     } 
     if (!fits_db_load (&temp_db)) {
       fprintf (stderr, "error reading temp db\n");
-      goto next;
+      exit (1);
     }
-    image = fits_table_get_RegImage (&temp_db.ftable, &Nimage);
+    subset = fits_table_get_RegImage (&temp_db.ftable, &Nsubset);
     fprintf (stderr, "temporary database read\n");
 
-    /* delete and unlock temporary database */
+    /* DELETE and unlock temporary database */
+    truncate or delete_db; /****** *****/
     fits_db_close (&temp_db);
     fprintf (stderr, "temporary database closed\n");
@@ -80,49 +82,31 @@
       fprintf (stderr, "main database empty\n");
       fits_db_close (&image_db);
+      fits_db_free (&temp_db);
+      goto next;
+      /* this is a type of error: we read entries from the
+	 temp db, but there were no entries to match in the main db
+	 we will just drop the temp db data */
+    }
+    image = fits_table_get_RegImage (&image_db.ftable, &Nimage);
+    fprintf (stderr, "main database read\n");
 
-      /* add temp data back to temp database */
-      fprintf (stderr, "resave temporary data\n");
-      if (!fits_db_lock (&temp_db, TempDB)) {
-	fits_db_close (&temp_db);
-	fprintf (stderr, "error locking temp db (path missing? access permission?)\n");
-	exit (1);
-      }
-      if (temp_db.dbstate == LCK_EMPTY) {
-	fprintf (stderr, "temporary database empty\n");
-	goto next;
-      } 
-      if (!fits_db_load (&temp_db)) {
-	fprintf (stderr, "error reading temp db\n");
-	goto next;
-      }
-      image = fits_table_get_RegImage (&temp_db.ftable, &Nimage);
-      fprintf (stderr, "temporary database read\n");
+    /* match temp image with main images */
+    match = match_images (image, Nimage, subset, Nsubset, &Nmatch);
+    if (Nmatch != Nsubset) fprintf (stderr, "WARNING: some images missed\n");
 
-      fits_convert_Spectrum (spectrum, sizeof (Spectrum), 1);
-      fits_table_to_vtable (&db.ftable, &vtable, 0, 0);
-      fits_vadd_rows (&vtable, (char *) spectrum, 1, sizeof(Spectrum));
+    /* update entries in main db */
+    fits_vtable_from_ftable (&image_db.ftable, &vtable, match, Nmatch);
+    for (i = 0; i < Nmatch; i++) {
+      fits_convert_RegImage ((RegImage *) vtable.buffer[i], sizeof (RegImage), 1);
+    }
+    fits_db_update (&image_db, &vtable);
+    fits_db_close (&image_db);
 
-      fits_db_update (&db, &vtable);
-      fits_db_close (&db);
-      fits_db_free (&db);
-
-      print_db_status ("temporary database resaved");
-      goto next;
-    }
-
-    /************* this is kind of convoluted, need to simplify a bit ****/
-
-    /* match temp image with main images / update main DB */
-    match = match_images (image, Nentry, &Nmatch);
-    if (Nmatch != Nentry) fprintf (stderr, "WARNING: some images missed\n");
-    print_db_status ("main database read");
-    update_db (match, Nmatch);
+    fits_db_free (&image_db);
+    fits_db_free (&temp_db);
+    fits_free_vtable (&vtable);
     if (match != NULL) free (match);
     if (image != NULL) free (image);
       
-    /* clear temp DB hardlock */
-    unlink (TempDB); 
-    clearlockfile2 (TempDB, -1, LCK_HARD, &status);
-
   next:
     fflush (stderr);
Index: /trunk/Ohana/src/imregister/src/photreg.c
===================================================================
--- /trunk/Ohana/src/imregister/src/photreg.c	(revision 3648)
+++ /trunk/Ohana/src/imregister/src/photreg.c	(revision 3649)
@@ -1,5 +1,5 @@
 # include "imregister.h"
 # include "photreg.h"
-static char *version = "photreg $Revision: 1.6 $";
+static char *version = "photreg $Revision: 1.7 $";
 
 int main (int argc, char **argv) {
@@ -20,6 +20,9 @@
   if (!strcmp (output.db, "trans")) filename = TransDB;
 
-  if (!fits_db_lock (&db, filename)) exit (1);
-
+  if (!fits_db_lock (&db, filename)) {
+    fprintf (stderr, "ERROR: failure to lock db\n");
+    fits_db_close (&db);
+    exit (1);
+  }
   if (db.dbstate == LCK_EMPTY) {
     fits_db_create (&db);
@@ -31,4 +34,5 @@
   } else {  
     if (!fits_db_load (&db)) {
+      fprintf (stderr, "ERROR: failure to load db\n");
       fits_db_close (&db);
       exit (1);
Index: /trunk/Ohana/src/imregister/src/photsearch.c
===================================================================
--- /trunk/Ohana/src/imregister/src/photsearch.c	(revision 3648)
+++ /trunk/Ohana/src/imregister/src/photsearch.c	(revision 3649)
@@ -1,5 +1,5 @@
 # include "imregister.h"
 # include "photreg.h"
-static char *version = "photsearch $Revision: 1.6 $";
+static char *version = "photsearch $Revision: 1.7 $";
 
 int main (int argc, char **argv) {
@@ -25,7 +25,12 @@
   }
 
-  if (!fits_db_lock (&db, filename)) exit (1);
+  if (!fits_db_lock (&db, filename)) {
+    fprintf (stderr, "ERROR: failure to lock db\n");
+    fits_db_close (&db);
+    exit (1);
+  }
   
   if (!fits_db_load (&db)) {
+    fprintf (stderr, "ERROR: failure to load db\n");
     fits_db_close (&db);
     exit (1);
Index: /trunk/Ohana/src/imregister/src/spregister.c
===================================================================
--- /trunk/Ohana/src/imregister/src/spregister.c	(revision 3648)
+++ /trunk/Ohana/src/imregister/src/spregister.c	(revision 3649)
@@ -1,5 +1,5 @@
 # include "imregister.h"
 # include "spreg.h"
-static char *version = "spregister $Revision: 1.6 $";
+static char *version = "spregister $Revision: 1.7 $";
 
 int main (int argc, char **argv) {
@@ -23,4 +23,5 @@
 
   if (!fits_db_lock (&db, SpectrumDB)) {
+    fprintf (stderr, "ERROR: failure to lock db\n");
     fits_db_close (&db);
     exit (1);
@@ -31,4 +32,5 @@
   } else {  
     if (!fits_db_load (&db)) {
+      fprintf (stderr, "ERROR: failure to load db\n");
       fits_db_close (&db);
       exit (1);
@@ -44,4 +46,5 @@
   fits_db_free (&db);
 
+  fprintf (stderr, "SUCCESS\n");
   exit (0);
 }
Index: /trunk/Ohana/src/imregister/src/spsearch.c
===================================================================
--- /trunk/Ohana/src/imregister/src/spsearch.c	(revision 3648)
+++ /trunk/Ohana/src/imregister/src/spsearch.c	(revision 3649)
@@ -1,5 +1,5 @@
 # include "imregister.h"
 # include "spreg.h"
-static char *version = "spsearch $Revision: 1.5 $";
+static char *version = "spsearch $Revision: 1.6 $";
 
 int main (int argc, char **argv) {
@@ -17,8 +17,10 @@
 
   if (!fits_db_lock (&db, SpectrumDB)) {
+    fprintf (stderr, "ERROR: failure to lock db\n");
     fits_db_close (&db);
     exit (1);
   }
   if (!fits_db_load (&db)) {
+    fprintf (stderr, "ERROR: failure to load db\n");
     fits_db_close (&db);
     exit (1);
@@ -36,5 +38,4 @@
 
   OutputSubset (spectrum, Nspectrum, match, Nmatch);
-
   exit (0);
 }
Index: /trunk/Ohana/src/libohana/doc/glockfile.txt
===================================================================
--- /trunk/Ohana/src/libohana/doc/glockfile.txt	(revision 3648)
+++ /trunk/Ohana/src/libohana/doc/glockfile.txt	(revision 3649)
@@ -32,14 +32,24 @@
  Error Conditions:
 
- on error, fsetlockfile returns NULL and clears the lock state of the
- file
+ on error, fsetlockfile closes the file, closes the lockfile, and
+ returns NULL.  In come cases, an empty file may be left behind
+ (LCK_TIMEOUT, LCK_HARDOPEN, LCK_HARDLOCK, LCK_HARDLOCKHARD,
+ LCK_HARDLOCKCLOSE)
 
- - invalid type 
- - SOFT and file does not exist
- - file not accessible (cannot be opened)
- - lockfile not accessible
- - cannot lock lockfile (held by another user)
- - lockfile says 'BUSY' (previous holder crashed)
- - cannot unlock lockfile (file system error)
- - error writing 'BUSY' to lockfile
+ - invalid type (LCK_INVALID)
+   * no file created, no lock file created : OK
+ - SOFT and file does not exist (LCK_EMPTY)
+   * no file created, no lock file created : OK 
+ - file not accessible, cannot be opened (LCK_ACCESS)
+   * no file created, no lock file created : OK
+ - FS lock timed out (LCK_TIMEOUT)
+   * new file is created, file is open, file is unlocked, lock file is not created : OK
+ - lockfile not accessible (LCK_HARDOPEN)
+   * new file is created, file is open, file is locked, lock file is not created : OK
+ - cannot lock lockfile, held by another user (LCK_HARDLOCK)
+   * new file is created, file is open, file is locked, lock file is created : OK
+ - lockfile says 'BUSY', previous holder crashed (LCK_HARDLOCKHARD)
+   * new file is created, file is open, file is locked, lock file exists, lock file locked : OK
+ - error writing 'BUSY' to lockfile (LCK_HARDCLOSE)
+   * new file is created, file is open, file is locked, lock file exists, lock file locked
 
Index: /trunk/Ohana/src/libohana/include/ohana.h
===================================================================
--- /trunk/Ohana/src/libohana/include/ohana.h	(revision 3648)
+++ /trunk/Ohana/src/libohana/include/ohana.h	(revision 3649)
@@ -36,17 +36,23 @@
 # define DTIME(A,B) ((A.tv_sec - B.tv_sec) + 1e-6*(A.tv_usec - B.tv_usec))
 
-# define LCK_UNLOCK  0   /* file is unlocked */
-# define LCK_ACCESS  1   /* can't get access to file */
-# define LCK_TIMEOUT 2   /* timeout setting lock */
-# define LCK_HARDLCK 3   /* error setting hard lockfile */
-# define LCK_HUNLOCK 4   /* error clearing hard lockfile */
+enum {
+ LCK_UNLOCK,        /* file is unlocked */
+ LCK_INVALID,       /* invalid locktype requested */
+ LCK_MISSING,       /* soft requested and file missing (error) */
+ LCK_ACCESS,        /* can't get access to file */
+ LCK_TIMEOUT,       /* timeout setting lock */
+ LCK_HARDOPEN,      /* cannot open hard lockfile */
+ LCK_HARDLOCK,      /* cannot lock hard lockfile */
+ LCK_HARDLOCKHARD,  /* hard lockfile is locked */
+ LCK_HARDCLOSE,     /* cannot write to hardlock */
 
-# define LCK_EMPTY   5   /* locked file is empty */
-# define LCK_FULL    6   /* locked file is not empty */
-# define LCK_UNKNOWN 7   /* can't stat file to get size */
+ LCK_EMPTY,         /* file is locked and empty */
+ LCK_FULL,          /* file is locked and not empty */
+ LCK_UNKNOWN,       /* file is locked, but can't get size */
 
-# define LCK_SOFT    10  /* soft lock */
-# define LCK_HARD    11  /* hard lock */
-# define LCK_XCLD    12  /* exclusive soft lock */
+ LCK_SOFT,          /* soft lock */
+ LCK_HARD,          /* hard lock */
+ LCK_XCLD,          /* exclusive soft lock */
+};
 
 # ifndef M_PI
@@ -126,6 +132,4 @@
 FILE *fsetlockfile (char *filename, double timeout, int type, int *state);
 int   fclearlockfile (char *filename, FILE *f, int type, int *state);
-int   setlockfile2 (char *filename, double timeout, int type, int *state);
-int   clearlockfile2 (char *filename, int fd, int type, int *state);
 
 /* in config.c */
Index: /trunk/Ohana/src/libohana/src/fits_db.c
===================================================================
--- /trunk/Ohana/src/libohana/src/fits_db.c	(revision 3648)
+++ /trunk/Ohana/src/libohana/src/fits_db.c	(revision 3649)
@@ -41,5 +41,9 @@
   db[0].f = fsetlockfile (filename, db[0].timeout, db[0].lockstate, &db[0].dbstate);
   if (db[0].f == NULL) {
-    fprintf (stderr, "cannot set lock on db file %s\n", filename);
+    if (db[0].dbstate == LCK_MISSING) {
+      fprintf (stderr, "no data in db %s\n", filename);
+    } else {
+      fprintf (stderr, "cannot set lock on db file %s\n", filename);
+    }
     return (FALSE);
   }
@@ -148,11 +152,10 @@
 /* close the db files (close open file & unlock) */
 int fits_db_close (FITS_DB *db) {
-  if (db[0].dbstate == LCK_UNLOCK) return (TRUE);
+  if (db[0].f == NULL) return (TRUE);
   fclearlockfile (db[0].filename, db[0].f, db[0].lockstate, &db[0].dbstate);
-  /* fclearlockfile also performs fclose (f) */
-
-  /* for this to be atomic, need to unlink before we unlock */
-  /* unlink file here if resulting file is empty? */
-  /* if (db[0].Nrow == 0) && (lockstate != LCK_SOFT)) unlink (dBFile); */
   return (TRUE);
 }  
+
+/* for this to be atomic, need to unlink before we unlock */
+/* unlink file here if resulting file is empty? */
+/* if (db[0].Nrow == 0) && (lockstate != LCK_SOFT)) unlink (dBFile); */
Index: /trunk/Ohana/src/libohana/src/glockfile.c
===================================================================
--- /trunk/Ohana/src/libohana/src/glockfile.c	(revision 3648)
+++ /trunk/Ohana/src/libohana/src/glockfile.c	(revision 3649)
@@ -5,13 +5,13 @@
   int i, done, nbytes, status;
   char *lockname, buffer[64];
-  char *file, *path;
+  char *file, *path, mode[3];
   int fd;
-  FILE *f, flock;
+  FILE *f, *flock;
   struct stat filestat;
   struct flock filelock;
   struct timeval now, then;
 
-  /* initial values at -1, close if the are set to another value */
-  file = path = lockname = (char *) NULL;
+  f = flock = NULL;
+  file = path = lockname = NULL;
 
   /* define lock type */
@@ -24,24 +24,29 @@
   case LCK_XCLD:
     filelock.l_type   = F_WRLCK;  /* set an exclusive lock */
+    strcpy (mode, "r+");
     break;
   case LCK_SOFT:
     filelock.l_type   = F_RDLCK;  /* set a shared lock */
+    strcpy (mode, "r");
     break;
   default:
-    fprintf (stderr, "invalid lock type %d\n", type);
+    *state = LCK_INVALID;
     goto failure;
   }
 
-  /* if soft & file does not exist, return LCK_ACCESS */
-  if (type == LCK_SOFT) {
-    status = stat (filename, &filestat);
-    if ((status == -1) && (errno == ENOENT)) {
-      *state = LCK_EMPTY;
+  /* check if file exists */
+  status = stat (filename, &filestat);
+  if ((status == -1) && (errno == ENOENT)) {
+    /* if soft, return LCK_ACCESS */
+    if (type == LCK_SOFT) {
+      *state = LCK_MISSING;
       goto failure;
     } 
+    /* otherwise, we need to be able to create file */ 
+    strcpy (mode, "w+");
   }
   
   /* try to open file (create if it does not exist) */
-  f = fopen (filename, "w+");
+  f = fopen (filename, mode);
   if (f == NULL) {
     *state = LCK_ACCESS;
@@ -66,7 +71,6 @@
 got_lock:
 
+  /* check if blocking hardlock exists */
   if (type == LCK_HARD) {
-    /* we've locked filename, now check lockfile */ 
-
     /* set up name to lockfile */
     path = pathname (filename);
@@ -75,10 +79,16 @@
     sprintf (lockname, "%s/.%s.lck", path, file);
 
+    status = stat (lockname, &filestat);
+    if ((status == -1) && (errno == ENOENT)) {
+      strcpy (mode, "w+");
+    } else {
+      strcpy (mode, "r+");
+    }
+
     /* try to open lockfile */
-    flock = fopen (lockname, "w+");
+    flock = fopen (lockname, mode);
     if (flock == NULL) {
-      *state = LCK_HARDLCK;
-      goto failure;
-      /***** this one has left the file locked ****/
+      *state = LCK_HARDOPEN;
+      goto failure;
     }
     fd = fileno (flock);
@@ -88,7 +98,6 @@
     for (i = 0; (i < 20) && (fcntl (fd, F_SETLK, &filelock) == -1); i++) usleep (10000);
     if (i == 20) {
-      *state = LCK_HARDLCK + 10;
-      goto failure;
-      /***** this one has left the file locked ****/
+      *state = LCK_HARDLOCK;
+      goto failure;
     }
     
@@ -98,29 +107,31 @@
       buffer[4] = 0;
       if (!strcmp (buffer, "BUSY")) { 
-	*state = LCK_HARDLCK + 20;
+	*state = LCK_HARDLOCKHARD;
 	goto failure;
-	/*** lock file is still 
       }
       /* note that we don't care if the lockfile has random garbage */
     }
-    
+  }
+    
+  /* set blocking hardlock */
+  if (type == LCK_HARD) {
     /* we've really got the lock, write BUSY to protect it */
-    lseek (flock, 0, SEEK_SET);
-    write (flock, "BUSY", 4);
-    
-    /* now unlock lockfile */
-    filelock.l_type = F_UNLCK;
-    if (fcntl (flock, F_SETLK, &filelock) == -1) {
-      *state = LCK_HARDLCK + 30;
-      goto failure;
-    }
-    if (close (flock)) {
-      fprintf (stderr, "error closing lock\n");
-      goto failure;
-    }
-    flock = -1;
+    fseek (flock, 0, SEEK_SET);
+    nbytes = fprintf (flock, "BUSY\n");
+    if (nbytes != 5) {
+      *state = LCK_HARDCLOSE;
+      goto failure;
+    }
+    
+    /* now fclose lockfile (also unlocks file) */
+    if (fclose (flock)) {
+      *state = LCK_HARDCLOSE;
+      goto failure;
+    }
+    flock = NULL;
   }
 
   /* check if file is empty or not */
+  fd = fileno (f);
   if (fstat (fd, &filestat)) {
     *state = LCK_UNKNOWN;
@@ -133,16 +144,17 @@
   }
 
-  if (path != NULL) free (path);
-  if (file != NULL) free (file);
-  if (lockname != NULL) free (lockname);
-  return (fd);
+  if (path     != NULL) free (path);
+  if (file     != NULL) free (file);
+  if (lockname != NULL) free (lockname);
+  if (flock    != NULL) fclose (flock);
+  return (f);
 
 failure:
-  if (fd != -1) close (fd);
-  if (flock != -1) close (flock);
-  if (path != NULL) free (path);
-  if (file != NULL) free (file);
-  if (lockname != NULL) free (lockname);
-  return (-1);
+  if (f        != NULL) fclose (f);
+  if (flock    != NULL) fclose (flock);
+  if (path     != NULL) free (path);
+  if (file     != NULL) free (file);
+  if (lockname != NULL) free (lockname);
+  return (NULL);
 }
   
@@ -150,23 +162,7 @@
 int fclearlockfile (char *filename, FILE *f, int type, int *state) {
 
-  int fd, status;
-
-  fd = -1;
-  if (f != NULL) {
-    fflush (f);
-    fd = fileno (f);
-  }
-  status = clearlockfile2 (filename, fd, type, state);
-  if (f != NULL) fclose (f);
-  return (status);
-}
-
-/* if we pass in -1, all we can do is clear the hardlock */
-int clearlockfile2 (char *filename, int fd, int type, int *state) {
-  
-  char *lockname;
-  int i, status;
-  char *path, *file;
-  int flock, filemode;
+  int i, fd, status, nbytes;
+  char *lockname, *path, *file;
+  FILE *flock;
   struct stat filestat;
   struct flock filelock;
@@ -180,5 +176,4 @@
   filelock.l_len    = 0;
   filelock.l_pid    = getpid ();
-  filemode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
 
   /* first clear hard lockfile */
@@ -191,63 +186,55 @@
     sprintf (lockname, "%s/.%s.lck", path, file);
     
-    /* check for lockfile existence */
+    /* check for lockfile existance */
     status = stat (lockname, &filestat);
     if (status == -1) {
-      *state = LCK_HUNLOCK;
+      *state = LCK_HARDLOCK;
       goto failure;
     } 
   
-    /* open lockfile */
-    flock = open (lockname, O_RDWR | O_CREAT, filemode);
-    if (flock == -1) {
-      *state = LCK_HUNLOCK;
-      goto failure;
-    }
+    /* try to open lockfile */
+    flock = fopen (lockname, "w+");
+    if (flock == NULL) {
+      *state = LCK_HARDOPEN;
+      goto failure;
+    }
+    fd = fileno (flock);
 
     /* try a few times to lock lockfile */
     filelock.l_type = F_WRLCK;
-    for (i = 0; (i < 20) && (fcntl (flock, F_SETLK, &filelock) == -1); i++) {
-      usleep (10000);
-    }
+    for (i = 0; (i < 20) && (fcntl (fd, F_SETLK, &filelock) == -1); i++) usleep (10000);
     if (i == 20) {
-      *state = LCK_HUNLOCK;
+      *state = LCK_HARDLOCKHARD;
       goto failure;
     }
 
     /* set value to IDLE */
-    lseek (flock, 0, SEEK_SET);
-    write (flock, "IDLE", 4);
-
-    /* unlock lockfile */
-    filelock.l_type = F_UNLCK;
-    if (fcntl (flock, F_SETLK, &filelock) == -1) {
-      *state = LCK_HUNLOCK;
-      goto failure;
-    }
-    if (close (flock)) {
-      fprintf (stderr, "error closing lock\n");
-      goto failure;
-    }
+    fseek (flock, 0, SEEK_SET);
+    nbytes = fprintf (flock, "IDLE\n");
+    if (nbytes != 5) {
+      *state = LCK_HARDCLOSE;
+      goto failure;
+    }
+
+    if (fclose (flock)) {
+      *state = LCK_HARDCLOSE;
+      goto failure;
+    }
+    flock = NULL;
   }
   
   /* now unlock the file */
-  if (fd != -1) {
-    if (fcntl (fd, F_SETLK, &filelock) == -1) {
-      *state = LCK_HUNLOCK;
-      goto failure;
-    }
-  }
+  fclose (f);
 
   *state = LCK_UNLOCK;
-  if (path != NULL) free (path);
-  if (file != NULL) free (file);
+  if (path != NULL)     free (path);
+  if (file != NULL)     free (file);
   if (lockname != NULL) free (lockname);
   return (1);
 
 failure:
-  if (path != NULL) free (path);
-  if (file != NULL) free (file);
+  if (path != NULL)     free (path);
+  if (file != NULL)     free (file);
   if (lockname != NULL) free (lockname);
   return (0);
-
 }
Index: /trunk/Ohana/src/libohana/src/phot_catalog.c
===================================================================
--- /trunk/Ohana/src/libohana/src/phot_catalog.c	(revision 3648)
+++ /trunk/Ohana/src/libohana/src/phot_catalog.c	(revision 3649)
@@ -13,8 +13,9 @@
 
   /* set lock on database, create stream f */
-  catalog[0].f  = (FILE *) NULL;
+  catalog[0].f = NULL;
   catalog[0].f = fsetlockfile (catalog[0].filename, 3600.0, lockmode, &dbstate);
-  if (dbstate == LCK_EMPTY) return (2);
-  if (catalog[0].f == (FILE *) NULL) return (0);
+  if (dbstate == LCK_MISSING) return (2);
+  if (dbstate == LCK_EMPTY)   return (2);
+  if (catalog[0].f == NULL)   return (0);
 
   fseek (catalog[0].f, 0, SEEK_SET);
Index: /trunk/Ohana/src/misc/Makefile
===================================================================
--- /trunk/Ohana/src/misc/Makefile	(revision 3648)
+++ /trunk/Ohana/src/misc/Makefile	(revision 3649)
@@ -16,7 +16,7 @@
 #  
 INCS	= 	-I$(LINC) -I$(XINC)
-LIBS	= 	-L$(LLIB) -lFITS -lohana -lm
-CFLAGS	=	
-CCFLAGS	=	$(INCS) $(LIBS) 
+LIBS	= 	-L$(LLIB) -lohana -lFITS -lm
+CFLAGS	=	$(INCS)
+LFLAGS	=	$(LIBS)
 
 PROGRAMS = gconfig mknames fhead ftable fields list_astro glockfile \
@@ -24,23 +24,5 @@
 medianfilter mefhead mkfringetable gtfringetable ckfits applyscat fiximg
 
-CFHT = cfhtlog
-
-$(CFHT): 
-	make $(BIN)/$@.$(ARCH)
-
-$(PROGRAMS):
-	make $(BIN)/$@.$(ARCH)
-
 misc: $(PROGRAMS)
-
-clean:
-	for i in $(PROGRAMS); do make $$i.clean; done
-	rm -f */*~
-	rm -f */#*
-	rm -f *~
-	rm -f #*
-
-install:
-	for i in $(PROGRAMS); do make $$i.install; done
 
 FAKESRC = $(SRC)/fakestars.$(ARCH).o $(SRC)/random.$(ARCH).o
@@ -50,26 +32,28 @@
 	$(CC) -o $(BIN)/fakestars.$(ARCH) $(FAKESRC) $(LIBS) $(CCFLAGS)
 
-fakestars: $(BIN)/fakestars.$(ARCH)
-	cp $(BIN)/fakestars.$(ARCH) $(DESTBIN)/fakestars
+fakestars: $(DESTBIN)/fakestars
 
-$(DESTBIN)/cfhtlog: $(SRC)/cfhtlog.$(ARCH)
+$(BIN)/cfhtlog.$(ARCH): $(SRC)/cfhtlog.c
+	$(CC) -I/cfht/include -L/cfht/lib -o $(BIN)/cfhtlog.$(ARCH) $(SRC)/cfhtlog.c -lcfht 
+
+$(DESTBIN)/cfhtlog: $(BIN)/cfhtlog.$(ARCH)
 	@if [ ! -d $(DESTBIN) ]; then mkdir -p $(DESTBIN); fi
 	cp $(SRC)/cfhtlog.$(ARCH) $(DESTBIN)/
 
-cfhtlog: $(SRC)/cfhtlog.$(ARCH)
-$(SRC)/cfhtlog.$(ARCH): $(SRC)/cfhtlog.c
-	$(CC) -I/cfht/include -L/cfht/lib -o $(SRC)/cfhtlog.$(ARCH) $(SRC)/cfhtlog.c -lcfht 
+cfhtlog:
+	make $(DESTBIN)/cfhtlog
 
-# RULES for making & installing
+$(PROGRAMS): % : $(BIN)/%.$(ARCH)
+
+# dependancy rules for binary code #########################
+.PRECIOUS: %.$(ARCH).o
+.PRECIOUS: $(BIN)/%.$(ARCH)
 
 %.$(ARCH).o : %.c
-	$(CC) $(INCS) -o $*.$(ARCH).o -c $< 
-
-$(SRC)/%.$(ARCH).o : %.c
-	$(CC) $(INCS) -o $*.$(ARCH).o -c $< 
+	$(CC) $(CFLAGS) -c $< -o $@ 
 
 $(BIN)/%.$(ARCH) : $(SRC)/%.$(ARCH).o
 	@if [ ! -d $(BIN) ]; then mkdir -p $(BIN); fi
-	$(CC) $^ -o $@ $(LIBS)
+	$(CC) $^ -o $@ $(LFLAGS)
 
 $(DESTBIN)/%: $(BIN)/%.$(ARCH)
@@ -84,2 +68,15 @@
 	rm -f $(SRC)/$*.$(ARCH).o
 	rm -f $(BIN)/$*.$(ARCH)
+
+# utilities #################################################
+
+install:
+	for i in $(PROGRAMS); do make $$i.install; done
+
+clean:
+	for i in $(PROGRAMS); do make $$i.clean; done
+	rm -f */*~
+	rm -f */#*
+	rm -f *~
+	rm -f #*
+
Index: /trunk/Ohana/src/misc/src/glockfile.c
===================================================================
--- /trunk/Ohana/src/misc/src/glockfile.c	(revision 3648)
+++ /trunk/Ohana/src/misc/src/glockfile.c	(revision 3649)
@@ -14,5 +14,5 @@
 
   filename = argv[1];
-  timeout = 120.0;
+  timeout = 30.0;
 
   holdtime = atoi (argv[3]);
@@ -30,9 +30,10 @@
 
   f = fsetlockfile (filename, timeout, type, &state);
-  if (fd == NULL) {
+  if (f == NULL) {
     fprintf (stderr, "ERROR: can't lock file %s\n", filename);
     exit (1);
   }
 
+  fprintf (stderr, "file is locked\n");
   sleep (holdtime);
   fclearlockfile (filename, f, type, &state);
