int processfile(FILE *srcfile, int start, int flags) { char srctemp[80]; char *srcline; int thispass; for(thispass=0; thispass<=1; thispass++) { asmpass=flags+(thispass<<1); rewind(srcfile); destadr=start; lineno=1; while(fgets(srctemp, 80, srcfile) != NULL) { srcline=srctemp; skipspace(srcline); if(*srcline=='.') { srcline=checklabel(srcline+1, codeorig+destadr); skipspace(srcline); } if(*srcline=='#') asmdirect(srcline); else if(*srcline!='/') assemble(srcline); lineno++; } } return(destadr); }
vector<int> exclusiveTime(int n, vector<string>& logs) { vector<int> ret(n,0); //func id as index, time is value stack<pair<int,int>> s; //func id, current execution time, when start, push, when end, pop for(int i=0;i<logs.size();i++){ int index = 0; int id = checkid(logs[i],index); int label = checklabel(logs[i],index); int t = checktime(logs[i],index); if(label==0){ s.push(make_pair(id,t)); }else{ pair<int,int> cur = s.top(); int last = t-cur.second+1; ret[cur.first]+=last; s.pop(); if (!s.empty()) { //to remove over count, not time for s.top().first id ret[s.top().first] -= last; } } } return ret; }
int checkoverlap(partinfo *lp, int nparts, int rawpart, int bsdpart) { int i, j; if (checklabel(lp, nparts, rawpart, bsdpart, &i, &j)) { msg_display(MSG_partitions_overlap,'a'+i,'a'+j); return 1; } return 0; }
static void cmd_label(struct disklabel *lp, char *s, int fd) { char line[BUFSIZ]; int i; i = getinput(line, "Label disk [n]?"); if (i <= 0 || (*line != 'y' && *line != 'Y') ) return; if (checklabel(lp) != 0) { printf("Label not written\n"); return; } if (writelabel(fd, lp) != 0) { printf("Label not written\n"); return; } printf("Label written\n"); }
int main(int argc, char *argv[]) { int ch, f, error = 0; struct disklabel *lp; FILE *t; while ((ch = getopt(argc, argv, "ABEf:F:hRb:cdenp:tvw")) != -1) switch (ch) { case 'A': ++aflag; break; #if NUMBOOT > 0 case 'B': ++installboot; break; case 'b': xxboot = optarg; break; #endif case 'R': if (op != UNSPEC) usage(); op = RESTORE; break; case 'c': ++cflag; break; case 'd': ++dflag; break; case 'e': if (op != UNSPEC) usage(); op = EDIT; break; case 'E': if (op != UNSPEC) usage(); op = EDITOR; break; case 'f': fstabfile = optarg; uidflag = 0; break; case 'F': fstabfile = optarg; ++uidflag; break; case 'h': print_unit = '*'; break; case 't': ++tflag; break; case 'w': if (op != UNSPEC) usage(); op = WRITE; break; case 'p': if (strchr("bckmgtBCKMGT", optarg[0]) == NULL || optarg[1] != '\0') { fprintf(stderr, "Valid units are bckmgt\n"); exit(1); } print_unit = tolower((unsigned char)optarg[0]); break; case 'n': donothing++; break; case 'v': verbose++; break; case '?': default: usage(); } argc -= optind; argv += optind; #if NUMBOOT > 0 if (installboot) { if (op == UNSPEC) op = WRITEBOOT; } else { if (op == UNSPEC) op = READ; } #else if (op == UNSPEC) op = READ; #endif if (argc < 1 || (fstabfile && !(op == EDITOR || aflag))) usage(); dkname = argv[0]; f = opendev(dkname, (op == READ ? O_RDONLY : O_RDWR), OPENDEV_PART, &specname); if (f < 0) err(4, "%s", specname); switch (op) { case EDIT: if (argc != 1) usage(); readlabel(f); error = edit(&lab, f); break; case EDITOR: if (argc != 1) usage(); readlabel(f); error = editor(f); break; case READ: if (argc != 1) usage(); readlabel(f); if (tflag) makedisktab(stdout, &lab); else display(stdout, &lab, print_unit, 1); error = checklabel(&lab); break; case RESTORE: if (argc < 2 || argc > 3) usage(); readlabel(f); #if NUMBOOT > 0 if (installboot && argc == 3) makelabel(argv[2], NULL, &lab); #endif lp = makebootarea(bootarea, &lab); *lp = lab; if (!(t = fopen(argv[1], "r"))) err(4, "%s", argv[1]); error = getasciilabel(t, lp); bzero(lp->d_uid, sizeof(lp->d_uid)); if (error == 0) error = writelabel(f, bootarea, lp); fclose(t); break; case WRITE: if (dflag || aflag) { readlabel(f); } else if (argc < 2 || argc > 3) usage(); else makelabel(argv[1], argc == 3 ? argv[2] : NULL, &lab); lp = makebootarea(bootarea, &lab); *lp = lab; error = checklabel(&lab); if (error == 0) error = writelabel(f, bootarea, lp); break; #if NUMBOOT > 0 case WRITEBOOT: { struct disklabel tlab; readlabel(f); tlab = lab; if (argc == 2) makelabel(argv[1], NULL, &lab); lp = makebootarea(bootarea, &lab); *lp = tlab; error = checklabel(&lab); if (error == 0) error = writelabel(f, bootarea, lp); break; } #endif default: break; } exit(error); }
/* * Read an ascii label in from FILE f, * in the same format as that put out by display(), * and fill in lp. */ int getasciilabel(FILE *f, struct disklabel *lp) { char **cpp, *cp; const char *errstr; struct partition *pp; char *tp, *s, line[BUFSIZ]; int lineno = 0, errors = 0; u_int32_t v, fsize; u_int64_t lv; lp->d_version = 1; lp->d_bbsize = BBSIZE; /* XXX */ lp->d_sbsize = SBSIZE; /* XXX */ while (fgets(line, sizeof(line), f)) { lineno++; if ((cp = strpbrk(line, "#\r\n"))) *cp = '\0'; cp = skip(line); if (cp == NULL) continue; tp = strchr(cp, ':'); if (tp == NULL) { warnx("line %d: syntax error", lineno); errors++; continue; } *tp++ = '\0', tp = skip(tp); if (!strcmp(cp, "type")) { if (tp == NULL) tp = "unknown"; else if (strcasecmp(tp, "IDE") == 0) tp = "ESDI"; cpp = dktypenames; for (; cpp < &dktypenames[DKMAXTYPES]; cpp++) if ((s = *cpp) && !strcasecmp(s, tp)) { lp->d_type = cpp - dktypenames; goto next; } v = GETNUM(lp->d_type, tp, 0, &errstr); if (errstr || v >= DKMAXTYPES) warnx("line %d: warning, unknown disk type: %s", lineno, tp); lp->d_type = v; continue; } if (!strcmp(cp, "flags")) { for (v = 0; (cp = tp) && *cp != '\0';) { tp = word(cp); if (!strcmp(cp, "badsect")) v |= D_BADSECT; else if (!strcmp(cp, "vendor")) v |= D_VENDOR; else { warnx("line %d: bad flag: %s", lineno, cp); errors++; } } lp->d_flags = v; continue; } if (!strcmp(cp, "drivedata")) { int i; for (i = 0; (cp = tp) && *cp != '\0' && i < NDDATA;) { v = GETNUM(lp->d_drivedata[i], cp, 0, &errstr); if (errstr) warnx("line %d: bad drivedata %s", lineno, cp); lp->d_drivedata[i++] = v; tp = word(cp); } continue; } if (sscanf(cp, "%d partitions", &v) == 1) { if (v == 0 || v > MAXPARTITIONS) { warnx("line %d: bad # of partitions", lineno); lp->d_npartitions = MAXPARTITIONS; errors++; } else lp->d_npartitions = v; continue; } if (tp == NULL) tp = ""; if (!strcmp(cp, "disk")) { strncpy(lp->d_typename, tp, sizeof (lp->d_typename)); continue; } if (!strcmp(cp, "label")) { strncpy(lp->d_packname, tp, sizeof (lp->d_packname)); continue; } if (!strcmp(cp, "duid")) { if (duid_parse(lp, tp) != 0) { warnx("line %d: bad %s: %s", lineno, cp, tp); errors++; } continue; } if (!strcmp(cp, "bytes/sector")) { v = GETNUM(lp->d_secsize, tp, 1, &errstr); if (errstr || (v % 512) != 0) { warnx("line %d: bad %s: %s", lineno, cp, tp); errors++; } else lp->d_secsize = v; continue; } if (!strcmp(cp, "sectors/track")) { v = GETNUM(lp->d_nsectors, tp, 1, &errstr); if (errstr) { warnx("line %d: bad %s: %s", lineno, cp, tp); errors++; } else lp->d_nsectors = v; continue; } if (!strcmp(cp, "sectors/cylinder")) { v = GETNUM(lp->d_secpercyl, tp, 1, &errstr); if (errstr) { warnx("line %d: bad %s: %s", lineno, cp, tp); errors++; } else lp->d_secpercyl = v; continue; } if (!strcmp(cp, "tracks/cylinder")) { v = GETNUM(lp->d_ntracks, tp, 1, &errstr); if (errstr) { warnx("line %d: bad %s: %s", lineno, cp, tp); errors++; } else lp->d_ntracks = v; continue; } if (!strcmp(cp, "cylinders")) { v = GETNUM(lp->d_ncylinders, tp, 1, &errstr); if (errstr) { warnx("line %d: bad %s: %s", lineno, cp, tp); errors++; } else lp->d_ncylinders = v; continue; } if (!strcmp(cp, "total sectors")) { lv = GETNUM(lv, tp, 1, &errstr); if (errstr) { warnx("line %d: bad %s: %s", lineno, cp, tp); errors++; } else { DL_SETDSIZE(lp, lv); } continue; } /* Ignore fields that are no longer in the disklabel. */ if (!strcmp(cp, "rpm") || !strcmp(cp, "interleave") || !strcmp(cp, "trackskew") || !strcmp(cp, "cylinderskew") || !strcmp(cp, "headswitch") || !strcmp(cp, "track-to-track seek")) continue; /* Ignore fields that are forcibly set when label is read. */ if (!strcmp(cp, "total sectors") || !strcmp(cp, "boundstart") || !strcmp(cp, "boundend")) continue; if ('a' <= *cp && *cp <= 'z' && cp[1] == '\0') { unsigned int part = *cp - 'a'; if (part >= lp->d_npartitions) { if (part >= MAXPARTITIONS) { warnx("line %d: bad partition name: %s", lineno, cp); errors++; continue; } else { lp->d_npartitions = part + 1; } } pp = &lp->d_partitions[part]; #define NXTNUM(n, field, errstr) { \ if (tp == NULL) { \ warnx("line %d: too few fields", lineno); \ errors++; \ break; \ } else \ cp = tp, tp = word(cp), (n) = GETNUM(field, cp, 0, errstr); \ } NXTNUM(lv, lv, &errstr); if (errstr) { warnx("line %d: bad partition size: %s", lineno, cp); errors++; } else { DL_SETPSIZE(pp, lv); } NXTNUM(lv, lv, &errstr); if (errstr) { warnx("line %d: bad partition offset: %s", lineno, cp); errors++; } else { DL_SETPOFFSET(pp, lv); } if (tp == NULL) { pp->p_fstype = FS_UNUSED; goto gottype; } cp = tp, tp = word(cp); cpp = fstypenames; for (; cpp < &fstypenames[FSMAXTYPES]; cpp++) if ((s = *cpp) && !strcasecmp(s, cp)) { pp->p_fstype = cpp - fstypenames; goto gottype; } if (isdigit((unsigned char)*cp)) v = GETNUM(pp->p_fstype, cp, 0, &errstr); else v = FSMAXTYPES; if (errstr || v >= FSMAXTYPES) { warnx("line %d: warning, unknown filesystem type: %s", lineno, cp); v = FS_UNUSED; } pp->p_fstype = v; gottype: switch (pp->p_fstype) { case FS_UNUSED: /* XXX */ if (tp == NULL) /* ok to skip fsize/bsize */ break; NXTNUM(fsize, fsize, &errstr); if (fsize == 0) break; NXTNUM(v, v, &errstr); pp->p_fragblock = DISKLABELV1_FFS_FRAGBLOCK(fsize, v / fsize); break; case FS_BSDFFS: NXTNUM(fsize, fsize, &errstr); if (fsize == 0) break; NXTNUM(v, v, &errstr); pp->p_fragblock = DISKLABELV1_FFS_FRAGBLOCK(fsize, v / fsize); NXTNUM(pp->p_cpg, pp->p_cpg, &errstr); break; default: break; } continue; } warnx("line %d: unknown field: %s", lineno, cp); errors++; next: ; } errors += checklabel(lp); return (errors > 0); }
/* * Read an ascii label in from fd f, * in the same format as that put out by display(), * and fill in lp. */ static int getasciilabel(FILE *f, struct disklabel *lp) { char *cp, *endp; const char **cpp; u_int part; char *tp, line[BUFSIZ]; u_long v; int lineno = 0, errors = 0; int i; makelabel("auto", lp); bzero(&part_set, sizeof(part_set)); bzero(&part_size_type, sizeof(part_size_type)); bzero(&part_offset_type, sizeof(part_offset_type)); lp->d_bbsize = BBSIZE; /* XXX */ lp->d_sbsize = 0; /* XXX */ while (fgets(line, sizeof(line) - 1, f)) { lineno++; if ((cp = strchr(line,'\n')) != 0) *cp = '\0'; cp = skip(line); if (cp == NULL) continue; tp = strchr(cp, ':'); if (tp == NULL) { fprintf(stderr, "line %d: syntax error\n", lineno); errors++; continue; } *tp++ = '\0', tp = skip(tp); if (!strcmp(cp, "type")) { if (tp == NULL) tp = unknown; cpp = dktypenames; for (; cpp < &dktypenames[DKMAXTYPES]; cpp++) if (*cpp && !strcmp(*cpp, tp)) { lp->d_type = cpp - dktypenames; break; } if (cpp < &dktypenames[DKMAXTYPES]) continue; errno = 0; v = strtoul(tp, &endp, 10); if (errno != 0 || *endp != '\0') v = DKMAXTYPES; if (v >= DKMAXTYPES) fprintf(stderr, "line %d:%s %lu\n", lineno, "Warning, unknown disk type", v); else lp->d_type = v; continue; } if (!strcmp(cp, "flags")) { for (v = 0; (cp = tp) && *cp != '\0';) { tp = word(cp); if (!strcmp(cp, "removeable")) v |= D_REMOVABLE; else if (!strcmp(cp, "ecc")) v |= D_ECC; else if (!strcmp(cp, "badsect")) v |= D_BADSECT; else { fprintf(stderr, "line %d: %s: bad flag\n", lineno, cp); errors++; } } lp->d_flags = v; continue; } if (!strcmp(cp, "drivedata")) { for (i = 0; (cp = tp) && *cp != '\0' && i < NDDATA;) { lp->d_drivedata[i++] = strtoul(cp, NULL, 10); tp = word(cp); } continue; } if (sscanf(cp, "%lu partitions", &v) == 1) { if (v > MAXPARTITIONS) { fprintf(stderr, "line %d: bad # of partitions\n", lineno); lp->d_npartitions = MAXPARTITIONS; errors++; } else if (v < DEFPARTITIONS) { fprintf(stderr, "line %d: bad # of partitions\n", lineno); lp->d_npartitions = DEFPARTITIONS; errors++; } else lp->d_npartitions = v; continue; } if (tp == NULL) tp = blank; if (!strcmp(cp, "disk")) { strncpy(lp->d_typename, tp, sizeof (lp->d_typename)); continue; } if (!strcmp(cp, "label")) { strncpy(lp->d_packname, tp, sizeof (lp->d_packname)); continue; } if (!strcmp(cp, "bytes/sector")) { v = strtoul(tp, NULL, 10); if (v == 0 || (v % DEV_BSIZE) != 0) { fprintf(stderr, "line %d: %s: bad sector size\n", lineno, tp); errors++; } else lp->d_secsize = v; continue; } if (!strcmp(cp, "sectors/track")) { v = strtoul(tp, NULL, 10); #if (ULONG_MAX != 0xffffffffUL) if (v == 0 || v > 0xffffffff) #else if (v == 0) #endif { fprintf(stderr, "line %d: %s: bad %s\n", lineno, tp, cp); errors++; } else lp->d_nsectors = v; continue; } if (!strcmp(cp, "sectors/cylinder")) { v = strtoul(tp, NULL, 10); if (v == 0) { fprintf(stderr, "line %d: %s: bad %s\n", lineno, tp, cp); errors++; } else lp->d_secpercyl = v; continue; } if (!strcmp(cp, "tracks/cylinder")) { v = strtoul(tp, NULL, 10); if (v == 0) { fprintf(stderr, "line %d: %s: bad %s\n", lineno, tp, cp); errors++; } else lp->d_ntracks = v; continue; } if (!strcmp(cp, "cylinders")) { v = strtoul(tp, NULL, 10); if (v == 0) { fprintf(stderr, "line %d: %s: bad %s\n", lineno, tp, cp); errors++; } else lp->d_ncylinders = v; continue; } if (!strcmp(cp, "sectors/unit")) { v = strtoul(tp, NULL, 10); if (v == 0) { fprintf(stderr, "line %d: %s: bad %s\n", lineno, tp, cp); errors++; } else lp->d_secperunit = v; continue; } if (!strcmp(cp, "rpm")) { v = strtoul(tp, NULL, 10); if (v == 0 || v > USHRT_MAX) { fprintf(stderr, "line %d: %s: bad %s\n", lineno, tp, cp); errors++; } else lp->d_rpm = v; continue; } if (!strcmp(cp, "interleave")) { v = strtoul(tp, NULL, 10); if (v == 0 || v > USHRT_MAX) { fprintf(stderr, "line %d: %s: bad %s\n", lineno, tp, cp); errors++; } else lp->d_interleave = v; continue; } if (!strcmp(cp, "trackskew")) { v = strtoul(tp, NULL, 10); if (v > USHRT_MAX) { fprintf(stderr, "line %d: %s: bad %s\n", lineno, tp, cp); errors++; } else lp->d_trackskew = v; continue; } if (!strcmp(cp, "cylinderskew")) { v = strtoul(tp, NULL, 10); if (v > USHRT_MAX) { fprintf(stderr, "line %d: %s: bad %s\n", lineno, tp, cp); errors++; } else lp->d_cylskew = v; continue; } if (!strcmp(cp, "headswitch")) { v = strtoul(tp, NULL, 10); lp->d_headswitch = v; continue; } if (!strcmp(cp, "track-to-track seek")) { v = strtoul(tp, NULL, 10); lp->d_trkseek = v; continue; } /* the ':' was removed above */ if (*cp < 'a' || *cp > MAX_PART || cp[1] != '\0') { fprintf(stderr, "line %d: %s: Unknown disklabel field\n", lineno, cp); errors++; continue; } /* Process a partition specification line. */ part = *cp - 'a'; if (part >= lp->d_npartitions) { fprintf(stderr, "line %d: partition name out of range a-%c: %s\n", lineno, 'a' + lp->d_npartitions - 1, cp); errors++; continue; } part_set[part] = 1; if (getasciipartspec(tp, lp, part, lineno) != 0) { errors++; break; } } errors += checklabel(lp); return (errors == 0); }
int main(int argc, char *argv[]) { FILE *t; int ch, error, fd; const char *name; error = 0; name = NULL; while ((ch = getopt(argc, argv, "ABb:efm:nRrw")) != -1) switch (ch) { case 'A': allfields = 1; break; case 'B': ++installboot; break; case 'b': xxboot = optarg; break; case 'f': is_file=1; break; case 'm': if (!strcmp(optarg, "i386") || !strcmp(optarg, "amd64") || !strcmp(optarg, "ia64") || !strcmp(optarg, "pc98")) { labelsoffset = 1; labeloffset = 0; bbsize = 8192; } else { errx(1, "Unsupported architecture"); } break; case 'n': disable_write = 1; break; case 'R': if (op != UNSPEC) usage(); op = RESTORE; break; case 'e': if (op != UNSPEC) usage(); op = EDIT; break; case 'r': /* * We accept and ignore -r for compatibility with * historical disklabel usage. */ break; case 'w': if (op != UNSPEC) usage(); op = WRITE; break; case '?': default: usage(); } argc -= optind; argv += optind; if (argc < 1) usage(); if (labelsoffset < 0 || labeloffset < 0) errx(1, "a -m <architecture> option must be specified"); /* Figure out the names of the thing we're working on */ if (is_file) { specname = argv[0]; } else { specname = g_device_path(argv[0]); if (specname == NULL) { warn("unable to get correct path for %s", argv[0]); return(1); } fd = open(specname, O_RDONLY); if (fd < 0) { warn("error opening %s", specname); return(1); } pname = g_providername(fd); if (pname == NULL) { warn("error getting providername for %s", specname); close(fd); return(1); } close(fd); } if (installboot && op == UNSPEC) op = WRITEBOOT; else if (op == UNSPEC) op = READ; switch(op) { case UNSPEC: break; case EDIT: if (argc != 1) usage(); readlabel(1); fixlabel(&lab); error = edit(); break; case READ: if (argc != 1) usage(); readlabel(1); display(stdout, NULL); error = checklabel(NULL); break; case RESTORE: if (argc != 2) usage(); if (!(t = fopen(argv[1], "r"))) err(4, "fopen %s", argv[1]); readlabel(0); if (!getasciilabel(t, &lab)) exit(1); error = writelabel(); break; case WRITE: if (argc == 2) name = argv[1]; else if (argc == 1) name = "auto"; else usage(); readlabel(0); makelabel(name, &lab); fixlabel(&lab); if (checklabel(NULL) == 0) error = writelabel(); break; case WRITEBOOT: readlabel(1); fixlabel(&lab); if (argc == 2) makelabel(argv[1], &lab); if (checklabel(NULL) == 0) error = writelabel(); break; } exit(error); }
/* * Read an ascii label in from fd f, * in the same format as that put out by display(), * and fill in lp. */ int getasciilabel(FILE *f, struct disklabel32 *lp) { char *cp; const char **cpp; u_int part; char *tp, line[BUFSIZ]; u_long v; int lineno = 0, errors = 0; int i; char empty[] = ""; char unknown[] = "unknown"; bzero(&part_set, sizeof(part_set)); bzero(&part_size_type, sizeof(part_size_type)); bzero(&part_offset_type, sizeof(part_offset_type)); lp->d_bbsize = BBSIZE; /* XXX */ lp->d_sbsize = SBSIZE; /* XXX */ while (fgets(line, sizeof(line) - 1, f)) { lineno++; if ((cp = strchr(line,'\n')) != NULL) *cp = '\0'; cp = skip(line); if (cp == NULL) continue; tp = strchr(cp, ':'); if (tp == NULL) { fprintf(stderr, "line %d: syntax error\n", lineno); errors++; continue; } *tp++ = '\0', tp = skip(tp); if (streq(cp, "type")) { if (tp == NULL) tp = unknown; cpp = dktypenames; for (; cpp < &dktypenames[DKMAXTYPES]; cpp++) { if (*cpp && strcasecmp(*cpp, tp) == 0) { lp->d_type = cpp - dktypenames; break; } } if (cpp < &dktypenames[DKMAXTYPES]) continue; v = strtoul(tp, NULL, 10); if (v >= DKMAXTYPES) { fprintf(stderr, "line %d:%s %lu\n", lineno, "Warning, unknown disk type", v); } lp->d_type = v; continue; } if (streq(cp, "flags")) { for (v = 0; (cp = tp) && *cp != '\0';) { tp = word(cp); if (streq(cp, "removeable")) v |= 0; /* obsolete */ else if (streq(cp, "ecc")) v |= 0; /* obsolete */ else if (streq(cp, "badsect")) v |= 0; /* obsolete */ else { fprintf(stderr, "line %d: %s: bad flag\n", lineno, cp); errors++; } } lp->d_flags = v; continue; } if (streq(cp, "drivedata")) { for (i = 0; (cp = tp) && *cp != '\0' && i < NDDATA32;) { lp->d_drivedata[i++] = strtoul(cp, NULL, 10); tp = word(cp); } continue; } if (sscanf(cp, "%lu partitions", &v) == 1) { if (v == 0 || v > MAXPARTITIONS32) { fprintf(stderr, "line %d: bad # of partitions\n", lineno); lp->d_npartitions = MAXPARTITIONS32; errors++; } else lp->d_npartitions = v; continue; } if (tp == NULL) tp = empty; if (streq(cp, "disk")) { strncpy(lp->d_typename, tp, sizeof (lp->d_typename)); continue; } if (streq(cp, "label")) { strncpy(lp->d_packname, tp, sizeof (lp->d_packname)); continue; } if (streq(cp, "bytes/sector")) { v = strtoul(tp, NULL, 10); if (v == 0 || (v % DEV_BSIZE) != 0) { fprintf(stderr, "line %d: %s: bad sector size\n", lineno, tp); errors++; } else lp->d_secsize = v; continue; } if (streq(cp, "sectors/track")) { v = strtoul(tp, NULL, 10); #if (ULONG_MAX != 0xffffffffUL) if (v == 0 || v > 0xffffffff) { #else if (v == 0) { #endif fprintf(stderr, "line %d: %s: bad %s\n", lineno, tp, cp); errors++; } else lp->d_nsectors = v; continue; } if (streq(cp, "sectors/cylinder")) { v = strtoul(tp, NULL, 10); if (v == 0) { fprintf(stderr, "line %d: %s: bad %s\n", lineno, tp, cp); errors++; } else lp->d_secpercyl = v; continue; } if (streq(cp, "tracks/cylinder")) { v = strtoul(tp, NULL, 10); if (v == 0) { fprintf(stderr, "line %d: %s: bad %s\n", lineno, tp, cp); errors++; } else lp->d_ntracks = v; continue; } if (streq(cp, "cylinders")) { v = strtoul(tp, NULL, 10); if (v == 0) { fprintf(stderr, "line %d: %s: bad %s\n", lineno, tp, cp); errors++; } else lp->d_ncylinders = v; continue; } if (streq(cp, "sectors/unit")) { v = strtoul(tp, NULL, 10); if (v == 0) { fprintf(stderr, "line %d: %s: bad %s\n", lineno, tp, cp); errors++; } else lp->d_secperunit = v; continue; } if (streq(cp, "rpm")) { v = strtoul(tp, NULL, 10); if (v == 0 || v > USHRT_MAX) { fprintf(stderr, "line %d: %s: bad %s\n", lineno, tp, cp); errors++; } else lp->d_rpm = v; continue; } if (streq(cp, "interleave")) { v = strtoul(tp, NULL, 10); if (v == 0 || v > USHRT_MAX) { fprintf(stderr, "line %d: %s: bad %s\n", lineno, tp, cp); errors++; } else lp->d_interleave = v; continue; } if (streq(cp, "trackskew")) { v = strtoul(tp, NULL, 10); if (v > USHRT_MAX) { fprintf(stderr, "line %d: %s: bad %s\n", lineno, tp, cp); errors++; } else lp->d_trackskew = v; continue; } if (streq(cp, "cylinderskew")) { v = strtoul(tp, NULL, 10); if (v > USHRT_MAX) { fprintf(stderr, "line %d: %s: bad %s\n", lineno, tp, cp); errors++; } else lp->d_cylskew = v; continue; } if (streq(cp, "headswitch")) { v = strtoul(tp, NULL, 10); lp->d_headswitch = v; continue; } if (streq(cp, "track-to-track seek")) { v = strtoul(tp, NULL, 10); lp->d_trkseek = v; continue; } /* the ':' was removed above */ if (*cp < 'a' || *cp > MAX_PART || cp[1] != '\0') { fprintf(stderr, "line %d: %s: Unknown disklabel field\n", lineno, cp); errors++; continue; } /* Process a partition specification line. */ part = *cp - 'a'; if (part >= lp->d_npartitions) { fprintf(stderr, "line %d: partition name out of range a-%c: %s\n", lineno, 'a' + lp->d_npartitions - 1, cp); errors++; continue; } part_set[part] = 1; if (getasciipartspec(tp, lp, part, lineno) != 0) { errors++; break; } } errors += checklabel(lp); return (errors == 0); } #define NXTNUM(n) do { \ if (tp == NULL) { \ fprintf(stderr, "line %d: too few numeric fields\n", lineno); \ return (1); \ } else { \ cp = tp, tp = word(cp); \ (n) = strtoul(cp, NULL, 10); \ } \ } while (0) /* retain 1 character following number */ #define NXTWORD(w,n) do { \ if (tp == NULL) { \ fprintf(stderr, "line %d: too few numeric fields\n", lineno); \ return (1); \ } else { \ char *tmp; \ cp = tp, tp = word(cp); \ (n) = strtoul(cp, &tmp, 10); \ if (tmp) (w) = *tmp; \ } \ } while (0) /* * Read a partition line into partition `part' in the specified disklabel. * Return 0 on success, 1 on failure. */ int getasciipartspec(char *tp, struct disklabel32 *lp, int part, int lineno) { struct partition32 *pp; char *cp; const char **cpp; u_long v; pp = &lp->d_partitions[part]; cp = NULL; /* * size */ v = 0; NXTWORD(part_size_type[part],v); if (v == 0 && part_size_type[part] != '*') { fprintf(stderr, "line %d: %s: bad partition size\n", lineno, cp); return (1); } pp->p_size = v; /* * offset */ v = 0; NXTWORD(part_offset_type[part],v); if (v == 0 && part_offset_type[part] != '*' && part_offset_type[part] != '\0') { fprintf(stderr, "line %d: %s: bad partition offset\n", lineno, cp); return (1); } pp->p_offset = v; /* * fstype */ if (tp == NULL) { fprintf(stderr, "line %d: no filesystem type was specified\n", lineno); return(1); } cp = tp; tp = word(cp); for (cpp = fstypenames; cpp < &fstypenames[FSMAXTYPES]; cpp++) { if (*cpp && strcasecmp(*cpp, cp) == 0) break; } if (*cpp != NULL) { pp->p_fstype = cpp - fstypenames; } else { if (isdigit(*cp)) v = strtoul(cp, NULL, 10); else v = FSMAXTYPES; if (v >= FSMAXTYPES) { fprintf(stderr, "line %d: Warning, unknown filesystem type %s\n", lineno, cp); v = FS_UNUSED; } pp->p_fstype = v; } pp->p_fsize = 0; pp->p_frag = 0; pp->p_cpg = 0; cp = tp; if (tp) { fprintf(stderr, "line %d: Warning, fragment, block, " "and bps/cpg fields are no\n" "longer supported and must be specified " "via newfs options instead.\n", lineno); } return(0); } /* * Check disklabel for errors and fill in * derived fields according to supplied values. */ int checklabel(struct disklabel32 *lp) { struct partition32 *pp; int i, errors = 0; char part; u_long base_offset, needed, total_size, total_percent, current_offset; long free_space; int seen_default_offset; int hog_part; int j; struct partition32 *pp2; if (lp->d_secsize == 0) { fprintf(stderr, "sector size 0\n"); return (1); } if (lp->d_nsectors == 0) { fprintf(stderr, "sectors/track 0\n"); return (1); } if (lp->d_ntracks == 0) { fprintf(stderr, "tracks/cylinder 0\n"); return (1); } if (lp->d_ncylinders == 0) { fprintf(stderr, "cylinders/unit 0\n"); errors++; } if (lp->d_rpm == 0) Warning("revolutions/minute 0"); if (lp->d_secpercyl == 0) lp->d_secpercyl = lp->d_nsectors * lp->d_ntracks; if (lp->d_secperunit == 0) lp->d_secperunit = lp->d_secpercyl * lp->d_ncylinders; if (lp->d_bbsize == 0) { fprintf(stderr, "boot block size 0\n"); errors++; } else if (lp->d_bbsize % lp->d_secsize) Warning("boot block size %% sector-size != 0"); if (lp->d_sbsize == 0) { fprintf(stderr, "super block size 0\n"); errors++; } else if (lp->d_sbsize % lp->d_secsize) Warning("super block size %% sector-size != 0"); if (lp->d_npartitions > MAXPARTITIONS32) Warning("number of partitions (%lu) > MAXPARTITIONS (%d)", (u_long)lp->d_npartitions, MAXPARTITIONS32); /* first allocate space to the partitions, then offsets */ total_size = 0; /* in sectors */ total_percent = 0; /* in percent */ hog_part = -1; /* find all fixed partitions */ for (i = 0; i < lp->d_npartitions; i++) { pp = &lp->d_partitions[i]; if (part_set[i]) { if (part_size_type[i] == '*') { if (i == RAW_PART) { pp->p_size = lp->d_secperunit; } else { if (part_offset_type[i] != '*') { if (total_size < pp->p_offset) total_size = pp->p_offset; } if (hog_part != -1) Warning("Too many '*' partitions (%c and %c)", hog_part + 'a',i + 'a'); else hog_part = i; } } else { off_t size; size = pp->p_size; switch (part_size_type[i]) { case '%': total_percent += size; break; case 't': case 'T': size *= 1024ULL; /* FALLTHROUGH */ case 'g': case 'G': size *= 1024ULL; /* FALLTHROUGH */ case 'm': case 'M': size *= 1024ULL; /* FALLTHROUGH */ case 'k': case 'K': size *= 1024ULL; break; case '\0': break; default: Warning("unknown size specifier '%c' (K/M/G/T are valid)",part_size_type[i]); break; } /* don't count %'s yet */ if (part_size_type[i] != '%') { /* * for all not in sectors, convert to * sectors */ if (part_size_type[i] != '\0') { if (size % lp->d_secsize != 0) Warning("partition %c not an integer number of sectors", i + 'a'); size /= lp->d_secsize; pp->p_size = size; } /* else already in sectors */ if (i != RAW_PART) total_size += size; } } } } /* Find out the total free space, excluding the boot block area. */ base_offset = BBSIZE / lp->d_secsize; free_space = 0; for (i = 0; i < lp->d_npartitions; i++) { pp = &lp->d_partitions[i]; if (!part_set[i] || i == RAW_PART || part_size_type[i] == '%' || part_size_type[i] == '*') continue; if (pp->p_offset > base_offset) free_space += pp->p_offset - base_offset; if (pp->p_offset + pp->p_size > base_offset) base_offset = pp->p_offset + pp->p_size; } if (base_offset < lp->d_secperunit) free_space += lp->d_secperunit - base_offset; /* handle % partitions - note %'s don't need to add up to 100! */ if (total_percent != 0) { if (total_percent > 100) { fprintf(stderr,"total percentage %lu is greater than 100\n", total_percent); errors++; } if (free_space > 0) { for (i = 0; i < lp->d_npartitions; i++) { pp = &lp->d_partitions[i]; if (part_set[i] && part_size_type[i] == '%') { /* careful of overflows! and integer roundoff */ pp->p_size = ((double)pp->p_size/100) * free_space; total_size += pp->p_size; /* FIX we can lose a sector or so due to roundoff per partition. A more complex algorithm could avoid that */ } } } else { fprintf(stderr, "%ld sectors available to give to '*' and '%%' partitions\n", free_space); errors++; /* fix? set all % partitions to size 0? */ } } /* give anything remaining to the hog partition */ if (hog_part != -1) { /* * Find the range of offsets usable by '*' partitions around * the hog partition and how much space they need. */ needed = 0; base_offset = BBSIZE / lp->d_secsize; for (i = hog_part - 1; i >= 0; i--) { pp = &lp->d_partitions[i]; if (!part_set[i] || i == RAW_PART) continue; if (part_offset_type[i] == '*') { needed += pp->p_size; continue; } base_offset = pp->p_offset + pp->p_size; break; } current_offset = lp->d_secperunit; for (i = lp->d_npartitions - 1; i > hog_part; i--) { pp = &lp->d_partitions[i]; if (!part_set[i] || i == RAW_PART) continue; if (part_offset_type[i] == '*') { needed += pp->p_size; continue; } current_offset = pp->p_offset; } if (current_offset - base_offset <= needed) { fprintf(stderr, "Cannot find space for partition %c\n", hog_part + 'a'); fprintf(stderr, "Need more than %lu sectors between %lu and %lu\n", needed, base_offset, current_offset); errors++; lp->d_partitions[hog_part].p_size = 0; } else { lp->d_partitions[hog_part].p_size = current_offset - base_offset - needed; total_size += lp->d_partitions[hog_part].p_size; } } /* Now set the offsets for each partition */ current_offset = BBSIZE / lp->d_secsize; /* in sectors */ seen_default_offset = 0; for (i = 0; i < lp->d_npartitions; i++) { part = 'a' + i; pp = &lp->d_partitions[i]; if (part_set[i]) { if (part_offset_type[i] == '*') { if (i == RAW_PART) { pp->p_offset = 0; } else { pp->p_offset = current_offset; seen_default_offset = 1; } } else { /* allow them to be out of order for old-style tables */ if (pp->p_offset < current_offset && seen_default_offset && i != RAW_PART && pp->p_fstype != FS_VINUM) { fprintf(stderr, "Offset %ld for partition %c overlaps previous partition which ends at %lu\n", (long)pp->p_offset,i+'a',current_offset); fprintf(stderr, "Labels with any *'s for offset must be in ascending order by sector\n"); errors++; } else if (pp->p_offset != current_offset && i != RAW_PART && seen_default_offset) { /* * this may give unneeded warnings if * partitions are out-of-order */ Warning( "Offset %ld for partition %c doesn't match expected value %ld", (long)pp->p_offset, i + 'a', current_offset); } } if (i != RAW_PART) current_offset = pp->p_offset + pp->p_size; } } for (i = 0; i < lp->d_npartitions; i++) { part = 'a' + i; pp = &lp->d_partitions[i]; if (pp->p_size == 0 && pp->p_offset != 0) Warning("partition %c: size 0, but offset %lu", part, (u_long)pp->p_offset); #ifdef notdef if (pp->p_size % lp->d_secpercyl) Warning("partition %c: size %% cylinder-size != 0", part); if (pp->p_offset % lp->d_secpercyl) Warning("partition %c: offset %% cylinder-size != 0", part); #endif if (pp->p_offset > lp->d_secperunit) { fprintf(stderr, "partition %c: offset past end of unit\n", part); errors++; } if (pp->p_offset + pp->p_size > lp->d_secperunit) { fprintf(stderr, "partition %c: partition extends past end of unit\n", part); errors++; } if (i == RAW_PART) { if (pp->p_fstype != FS_UNUSED) Warning("partition %c is not marked as unused!",part); if (pp->p_offset != 0) Warning("partition %c doesn't start at 0!",part); if (pp->p_size != lp->d_secperunit) Warning("partition %c doesn't cover the whole unit!",part); if ((pp->p_fstype != FS_UNUSED) || (pp->p_offset != 0) || (pp->p_size != lp->d_secperunit)) { Warning("An incorrect partition %c may cause problems for " "standard system utilities",part); } } /* check for overlaps */ /* this will check for all possible overlaps once and only once */ for (j = 0; j < i; j++) { pp2 = &lp->d_partitions[j]; if (j != RAW_PART && i != RAW_PART && pp->p_fstype != FS_VINUM && pp2->p_fstype != FS_VINUM && part_set[i] && part_set[j]) { if (pp2->p_offset < pp->p_offset + pp->p_size && (pp2->p_offset + pp2->p_size > pp->p_offset || pp2->p_offset >= pp->p_offset)) { fprintf(stderr,"partitions %c and %c overlap!\n", j + 'a', i + 'a'); errors++; } } } } for (; i < 8 || i < lp->d_npartitions; i++) { part = 'a' + i; pp = &lp->d_partitions[i]; if (pp->p_size || pp->p_offset) Warning("unused partition %c: size %d offset %lu", 'a' + i, pp->p_size, (u_long)pp->p_offset); } return (errors); } /* * When operating on a "virgin" disk, try getting an initial label * from the associated device driver. This might work for all device * drivers that are able to fetch some initial device parameters * without even having access to a (BSD) disklabel, like SCSI disks, * most IDE drives, or vn devices. */ static struct disklabel32 dlab; struct disklabel32 * getvirginlabel(void) { struct partinfo info; struct disklabel32 *dl = &dlab; int f; if ((f = open(dkname, O_RDONLY)) == -1) { warn("cannot open %s", dkname); return (NULL); } /* * Check to see if the media is too big for a 32 bit disklabel. */ if (ioctl(f, DIOCGPART, &info) == 0) { if (info.media_size >= 0x100000000ULL * 512) { warnx("The media is too large for a 32 bit disklabel," " please use disklabel64."); return (NULL); } } /* * Generate a virgin disklabel via ioctl */ if (ioctl(f, DIOCGDVIRGIN32, dl) < 0) { l_perror("ioctl DIOCGDVIRGIN32"); close(f); return(NULL); } close(f); return (dl); } struct disklabel32 * getdisklabelfromdisktab(const char *name) { struct disktab *dt; struct disklabel32 *dl = &dlab; int i; if ((dt = getdisktabbyname(name)) == NULL) return(NULL); dl->d_magic = DISKMAGIC32; dl->d_type = dt->d_typeid; dl->d_subtype = 0; dl->d_secsize = dt->d_media_blksize; dl->d_nsectors = dt->d_secpertrack; dl->d_ntracks = dt->d_nheads; dl->d_ncylinders = dt->d_ncylinders; dl->d_secpercyl = dt->d_secpercyl; dl->d_secperunit = dt->d_media_blocks; dl->d_rpm = dt->d_rpm; dl->d_interleave = dt->d_interleave; dl->d_trackskew = dt->d_trackskew; dl->d_cylskew = dt->d_cylskew; dl->d_headswitch = dt->d_headswitch; dl->d_trkseek = dt->d_trkseek; dl->d_magic2 = DISKMAGIC32; dl->d_npartitions = dt->d_npartitions; dl->d_bbsize = dt->d_bbsize; dl->d_sbsize = dt->d_sbsize; for (i = 0; i < dt->d_npartitions; ++i) { struct partition32 *dlp = &dl->d_partitions[i]; struct dt_partition *dtp = &dt->d_partitions[i]; dlp->p_size = dtp->p_size; dlp->p_offset = dtp->p_offset; dlp->p_fsize = dtp->p_fsize; dlp->p_fstype = dtp->p_fstype; dlp->p_frag = dtp->p_fsize; } return(dl); } /* * If we are installing a boot program that doesn't fit in d_bbsize * we need to mark those partitions that the boot overflows into. * This allows newfs to prevent creation of a filesystem where it might * clobber bootstrap code. */ void setbootflag(struct disklabel32 *lp) { struct partition32 *pp; int i, errors = 0; char part; u_long boffset; if (bootbuf == NULL) return; boffset = bootsize / lp->d_secsize; for (i = 0; i < lp->d_npartitions; i++) { part = 'a' + i; pp = &lp->d_partitions[i]; if (pp->p_size == 0) continue; if (boffset <= pp->p_offset) { if (pp->p_fstype == FS_BOOT) pp->p_fstype = FS_UNUSED; } else if (pp->p_fstype != FS_BOOT) { if (pp->p_fstype != FS_UNUSED) { fprintf(stderr, "boot overlaps used partition %c\n", part); errors++; } else { pp->p_fstype = FS_BOOT; Warning("boot overlaps partition %c, %s", part, "marked as FS_BOOT"); } } } if (errors) errx(4, "cannot install boot program"); } /*VARARGS1*/ void Warning(const char *fmt, ...) { va_list ap; fprintf(stderr, "Warning, "); va_start(ap, fmt); vfprintf(stderr, fmt, ap); fprintf(stderr, "\n"); va_end(ap); }
int main(int argc, char *argv[]) { struct disklabel32 *lp; FILE *t; int ch, f = 0, flag, error = 0; char *name = NULL; while ((ch = getopt(argc, argv, OPTIONS)) != -1) switch (ch) { #if NUMBOOT > 0 case 'B': ++installboot; break; case 'b': xxboot = optarg; break; case 'f': forceflag = 1; slice_start_lba = strtoul(optarg, NULL, 0); break; #if NUMBOOT > 1 case 's': bootxx = optarg; break; #endif #endif case 'N': if (op != UNSPEC) usage(); op = NOWRITE; break; case 'n': disable_write = 1; break; case 'R': if (op != UNSPEC) usage(); op = RESTORE; break; case 'W': if (op != UNSPEC) usage(); op = WRITEABLE; break; case 'e': if (op != UNSPEC) usage(); op = EDIT; break; case 'r': ++rflag; break; case 'w': if (op != UNSPEC) usage(); op = WRITE; break; #ifdef DEBUG case 'd': debug++; break; #endif case '?': default: usage(); } argc -= optind; argv += optind; #if NUMBOOT > 0 if (installboot) { rflag++; if (op == UNSPEC) op = WRITEBOOT; } else { if (op == UNSPEC) op = READ; xxboot = bootxx = NULL; } #else if (op == UNSPEC) op = READ; #endif if (argc < 1) usage(); dkname = getdevpath(argv[0], 0); specname = dkname; f = open(specname, op == READ ? O_RDONLY : O_RDWR); if (f < 0) err(4, "%s", specname); switch(op) { case UNSPEC: break; case EDIT: if (argc != 1) usage(); lp = readlabel(f); error = edit(lp, f); break; case NOWRITE: flag = 0; if (ioctl(f, DIOCWLABEL, (char *)&flag) < 0) err(4, "ioctl DIOCWLABEL"); break; case READ: if (argc != 1) usage(); lp = readlabel(f); display(stdout, lp); error = checklabel(lp); if (checkoldboot(f, NULL)) warnx("Warning, old bootblocks detected, install new bootblocks & reinstall the disklabel"); break; case RESTORE: #if NUMBOOT > 0 if (installboot && argc == 3) { makelabel(argv[2], 0, &lab); argc--; /* * We only called makelabel() for its side effect * of setting the bootstrap file names. Discard * all changes to `lab' so that all values in the * final label come from the ASCII label. */ bzero((char *)&lab, sizeof(lab)); } #endif if (argc != 2) usage(); if (!(t = fopen(argv[1], "r"))) err(4, "%s", argv[1]); if (!getasciilabel(t, &lab)) exit(1); lp = makebootarea(bootarea, &lab, f); *lp = lab; error = writelabel(f, bootarea, lp); break; case WRITE: if (argc == 3) { name = argv[2]; argc--; } if (argc != 2) usage(); makelabel(argv[1], name, &lab); lp = makebootarea(bootarea, &lab, f); *lp = lab; if (checklabel(lp) == 0) error = writelabel(f, bootarea, lp); break; case WRITEABLE: flag = 1; if (ioctl(f, DIOCWLABEL, (char *)&flag) < 0) err(4, "ioctl DIOCWLABEL"); break; #if NUMBOOT > 0 case WRITEBOOT: { struct disklabel32 tlab; lp = readlabel(f); tlab = *lp; if (argc == 2) makelabel(argv[1], 0, &lab); lp = makebootarea(bootarea, &lab, f); *lp = tlab; if (checklabel(lp) == 0) error = writelabel(f, bootarea, lp); break; } #endif } exit(error); }
int main(int argc, char *argv[]) { int ch, f, error = 0; FILE *t; char *autotable = NULL; getphysmem(); while ((ch = getopt(argc, argv, "AEf:F:hRcdenp:tT:vw")) != -1) switch (ch) { case 'A': aflag = 1; break; case 'R': if (op != UNSPEC) usage(); op = RESTORE; break; case 'c': cflag = 1; break; case 'd': dflag = 1; break; case 'e': if (op != UNSPEC) usage(); op = EDIT; break; case 'E': if (op != UNSPEC) usage(); op = EDITOR; break; case 'f': fstabfile = optarg; uidflag = 0; break; case 'F': fstabfile = optarg; uidflag = 1; break; case 'h': print_unit = '*'; break; case 't': tflag = 1; break; case 'T': autotable = optarg; break; case 'w': if (op != UNSPEC) usage(); op = WRITE; break; case 'p': if (strchr("bckmgtBCKMGT", optarg[0]) == NULL || optarg[1] != '\0') { fprintf(stderr, "Valid units are bckmgt\n"); return 1; } print_unit = tolower((unsigned char)optarg[0]); break; case 'n': donothing = 1; break; case 'v': verbose = 1; break; case '?': default: usage(); } argc -= optind; argv += optind; if (op == UNSPEC) op = READ; if (argc < 1 || (fstabfile && !(op == EDITOR || op == RESTORE || aflag))) usage(); if (argv[0] == NULL) usage(); dkname = argv[0]; f = opendev(dkname, (op == READ ? O_RDONLY : O_RDWR), OPENDEV_PART, &specname); if (f < 0) err(4, "%s", specname); if (op != WRITE || aflag || dflag) { readlabel(f); if (op == EDIT || op == EDITOR || aflag) { if (pledge("stdio rpath wpath cpath disklabel proc " "exec", NULL) == -1) err(1, "pledge"); } else if (fstabfile) { if (pledge("stdio rpath wpath cpath disklabel", NULL) == -1) err(1, "pledge"); } else { if (pledge("stdio rpath wpath disklabel", NULL) == -1) err(1, "pledge"); } if (autotable != NULL) parse_autotable(autotable); parselabel(); } else if (argc == 2 || argc == 3) { /* Ensure f is a disk device before pledging. */ if (ioctl(f, DIOCGDINFO, &lab) < 0) err(4, "ioctl DIOCGDINFO"); if (pledge("stdio rpath wpath disklabel", NULL) == -1) err(1, "pledge"); makelabel(argv[1], argc == 3 ? argv[2] : NULL, &lab); } else usage(); switch (op) { case EDIT: if (argc != 1) usage(); error = edit(&lab, f); break; case EDITOR: if (argc != 1) usage(); error = editor(f); break; case READ: if (argc != 1) usage(); if (pledge("stdio", NULL) == -1) err(1, "pledge"); if (tflag) makedisktab(stdout, &lab); else display(stdout, &lab, print_unit, 1); error = checklabel(&lab); break; case RESTORE: if (argc < 2 || argc > 3) usage(); if (!(t = fopen(argv[1], "r"))) err(4, "%s", argv[1]); error = getasciilabel(t, &lab); memset(&lab.d_uid, 0, sizeof(lab.d_uid)); if (error == 0) { error = writelabel(f, &lab); if (error == 0) { if (ioctl(f, DIOCGDINFO, &lab) < 0) err(4, "ioctl DIOCGDINFO"); mpsave(&lab); } } fclose(t); break; case WRITE: error = checklabel(&lab); if (error == 0) error = writelabel(f, &lab); break; default: break; } return error; }