Index: trunk/src/lbatest/readsect.c
===================================================================
--- trunk/src/lbatest/readsect.c	(revision 106)
+++ trunk/src/lbatest/readsect.c	(revision 107)
@@ -35,4 +35,5 @@
 #include <stddef.h>
 #include <stdlib.h>
+#include <stdarg.h>
 #include <ctype.h>
 #include <string.h>
@@ -48,11 +49,12 @@
 
 int           read_sectors     (char *drv, unsigned long sectors_to_read, 
-                                FILE *fpo);
-
-void          signal_handler   (int sig_no);
+                                unsigned long sectors_per_read, FILE *fpo);
+
+void          log              (char *fmt, ...);
+
 
 /*--- global data -----------------------------------------------------------*/
 
-unsigned long sectors_per_read = SECTORS_PER_READ;
+char *log_fn = NULL;
 
 
@@ -69,4 +71,7 @@
   int ret;
   unsigned long sectors_to_read;
+  unsigned long loop_sectors_start;
+  unsigned long i;
+  unsigned long sectors_per_read = 1;
 
   if (argc < 4) {
@@ -101,5 +106,20 @@
 
   if (argc > 4) {
-    sectors_per_read = (unsigned long) atoi(argv[4]);
+    if (*argv[4] == '-') {
+      /* use a loop from 1 sectors to number specfied */
+      loop_sectors_start = 1;
+      sectors_per_read = (unsigned long) atoi(argv[4] + 1);
+      log("Looping sector count from 1 to %u; not using output file\n",
+             sectors_per_read);
+      if (fpo && fpo != stdin) {
+        fclose(fpo);
+        fpo = NULL;
+      }
+
+    } else {
+      /* just one run */
+      sectors_per_read = (unsigned long) atoi(argv[4]);
+      loop_sectors_start = sectors_per_read;
+    }
     if (sectors_per_read == 0) {
       sectors_per_read = SECTORS_PER_READ;
@@ -107,13 +127,20 @@
   }
 
-  printf("\nreadsect started; using %d sectors per read\n", 
-         (int) sectors_per_read);
+  if (argc > 5) {
+    /* optional log file name */
+    log_fn = argv[5];
+    unlink(log_fn);
+  }
 
   /* go */
-  ret = read_sectors(drv, sectors_to_read, fpo);
-  if (fpo != stdout) {
+  for (i = loop_sectors_start; i <= sectors_per_read; i++) {
+    ret = read_sectors(drv, sectors_to_read, i, fpo);
+  }
+
+  if (fpo && fpo != stdout) {
     fclose(fpo);
   }
 
+  log("\nDone\n");
   return ret;
 
@@ -123,5 +150,6 @@
  * read_sectors() - read sectors and dump to output file
  */
-int read_sectors(char *drv, unsigned long sectors_to_read, FILE *fpo)
+int read_sectors(char *drv, unsigned long sectors_to_read, 
+                 unsigned long sectors_per_read, FILE *fpo)
 {
   unsigned long ret;
@@ -136,7 +164,8 @@
 
   buf = calloc(SECTOR_SIZE, sectors_per_read);
+  log("\nusing %u sectors per read\n", sectors_per_read);
 
   /* open drive */
-  printf("Getting handle for drive %s\n", drv);
+  log("Getting handle for drive %s\n", drv);
   ret = DosOpen(drv, &hf_disk, &action, 0, 0, 
                 OPEN_ACTION_OPEN_IF_EXISTS,
@@ -149,5 +178,4 @@
 
   /* go... */
-  memset(buf, 0x00, sizeof(buf));
   while (sectors_read < sectors_to_read) {
 
@@ -155,4 +183,6 @@
     cb_take = SECTOR_SIZE * sectors_take;
 
+    log("\rReading %u sector(s) (%u sectors read)...",
+           sectors_take, sectors_read);
     ret = DosRead(hf_disk, buf, cb_take, &cb_read);
     if (ret != NO_ERROR) {
@@ -167,17 +197,14 @@
 
     if (cb_read != cb_take) {
-      printf("\n\nRead only %d instead of %d bytes...\n\n", cb_read, cb_take);
+      log("\n\nRead only %u instead of %u bytes...\n\n", cb_read, cb_take);
       break;
     }
 
     sectors_read += cb_read / SECTOR_SIZE;
-    if (fwrite(buf, 1, cb_read, fpo) != cb_read) {
+    if (fpo && fwrite(buf, 1, cb_read, fpo) != cb_read) {
       perror("Failed to write to output file");
       rc = -1;
       break;
     }
-
-    /* progress */
-    printf("\r%d sectors read", sectors_read);
 
   }
@@ -198,5 +225,6 @@
          "physical drive)\n\n"
 	 "Usage:\n\n"
-	 "readsect <drive letter|number> <number of sectors> <outfile> [buffer size]\n\n"
+	 "readsect <drive letter|number> <number of sectors> <outfile>\n"
+         "         [read buffer size] [log file]\n"
          "where:\n"
          "drive letter         drive letter of logical drive to read from\n"
@@ -204,8 +232,33 @@
          "number of sectors    number of sectors to read (e.g. 1024)\n"
          "outfile              path and filename of output file\n"
-         "buffer size          buffer size in number of sectors (512 byte)\n"
-         "                      (default is %d sectors)\n",
+         "read buffer size     optional buffer size in number of sectors (512 byte)\n"
+         "                      (default is %d sectors)\n"
+         "log file             optional log file name\n",
          SECTORS_PER_READ);
 }
 
 
+/*******************************************************************************
+ * log() - log a string to the log file (if any)
+ */
+void log(char *fmt, ...)
+{
+  va_list arglist;
+  FILE *fp;
+  char buf[500];
+
+  /* assemble arguments */
+  va_start(arglist, fmt);
+  vsprintf(buf, fmt, arglist);
+
+  if (log_fn && (fp = fopen(log_fn, "a")) != NULL) {
+    /* write string to log file */
+    fprintf(fp, buf);
+    fflush(fp);
+    fclose(fp);
+  }
+
+  /* write to stdout */
+  printf(buf);
+
+}
