Index: trunk/src/lbatest/readsect.c
===================================================================
--- trunk/src/lbatest/readsect.c	(revision 108)
+++ trunk/src/lbatest/readsect.c	(revision 109)
@@ -1,9 +1,10 @@
 /******************************************************************************
 
-  readsect.c - simple (dumb) HD sector read program
+  readsect.c - simple (dumb) HD sector read and test program
 
   Author: Markus Thielen
 
   Compilation (Watcom): wcl386 -bt=os2 readsect.c
+                        wcl386 -bt=os2 -d3 readsect.c (for debug)
 
   Copyright (c) 2010 by thi.guten Software development, www.thiguten.de
@@ -33,4 +34,5 @@
 #include <os2.h>
 #include <stdio.h>
+#include <io.h>
 #include <stddef.h>
 #include <stdlib.h>
@@ -57,4 +59,5 @@
 
 char *log_fn = NULL;
+unsigned long buffer_offset = 0;
 
 
@@ -68,10 +71,11 @@
   char drv[50];
   char *output;
-  FILE *fpo; /* output file */
+  FILE *fpo = NULL; /* output file */
   int ret;
-  unsigned long sectors_to_read;
-  unsigned long loop_sectors_start;
+  unsigned long sectors_to_read = 1024;
   unsigned long i;
-  unsigned long sectors_per_read = 1;
+  unsigned long sectors_per_read = SECTORS_PER_READ;
+  unsigned long loop_sectors_start = sectors_per_read;
+  int a;
 
   if (argc < 4) {
@@ -80,55 +84,80 @@
   }
 
-  if (isalpha(argv[1][0])) {
-    sprintf(drv, "\\\\.\\%c", toupper(argv[1][0]));
-  } else if (isdigit(argv[1][0])) {
-    sprintf(drv, "\\\\.\\Physical_Disk%c", argv[1][0]);
-  } else {
-    usage();
-    return -1;
-  }
-
-  /* get sectors to read */
-  sectors_to_read = strtoul(argv[2], NULL, 10);
-
-  /* get output file */
-  output = argv[3];
-  if (*output == '-' && output[1] == 0) {
-    /* use stdout */
-    fpo = stdout;
-  } else {
-    fpo = fopen(output, "wb");
-    if (!fpo) {
-      perror("Failed to open output file");
-      return(-1);
-    }
-  }
-
-  if (argc > 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;
-    }
-  }
-
-  if (argc > 5) {
-    /* optional log file name */
-    log_fn = argv[5];
-    unlink(log_fn);
+  /* parse args */
+  for (a = 1; a < argc; a++) {
+
+    if (argv[a][0] != '-' && argv[a][0] != '/') {
+      fprintf(stderr, "unknown argument %s - aborting\n", argv[a]);
+      return (-1);
+    }
+
+    switch (tolower(argv[a][1])) {
+
+    case 'd':
+      /* drive spec */
+      if (isalpha(argv[++a][0])) {
+        sprintf(drv, "\\\\.\\%c", toupper(argv[a][0]));
+      } else if (isdigit(argv[a][0])) {
+        sprintf(drv, "\\\\.\\Physical_Disk%c", argv[a][0]);
+      } else {
+        usage();
+        return -1;
+      }
+      break;
+
+    case 's':
+      /* get sectors to read */
+      sectors_to_read = strtoul(argv[++a], NULL, 10);
+      break;
+
+    case 'o':
+      /* get output file */
+      output = argv[++a];
+      if (*output == '-' && output[1] == 0) {
+        /* use stdout */
+        fpo = stdout;
+      } else {
+        fpo = fopen(output, "wb");
+        if (!fpo) {
+          perror("Failed to open output file");
+          return(-1);
+        }
+      }
+      break;
+
+    case 'b':
+      /* optional buffer size */
+      if (*argv[++a] == '-') {
+        /* use a loop from 1 sectors to number specfied */
+        loop_sectors_start = 1;
+        sectors_per_read = (unsigned long) atoi(argv[a] + 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[a]);
+        loop_sectors_start = sectors_per_read;
+      }
+      if (sectors_per_read == 0) {
+        sectors_per_read = SECTORS_PER_READ;
+      }
+      break;
+
+    case 'l':
+      /* optional log file name */
+      log_fn = argv[++a];
+      unlink(log_fn);
+      break;
+
+    case 'u':
+      /* use unaligned read buffer */
+      buffer_offset = 1;
+      break;
+    }
   }
 
@@ -145,5 +174,5 @@
   return ret;
 
-}
+  }
 
 /******************************************************************************
@@ -163,6 +192,8 @@
   unsigned long sectors_read = 0;
 
-  buf = calloc(SECTOR_SIZE, sectors_per_read);
+  buf = malloc(SECTOR_SIZE * sectors_per_read + buffer_offset);
   log("\nusing %u sectors per read\n", sectors_per_read);
+  log("read buffer address: 0x%u %s\n", buf + buffer_offset, 
+      buffer_offset ? "(unaligned)" : "");
 
   /* open drive */
@@ -183,7 +214,7 @@
     cb_take = SECTOR_SIZE * sectors_take;
 
-    log("\rReading %u sector(s) (%u sectors read)...",
+    log("\rReading %u sector(s) this run; (%u sectors read so far)...",
            sectors_take, sectors_read);
-    ret = DosRead(hf_disk, buf, cb_take, &cb_read);
+    ret = DosRead(hf_disk, buf + buffer_offset, cb_take, &cb_read);
     if (ret != NO_ERROR) {
       fprintf(stderr, "\nFailed to read from disk %s, code %d\n", drv, ret);
@@ -202,5 +233,5 @@
 
     sectors_read += cb_read / SECTOR_SIZE;
-    if (fpo && fwrite(buf, 1, cb_read, fpo) != cb_read) {
+    if (fpo && fwrite(buf + buffer_offset, 1, cb_read, fpo) != cb_read) {
       perror("Failed to write to output file");
       rc = -1;
@@ -209,4 +240,6 @@
 
   }
+
+  log("\n%u sectors read.\n", sectors_read);
 
   DosClose(hf_disk);
@@ -226,13 +259,14 @@
 	 "Usage:\n\n"
 	 "readsect <drive letter|number> <number of sectors> <outfile>\n"
-         "         [read buffer size] [log file]\n"
+         "         [<read buffer size>] [-lf <log file>] [-unaligned]\n"
          "where:\n"
-         "drive letter         drive letter of logical drive to read from\n"
-         "drive number         1-based physical disk number to read from\n"
-         "number of sectors    number of sectors to read (e.g. 1024)\n"
-         "outfile              path and filename of output file\n"
-         "read buffer size     optional buffer size in number of sectors (512 byte)\n"
+         "-d drive letter       drive letter of logical drive to read from\n"
+         "   drive number       1-based physical disk number to read from\n"
+         "-s number of sectors  number of sectors to read (e.g. 1024)\n"
+         "-o outfile            path and filename of output file\n"
+         "-b 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",
+         "-l log file           optional log file name\n"
+         "-u                    use unaligned buffer\n",
          SECTORS_PER_READ);
 }
