strlcpy(name, argv[0], MAXPATHLEN);
strlcpy(name, argv[0], sizeof(name));
main(argc, argv)	int argc;	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)++;		}		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);	}	if (argv[0][0] != '/')		(void)snprintf(name, sizeof(name), "%sr%s%c", _PATH_DEV,		    argv[0], 'a' + RAW_PART);	else		strlcpy(name, argv[0], MAXPATHLEN);	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	/* obtain label and adjust to fit */	dp = (struct disklabel *)&label;	if (ioctl(f, DIOCGDINFO, dp) < 0)		Perror("ioctl DIOCGDINFO");#endif	if (dp->d_magic != DISKMAGIC || dp->d_magic2 != DISKMAGIC		/* dkcksum(lp) != 0 */ ) {		fprintf(stderr, "Bad pack magic number (pack is unlabeled)\n");		exit(1);	}	if (dp->d_secsize > MAXSECSIZE || dp->d_secsize <= 0) {		fprintf(stderr, "Disk sector size too large/small (%d)\n",			dp->d_secsize);		exit(7);	}#ifdef __i386__	if (dp->d_type == DTYPE_SCSI) {		fprintf(stderr, "SCSI disks don't use bad144!\n");		exit(1);	}	/* are we inside a DOS partition? */	if (dp->d_partitions[0].p_offset) {		/* yes, rules change. assume bad tables at end of partition C,		   which maps all of DOS partition we are within -wfj */		size = dp->d_partitions[2].p_offset + dp->d_partitions[2].p_size;	} else#endif	size = dp->d_nsectors * dp->d_ntracks * dp->d_ncylinders; 	argc--;	argv++;	if (argc == 0) {		sn = getold(f, &oldbad);		printf("bad block information at sector %d in %s:\n",		    sn, name);		printf("cartridge serial number: %d(10)\n", oldbad.bt_csn);		switch (oldbad.bt_flag) {		case (u_short)-1:			printf("alignment cartridge\n");			break;		case DKBAD_MAGIC:			break;		default:			printf("bt_flag=%x(16)?\n", oldbad.bt_flag);			break;		}		bt = oldbad.bt_bad;		for (i = 0; i < 126; i++) {			bad = (bt->bt_cyl<<16) + bt->bt_trksec;			if (bad < 0)				break;			printf("sn=%d, cn=%d, tn=%d, sn=%d\n", badsn(bt),			    bt->bt_cyl, bt->bt_trksec>>8, bt->bt_trksec&0xff);			bt++;		}		(void) checkold();		exit(0);	}	if (add) {		/*		 * Read in the old badsector table.		 * Verify that it makes sense, and the bad sectors		 * are in order.  Copy the old table to the new one.		 */		(void) getold(f, &oldbad);		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);		}		curbad = oldbad;	} else {		curbad.bt_csn = atoi(*argv++);		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);		}		i = 0;	}	errs = 0;	new = argc;	while (argc > 0) {		daddr_t sn = atoi(*argv++);		argc--;		if (sn < 0 || sn >= size) {			printf("%d: out of range [0,%d) for disk %s\n",			    sn, size, dp->d_typename);			errs++;			continue;		}		bn[i] = sn;		curbad.bt_bad[i].bt_cyl = sn / (dp->d_nsectors*dp->d_ntracks);		sn %= (dp->d_nsectors*dp->d_ntracks);		curbad.bt_bad[i].bt_trksec =		    ((sn/dp->d_nsectors) << 8) + (sn%dp->d_nsectors);		i++;	}	if (errs)		exit(1);	nbad = i;	while (i < 126) {		curbad.bt_bad[i].bt_trksec = -1;		curbad.bt_bad[i].bt_cyl = -1;		i++;	}	if (add) {		/*		 * Sort the new bad sectors into the list.		 * Then shuffle the replacement sectors so that		 * the previous bad sectors get the same replacement data.		 */		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);		}		shift(f, nbad, nbad-new);	}	if (badfile == -1)		i = 0;	else		i = badfile * 2;	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");		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);		}		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");#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);		}	}	exit(0);}