Index: trunk/extsrc/gpcsw/gpcsrc/fits/libfh/fh.c
===================================================================
--- trunk/extsrc/gpcsw/gpcsrc/fits/libfh/fh.c	(revision 24391)
+++ trunk/extsrc/gpcsw/gpcsrc/fits/libfh/fh.c	(revision 25299)
@@ -1039,4 +1039,5 @@
 	 newval[strlen(newval) - 1] = '\0';
 	 fh_set_str(hu, idx, name, newval, comment);
+	 free(newval);
 	 return;
       }
@@ -1519,4 +1520,29 @@
       i++;
    }
+   return FH_SUCCESS;
+}
+
+static void
+pad_header(HeaderUnit hu, int n)
+{
+   int i;
+   double idx = 900000000;
+
+   for (i = 0; i < n; i++)
+   {
+      fh_set_card(hu, idx++, FH_RESERVE);
+   }
+}
+
+fh_result
+fh_copy(HeaderUnit hu, const HeaderUnit source_)
+{
+   fh_result result;
+   int reserve;
+
+   result = fh_merge(hu, source_);
+   if (result != FH_SUCCESS) return result;
+   reserve = fh_get_reserve(source_);
+   if (reserve) pad_header(hu, reserve);
    return FH_SUCCESS;
 }
@@ -1757,4 +1783,21 @@
 }
 
+int
+fh_get_reserve(HeaderUnit hu)
+{
+   HeaderUnitStruct* list = FH_HU(hu);
+
+   if (!list)
+   {
+      log_error("invalid argument to fh_get_reserve()");
+      return -1;
+   }
+
+   if (list->reserve)
+      return list->reserve;
+   else
+      return list->reserve_found;
+}
+
 fh_result
 fh_rewrite(HeaderUnit hu)
Index: trunk/extsrc/gpcsw/gpcsrc/fits/libfh/fh.h
===================================================================
--- trunk/extsrc/gpcsw/gpcsrc/fits/libfh/fh.h	(revision 24391)
+++ trunk/extsrc/gpcsw/gpcsrc/fits/libfh/fh.h	(revision 25299)
@@ -174,4 +174,9 @@
  * is used to create a new FITS header.  NOT for use with fh_rewrite().
  */
+int fh_get_reserve(HeaderUnit hu);
+/*
+ * Get the current setting or the number of reserve cards found in
+ * a header unit.  Returns -1 if hu is invalid.
+ */
 fh_result fh_validate(HeaderUnit hu); /* fh_validate.c; for use by fhtool.c */
 
@@ -386,4 +391,5 @@
 double fh_idx(HeaderUnit hu); /* idx of the last card returned by fh_next */
 fh_result fh_merge(HeaderUnit hu, const HeaderUnit source); /* source unchanged */
+fh_result fh_copy(HeaderUnit hu, const HeaderUnit source); /* fh_merge+keep reserve */
 
 /* ---------------------------------------------------------
@@ -459,4 +465,3 @@
  */
 
-
 #endif /* _INCLUDED_fh */
