diff options
author | Miod Vallat <miod@cvs.openbsd.org> | 2002-06-10 00:40:54 +0000 |
---|---|---|
committer | Miod Vallat <miod@cvs.openbsd.org> | 2002-06-10 00:40:54 +0000 |
commit | 19628eff0b20fb6e8c57b1a96d9866f0f0f88ca7 (patch) | |
tree | c7a7f51936fcae498327da8db059168c9dda2e96 /usr.sbin/bad144 | |
parent | 8c913293005db33c671612618dda7a754cf05ff8 (diff) |
Switch to modern (post 4.xBSD) world, with changes lying in my tree for a
lot of time:
- use getopt()
- use err() and warn()
- NBT_BAD instead of 126
- remove the vax-only parts since this is only build on i386. They can
come back later whenever necessary.
Diffstat (limited to 'usr.sbin/bad144')
-rw-r--r-- | usr.sbin/bad144/bad144.c | 250 |
1 files changed, 102 insertions, 148 deletions
diff --git a/usr.sbin/bad144/bad144.c b/usr.sbin/bad144/bad144.c index 5a19c907e5a..d49cfa407b6 100644 --- a/usr.sbin/bad144/bad144.c +++ b/usr.sbin/bad144/bad144.c @@ -1,3 +1,4 @@ +/* $OpenBSD: bad144.c,v 1.14 2002/06/10 00:40:53 miod Exp $ */ /* * Copyright (c) 1980, 1986, 1988, 1993 * The Regents of the University of California. All rights reserved. @@ -39,7 +40,7 @@ static char copyright[] = #ifndef lint /*static char sccsid[] = "from: @(#)bad144.c 8.1 (Berkeley) 6/6/93";*/ -static char *rcsid = "$Id: bad144.c,v 1.13 2002/06/09 02:38:11 itojun Exp $"; +static char *rcsid = "$Id: bad144.c,v 1.14 2002/06/10 00:40:53 miod Exp $"; #endif /* not lint */ /* @@ -68,10 +69,11 @@ static char *rcsid = "$Id: bad144.c,v 1.13 2002/06/09 02:38:11 itojun Exp $"; #include <stdio.h> #include <paths.h> #include <string.h> +#include <err.h> #define RETRIES 10 /* number of retries on reading old sectors */ -int fflag, add, copy, verbose, nflag; +int add, copy, verbose, nflag; int dups; int badfile = -1; /* copy of badsector table to use, -1 if any */ #define MAXSECSIZE 1024 @@ -91,6 +93,7 @@ int checkold(void); int compare(const void *, const void *); daddr_t getold(int, struct dkbad *); void shift(int, int, int); +void usage(void); int main(argc, argv) @@ -98,57 +101,38 @@ main(argc, argv) char *argv[]; { struct bt_bad *bt; - daddr_t sn, bn[126]; - int i, f, nbad, new, bad, errs; - - argc--, argv++; - while (argc > 0 && **argv == '-') { - (*argv)++; - while (**argv) { - switch (**argv) { -#if __vax__ - case 'f': - fflag++; - break; -#endif - case 'a': - add++; - break; - case 'c': - copy++; - break; - case 'v': - verbose++; - break; - case 'n': - nflag++; - verbose++; - break; - default: - if (**argv >= '0' && **argv <= '4') { - badfile = **argv - '0'; - break; - } - goto usage; - } - (*argv)++; + daddr_t sn, bn[NBT_BAD]; + int i, f, nbad, new, bad, errs, ch; + + while ((ch = getopt(argc, argv, "acnv01234")) != -1) { + switch(ch) { + case 'a': + add = 1; + break; + case 'c': + copy = 1; + break; + case 'n': + nflag = 1; + /* FALLTHROUGH */ + case 'v': + verbose = 1; + break; + case '0': + case '1': + case '2': + case '3': + case '4': + badfile = ch - '0'; + break; } - argc--, argv++; - } - if (argc < 1) { -usage: - fprintf(stderr, - "usage: bad144 [ -f ] disk [ snum [ bn ... ] ]\n"); - fprintf(stderr, - "to read or overwrite bad-sector table, e.g.: bad144 hp0\n"); - fprintf(stderr, - "or bad144 -a [ -f ] [ -c ] disk bn ...\n"); - fprintf(stderr, "where options are:\n"); - fprintf(stderr, "\t-a add new bad sectors to the table\n"); - fprintf(stderr, "\t-f reformat listed sectors as bad\n"); - fprintf(stderr, "\t-c copy original sector to replacement\n"); - exit(1); } + argc -= optind; + argv += optind; + + if (argc < 1) + usage(); + if (argv[0][0] != '/') (void)snprintf(name, sizeof(name), "%sr%s%c", _PATH_DEV, argv[0], 'a' + RAW_PART); @@ -156,37 +140,25 @@ usage: strlcpy(name, argv[0], sizeof(name)); f = open(name, argc == 1? O_RDONLY : O_RDWR); if (f < 0) - Perror(name); -#ifdef was - if (read(f, label, sizeof(label)) < 0) - Perror("read"); - for (dp = (struct disklabel *)(label + LABELOFFSET); - dp < (struct disklabel *) - (label + sizeof(label) - sizeof(struct disklabel)); - dp = (struct disklabel *)((char *)dp + 64)) - if (dp->d_magic == DISKMAGIC && dp->d_magic2 == DISKMAGIC) - break; -#else + err(4, "can't open %s", name); + /* obtain label and adjust to fit */ dp = (struct disklabel *)&label; if (ioctl(f, DIOCGDINFO, dp) < 0) - Perror("ioctl DIOCGDINFO"); -#endif + err(4, "DIOCGDINFO"); + if (dp->d_magic != DISKMAGIC || dp->d_magic2 != DISKMAGIC /* dkcksum(lp) != 0 */ ) { - fprintf(stderr, "Bad pack magic number (pack is unlabeled)\n"); - exit(1); + errx(1, "bad pack magic number (pack is unlabeled)"); } if (dp->d_secsize > MAXSECSIZE || dp->d_secsize <= 0) { - fprintf(stderr, "Disk sector size too large/small (%d)\n", + errx(7, "Disk sector size too large/small (%d)", dp->d_secsize); - exit(7); } -#ifdef __i386__ if (dp->d_type == DTYPE_SCSI) { - fprintf(stderr, "SCSI disks don't use bad144!\n"); - exit(1); + errx(1, "SCSI disks don't need bad144!"); } +#ifdef __i386__ /* are we inside a DOS partition? */ if (dp->d_partitions[0].p_offset) { /* yes, rules change. assume bad tables at end of partition C, @@ -195,6 +167,7 @@ usage: } else #endif size = dp->d_nsectors * dp->d_ntracks * dp->d_ncylinders; + argc--; argv++; if (argc == 0) { @@ -216,7 +189,7 @@ usage: break; } bt = oldbad.bt_bad; - for (i = 0; i < 126; i++) { + for (i = 0; i < NBT_BAD; i++) { bad = (bt->bt_cyl<<16) + bt->bt_trksec; if (bad < 0) break; @@ -237,22 +210,21 @@ usage: i = checkold(); if (verbose) printf("Had %d bad sectors, adding %d\n", i, argc); - if (i + argc > 126) { - printf("bad144: not enough room for %d more sectors\n", - argc); - printf("limited to 126 by information format\n"); - exit(1); + if (i + argc > NBT_BAD) { + errx(1, "not enough room for %d more sectors, " + "limited to %d by information format", + argc, NBT_BAD); } curbad = oldbad; } else { - curbad.bt_csn = atoi(*argv++); + curbad.bt_csn = strtol(*argv++, NULL, 10); argc--; curbad.bt_mbz = 0; curbad.bt_flag = DKBAD_MAGIC; - if (argc > 126) { - printf("bad144: too many bad sectors specified\n"); - printf("limited to 126 by information format\n"); - exit(1); + if (argc > NBT_BAD) { + errx(1, "too many bad sectors specified, " + "limited to %d by information format", + NBT_BAD); } i = 0; } @@ -277,7 +249,7 @@ usage: if (errs) exit(1); nbad = i; - while (i < 126) { + while (i < NBT_BAD) { curbad.bt_bad[i].bt_trksec = -1; curbad.bt_bad[i].bt_cyl = -1; i++; @@ -291,9 +263,8 @@ usage: qsort((char *)curbad.bt_bad, nbad, sizeof (struct bt_bad), compare); if (dups) { - fprintf(stderr, -"bad144: bad sectors have been duplicated; can't add existing sectors\n"); - exit(3); + errx(3, "bad sectors have been duplicated; " + "can't add existing sectors"); } shift(f, nbad, nbad-new); } @@ -304,37 +275,26 @@ usage: for (; i < 10 && i < dp->d_nsectors; i += 2) { if (lseek(f, dp->d_secsize * (size - dp->d_nsectors + i), SEEK_SET) < 0) - Perror("lseek"); + err(4, "lseek"); if (verbose) printf("write badsect file at %d\n", size - dp->d_nsectors + i); if (nflag == 0 && write(f, (caddr_t)&curbad, sizeof(curbad)) != sizeof(curbad)) { - char msg[80]; - (void)snprintf(msg, sizeof msg, - "bad144: write bad sector file %d", - i/2); - perror(msg); + warn("write bad sector file %d", i/2); } if (badfile != -1) break; } -#ifdef vax - if (nflag == 0 && fflag) - for (i = nbad - new; i < nbad; i++) - format(f, bn[i]); -#endif #ifdef DIOCSBAD if (nflag == 0 && ioctl(f, DIOCSBAD, (caddr_t)&curbad) < 0) - fprintf(stderr, - "Can't sync bad-sector file; reboot for changes to take effect\n"); + warn("can't sync bad-sector file"); + warnx("reboot for changes to take effect"); #endif if ((dp->d_flags & D_BADSECT) == 0 && nflag == 0) { dp->d_flags |= D_BADSECT; if (ioctl(f, DIOCWDINFO, dp) < 0) { - perror("label"); - fprintf(stderr, "Can't write label to enable bad sector handling\n"); - exit(1); + err(1, "can't write label to enable bad sector handling"); } } exit(0); @@ -342,11 +302,11 @@ usage: daddr_t getold(f, bad) -struct dkbad *bad; + int f; + struct dkbad *bad; { int i; daddr_t sn; - char msg[80]; if (badfile == -1) i = 0; @@ -355,20 +315,17 @@ struct dkbad *bad; for (; i < 10 && i < dp->d_nsectors; i += 2) { sn = size - dp->d_nsectors + i; if (lseek(f, sn * dp->d_secsize, SEEK_SET) < 0) - Perror("lseek"); + err(4, "lseek"); if (read(f, (char *) bad, dp->d_secsize) == dp->d_secsize) { if (i > 0) printf("Using bad-sector file %d\n", i/2); return(sn); } - (void)snprintf(msg, sizeof msg, - "bad144: read bad sector file at sn %d", sn); - perror(msg); + warn("read bad sector file at sn %d", sn); if (badfile != -1) break; } - fprintf(stderr, "bad144: %s: can't read bad block info\n", name); - exit(1); + errx(1, "can't read bad block info from %s", name); /*NOTREACHED*/ } @@ -381,24 +338,22 @@ checkold() int errors = 0, warned = 0; if (oldbad.bt_flag != DKBAD_MAGIC) { - fprintf(stderr, "bad144: %s: bad flag in bad-sector table\n", - name); + warnx("bad flag in bad-sector table %s", name); errors++; } if (oldbad.bt_mbz != 0) { - fprintf(stderr, "bad144: %s: bad magic number\n", name); + warnx("%s: bad magic number", name); errors++; } bt = oldbad.bt_bad; - for (i = 0; i < 126; i++, bt++) { + for (i = 0; i < NBT_BAD; i++, bt++) { if (bt->bt_cyl == 0xffff && bt->bt_trksec == 0xffff) break; if ((bt->bt_cyl >= dp->d_ncylinders) || ((bt->bt_trksec >> 8) >= dp->d_ntracks) || ((bt->bt_trksec & 0xff) >= dp->d_nsectors)) { - fprintf(stderr, - "bad144: cyl/trk/sect out of range in existing entry: "); - fprintf(stderr, "sn=%d, cn=%d, tn=%d, sn=%d\n", + warnx("cyl/trk/sect out of range in existing entry: " + "sn=%d, cn=%d, tn=%d, sn=%d", badsn(bt), bt->bt_cyl, bt->bt_trksec>>8, bt->bt_trksec & 0xff); errors++; @@ -407,14 +362,12 @@ checkold() (bt->bt_trksec >> 8)) * dp->d_nsectors + (bt->bt_trksec & 0xff); if (i > 0 && sn < lsn && !warned) { - fprintf(stderr, - "bad144: bad sector file is out of order\n"); + warnx("bad sector file is out of order"); errors++; warned++; } if (i > 0 && sn == lsn) { - fprintf(stderr, - "bad144: bad sector file contains duplicates (sn %d)\n", + warnx("bad sector file contains duplicates (sn %d)", sn); errors++; } @@ -432,6 +385,7 @@ checkold() */ void shift(f, new, old) + int f, new, old; { daddr_t repl; @@ -469,37 +423,33 @@ char *buf; */ int blkcopy(f, s1, s2) -daddr_t s1, s2; + int f; + daddr_t s1, s2; { int tries, n; - if (buf == (char *)NULL) { + if (buf == NULL) { buf = malloc((unsigned)dp->d_secsize); - if (buf == (char *)NULL) { - fprintf(stderr, "Out of memory\n"); - exit(20); + if (buf == NULL) { + err(20, NULL); } } for (tries = 0; tries < RETRIES; tries++) { if (lseek(f, dp->d_secsize * s1, SEEK_SET) < 0) - Perror("lseek"); + err(4, "lseek"); if ((n = read(f, buf, dp->d_secsize)) == dp->d_secsize) break; } if (n != dp->d_secsize) { - fprintf(stderr, "bad144: can't read sector, %d: ", s1); - if (n < 0) - perror((char *)0); + warnx("can't read sector %d", s1); return(0); } if (lseek(f, dp->d_secsize * s2, SEEK_SET) < 0) - Perror("lseek"); + err(4, "lseek"); if (verbose) printf("copying %d to %d\n", s1, s2); if (nflag == 0 && write(f, buf, dp->d_secsize) != dp->d_secsize) { - fprintf(stderr, - "bad144: can't write replacement sector, %d: ", s2); - perror((char *)0); + warn("can't write replacement sector %d", s2); return(0); } return(1); @@ -509,24 +459,22 @@ char *zbuf; void blkzero(f, sn) -daddr_t sn; + int f; + daddr_t sn; { - if (zbuf == (char *)NULL) { + if (zbuf == NULL) { zbuf = malloc((unsigned)dp->d_secsize); - if (zbuf == (char *)NULL) { - fprintf(stderr, "Out of memory\n"); - exit(20); + if (zbuf == NULL) { + err(20, NULL); } } if (lseek(f, dp->d_secsize * sn, SEEK_SET) < 0) - Perror("lseek"); + err(4, "lseek"); if (verbose) printf("zeroing %d\n", sn); if (nflag == 0 && write(f, zbuf, dp->d_secsize) != dp->d_secsize) { - fprintf(stderr, - "bad144: can't write replacement sector, %d: ", sn); - perror((char *)0); + warn("can't write replacement sector %d", sn); } } @@ -703,10 +651,16 @@ format(fd, blk) #endif void -Perror(op) - const char *op; +usage() { - fprintf(stderr, "bad144: "); - perror(op); - exit(4); + fprintf(stderr, + "usage: bad144 disk [ snum [ bn ... ] ]\n"); + fprintf(stderr, + "to read or overwrite bad-sector table, e.g.: bad144 hp0\n"); + fprintf(stderr, + "or bad144 -a [ -c ] disk bn ...\n"); + fprintf(stderr, "where options are:\n"); + fprintf(stderr, "\t-a add new bad sectors to the table\n"); + fprintf(stderr, "\t-c copy original sector to replacement\n"); + exit(1); } |