source: trunk/src/lbatest/readsect.c@ 104

Last change on this file since 104 was 104, checked in by Markus Thielen, 14 years ago

added readtest.c (sector read program)

File size: 5.1 KB
Line 
1/******************************************************************************
2
3 readsect.c - simple (dumb) HD sector read program
4
5 Author: Markus Thielen
6
7 Compilation (Watcom): wcl386 -bt=os2 readsect.c
8
9 Copyright (c) 2010 by thi.guten Software development, www.thiguten.de
10
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2 of the License, or
14 (at your option) any later version.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24
25******************************************************************************/
26
27/*---- includes -------------------------------------------------------------*/
28
29#define INCL_DOS
30#define INCL_ERRORS
31#define INCL_DOSDEVICES
32#define INCL_DOSDEVIOCTL
33#include <os2.h>
34#include <stdio.h>
35#include <stddef.h>
36#include <stdlib.h>
37#include <ctype.h>
38#include <string.h>
39#include <errno.h>
40
41#define SECTOR_SIZE 512
42#define SECTORS_PER_READ 200
43
44
45/*--- function prototypes ---------------------------------------------------*/
46
47void usage (void);
48
49int read_sectors (char *drv, unsigned long sectors_to_read,
50 FILE *fpo);
51
52void signal_handler (int sig_no);
53
54/*--- global data -----------------------------------------------------------*/
55
56
57/*--- start of code ---------------------------------------------------------*/
58
59/******************************************************************************
60 * main()
61 */
62int main(int argc, char **argv)
63{
64 HFILE hf;
65 char drv[50];
66 char *output;
67 FILE *fpo; /* output file */
68 int ret;
69 unsigned long sectors_to_read;
70
71 if (argc < 4) {
72 usage();
73 return -1;
74 }
75
76 if (isalpha(argv[1][0])) {
77 sprintf(drv, "\\\\.\\%c", toupper(argv[1][0]));
78 } else if (isdigit(argv[1][0])) {
79 sprintf(drv, "\\\\.\\Physical_Disk%c", argv[1][0]);
80 } else {
81 usage();
82 return -1;
83 }
84
85 /* get sectors to read */
86 sectors_to_read = strtoul(argv[2], NULL, 10);
87
88 /* get output file */
89 output = argv[3];
90 if (*output == '-' && output[1] == 0) {
91 /* use stdout */
92 fpo = stdout;
93 } else {
94 fpo = fopen(output, "wb");
95 if (!fpo) {
96 perror("Failed to open output file");
97 return(-1);
98 }
99 }
100
101 /* go */
102 ret = read_sectors(drv, sectors_to_read, fpo);
103 if (fpo != stdout) {
104 fclose(fpo);
105 }
106
107 return ret;
108
109}
110
111/******************************************************************************
112 * read_sectors() - read sectors and dump to output file
113 */
114int read_sectors(char *drv, unsigned long sectors_to_read, FILE *fpo)
115{
116 unsigned long ret;
117 HFILE hf_disk;
118 unsigned long action;
119 unsigned long cb_read;
120 char *buf;
121 int rc = 0;
122 unsigned long cb_take;
123 unsigned long sectors_take;
124 unsigned long sectors_read = 0;
125
126 buf = calloc(SECTOR_SIZE, SECTORS_PER_READ);
127
128 /* open drive */
129 printf("Getting handle for drive %s\n", drv);
130 ret = DosOpen(drv, &hf_disk, &action, 0, 0,
131 OPEN_ACTION_OPEN_IF_EXISTS,
132 OPEN_SHARE_DENYNONE | OPEN_ACCESS_READONLY,
133 NULL);
134 if (ret != NO_ERROR) {
135 fprintf(stderr, "Failed to open disk %s for reading: %d\n", drv, ret);
136 return -1;
137 }
138
139 /* go... */
140 memset(buf, 0x00, sizeof(buf));
141 while (sectors_read < sectors_to_read) {
142
143 sectors_take = min(sectors_to_read - sectors_read, SECTORS_PER_READ);
144 cb_take = SECTOR_SIZE * sectors_take;
145
146 ret = DosRead(hf_disk, buf, cb_take, &cb_read);
147 if (ret != NO_ERROR) {
148 fprintf(stderr, "\nFailed to read from disk %s, code %d\n", drv, ret);
149 rc = -1;
150 break;
151 }
152
153 if (cb_read == 0) {
154 break;
155 }
156
157 if (cb_read != cb_take) {
158 printf("\n\nRead only %d instead of %d bytes...\n\n", cb_read, cb_take);
159 break;
160 }
161
162 sectors_read += cb_read / SECTOR_SIZE;
163 if (fwrite(buf, 1, cb_read, fpo) != cb_read) {
164 perror("Failed to write to output file");
165 rc = -1;
166 break;
167 }
168
169 /* progress */
170 printf("\r%d sectors read", sectors_read);
171
172 }
173
174 DosClose(hf_disk);
175 free(buf);
176
177 return rc;
178}
179
180/******************************************************************************
181 * usage() - print usage summary to STDOUT
182 */
183void usage(void)
184{
185 printf("readsect.exe - read HD sectors and store them to an output file.\n"
186 "Call with a drive letter (for logical drive) or 1-based disk number (for\n"
187 "physical drive)\n\n"
188 "Usage:\n\n"
189 "lbatest <drive letter|drive number> <number of sectors to read> "
190 "<output file>\n\n");
191}
192
193
Note: See TracBrowser for help on using the repository browser.