Index: /branches/eam_branches/ipp-20111122/Ohana/src/photdbc/Makefile
===================================================================
--- /branches/eam_branches/ipp-20111122/Ohana/src/photdbc/Makefile	(revision 33217)
+++ /branches/eam_branches/ipp-20111122/Ohana/src/photdbc/Makefile	(revision 33218)
@@ -18,5 +18,6 @@
 
 photdbc: $(BIN)/photdbc.$(ARCH)
-install: $(DESTBIN)/photdbc
+dvodist: $(BIN)/dvodist.$(ARCH)
+install: $(DESTBIN)/photdbc $(DESTBIN)/dvodist
 
 PHOTDBC = \
@@ -29,4 +30,18 @@
 $(SRC)/join_stars.$(ARCH).o        \
 $(SRC)/make_subcatalog.$(ARCH).o        
+
+DVODIST = \
+$(SRC)/dvodist.$(ARCH).o	    	\
+$(SRC)/initialize_dvodist.$(ARCH).o 	\
+$(SRC)/Shutdown_dvodist.$(ARCH).o     	\
+$(SRC)/SetSignals.$(ARCH).o             \
+$(SRC)/rconnect.$(ARCH).o               \
+$(SRC)/md5_ops.$(ARCH).o                \
+$(SRC)/md5.$(ARCH).o                    \
+$(SRC)/memstr.$(ARCH).o                 \
+$(SRC)/HostTableLoad.$(ARCH).o          \
+$(SRC)/AssignSkyToHost.$(ARCH).o        \
+$(SRC)/CopyToHostLocation.$(ARCH).o     \
+$(SRC)/CopyFromHostLocation.$(ARCH).o
 
 OLD = \
@@ -50,5 +65,7 @@
 $(SRC)/wcatalog.$(ARCH).o
 
-
 $(PHOTDBC): $(INC)/photdbc.h
 $(BIN)/photdbc.$(ARCH): $(PHOTDBC)
+
+$(DVODIST): $(INC)/dvodist.h
+$(BIN)/dvodist.$(ARCH): $(DVODIST)
Index: /branches/eam_branches/ipp-20111122/Ohana/src/photdbc/include/dvodist.h
===================================================================
--- /branches/eam_branches/ipp-20111122/Ohana/src/photdbc/include/dvodist.h	(revision 33217)
+++ /branches/eam_branches/ipp-20111122/Ohana/src/photdbc/include/dvodist.h	(revision 33218)
@@ -4,4 +4,6 @@
 # include <md5.h>
 # include <unistd.h> // needed?
+
+# define myAssert(LOGIC,MSG) { if (!(LOGIC)) { fprintf (stderr, "%s\n", MSG); abort(); } }
 
 // used by the md5 stuff
@@ -39,14 +41,19 @@
 int           args (int argc, char **argv); 
 void          initialize (int argc, char **argv); 
-void          ConfigInit (int *argc, char **argv); 
 
+void          LockDatabase (char *catdir);
+HostTable    *HostTableLoad (char *catdir, char *rootname);
+int           AssignSkyToHost (SkyList *skylist, HostTable *table);
+int           CopyToHostLocation (char *catdir, SkyList *skylist, HostTable *table);
+int           CopyFromHostLocation(char *catdir, SkyList *skylist, HostTable *table);
 
 int           Shutdown (char *format, ...) OHANA_FORMAT(printf, 1, 2);
-void 	      set_db (FITS_DB *in);
 void 	      lock_image_db (FITS_DB *db, char *filename);
-void 	      unlock_image_db (FITS_DB *db);
-void 	      check_permissions (char *basefile);
 void 	      TrapSignal (int sig);
 void 	      SetProtect (int mode);
 int 	      SetSignals (void);
-int 	      copy_images (char *outdir);
+int           rconnect (char *command, char *hostname, char *shell, int *stdio);
+
+int           write_fmt (int fd, char *format, ...);
+char         *memstr (char *m1, char *m2, int n);
+int           get_md5_with_copy (char *input, char *output, md5_byte_t *digest);
Index: /branches/eam_branches/ipp-20111122/Ohana/src/photdbc/src/AssignSkyToHost.c
===================================================================
--- /branches/eam_branches/ipp-20111122/Ohana/src/photdbc/src/AssignSkyToHost.c	(revision 33217)
+++ /branches/eam_branches/ipp-20111122/Ohana/src/photdbc/src/AssignSkyToHost.c	(revision 33218)
@@ -1,3 +1,3 @@
-# include "photdbc.h"
+# include "dvodist.h"
 # define DEBUG 1
 
