Index: /trunk/ippTools/src/pztool.c
===================================================================
--- /trunk/ippTools/src/pztool.c	(revision 15292)
+++ /trunk/ippTools/src/pztool.c	(revision 15293)
@@ -40,5 +40,5 @@
 static bool copydoneCompleteExp(pxConfig *config);
 static psArray *pzGetPendingCameras(pxConfig *config);
-static psArray *pzArrayZip(psArray *arraySet);
+static psArray *pzArrayZip(psArray *arraySet, psS64 limit);
 
 # define MODECASE(caseName, func) \
@@ -322,8 +322,4 @@
     psArray *cameraImfiles = psArrayAlloc(0);
 
-    // the total number of imfiles retreived so far
-    long imfiles = 0;
-    // any slots left over by a query returning less than the per camera limit
-    long leftOvers = 0;
     for (long i = 0; i < psArrayLength(cameras); i++) {
         psString query = pxDataGet("pztool_pendingimfile.sql");
@@ -343,30 +339,12 @@
         }
 
-        long camLimit = 0;
-        // avoid possible div by zero
+        // request the full "limit" from each known camera and throw away any
+        // "extra" rows that we may have after merging the results.  This is
+        // a lot simplier than a complicated scheme (tried that) to attempt to
+        // request on the right number of rows for each camera
+        
+        // treat limit == 0 as "no limit"
         if (limit) {
-            // in the case where the limit asked for is less then the number of
-            // cameras we have, return 1 imfile per camera and we'll stop
-            // making per camera queries when we have reached limit.
-            if (limit < psArrayLength(cameras)) {
-                camLimit = 1;
-            } else {
-                camLimit = limit / psArrayLength(cameras);
-                // add the modulous to the first camera in the list, so if
-                // limit is not even divsable by the number of cameras, the
-                // left overs don't get dropped on the floor
-                if (i == 0) {
-                    camLimit += limit % psArrayLength(cameras);
-                }
-            }
-        }
-
-        // treat limit == 0 as "no limit"
-        if (camLimit) {
-            // pickup the left overs from the prevous camera
-            camLimit += leftOvers;
-
-            // divide limit by the number of cameras
-            psString limitString = psDBGenerateLimitSQL(camLimit);
+            psString limitString = psDBGenerateLimitSQL(limit);
             psStringAppend(&query, " %s", limitString);
             psFree(limitString);
@@ -393,25 +371,11 @@
         }
 
-        // if we got less rows then the limit for this camera, give the extra
-        // slots to the next camera.
-        if (camLimit) {
-            leftOvers = camLimit - psArrayLength(result);
-        }
-
-        // update the total count of imfiles fetched
-        imfiles += psArrayLength(result);
-
         // add this query into the array of result set
         psArrayAdd(cameraImfiles, 0, result);
         psFree(result);
-
-        // check to see if we've retreived enough imfiles
-        if (imfiles >= limit) {
-            break;
-        }
     }
 
     // stitch the arrays of imfiles together
-    psArray *output = pzArrayZip(cameraImfiles);
+    psArray *output = pzArrayZip(cameraImfiles, limit);
     psFree(cameraImfiles);
 
@@ -740,21 +704,33 @@
 }
 
-static psArray *pzArrayZip(psArray *arraySet)
-{
+static psArray *pzArrayZip(psArray *arraySet, psS64 limit)
+{
+    // figure out the combined size of all arrays in the set
     long setSize = 0;
-
-    // figure out the size of the combined arrays
     for (long i = 0; i < psArrayLength(arraySet); i++) {
         setSize += psArrayLength(arraySet->data[i]);
     }
 
-    psArray *output = psArrayAllocEmpty(setSize);
+    // treat 0 as "no limit"
+    if (limit == 0) {
+        limit = setSize;
+    }
+
+    psArray *output = psArrayAllocEmpty(limit);
     // loop over each array in the set forever
-    long index = 0; // the depth into each array
-    long counter; // counter - the total number of elements zipped so far
-    long i; // i - which array in the set 
-    for (counter = 0, i = 0;
-            (counter < setSize) && (i < psArrayLength(arraySet));
-            counter++, ++i, i = i % psArrayLength(arraySet), i % psArrayLength(arraySet) ? : ++index) {
+    for (
+            // init
+            long counter = 0,   // the total number of elements zipped so far
+            i = 0,              // which array in the set 
+            index = 0;          // the depth into each array
+            // test
+            (counter < setSize)
+            && (counter < limit)
+            && (i < psArrayLength(arraySet));
+            // incr
+            counter++, ++i,
+            i = i % psArrayLength(arraySet),
+            i % psArrayLength(arraySet) ? : ++index
+        ) {
 
         psArray *array = arraySet->data[i];
