Index: trunk/psLib/src/fits/psFits.c
===================================================================
--- trunk/psLib/src/fits/psFits.c	(revision 21107)
+++ trunk/psLib/src/fits/psFits.c	(revision 21116)
@@ -7,6 +7,6 @@
  *  @author Robert DeSonia, MHPCC
  *
- *  @version $Revision: 1.82 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2009-01-03 01:05:47 $
+ *  @version $Revision: 1.83 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2009-01-13 20:53:38 $
  *
  *  Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii
@@ -36,8 +36,13 @@
 
 #define MAX_STRING_LENGTH 256  // maximum length string for FITS routines
+
+#define FITS_OPEN_RETRIES 5             // Number of retries to attempt when opening a FITS file
+#define FITS_OPEN_RETRY_WAIT 200000     // Wait between retries (usec)
+
 static char *defaultExtword = "EXTNAME";
 
-bool p_psFitsDumpErrors (const char* filename, unsigned int lineno, const char* func, 
-			 psErrorCode code, const char *message, ...) {
+
+bool p_psFitsDumpErrors (const char* filename, unsigned int lineno, const char* func,
+                         psErrorCode code, const char *message, ...) {
 
     char fitsErr[MAX_STRING_LENGTH];
@@ -49,5 +54,5 @@
 
     while (fits_read_errmsg(fitsErr)) {
-	p_psError(filename, lineno, func, PS_ERR_BAD_FITS, false, "CFITSIO error: %s", fitsErr);
+        p_psError(filename, lineno, func, PS_ERR_BAD_FITS, false, "CFITSIO error: %s", fitsErr);
     }
 
@@ -55,6 +60,6 @@
 }
 
-psErrorCode p_psFitsError(const char* filename, unsigned int lineno, const char* func, 
-			  int status, bool new, const char *errorMsg, ...)
+psErrorCode p_psFitsError(const char* filename, unsigned int lineno, const char* func,
+                          int status, bool new, const char *errorMsg, ...)
 {
     char fitsErr[MAX_STRING_LENGTH];
@@ -68,5 +73,5 @@
 
     while (fits_read_errmsg(fitsErr)) {
-	psError(PS_ERR_IO, false, "[CFITSIO error: %s]", fitsErr);
+        psError(PS_ERR_IO, false, "[CFITSIO error: %s]", fitsErr);
     }
     return PS_ERR_IO;
@@ -97,5 +102,5 @@
     if (fits != NULL) {
         if (fits_close_file(fits->fd, &status)) {
-	    psFitsDumpErrors (PS_ERR_IO, "Error while closing psFits object");
+            psFitsDumpErrors (PS_ERR_IO, "Error while closing psFits object");
             return false;
         }
@@ -124,52 +129,43 @@
 }
 
-# define FITS_OPEN_RETRIES 5
-# define FITS_OPEN_RETRY_WAIT 200000
-
 psFits* psFitsOpen(const char* name, const char* mode)
 {
     PS_ASSERT_STRING_NON_EMPTY(name, NULL);
 
-    int status = 0;
-    fitsfile *fptr = NULL;      /* Pointer to the FITS file */
-
-    // check that the directory exists : for NFS-mounted file systems, the directory may
+    // Check that the directory exists: for NFS-mounted file systems, the directory may
     // take some time to appear
 
-    int dirstat = 0;
-    char *tmpname = psStringCopy (name);
-    char *tmpdir = dirname (tmpname);
-    char *dir = psStringCopy (tmpdir);
+    int dirstat = 0;                    // Status of directory access
+    psString tmpname = psStringCopy(name); // Copy of filename, since dirname() may modify
+    const char *dir = dirname(tmpname);
     useconds_t waittime = FITS_OPEN_RETRY_WAIT;
-    
-    for (int i = 0; (i < FITS_OPEN_RETRIES) && ((dirstat = access (dir, F_OK)) != 0); i++) {
-	// if the error is not ENOENT, then the error is more serious than NFS-mount delays
-	if (errno != ENOENT) {
-	    int thisErrno = errno;
-	    char errorBuf[64], *errorMsg;
-# if ((_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) && ! _GNU_SOURCE)
-	    errorMsg = strerror_r (thisErrno, errorBuf, 64);
-# else
-	    strerror_r (thisErrno, errorBuf, 64);
-	    errorMsg = errorBuf;		
-# endif
-	    psError (PS_ERR_IO, true, "Directory (%s) for requested file is not accessible: %s", dir, errorMsg);
-
-	    psFree (dir);
-	    psFree (tmpname);
-	    return NULL;
-	}
-
-	usleep (waittime);
-	waittime *= 2;
+
+    for (int i = 0; (i < FITS_OPEN_RETRIES) && ((dirstat = access(dir, F_OK)) != 0); i++) {
+        if (errno != ENOENT) {
+            // The error is more serious than NFS-mount delays
+            int thisErrno = errno;      // Error number
+            char errorBuf[MAX_STRING_LENGTH], *errorMsg;
+#if ((_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) && ! _GNU_SOURCE)
+            errorMsg = strerror_r(thisErrno, errorBuf, MAX_STRING_LENGTH);
+#else
+            strerror_r(thisErrno, errorBuf, MAX_STRING_LENGTH);
+            errorMsg = errorBuf;
+#endif
+            psError(PS_ERR_IO, true, "Directory (%s) for requested file is not accessible: %s",
+                    dir, errorMsg);
+
+            psFree(tmpname);
+            return NULL;
+        }
+
+        usleep(waittime);
+        waittime *= 2;
     }
     if (dirstat != 0) {
-	psError (PS_ERR_IO, true, "Directory (%s) for requested file is not accessible, timed out", dir);
-	psFree (dir);
-	psFree (tmpname);
-	return NULL;
-    }
-    psFree (dir);
-    psFree (tmpname);
+        psError(PS_ERR_IO, true, "Directory (%s) for requested file is not accessible, timed out", dir);
+        psFree(tmpname);
+        return NULL;
+    }
+    psFree(tmpname);
 
     /* check the mode to determine how to open/create file */
@@ -196,4 +192,7 @@
     }
 
+    int status = 0;                     // CFITSIO status
+    fitsfile *fptr = NULL;              // Pointer to the FITS file
+
     if (newFile) {
         /* Check if an existing file is in the way before creating file */
@@ -201,21 +200,21 @@
             // file exists, delete old one first
             if (remove(name)) {
-		int thisErrno = errno;
-		char errorBuf[64], *errorMsg;
+                int thisErrno = errno;
+                char errorBuf[64], *errorMsg;
 # if ((_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) && ! _GNU_SOURCE)
-		errorMsg = strerror_r (errno, errorBuf, 64);
+                errorMsg = strerror_r (errno, errorBuf, 64);
 # else
-		strerror_r (errno, errorBuf, 64);
-		errorMsg = errorBuf;		
+                strerror_r (errno, errorBuf, 64);
+                errorMsg = errorBuf;
 # endif
-		psError (PS_ERR_IO, true, "Failed to delete a previously-existing file: %s", errorMsg);
-		fprintf (stderr, "errno: %d, %s, %s : %lx, %lx\n", thisErrno, errorMsg, errorBuf, (long int) errorMsg, (long int) errorBuf);
-		return NULL;
-	    }
+                psError (PS_ERR_IO, true, "Failed to delete a previously-existing file: %s", errorMsg);
+                fprintf (stderr, "errno: %d, %s, %s : %lx, %lx\n", thisErrno, errorMsg, errorBuf, (long int) errorMsg, (long int) errorBuf);
+                return NULL;
+            }
         }
         if (!access(name, F_OK)) {
-	    psError (PS_ERR_IO, true, "deleted file still exists!");
-	    return NULL;
-	}
+            psError (PS_ERR_IO, true, "deleted file still exists!");
+            return NULL;
+        }
 
         #if ( CFITSIO_DISKFILE == 1 )
