fdread.c (79110) | fdread.c (139905) |
---|---|
1/* 2 * Copyright (c) 2001 Joerg Wunsch 3 * 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: --- 9 unchanged lines hidden (view full) --- 18 * IN NO EVENT SHALL THE DEVELOPERS BE LIABLE FOR ANY DIRECT, INDIRECT, 19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 * | 1/* 2 * Copyright (c) 2001 Joerg Wunsch 3 * 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: --- 9 unchanged lines hidden (view full) --- 18 * IN NO EVENT SHALL THE DEVELOPERS BE LIABLE FOR ANY DIRECT, INDIRECT, 19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 * |
26 * $FreeBSD: head/usr.sbin/fdread/fdread.c 79110 2001-07-02 21:21:58Z joerg $ | 26 * $FreeBSD: head/usr.sbin/fdread/fdread.c 139905 2005-01-08 15:46:06Z delphij $ |
27 */ 28 29#include <sys/types.h> 30#include <sys/stat.h> 31#include <sys/fdcio.h> 32 33#include <err.h> 34#include <errno.h> --- 7 unchanged lines hidden (view full) --- 42 43#include <dev/ic/nec765.h> 44 45#include "fdutil.h" 46 47int quiet, recover; 48unsigned char fillbyte = 0xf0; /* "foo" */ 49 | 27 */ 28 29#include <sys/types.h> 30#include <sys/stat.h> 31#include <sys/fdcio.h> 32 33#include <err.h> 34#include <errno.h> --- 7 unchanged lines hidden (view full) --- 42 43#include <dev/ic/nec765.h> 44 45#include "fdutil.h" 46 47int quiet, recover; 48unsigned char fillbyte = 0xf0; /* "foo" */ 49 |
50int doread(int fd, FILE *of, const char *devname); | 50int doread(int fd, FILE *of, const char *_devname); |
51int doreadid(int fd, unsigned int numids, unsigned int trackno); 52void usage(void); 53 54void 55usage(void) 56{ 57 58 errx(EX_USAGE, 59 "usage: fdread [-qr] [-d device] [-f fillbyte]\n" 60 " fdread [-d device] -I numids [-t trackno]"); 61} 62 63 64int 65main(int argc, char **argv) 66{ 67 int c, errs = 0; 68 unsigned int numids = 0, trackno = 0; | 51int doreadid(int fd, unsigned int numids, unsigned int trackno); 52void usage(void); 53 54void 55usage(void) 56{ 57 58 errx(EX_USAGE, 59 "usage: fdread [-qr] [-d device] [-f fillbyte]\n" 60 " fdread [-d device] -I numids [-t trackno]"); 61} 62 63 64int 65main(int argc, char **argv) 66{ 67 int c, errs = 0; 68 unsigned int numids = 0, trackno = 0; |
69 const char *fname = 0, *devname = "/dev/fd0"; | 69 const char *fname = 0, *_devname = "/dev/fd0"; |
70 char *cp; 71 FILE *of = stdout; 72 int fd; 73 unsigned long ul; 74 75 while ((c = getopt(argc, argv, "d:f:I:o:qrt:")) != -1) 76 switch (c) { 77 case 'd': | 70 char *cp; 71 FILE *of = stdout; 72 int fd; 73 unsigned long ul; 74 75 while ((c = getopt(argc, argv, "d:f:I:o:qrt:")) != -1) 76 switch (c) { 77 case 'd': |
78 devname = optarg; | 78 _devname = optarg; |
79 break; 80 81 case 'f': 82 ul = strtoul(optarg, &cp, 0); 83 if (*cp != '\0') { 84 fprintf(stderr, 85 "Bad argument %s to -f option; must be numeric\n", 86 optarg); --- 57 unchanged lines hidden (view full) --- 144 usage(); 145 } 146 147 if (fname) { 148 if ((of = fopen(fname, "w")) == NULL) 149 err(EX_OSERR, "cannot create output file %s", fname); 150 } 151 | 79 break; 80 81 case 'f': 82 ul = strtoul(optarg, &cp, 0); 83 if (*cp != '\0') { 84 fprintf(stderr, 85 "Bad argument %s to -f option; must be numeric\n", 86 optarg); --- 57 unchanged lines hidden (view full) --- 144 usage(); 145 } 146 147 if (fname) { 148 if ((of = fopen(fname, "w")) == NULL) 149 err(EX_OSERR, "cannot create output file %s", fname); 150 } 151 |
152 if ((fd = open(devname, O_RDONLY)) == -1) 153 err(EX_OSERR, "cannot open device %s", devname); | 152 if ((fd = open(_devname, O_RDONLY)) == -1) 153 err(EX_OSERR, "cannot open device %s", _devname); |
154 | 154 |
155 return (numids? doreadid(fd, numids, trackno): doread(fd, of, devname)); | 155 return (numids? doreadid(fd, numids, trackno): doread(fd, of, _devname)); |
156} 157 158int | 156} 157 158int |
159doread(int fd, FILE *of, const char *devname) | 159doread(int fd, FILE *of, const char *_devname) |
160{ 161 char *trackbuf; 162 int rv, fdopts, recoverable, nerrs = 0; 163 unsigned int nbytes, tracksize, mediasize, secsize, n; 164 struct fdc_status fdcs; 165 struct fd_type fdt; 166 167 if (ioctl(fd, FD_GTYPE, &fdt) == -1) --- 5 unchanged lines hidden (view full) --- 173 secsize = 128 << fdt.secsize; 174 tracksize = fdt.sectrac * secsize; 175 mediasize = tracksize * fdt.tracks * fdt.heads; 176 if ((trackbuf = malloc(tracksize)) == 0) 177 errx(EX_TEMPFAIL, "out of memory"); 178 179 if (!quiet) 180 fprintf(stderr, "Reading %d * %d * %d * %d medium at %s\n", | 160{ 161 char *trackbuf; 162 int rv, fdopts, recoverable, nerrs = 0; 163 unsigned int nbytes, tracksize, mediasize, secsize, n; 164 struct fdc_status fdcs; 165 struct fd_type fdt; 166 167 if (ioctl(fd, FD_GTYPE, &fdt) == -1) --- 5 unchanged lines hidden (view full) --- 173 secsize = 128 << fdt.secsize; 174 tracksize = fdt.sectrac * secsize; 175 mediasize = tracksize * fdt.tracks * fdt.heads; 176 if ((trackbuf = malloc(tracksize)) == 0) 177 errx(EX_TEMPFAIL, "out of memory"); 178 179 if (!quiet) 180 fprintf(stderr, "Reading %d * %d * %d * %d medium at %s\n", |
181 fdt.tracks, fdt.heads, fdt.sectrac, secsize, devname); | 181 fdt.tracks, fdt.heads, fdt.sectrac, secsize, _devname); |
182 183 for (nbytes = 0; nbytes < mediasize;) { 184 if (lseek(fd, nbytes, SEEK_SET) != nbytes) 185 err(EX_OSERR, "cannot lseek()"); 186 rv = read(fd, trackbuf, tracksize); 187 if (rv == 0) { 188 /* EOF? */ 189 warnx("premature EOF after %u bytes", nbytes); 190 return (EX_OK); 191 } | 182 183 for (nbytes = 0; nbytes < mediasize;) { 184 if (lseek(fd, nbytes, SEEK_SET) != nbytes) 185 err(EX_OSERR, "cannot lseek()"); 186 rv = read(fd, trackbuf, tracksize); 187 if (rv == 0) { 188 /* EOF? */ 189 warnx("premature EOF after %u bytes", nbytes); 190 return (EX_OK); 191 } |
192 if (rv == tracksize) { | 192 if ((unsigned)rv == tracksize) { |
193 nbytes += rv; 194 if (!quiet) 195 fprintf(stderr, "%5d KB\r", nbytes / 1024); 196 fwrite(trackbuf, sizeof(unsigned char), rv, of); 197 fflush(of); 198 continue; 199 } | 193 nbytes += rv; 194 if (!quiet) 195 fprintf(stderr, "%5d KB\r", nbytes / 1024); 196 fwrite(trackbuf, sizeof(unsigned char), rv, of); 197 fflush(of); 198 continue; 199 } |
200 if (rv < tracksize) { 201 /* should not happen */ 202 nbytes += rv; 203 if (!quiet) 204 fprintf(stderr, "\nshort after %5d KB\r", 205 nbytes / 1024); 206 fwrite(trackbuf, sizeof(unsigned char), rv, of); 207 fflush(of); 208 continue; 209 } | |
210 if (rv == -1) { 211 /* fall back reading one sector at a time */ 212 for (n = 0; n < tracksize; n += secsize) { 213 if (lseek(fd, nbytes, SEEK_SET) != nbytes) 214 err(EX_OSERR, "cannot lseek()"); 215 rv = read(fd, trackbuf, secsize); | 200 if (rv == -1) { 201 /* fall back reading one sector at a time */ 202 for (n = 0; n < tracksize; n += secsize) { 203 if (lseek(fd, nbytes, SEEK_SET) != nbytes) 204 err(EX_OSERR, "cannot lseek()"); 205 rv = read(fd, trackbuf, secsize); |
216 if (rv == secsize) { | 206 if ((unsigned) rv == secsize) { |
217 nbytes += rv; 218 if (!quiet) 219 fprintf(stderr, "%5d KB\r", 220 nbytes / 1024); 221 fwrite(trackbuf, sizeof(unsigned char), 222 rv, of); 223 fflush(of); 224 continue; --- 27 unchanged lines hidden (view full) --- 252 if (recoverable) { 253 fdopts |= FDOPT_NOERROR; 254 if (ioctl(fd, FD_SOPTS, 255 &fdopts) == -1) 256 err(EX_OSERR, 257 "ioctl(fd, FD_SOPTS, FDOPT_NOERROR)"); 258 rv = read(fd, trackbuf, 259 secsize); | 207 nbytes += rv; 208 if (!quiet) 209 fprintf(stderr, "%5d KB\r", 210 nbytes / 1024); 211 fwrite(trackbuf, sizeof(unsigned char), 212 rv, of); 213 fflush(of); 214 continue; --- 27 unchanged lines hidden (view full) --- 242 if (recoverable) { 243 fdopts |= FDOPT_NOERROR; 244 if (ioctl(fd, FD_SOPTS, 245 &fdopts) == -1) 246 err(EX_OSERR, 247 "ioctl(fd, FD_SOPTS, FDOPT_NOERROR)"); 248 rv = read(fd, trackbuf, 249 secsize); |
260 if (rv != secsize) | 250 if ((unsigned)rv != secsize) |
261 err(EX_IOERR, 262 "read() with FDOPT_NOERROR still fails"); 263 fdopts &= ~FDOPT_NOERROR; 264 (void)ioctl(fd, FD_SOPTS, 265 &fdopts); 266 } 267 if (!quiet) { 268 if (recoverable) --- 12 unchanged lines hidden (view full) --- 281 secsize, of); 282 fflush(of); 283 continue; 284 } 285 errx(EX_OSERR, "unexpected read() result: %d", 286 rv); 287 } 288 } | 251 err(EX_IOERR, 252 "read() with FDOPT_NOERROR still fails"); 253 fdopts &= ~FDOPT_NOERROR; 254 (void)ioctl(fd, FD_SOPTS, 255 &fdopts); 256 } 257 if (!quiet) { 258 if (recoverable) --- 12 unchanged lines hidden (view full) --- 271 secsize, of); 272 fflush(of); 273 continue; 274 } 275 errx(EX_OSERR, "unexpected read() result: %d", 276 rv); 277 } 278 } |
279 if ((unsigned)rv < tracksize) { 280 /* should not happen */ 281 nbytes += rv; 282 if (!quiet) 283 fprintf(stderr, "\nshort after %5d KB\r", 284 nbytes / 1024); 285 fwrite(trackbuf, sizeof(unsigned char), rv, of); 286 fflush(of); 287 continue; 288 } |
|
289 } 290 if (!quiet) { 291 putc('\n', stderr); 292 if (nerrs) 293 fprintf(stderr, "%d error%s\n", 294 nerrs, nerrs > 1? "s": ""); 295 } 296 --- 41 unchanged lines hidden --- | 289 } 290 if (!quiet) { 291 putc('\n', stderr); 292 if (nerrs) 293 fprintf(stderr, "%d error%s\n", 294 nerrs, nerrs > 1? "s": ""); 295 } 296 --- 41 unchanged lines hidden --- |