Index: trunk/Ohana/src/libohana/include/ohana.h
===================================================================
--- trunk/Ohana/src/libohana/include/ohana.h	(revision 5391)
+++ trunk/Ohana/src/libohana/include/ohana.h	(revision 5451)
@@ -118,4 +118,5 @@
 int     mkdirhier              PROTO((char *path));
 void    make_backup            PROTO((char *filename));
+int     check_file_access      PROTO((char *basefile, int backup, int verbose));
 
 /* in glockfile.c */
Index: trunk/Ohana/src/libohana/src/findexec.c
===================================================================
--- trunk/Ohana/src/libohana/src/findexec.c	(revision 5391)
+++ trunk/Ohana/src/libohana/src/findexec.c	(revision 5451)
@@ -25,6 +25,75 @@
 }
 
-/* pathname
-
+/* check that file can be written to:
+   - dir exists
+   - dir permissions OK
+   - file permissions OK, 
+   - file backup permission OK (optional)
+*/
+int check_file_access (char *basefile, int BACKUP, int VERBOSE) {
+  
+  char *path, *filename;
+  struct stat filestat;
+  uid_t uid;
+  gid_t gid;
+  int status, cmode;
+
+  uid = getuid();
+  gid = getgid();
+
+  /* check permission to write to directory */
+  path = pathname (basefile);
+  status = stat (path, &filestat);
+  if (status == -1) {
+    if (VERBOSE) fprintf (stderr, "directory %s does not exist, creating...\n", path);
+    cmode = S_IRWXU | S_IRWXG | S_IRWXO;
+    status = mkdir (path, cmode);
+    if (status == -1) {
+      if (VERBOSE) fprintf (stderr, "can't create %s\n", path);
+      return (FALSE);
+    }
+  } 
+  status = stat (path, &filestat);
+  if (((uid == filestat.st_uid) && (filestat.st_mode & S_IRWXU)) ||
+      ((gid == filestat.st_gid) && (filestat.st_mode & S_IRWXG)) || 
+      (filestat.st_mode & S_IRWXO)) {
+  } else {
+    if (VERBOSE) fprintf (stderr, "can't write to %s\n", path);
+    return (FALSE);
+  }
+  free (path);
+  
+  /* check permission to write to file */
+  status = stat (basefile, &filestat);
+  if (status == 0) { /* file exists, are permissions OK? */
+    if (((uid == filestat.st_uid) && (filestat.st_mode & S_IRUSR) && (filestat.st_mode & S_IWUSR)) ||
+	((gid == filestat.st_gid) && (filestat.st_mode & S_IRGRP) && (filestat.st_mode & S_IWGRP)) || 
+	((filestat.st_mode & S_IROTH) && (filestat.st_mode & S_IWOTH))) {
+    } else {
+      if (VERBOSE) fprintf (stderr, "can't write to %s\n", basefile);
+      return (FALSE);
+    }
+  }
+  
+  /* check permission to write to backup file */
+  if (BACKUP) {
+    ALLOCATE (filename, char, strlen(basefile) + 2);
+    sprintf (filename, "%s~", basefile);
+    status = stat (filename, &filestat);
+    if (status == 0) { /* file exists, are permissions OK? */
+      if (((uid == filestat.st_uid) && (filestat.st_mode & S_IRUSR) && (filestat.st_mode & S_IWUSR)) ||
+	  ((gid == filestat.st_gid) && (filestat.st_mode & S_IRGRP) && (filestat.st_mode & S_IWGRP)) || 
+	  ((filestat.st_mode & S_IROTH) && (filestat.st_mode & S_IWOTH))) {
+      } else {
+	if (VERBOSE) fprintf (stderr, "can't write to %s\n", filename);
+	return (FALSE);
+      }
+    }
+    free (filename);
+  }
+  return (TRUE);
+}
+
+/* pathname:
    given path/filename, returns path
    given just filename, returns . 