@@ -226,5 +225,5 @@
         (&fptr, name, &status);
         if (fptr == NULL || status != 0) {
-	    psFitsDumpErrors (PS_ERR_IO, _("Could not create file,'%s'"), name);
+            psFitsDumpErrors (PS_ERR_IO, _("Could not create file,'%s'"), name);
             return NULL;
         }
@@ -237,5 +236,5 @@
         (&fptr, name, iomode, &status);
         if (fptr == NULL || status != 0) {
-	    psFitsDumpErrors(PS_ERR_IO, _("Could not open file,'%s'"), name);
+            psFitsDumpErrors(PS_ERR_IO, _("Could not open file,'%s'"), name);
             return NULL;
         }
@@ -414,5 +413,5 @@
         fits_movrel_hdu(fits->fd, extnum, &hdutype, &status);
         if (status != 0) {
-	    psFitsDumpErrors (PS_ERR_LOCATION_INVALID, _("Could not move %d HDUs from current position"), extnum);
+            psFitsDumpErrors (PS_ERR_LOCATION_INVALID, _("Could not move %d HDUs from current position"), extnum);
             return false;
         }
@@ -420,5 +419,5 @@
         fits_movabs_hdu(fits->fd, extnum+1, &hdutype, &status);
         if (status != 0) {
-	    psFitsDumpErrors (PS_ERR_LOCATION_INVALID, _("Could not move to specified HDU #%d."), extnum);
+            psFitsDumpErrors (PS_ERR_LOCATION_INVALID, _("Could not move to specified HDU #%d."), extnum);
             return false;
         }
@@ -475,5 +474,5 @@
 
     if (fits_update_key_str(fits->fd, extword, (char*)name, NULL, &status) != 0) {
-	psFitsDumpErrors (PS_ERR_IO, _("Could not write data to file %s"), name);
+        psFitsDumpErrors (PS_ERR_IO, _("Could not write data to file %s"), name);
         return false;
     }
