Use getopt instead of manual command line grovelling, put usage

in a function and adapt it the man page, and some lint cleanups.
This commit is contained in:
wiz 2002-06-13 15:27:09 +00:00
parent ff7391b803
commit d1677084c1

View File

@ -1,4 +1,4 @@
/* $NetBSD: bad144.c,v 1.18 2002/06/13 13:49:08 wiz Exp $ */ /* $NetBSD: bad144.c,v 1.19 2002/06/13 15:27:09 wiz Exp $ */
/* /*
* Copyright (c) 1980, 1986, 1988, 1993 * Copyright (c) 1980, 1986, 1988, 1993
@ -43,7 +43,7 @@ __COPYRIGHT("@(#) Copyright (c) 1980, 1986, 1988, 1993\n\
#if 0 #if 0
static char sccsid[] = "@(#)bad144.c 8.2 (Berkeley) 4/27/95"; static char sccsid[] = "@(#)bad144.c 8.2 (Berkeley) 4/27/95";
#else #else
__RCSID("$NetBSD: bad144.c,v 1.18 2002/06/13 13:49:08 wiz Exp $"); __RCSID("$NetBSD: bad144.c,v 1.19 2002/06/13 15:27:09 wiz Exp $");
#endif #endif
#endif /* not lint */ #endif /* not lint */
@ -79,16 +79,19 @@ __RCSID("$NetBSD: bad144.c,v 1.18 2002/06/13 13:49:08 wiz Exp $");
#define RETRIES 10 /* number of retries on reading old sectors */ #define RETRIES 10 /* number of retries on reading old sectors */
int fflag, add, copy, verbose, nflag; #ifdef __vax__
int fflag;
#endif
int add, copy, verbose, nflag;
int dups; int dups;
int badfile = -1; /* copy of badsector table to use, -1 if any */ int badfile = -1; /* copy of badsector table to use, -1 if any */
#define MAXSECSIZE 1024 #define MAXSECSIZE 1024
struct dkbad curbad, oldbad; struct dkbad curbad, oldbad;
#define DKBAD_MAGIC 0x4321 #define DKBAD_MAGIC 0x4321
char label[BBSIZE];
daddr_t size; daddr_t size;
struct disklabel *dp; struct disklabel *dp;
struct disklabel label;
char name[BUFSIZ]; char name[BUFSIZ];
daddr_t badsn(const struct bt_bad *); daddr_t badsn(const struct bt_bad *);
@ -99,86 +102,72 @@ int compare(const void *, const void *);
daddr_t getold(int, struct dkbad *); daddr_t getold(int, struct dkbad *);
int main(int, char **); int main(int, char **);
void shift(int, int, int); void shift(int, int, int);
void usage(void);
#ifdef __vax__
#define OPTSTRING "01234acfvn"
#else
#define OPTSTRING "01234acvn"
#endif
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
struct bt_bad *bt; struct bt_bad *bt;
daddr_t sn, bn[NBT_BAD]; daddr_t sn, bn[NBT_BAD];
int i, f, nbad, new, bad, errs; int i, f, nbad, new, bad, errs, ch;
char diskname[MAXPATHLEN]; char diskname[MAXPATHLEN];
argc--, argv++; while ((ch = getopt(argc, argv, OPTSTRING)) != -1) {
while (argc > 0 && **argv == '-') { switch (ch) {
(*argv)++; case '0':
while (**argv) { case '1':
switch (**argv) { case '2':
#if __vax__ case '3':
case 'f': case '4':
fflag++; badfile = ch - '0';
break; break;
case 'a':
add = 1;
break;
case 'c':
copy = 1;
break;
#ifdef __vax__
case 'f':
fflag = 1;
break;
#endif #endif
case 'a': case 'n':
add++; nflag = 1;
break; /* FALLTHROUGH */
case 'c': case 'v':
copy++; verbose = 1;
break; break;
case 'v': case '?':
verbose++; default:
break; usage();
case 'n':
nflag++;
verbose++;
break;
default:
if (**argv >= '0' && **argv <= '4') {
badfile = **argv - '0';
break;
}
goto usage;
}
(*argv)++;
} }
argc--, argv++;
} }
argc -= optind;
argv += optind;
if (argc < 1) { if (argc < 1) {
usage: 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);
} }
f = opendisk(argv[0], argc == 1 ? O_RDONLY : O_RDWR, diskname, f = opendisk(argv[0], argc == 1 ? O_RDONLY : O_RDWR, diskname,
sizeof(diskname), 0); sizeof(diskname), 0);
if (f < 0) if (f < 0)
err(4, "opendisk `%s'", diskname); err(4, "opendisk `%s'", diskname);
#ifdef was
if (read(f, label, sizeof(label)) < 0)
err(4, "read `%s'", diskname);
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
/* obtain label and adjust to fit */ /* obtain label and adjust to fit */
dp = (struct disklabel *)&label; dp = &label;
if (ioctl(f, DIOCGDINFO, dp) < 0) if (ioctl(f, DIOCGDINFO, dp) < 0)
err(4, "ioctl DIOCGDINFO `%s'", diskname); err(4, "ioctl DIOCGDINFO `%s'", diskname);
#endif
if (dp->d_magic != DISKMAGIC || dp->d_magic2 != DISKMAGIC if (dp->d_magic != DISKMAGIC || dp->d_magic2 != DISKMAGIC
/* dkcksum(lp) != 0 */ ) /* dkcksum(lp) != 0 */ )
errx(1, "Bad pack magic number (pack is unlabeled)"); errx(1, "Bad pack magic number (pack is unlabeled)");
if (dp->d_secsize > MAXSECSIZE || dp->d_secsize <= 0) if (dp->d_secsize > MAXSECSIZE || dp->d_secsize == 0)
errx(7, "Disk sector size too large/small (%d)", errx(7, "Disk sector size too large/small (%d)",
dp->d_secsize); dp->d_secsize);
#ifdef __i386__ #ifdef __i386__
@ -290,8 +279,8 @@ usage:
qsort((char *)curbad.bt_bad, nbad, sizeof (struct bt_bad), qsort((char *)curbad.bt_bad, nbad, sizeof (struct bt_bad),
compare); compare);
if (dups) if (dups)
errx(3, errx(3, "bad sectors have been duplicated; "
"bad sectors have been duplicated; can't add existing sectors"); "can't add existing sectors");
shift(f, nbad, nbad-new); shift(f, nbad, nbad-new);
} }
if (badfile == -1) if (badfile == -1)
@ -319,8 +308,8 @@ usage:
#endif #endif
#ifdef DIOCSBAD #ifdef DIOCSBAD
if (nflag == 0 && ioctl(f, DIOCSBAD, (caddr_t)&curbad) < 0) if (nflag == 0 && ioctl(f, DIOCSBAD, (caddr_t)&curbad) < 0)
warnx( warnx("Can't sync bad-sector file; reboot for changes "
"Can't sync bad-sector file; reboot for changes to take effect"); "to take effect");
#endif #endif
if ((dp->d_flags & D_BADSECT) == 0 && nflag == 0) { if ((dp->d_flags & D_BADSECT) == 0 && nflag == 0) {
dp->d_flags |= D_BADSECT; dp->d_flags |= D_BADSECT;
@ -330,7 +319,7 @@ usage:
"Can't write label to enable bad sector handling"); "Can't write label to enable bad sector handling");
} }
} }
exit(0); return (0);
} }
daddr_t daddr_t
@ -384,8 +373,8 @@ checkold(void)
if ((bt->bt_cyl >= dp->d_ncylinders) || if ((bt->bt_cyl >= dp->d_ncylinders) ||
((bt->bt_trksec >> 8) >= dp->d_ntracks) || ((bt->bt_trksec >> 8) >= dp->d_ntracks) ||
((bt->bt_trksec & 0xff) >= dp->d_nsectors)) { ((bt->bt_trksec & 0xff) >= dp->d_nsectors)) {
warnx( warnx("cyl/trk/sect out of range in existing entry: "
"cyl/trk/sect out of range in existing entry: sn=%d, cn=%d, tn=%d, sn=%d", "sn=%d, cn=%d, tn=%d, sn=%d",
badsn(bt), bt->bt_cyl, bt->bt_trksec>>8, badsn(bt), bt->bt_cyl, bt->bt_trksec>>8,
bt->bt_trksec & 0xff); bt->bt_trksec & 0xff);
errors++; errors++;
@ -489,11 +478,9 @@ blkzero(int f, daddr_t sn)
{ {
char *zbuf; char *zbuf;
if (zbuf == (char *)NULL) { zbuf = calloc(1, (unsigned int)dp->d_secsize);
zbuf = malloc((unsigned)dp->d_secsize); if (zbuf == NULL)
if (zbuf == (char *)NULL) errx(20, "Out of memory");
errx(20, "Out of memory");
}
if (lseek(f, (off_t)(dp->d_secsize * sn), SEEK_SET) < 0) if (lseek(f, (off_t)(dp->d_secsize * sn), SEEK_SET) < 0)
err(4, "lseek"); err(4, "lseek");
if (verbose) if (verbose)
@ -658,3 +645,24 @@ format(int fd, daddr_t blk)
warn("write format %d", blk); warn("write format %d", blk);
} }
#endif #endif
void
usage(void)
{
fprintf(stderr, "usage: bad144 [-%sv] disk [sno [bad ...]]\n"
"to read or overwrite the bad-sector table, e.g.: bad144 hp0\n"
"or bad144 -a [-c%sv] disk [bad ...]\n"
"where options are:\n"
"\t-a add new bad sectors to the table\n"
"\t-c copy original sector to replacement\n"
"%s"
"\t-v verbose mode\n",
#ifdef __vax__
"f", "f", "\t-f reformat listed sectors as bad\n"
#else
"", "", ""
#endif
);
exit(1);
}