Changeset 107 for trunk/src/lbatest/readsect.c
- Timestamp:
- Jun 8, 2011, 12:28:02 PM (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/lbatest/readsect.c
r106 r107 35 35 #include <stddef.h> 36 36 #include <stdlib.h> 37 #include <stdarg.h> 37 38 #include <ctype.h> 38 39 #include <string.h> … … 48 49 49 50 int 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 53 void log (char *fmt, ...); 54 53 55 54 56 /*--- global data -----------------------------------------------------------*/ 55 57 56 unsigned long sectors_per_read = SECTORS_PER_READ;58 char *log_fn = NULL; 57 59 58 60 … … 69 71 int ret; 70 72 unsigned long sectors_to_read; 73 unsigned long loop_sectors_start; 74 unsigned long i; 75 unsigned long sectors_per_read = 1; 71 76 72 77 if (argc < 4) { … … 101 106 102 107 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 } 104 124 if (sectors_per_read == 0) { 105 125 sectors_per_read = SECTORS_PER_READ; … … 107 127 } 108 128 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 } 111 134 112 135 /* 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) { 115 141 fclose(fpo); 116 142 } 117 143 144 log("\nDone\n"); 118 145 return ret; 119 146 … … 123 150 * read_sectors() - read sectors and dump to output file 124 151 */ 125 int read_sectors(char *drv, unsigned long sectors_to_read, FILE *fpo) 152 int read_sectors(char *drv, unsigned long sectors_to_read, 153 unsigned long sectors_per_read, FILE *fpo) 126 154 { 127 155 unsigned long ret; … … 136 164 137 165 buf = calloc(SECTOR_SIZE, sectors_per_read); 166 log("\nusing %u sectors per read\n", sectors_per_read); 138 167 139 168 /* open drive */ 140 printf("Getting handle for drive %s\n", drv);169 log("Getting handle for drive %s\n", drv); 141 170 ret = DosOpen(drv, &hf_disk, &action, 0, 0, 142 171 OPEN_ACTION_OPEN_IF_EXISTS, … … 149 178 150 179 /* go... */ 151 memset(buf, 0x00, sizeof(buf));152 180 while (sectors_read < sectors_to_read) { 153 181 … … 155 183 cb_take = SECTOR_SIZE * sectors_take; 156 184 185 log("\rReading %u sector(s) (%u sectors read)...", 186 sectors_take, sectors_read); 157 187 ret = DosRead(hf_disk, buf, cb_take, &cb_read); 158 188 if (ret != NO_ERROR) { … … 167 197 168 198 if (cb_read != cb_take) { 169 printf("\n\nRead only %d instead of %dbytes...\n\n", cb_read, cb_take);199 log("\n\nRead only %u instead of %u bytes...\n\n", cb_read, cb_take); 170 200 break; 171 201 } 172 202 173 203 sectors_read += cb_read / SECTOR_SIZE; 174 if (f write(buf, 1, cb_read, fpo) != cb_read) {204 if (fpo && fwrite(buf, 1, cb_read, fpo) != cb_read) { 175 205 perror("Failed to write to output file"); 176 206 rc = -1; 177 207 break; 178 208 } 179 180 /* progress */181 printf("\r%d sectors read", sectors_read);182 209 183 210 } … … 198 225 "physical drive)\n\n" 199 226 "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" 201 229 "where:\n" 202 230 "drive letter drive letter of logical drive to read from\n" … … 204 232 "number of sectors number of sectors to read (e.g. 1024)\n" 205 233 "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", 208 237 SECTORS_PER_READ); 209 238 } 210 239 211 240 241 /******************************************************************************* 242 * log() - log a string to the log file (if any) 243 */ 244 void 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.