Index: /branches/eam_branches/ipp-20111122/Ohana/src/photdbc/src/CopyFromHostLocation.c
===================================================================
--- /branches/eam_branches/ipp-20111122/Ohana/src/photdbc/src/CopyFromHostLocation.c	(revision 33217)
+++ /branches/eam_branches/ipp-20111122/Ohana/src/photdbc/src/CopyFromHostLocation.c	(revision 33218)
@@ -1,12 +1,18 @@
-# include "photdbc.h"
+# include "dvodist.h"
 
 # define DEBUG 1
+# define DELETE_ORIGINAL 0
+// make this a user option
 
-int CostFromHostLocation (SkyList *skylist, HostTable *table) {
+// XXX collapse this and CopyToHostLocation (only difference is name of src/tgt and bits set on success)
+int CopyFromHostLocation (char *catdir, SkyList *skylist, HostTable *table) {
 
-  int i;
+  int i, j, k;
   struct stat filestat;
   char srcname[1024];
   char tgtname[1024];
+    
+  md5_byte_t srcDigest[NDIGEST];
+  md5_byte_t tgtDigest[NDIGEST];
     
   uid_t uid = getuid();
@@ -29,5 +35,5 @@
 
     // find the host for the table
-    int realID = (skylist->regions[i]->hostID & DATA_HOST_ID)
+    int realID = (skylist->regions[i]->hostID & DATA_HOST_ID);
     short index = table->index[realID];
     HostInfo *host = &table->hosts[index];
@@ -40,6 +46,6 @@
 
       // set the in and out table names
-      sprintf (srcname, "%s/%s.%s", CATDIR, skylist->regions[i]->name, extname[j]);
-      sprintf (tgtname, "%s/%s.%s", host->pathname, skylist->regions[i]->name, extname[j]);
+      sprintf (srcname, "%s/%s.%s", host->pathname, skylist->regions[i]->name, extname[j]);
+      sprintf (tgtname, "%s/%s.%s", catdir, skylist->regions[i]->name, extname[j]);
 
       // does srcname exist & can it be read?
@@ -48,5 +54,5 @@
       int status = stat (srcname, &filestat);
       if (!status) {
-	if (errno == ENOEND) continue;  // file does not exist (handle broken table with cpt + cpm but no cpn,cps?)
+	if (errno == ENOENT) continue;  // file does not exist (handle broken table with cpt + cpm but no cpn,cps?)
 	perror ("stat:");
 	fprintf (stderr, "failure to access file %s\n", srcname);
@@ -69,17 +75,20 @@
 
     valid:
-      // XXX since we have to open and read the whole file anyway, 
-      // XXX perhaps we should just write the output file?
-	
       // read srcname, write to tgtname, get MD5 sum for srcname as we go:
-      md5_byte_t srcDigest[NDIGEST];
-      get_md5_with_copy (srcname, tgtname, srcDigest);
+      if (!get_md5_with_copy (srcname, tgtname, srcDigest)) {
+	fprintf (stderr, "error reading %s, getting md5, or writing %s\n", srcname, tgtname);
+	success = FALSE;
+	continue;
+      }
 	
       // need to re-open and re-read tgtname to check md5sum
-      md5_byte_t srcDigest[NDIGEST];
-      get_md5_with_copy (tgtname, NULL, tgtDigest);
+      if (!get_md5_with_copy (tgtname, NULL, tgtDigest)) {
+	fprintf (stderr, "error reading %s or getting md5\n", tgtname);
+	success = FALSE;
+	continue;
+      }
 
       // compare the two digest values
-      int match = true;
+      int match = TRUE;
       for (k = 0; match && (k < NDIGEST); k++) {
 	match &= (tgtDigest[k] == srcDigest[k]);
@@ -92,5 +101,5 @@
 	success = FALSE;
       }
-    }	 
+    }
 
     if (!success) {
@@ -104,12 +113,12 @@
       skylist->regions[i]->hostID |= DATA_COPY_FAILURE;
       for (j = 0; success && (j < 4); j++) {
-	sprintf (tgtname, "%s/%s.%s", host->pathname, skylist->regions[i]->name, extname[j]);
+	sprintf (tgtname, "%s/%s.%s", catdir, skylist->regions[i]->name, extname[j]);
 	if (!DEBUG) unlink (tgtname);
       }
       Nfailure ++;
     } else {
-      skylist->regions[i]->hostID |= DATA_ON_TGT;
+      skylist->regions[i]->hostID &= ~DATA_ON_TGT;
       for (j = 0; success && (j < 4); j++) {
-	sprintf (srcname, "%s/%s.%s", CATDIR, skylist->regions[i]->name, extname[j]);
+	sprintf (srcname, "%s/%s.%s", host->pathname, skylist->regions[i]->name, extname[j]);
 	if (!DEBUG && DELETE_ORIGINAL) unlink (srcname);
       }
Index: /branches/eam_branches/ipp-20111122/Ohana/src/photdbc/src/CopyToHostLocation.c
===================================================================
--- /branches/eam_branches/ipp-20111122/Ohana/src/photdbc/src/CopyToHostLocation.c	(revision 33217)
+++ /branches/eam_branches/ipp-20111122/Ohana/src/photdbc/src/CopyToHostLocation.c	(revision 33218)
@@ -1,12 +1,18 @@
-# include "photdbc.h"
+# include "dvodist.h"
 
 # define DEBUG 1
+# define DELETE_ORIGINAL 0
+// make this a user option
 
-int CostToHostLocation (SkyList *skylist, HostTable *table) {
+// XXX collapse this and CopyFromHostLocation (only difference is name of src/tgt and bits set on success)
+int CopyToHostLocation (char *catdir, SkyList *skylist, HostTable *table) {
 
-  int i;
+  int i, j, k;
   struct stat filestat;
   char srcname[1024];
   char tgtname[1024];
+
+  md5_byte_t srcDigest[NDIGEST];
+  md5_byte_t tgtDigest[NDIGEST];
     
   uid_t uid = getuid();
@@ -29,5 +35,5 @@
 
     // find the host for the table
-    int realID = (skylist->regions[i]->hostID & DATA_HOST_ID)
+    int realID = (skylist->regions[i]->hostID & DATA_HOST_ID);
     short index = table->index[realID];
     HostInfo *host = &table->hosts[index];
@@ -40,5 +46,5 @@
 
       // set the in and out table names
-      sprintf (srcname, "%s/%s.%s", CATDIR, skylist->regions[i]->name, extname[j]);
+      sprintf (srcname, "%s/%s.%s", catdir, skylist->regions[i]->name, extname[j]);
       sprintf (tgtname, "%s/%s.%s", host->pathname, skylist->regions[i]->name, extname[j]);
 
@@ -48,5 +54,5 @@
       int status = stat (srcname, &filestat);
       if (!status) {
-	if (errno == ENOEND) continue;  // file does not exist (handle broken table with cpt + cpm but no cpn,cps?)
+	if (errno == ENOENT) continue;  // file does not exist (handle broken table with cpt + cpm but no cpn,cps?)
 	perror ("stat:");
 	fprintf (stderr, "failure to access file %s\n", srcname);
@@ -69,17 +75,20 @@
 
     valid:
-      // XXX since we have to open and read the whole file anyway, 
-      // XXX perhaps we should just write the output file?
-	
       // read srcname, write to tgtname, get MD5 sum for srcname as we go:
-      md5_byte_t srcDigest[NDIGEST];
-      get_md5_with_copy (srcname, tgtname, srcDigest);
+      if (!get_md5_with_copy (srcname, tgtname, srcDigest)) {
+	fprintf (stderr, "error reading %s, getting md5, or writing %s\n", srcname, tgtname);
+	success = FALSE;
+	continue;
+      }
 	
       // need to re-open and re-read tgtname to check md5sum
-      md5_byte_t tgtDigest[NDIGEST];
-      get_md5_with_copy (tgtname, NULL, tgtDigest);
+      if (!get_md5_with_copy (tgtname, NULL, tgtDigest)) {
+	fprintf (stderr, "error reading %s or getting md5\n", tgtname);
+	success = FALSE;
+	continue;
+      }
 
       // compare the two digest values
-      int match = true;
+      int match = TRUE;
       for (k = 0; match && (k < NDIGEST); k++) {
 	match &= (tgtDigest[k] == srcDigest[k]);
@@ -111,5 +120,5 @@
       skylist->regions[i]->hostID |= DATA_ON_TGT;
       for (j = 0; success && (j < 4); j++) {
-	sprintf (srcname, "%s/%s.%s", CATDIR, skylist->regions[i]->name, extname[j]);
+	sprintf (srcname, "%s/%s.%s", catdir, skylist->regions[i]->name, extname[j]);
 	if (!DEBUG && DELETE_ORIGINAL) unlink (srcname);
       }
Index: /branches/eam_branches/ipp-20111122/Ohana/src/photdbc/src/HostTableLoad.c
===================================================================
--- /branches/eam_branches/ipp-20111122/Ohana/src/photdbc/src/HostTableLoad.c	(revision 33217)
+++ /branches/eam_branches/ipp-20111122/Ohana/src/photdbc/src/HostTableLoad.c	(revision 33218)
@@ -1,3 +1,3 @@
-# include "photdbc.h"
+# include "dvodist.h"
 # define DEBUG 1
 
@@ -23,10 +23,12 @@
 }
 
-HostTable *HostTableLoad (char *catdir) {
+HostTable *HostTableLoad (char *catdir, char *rootname) {
+
+  int i, Nline;
 
   char *filename = NULL;
 
-  ALLOCATE (filename, char, strlen(catdir) + strlen("/HostTable.dat") + 1);
-  sprintf (filename, "%s/HostTable.dat", catdir);
+  ALLOCATE (filename, char, strlen(catdir) + strlen(rootname) + 2); // one slash and one EOL
+  sprintf (filename, "%s/%s", catdir, rootname);
 
   FILE *f = fopen (filename, "r");
@@ -40,4 +42,5 @@
   int NHOSTS = 16;
   int Nhosts = 0;
+  HostInfo *hosts = NULL;
   ALLOCATE (hosts, HostInfo, NHOSTS);
   InitHosts (hosts, Nhosts, NHOSTS);
@@ -45,5 +48,4 @@
   int maxID = 0;
 
-  int Nline;
   for (Nline = 0; TRUE; Nline ++) {
     int ID;
@@ -52,6 +54,7 @@
     char line[1024];
 
-    int status = scan_line_maxlen (f, line, 1024);
-    if (status = EOF) break;
+    // XXXX use this for safety: int status = scan_line_maxlen (f, line, 1024);
+    int status = scan_line (f, line);
+    if (status == EOF) break;
 
     // find first non-whitespace char & skip commented lines
@@ -62,5 +65,5 @@
     if (status != 3) {
       fprintf (stderr, "error reading line %d of host table %s\n", Nline, filename);
-      FreeHost (hosts, Nhosts);
+      FreeHosts (hosts, Nhosts);
       return NULL;
     }
@@ -69,9 +72,9 @@
 
     if (ID < 1) {
-      fprint (stderr, "invalid host ID %d\n", ID);
+      fprintf (stderr, "invalid host ID %d\n", ID);
       exit (1);
     }
     if (ID > 65535) {
-      fprint (stderr, "invalid host ID %d\n", ID);
+      fprintf (stderr, "invalid host ID %d\n", ID);
       exit (1);
     }
@@ -90,4 +93,5 @@
   }    
 
+  HostTable *table = NULL;
   ALLOCATE (table, HostTable, 1);
   table->Nhosts = Nhosts;
@@ -123,5 +127,5 @@
     if (DEBUG) fprintf (stderr, "starting host within thread\n");
 
-    pid = rconnect (command, table->hosts[i].hostname, shell, stdio);
+    int pid = rconnect (command, table->hosts[i].hostname, shell, stdio);
     if (!pid) {     
       /** failure to start: extend retry period **/
@@ -137,5 +141,5 @@
     close(stdio[2]);
 
-    status = check_dir_access (table->hosts[i].pathname, DEBUG);
+    int status = check_dir_access (table->hosts[i].pathname, DEBUG);
     if (!status) {
       fprintf (stderr, "failed to find / create target path\n");
Index: /branches/eam_branches/ipp-20111122/Ohana/src/photdbc/src/Shutdown_dvodist.c
===================================================================
--- /branches/eam_branches/ipp-20111122/Ohana/src/photdbc/src/Shutdown_dvodist.c	(revision 33217)
+++ /branches/eam_branches/ipp-20111122/Ohana/src/photdbc/src/Shutdown_dvodist.c	(revision 33218)
@@ -11,5 +11,5 @@
 
   // lock the image db table 
-  status = dvo_image_lock (&db, ImageCat, 60.0, LCK_XCLD);
+  int status = dvo_image_lock (&db, ImageCat, 60.0, LCK_XCLD);
   if (!status) {
     Shutdown ("ERROR: failure to lock image catalog %s", db.filename);
@@ -42,9 +42,8 @@
   va_end (argp);
 
-  // XXX this is in SetSignals.c, which includes photdbc.c.  
+  // XXX this is in SetSignals.c, which includes photdbc.h
   SetProtect (TRUE);
-  gfits_db_close (db);
-  fprintf (stderr, "ERROR: addstar halted\n");
+  gfits_db_close (&db);
+  fprintf (stderr, "ERROR: dvodist halted\n");
   exit (1);
 }
-
Index: /branches/eam_branches/ipp-20111122/Ohana/src/photdbc/src/dvodist.c
===================================================================
--- /branches/eam_branches/ipp-20111122/Ohana/src/photdbc/src/dvodist.c	(revision 33217)
+++ /branches/eam_branches/ipp-20111122/Ohana/src/photdbc/src/dvodist.c	(revision 33218)
@@ -1,24 +1,20 @@
 # include "dvodist.h"
+
+// dvodist (-out | -in) (catdir)
+// dvodist -out : copy catalog tables to distributed locations in host table (from catdir)
+// dvodist -in  : copy catalog tables to catdir (from distributed locations in host table)
 
 int main (int argc, char **argv) {
 
-  int i;
   SkyTable *sky;
   SkyList *skylist;
 
   /* get configuration info, args, lockfile */
+  SetSignals ();
   initialize (argc, argv);
   char *catdir = strcreate (argv[1]);
 
   LockDatabase (catdir);
-
-  // dvodist (-out | -in)
-  // dvodist -out : host table represents new target locations
   
-  HostTable *hosts = HostTableLoad (catdir);
-  if (NULL) {
-    Shutdown ("failed to load Host Table for %s\n", catdir);
-  }
-
   // load the current SkyTable. If SkyTable does not exist, we must fail
   sky = SkyTableLoadOptimal (catdir, NULL, NULL, TRUE, SKY_DEPTH_HST, VERBOSE);
@@ -26,11 +22,16 @@
   skylist = SkyListByPatch (sky, -1, &UserPatch);
 
+  HostTable *hosts = HostTableLoad (catdir, sky->hosts);
+  if (!hosts) {
+    Shutdown ("failed to load Host Table %s for %s\n", catdir, sky->hosts);
+  }
+
   switch (MODE) {
     case MODE_OUT:
       AssignSkyToHost (skylist, hosts);
-      CopyToHostLocation (skylist, hosts);
+      CopyToHostLocation (catdir, skylist, hosts);
       break;
     case MODE_IN:
-      CopyFromHostLocation (skylist, hosts);
+      CopyFromHostLocation (catdir, skylist, hosts);
       break;
     default:
@@ -39,19 +40,7 @@
   }
 
-  // XXX probably need to make this a skyregion_io.c function
-  // char *SkyTableFilename (char *catdir);
-  char *skyfile;
-  ALLOCATE (skyfile, char, strlen(catdir) + strlen("/SkyTable.fits") + 1);
-  sprintf (skyfile, "%s/SkyTable.fits", catdir);
-  SkyTableSave (sky, filename);
+  char *skyfile = SkyTableFilename (catdir);
+  SkyTableSave (sky, skyfile);
 
   exit (0);
 }
-
-/* things still missing AFAIK :
-
- * switch based on MODE
- * add header keyword to SkyTable to note existence of hostID
- * code for CopyFromHostLocation
- 
- */
Index: /branches/eam_branches/ipp-20111122/Ohana/src/photdbc/src/initialize_dvodist.c
===================================================================
--- /branches/eam_branches/ipp-20111122/Ohana/src/photdbc/src/initialize_dvodist.c	(revision 33217)
+++ /branches/eam_branches/ipp-20111122/Ohana/src/photdbc/src/initialize_dvodist.c	(revision 33218)
@@ -9,5 +9,4 @@
   if (get_argument (argc, argv, "--help")) usage();
 
-  // XXX anything? ConfigInit (&argc, argv);
   args (argc, argv);
 }
Index: /branches/eam_branches/ipp-20111122/Ohana/src/photdbc/src/md5_ops.c
===================================================================
--- /branches/eam_branches/ipp-20111122/Ohana/src/photdbc/src/md5_ops.c	(revision 33217)
+++ /branches/eam_branches/ipp-20111122/Ohana/src/photdbc/src/md5_ops.c	(revision 33218)
@@ -16,5 +16,5 @@
   int fdOutput = 0;
   if (output) {
-    fOutput = fopen (tgtname, "w");
+    fOutput = fopen (output, "w");
     fdOutput = fileno (fOutput);
   }
@@ -28,16 +28,19 @@
     md5_append (&state, buffer, nbytes);
     if (output) {
-      nbytes = write (fdOutput, buffer, NBUFFER);
+      int nbytesOut = write (fdOutput, buffer, NBUFFER);
       // XXX check that we actually write out all nbytes...
+      if (nbytesOut != nbytes) {
+	return FALSE;
+      }
     }
   }
-  md5_finish(&state, digest);
+  md5_finish (&state, digest);
 	
   fclose (fInput);
-  flush (fInput);
+  fflush (fInput);
 
   if (output) {
     fclose (fOutput);
-    flush (fOutput);
+    fflush (fOutput);
   }
   return TRUE;
Index: /branches/eam_branches/ipp-20111122/Ohana/src/photdbc/src/memstr.c
===================================================================
--- /branches/eam_branches/ipp-20111122/Ohana/src/photdbc/src/memstr.c	(revision 33218)
+++ /branches/eam_branches/ipp-20111122/Ohana/src/photdbc/src/memstr.c	(revision 33218)
@@ -0,0 +1,37 @@
+# include "dvodist.h"
+
+// XXX move these to libohana?
+
+/* memstr returns a view, not an allocated string : don't free */
+/* returns pointer to start of m2 in m1, or NULL if failure */ 
+char *memstr (char *m1, char *m2, int n) {
+
+  int i, N;
+
+  N = strlen (m2);
+  for (i = 0; (i < n - N + 1) && memcmp (m1, m2, N); i++, m1++);
+  if (memcmp (m1, m2, N)) return (NULL);
+  return (m1);
+
+}
+
+/* formatted write statement, with intelligent allocation */
+int write_fmt (int fd, char *format, ...) {
+
+  int Nbyte, status;
+  char tmp, *line;
+  va_list argp;  
+
+  va_start (argp, format);
+  Nbyte = vsnprintf (&tmp, 0, format, argp);
+  va_end (argp);
+
+  va_start (argp, format);
+  ALLOCATE (line, char, Nbyte + 1);
+  vsnprintf (line, Nbyte + 1, format, argp);
+  status = write (fd, line, strlen(line));
+  va_end (argp);
+
+  free (line);
+  return (status);
+}
Index: /branches/eam_branches/ipp-20111122/Ohana/src/photdbc/src/photdbc.c
===================================================================
--- /branches/eam_branches/ipp-20111122/Ohana/src/photdbc/src/photdbc.c	(revision 33217)
+++ /branches/eam_branches/ipp-20111122/Ohana/src/photdbc/src/photdbc.c	(revision 33218)
@@ -81,6 +81,5 @@
   }
 
-  ALLOCATE (skyfile, char, strlen(argv[1]) + strlen("/SkyTable.fits") + 1);
-  sprintf (skyfile, "%s/SkyTable.fits", argv[1]);
+  skyfile = SkyTableFilename (argv[1]);
   SkyTableSave (sky, skyfile);
   exit (0);
Index: /branches/eam_branches/ipp-20111122/Ohana/src/photdbc/src/rconnect.c
===================================================================
--- /branches/eam_branches/ipp-20111122/Ohana/src/photdbc/src/rconnect.c	(revision 33217)
+++ /branches/eam_branches/ipp-20111122/Ohana/src/photdbc/src/rconnect.c	(revision 33218)
@@ -1,3 +1,5 @@
-# include "pcontrol.h"
+# include "dvodist.h"
+# include <sys/types.h>
+# include <sys/wait.h>
 
 /* connection can take a while, allow up to 2 sec */
@@ -18,8 +20,8 @@
   struct timespec request, remain;
 
-  ASSERT (command != NULL, "command is NULL");
-  ASSERT (hostname != NULL, "hostname is NULL");
-  ASSERT (shell != NULL, "shell is NULL");
-  ASSERT (stdio != NULL, "stdio is NULL");
+  myAssert (command != NULL, "command is NULL");
+  myAssert (hostname != NULL, "hostname is NULL");
+  myAssert (shell != NULL, "shell is NULL");
+  myAssert (stdio != NULL, "stdio is NULL");
 
   bzero (stdin_fd,  2*sizeof(int));
@@ -39,5 +41,5 @@
   pid = fork ();
   if (!pid) { /* must be child process */
-    if (VerboseMode()) gprint (GP_ERR, "starting remote connection to %s...", hostname);
+    if (VERBOSE) fprintf (stderr, "starting remote connection to %s...", hostname);
 
     /* close the other ends of the pipes */
@@ -57,6 +59,6 @@
 
     status = execvp (argv[0], argv);
-    gprint (GP_ERR, "error starting remote shell process\n");
-    pcontrol_exit (60);
+    fprintf (stderr, "error starting remote shell process\n");
+    exit (60);
   }
   free (argv);
@@ -90,8 +92,8 @@
   if (status == 0) goto connect_error;
   if (status == -1) goto connect_error;
-  if (VerboseMode()) gprint (GP_ERR, "%d cycles to connect\n", i);
+  if (VERBOSE) fprintf (stderr, "%d cycles to connect\n", i);
   FreeIOBuffer (&buffer);
 
-  if (VerboseMode()) gprint (GP_ERR, "Connected\n");
+  if (VERBOSE) fprintf (stderr, "Connected\n");
 
   stdio[0] = stdin_fd[1];
@@ -106,5 +108,5 @@
 
 connect_error:
-  if (VerboseMode()) gprint (GP_ERR, "error while connecting, status: %d, ncycles: %d\n", status, i);
+  if (VERBOSE) fprintf (stderr, "error while connecting, status: %d, ncycles: %d\n", status, i);
 
   /* avoid blocking on waitpid, test every 100 usec, up to 50 msec */
@@ -121,18 +123,18 @@
 
   if ((result == -1) && (errno != ECHILD)) {
-    gprint (GP_ERR, "unexpected error from waitpid (%d): programming error\n", errno);
-    pcontrol_exit (61);
+    fprintf (stderr, "unexpected error from waitpid (%d): programming error\n", errno);
+    exit (61);
   }
   if (result == 0) {
-    if (VerboseMode()) gprint (GP_ERR, "child did not exit (rconnect)??");
+    if (VERBOSE) fprintf (stderr, "child did not exit (rconnect)??");
   }
   if (result > 0) {
     if (result != pid) {
-      gprint (GP_ERR, "waitpid error: mis-matched PID (%d vs %d).  programming error\n", result, pid);
-      pcontrol_exit (62);
+      fprintf (stderr, "waitpid error: mis-matched PID (%d vs %d).  programming error\n", result, pid);
+      exit (62);
     }
     if (WIFSTOPPED(waitstatus)) {
-      gprint (GP_ERR, "waitpid returns 'stopped': programming error\n");
-      pcontrol_exit (63);
+      fprintf (stderr, "waitpid returns 'stopped': programming error\n");
+      exit (63);
     }
   }
