Index: trunk/src/emx/src/emxomf/emxomf.c
===================================================================
--- trunk/src/emx/src/emxomf/emxomf.c	(revision 462)
+++ trunk/src/emx/src/emxomf/emxomf.c	(revision 464)
@@ -31,4 +31,6 @@
 #include <sys/param.h>
 #include <sys/emxload.h>
+#include <sys/types.h>
+#include <sys/stat.h>
 #include <ar.h>
 
@@ -261,5 +263,5 @@
 /* This is the page size for OMF libraries.  It is set by the -p
    option. */
-static int page_size = 16;
+static int page_size = 0;
 
 /* The index of the next OMF name, used by find_lname().  Name indices
@@ -3548,6 +3550,25 @@
       if (!opt_x)
         {
+          /* calculate a page size based on the size of the aout library.
+             Since we don't yet know the number of files it contains, we'll
+             assum the converted library isn't more than the double it's size.
+             Or at least we can hope it's not... */
+          struct stat s;
+          int calculated_page_size = page_size;
+          if (calculated_page_size <= 0)
+            { /* For a better calculation we would need the file count. */
+              calculated_page_size = 16;
+              if (!stat(src_fname, &s))
+                {
+                  int cbPage = (s.st_size * 2) / 65536;
+                  while (calculated_page_size < cbPage)
+                      calculated_page_size <<= 1;
+                }
+              if (1)
+                printf("calculated page size %d\n", calculated_page_size);
+            }
+
           make_out_fname (dst_fname, inp_fname, ".lib");
-          out_lib = omflib_create (out_fname, page_size, lib_errmsg);
+          out_lib = omflib_create (out_fname, calculated_page_size, lib_errmsg);
           if (out_lib == NULL)
             error (lib_errmsg);
Index: trunk/src/emx/src/emxomf/emxomfar.c
===================================================================
--- trunk/src/emx/src/emxomf/emxomfar.c	(revision 462)
+++ trunk/src/emx/src/emxomf/emxomfar.c	(revision 464)
@@ -24,5 +24,8 @@
 #include <string.h>
 #include <io.h>
+#include <sys/types.h>
+#include <sys/stat.h>
 #include <sys/omflib.h>
+
 
 #define FALSE 0
@@ -154,4 +157,57 @@
 
 
+
+/**
+ * Calculate the expected page size for a library based on the
+ * parameters which is specified.
+ *
+ * @returns 
+ * @param   papszFiles  Pointer to a vector of files.
+ * @param   chCmd       The command we're processing.
+ * @remark  Only applies when creating a new library.
+ */
+static int calc_pagesize(char **papszFiles, const char chCmd)
+{
+    unsigned    cFiles;
+    size_t      cbFiles;
+    int         cbPage;
+    int         i;
+
+    /* if it's not a replace command return default size. */
+    if (chCmd != 'r')
+        return 16;
+
+    /*
+     * Count the files and sum their sizes.
+     */
+    for (i = 0, cFiles = 0, cbFiles = 0; papszFiles[i]; i++)
+    {
+        struct stat s;
+        cFiles++;
+        if (!stat(papszFiles[i], &s))
+            cbFiles += s.st_size;
+        else
+            cbFiles += 128 * 1024;      /* file not found? just guess a size, 128kb. */
+    }
+
+    /*
+     * We'll need an approximation of this formula:
+     * cbPage = (cbPage * cFiles + cbFiles) / 65535;
+     *
+     * Let's do the calculation assuming cbPage being 256.
+     * We'll also add a few files (just for case that any of these were a library),
+     * and a couple extra of bytes too.
+     */
+    cbFiles += cFiles * 4096;
+    cFiles += 64;
+    cbPage = (256 * cFiles + cbFiles) / 65536;
+    for (i = 16; i < cbPage; )
+        i <<= 1;
+
+    if (verbose)
+        printf("calculated page size %d\n", i);
+    return i;
+}
+
 /* Cleanup, for atexit. */
 
@@ -196,7 +252,8 @@
   _wildcard (&argc, &argv);
 
-  /* The default page size is 16. */
-
-  page_size = 16;
+  /* The default page size was 16, now we'll calculate one with 
+     a minimum of 16. */
+
+  page_size = 0;
 
   /* Parse the command line.  First, check for an -p# option. */
@@ -358,4 +415,6 @@
   if (cmd == 'r' || cmd == 'd')
     {
+      if (page_size <= 0)
+        page_size = calc_pagesize(&argv[i], cmd);
       new_lib = omflib_create (new_fname, page_size, error);
       if (new_lib == NULL)
