Changeset 107 for trunk


Ignore:
Timestamp:
Jun 8, 2011, 12:28:02 PM (14 years ago)
Author:
Markus Thielen
Message:

added incremental buffer size (loop) to readsect; added logging to readsect

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/lbatest/readsect.c

    r106 r107  
    3535#include <stddef.h>
    3636#include <stdlib.h>
     37#include <stdarg.h>
    3738#include <ctype.h>
    3839#include <string.h>
     
    4849
    4950int           read_sectors     (char *drv, unsigned long sectors_to_read,
    50                                 FILE *fpo);
    51 
    52 void          signal_handler   (int sig_no);
     51                                unsigned long sectors_per_read, FILE *fpo);
     52
     53void          log              (char *fmt, ...);
     54
    5355
    5456/*--- global data -----------------------------------------------------------*/
    5557
    56 unsigned long sectors_per_read = SECTORS_PER_READ;
     58char *log_fn = NULL;
    5759
    5860
     
    6971  int ret;
    7072  unsigned long sectors_to_read;
     73  unsigned long loop_sectors_start;
     74  unsigned long i;
     75  unsigned long sectors_per_read = 1;
    7176
    7277  if (argc < 4) {
     
    101106
    102107  if (argc > 4) {
    103     sectors_per_read = (unsigned long) atoi(argv[4]);
     108    if (*argv[4] == '-') {
     109      /* use a loop from 1 sectors to number specfied */
     110      loop_sectors_start = 1;
     111      sectors_per_read = (unsigned long) atoi(argv[4] + 1);
     112      log("Looping sector count from 1 to %u; not using output file\n",
     113             sectors_per_read);
     114      if (fpo && fpo != stdin) {
     115        fclose(fpo);
     116        fpo = NULL;
     117      }
     118
     119    } else {
     120      /* just one run */
     121      sectors_per_read = (unsigned long) atoi(argv[4]);
     122      loop_sectors_start = sectors_per_read;
     123    }
    104124    if (sectors_per_read == 0) {
    105125      sectors_per_read = SECTORS_PER_READ;
     
    107127  }
    108128
    109   printf("\nreadsect started; using %d sectors per read\n",
    110          (int) sectors_per_read);
     129  if (argc > 5) {
     130    /* optional log file name */
     131    log_fn = argv[5];
     132    unlink(log_fn);
     133  }
    111134
    112135  /* go */
    113   ret = read_sectors(drv, sectors_to_read, fpo);
    114   if (fpo != stdout) {
     136  for (i = loop_sectors_start; i <= sectors_per_read; i++) {
     137    ret = read_sectors(drv, sectors_to_read, i, fpo);
     138  }
     139
     140  if (fpo && fpo != stdout) {
    115141    fclose(fpo);
    116142  }
    117143
     144  log("\nDone\n");
    118145  return ret;
    119146
     
    123150 * read_sectors() - read sectors and dump to output file
    124151 */
    125 int read_sectors(char *drv, unsigned long sectors_to_read, FILE *fpo)
     152int read_sectors(char *drv, unsigned long sectors_to_read,
     153                 unsigned long sectors_per_read, FILE *fpo)
    126154{
    127155  unsigned long ret;
     
    136164
    137165  buf = calloc(SECTOR_SIZE, sectors_per_read);
     166  log("\nusing %u sectors per read\n", sectors_per_read);
    138167
    139168  /* open drive */
    140   printf("Getting handle for drive %s\n", drv);
     169  log("Getting handle for drive %s\n", drv);
    141170  ret = DosOpen(drv, &hf_disk, &action, 0, 0,
    142171                OPEN_ACTION_OPEN_IF_EXISTS,
     
    149178
    150179  /* go... */
    151   memset(buf, 0x00, sizeof(buf));
    152180  while (sectors_read < sectors_to_read) {
    153181
     
    155183    cb_take = SECTOR_SIZE * sectors_take;
    156184
     185    log("\rReading %u sector(s) (%u sectors read)...",
     186           sectors_take, sectors_read);
    157187    ret = DosRead(hf_disk, buf, cb_take, &cb_read);
    158188    if (ret != NO_ERROR) {
     
    167197
    168198    if (cb_read != cb_take) {
    169       printf("\n\nRead only %d instead of %d bytes...\n\n", cb_read, cb_take);
     199      log("\n\nRead only %u instead of %u bytes...\n\n", cb_read, cb_take);
    170200      break;
    171201    }
    172202
    173203    sectors_read += cb_read / SECTOR_SIZE;
    174     if (fwrite(buf, 1, cb_read, fpo) != cb_read) {
     204    if (fpo && fwrite(buf, 1, cb_read, fpo) != cb_read) {
    175205      perror("Failed to write to output file");
    176206      rc = -1;
    177207      break;
    178208    }
    179 
    180     /* progress */
    181     printf("\r%d sectors read", sectors_read);
    182209
    183210  }
     
    198225         "physical drive)\n\n"
    199226         "Usage:\n\n"
    200          "readsect <drive letter|number> <number of sectors> <outfile> [buffer size]\n\n"
     227         "readsect <drive letter|number> <number of sectors> <outfile>\n"
     228         "         [read buffer size] [log file]\n"
    201229         "where:\n"
    202230         "drive letter         drive letter of logical drive to read from\n"
     
    204232         "number of sectors    number of sectors to read (e.g. 1024)\n"
    205233         "outfile              path and filename of output file\n"
    206          "buffer size          buffer size in number of sectors (512 byte)\n"
    207          "                      (default is %d sectors)\n",
     234         "read buffer size     optional buffer size in number of sectors (512 byte)\n"
     235         "                      (default is %d sectors)\n"
     236         "log file             optional log file name\n",
    208237         SECTORS_PER_READ);
    209238}
    210239
    211240
     241/*******************************************************************************
     242 * log() - log a string to the log file (if any)
     243 */
     244void log(char *fmt, ...)
     245{
     246  va_list arglist;
     247  FILE *fp;
     248  char buf[500];
     249
     250  /* assemble arguments */
     251  va_start(arglist, fmt);
     252  vsprintf(buf, fmt, arglist);
     253
     254  if (log_fn && (fp = fopen(log_fn, "a")) != NULL) {
     255    /* write string to log file */
     256    fprintf(fp, buf);
     257    fflush(fp);
     258    fclose(fp);
     259  }
     260
     261  /* write to stdout */
     262  printf(buf);
     263
     264}
Note: See TracChangeset for help on using the changeset viewer.