From 79569512474f6bd5422c6018eb322faf0d772a10 Mon Sep 17 00:00:00 2001 From: christos Date: Sat, 26 May 2001 19:48:32 +0000 Subject: [PATCH] - KNF printlabel.c - add a function to print only one partition's info. - print the partition information if it was modified in interactive mode. - improve on the chaining code. [still assumes that partition offsets increase monotonically]. We could check for overlap too. --- sbin/disklabel/extern.h | 3 +- sbin/disklabel/interact.c | 24 ++-- sbin/disklabel/printlabel.c | 224 +++++++++++++++++++----------------- 3 files changed, 132 insertions(+), 119 deletions(-) diff --git a/sbin/disklabel/extern.h b/sbin/disklabel/extern.h index 6bc7e64674d0..995203c921dd 100644 --- a/sbin/disklabel/extern.h +++ b/sbin/disklabel/extern.h @@ -1,4 +1,4 @@ -/* $NetBSD: extern.h,v 1.6 2000/12/24 07:08:03 lukem Exp $ */ +/* $NetBSD: extern.h,v 1.7 2001/05/26 19:48:32 christos Exp $ */ /* * Copyright (c) 1997 Christos Zoulas. All rights reserved. @@ -33,6 +33,7 @@ int writelabel(int, const char *, struct disklabel *); int checklabel(struct disklabel *); void showinfo(FILE *, struct disklabel *, const char *); void showpartitions(FILE *, struct disklabel *, int); +void showpartition(FILE *, struct disklabel *, int, int); void interact(struct disklabel *, int); extern char bootarea[]; diff --git a/sbin/disklabel/interact.c b/sbin/disklabel/interact.c index 9ff76bebce97..4c58599f68c5 100644 --- a/sbin/disklabel/interact.c +++ b/sbin/disklabel/interact.c @@ -1,4 +1,4 @@ -/* $NetBSD: interact.c,v 1.17 2000/12/24 07:08:03 lukem Exp $ */ +/* $NetBSD: interact.c,v 1.18 2001/05/26 19:48:32 christos Exp $ */ /* * Copyright (c) 1997 Christos Zoulas. All rights reserved. @@ -31,7 +31,7 @@ #include #ifndef lint -__RCSID("$NetBSD: interact.c,v 1.17 2000/12/24 07:08:03 lukem Exp $"); +__RCSID("$NetBSD: interact.c,v 1.18 2001/05/26 19:48:32 christos Exp $"); #endif /* lint */ #include @@ -438,13 +438,15 @@ cmd_part(struct disklabel *lp, char *s, int fd) char line[BUFSIZ]; char def[BUFSIZ]; int part; - struct partition *p; + struct partition *p, ps; part = s[0] - 'a'; p = &lp->d_partitions[part]; if (part >= lp->d_npartitions) lp->d_npartitions = part + 1; + (void)memcpy(&ps, p, sizeof(ps)); + for (;;) { i = p->p_fstype; if (i < 0 || i >= FSMAXTYPES) @@ -502,16 +504,18 @@ cmd_part(struct disklabel *lp, char *s, int fd) } if (chaining) { - int offs = p[0].p_offset + p[0].p_size; - p = lp->d_partitions; - part = getrawpartition(); - for (i = 1; i < lp->d_npartitions; i++) { - if (i != part && p[i].p_fstype) { - p[i].p_offset = offs; - offs = p[i].p_offset + p[i].p_size; + int offs = -1; + struct partition *cp = lp->d_partitions; + for (i = 0; i < lp->d_npartitions; i++) { + if (cp[i].p_fstype != FS_UNUSED) { + if (offs != -1) + cp[i].p_offset = offs; + offs = cp[i].p_offset + cp[i].p_size; } } } + if (memcmp(&ps, p, sizeof(ps))) + showpartition(stdout, lp, part, Cflag); } diff --git a/sbin/disklabel/printlabel.c b/sbin/disklabel/printlabel.c index d167fa7b1987..9af0b1de2ee0 100644 --- a/sbin/disklabel/printlabel.c +++ b/sbin/disklabel/printlabel.c @@ -1,4 +1,4 @@ -/* $NetBSD: printlabel.c,v 1.1 2000/12/24 07:08:03 lukem Exp $ */ +/* $NetBSD: printlabel.c,v 1.2 2001/05/26 19:48:32 christos Exp $ */ /* * Copyright (c) 1987, 1993 @@ -38,7 +38,7 @@ #include #ifndef lint -__RCSID("$NetBSD: printlabel.c,v 1.1 2000/12/24 07:08:03 lukem Exp $"); +__RCSID("$NetBSD: printlabel.c,v 1.2 2001/05/26 19:48:32 christos Exp $"); #endif /* not lint */ #include @@ -57,131 +57,139 @@ showinfo(FILE *f, struct disklabel *lp, const char *specname) { int i, j; - (void) fprintf(f, "# %s:\n", specname); + (void)fprintf(f, "# %s:\n", specname); if ((unsigned) lp->d_type < DKMAXTYPES) - (void) fprintf(f, "type: %s\n", dktypenames[lp->d_type]); + (void)fprintf(f, "type: %s\n", dktypenames[lp->d_type]); else - (void) fprintf(f, "type: %d\n", lp->d_type); - (void) fprintf(f, "disk: %.*s\n", (int) sizeof(lp->d_typename), + (void)fprintf(f, "type: %d\n", lp->d_type); + (void)fprintf(f, "disk: %.*s\n", (int) sizeof(lp->d_typename), lp->d_typename); - (void) fprintf(f, "label: %.*s\n", (int) sizeof(lp->d_packname), + (void)fprintf(f, "label: %.*s\n", (int) sizeof(lp->d_packname), lp->d_packname); - (void) fprintf(f, "flags:"); + (void)fprintf(f, "flags:"); if (lp->d_flags & D_REMOVABLE) - (void) fprintf(f, " removable"); + (void)fprintf(f, " removable"); if (lp->d_flags & D_ECC) - (void) fprintf(f, " ecc"); + (void)fprintf(f, " ecc"); if (lp->d_flags & D_BADSECT) - (void) fprintf(f, " badsect"); - (void) fprintf(f, "\n"); - (void) fprintf(f, "bytes/sector: %ld\n", (long) lp->d_secsize); - (void) fprintf(f, "sectors/track: %ld\n", (long) lp->d_nsectors); - (void) fprintf(f, "tracks/cylinder: %ld\n", (long) lp->d_ntracks); - (void) fprintf(f, "sectors/cylinder: %ld\n", (long) lp->d_secpercyl); - (void) fprintf(f, "cylinders: %ld\n", (long) lp->d_ncylinders); - (void) fprintf(f, "total sectors: %ld\n", (long) lp->d_secperunit); - (void) fprintf(f, "rpm: %ld\n", (long) lp->d_rpm); - (void) fprintf(f, "interleave: %ld\n", (long) lp->d_interleave); - (void) fprintf(f, "trackskew: %ld\n", (long) lp->d_trackskew); - (void) fprintf(f, "cylinderskew: %ld\n", (long) lp->d_cylskew); - (void) fprintf(f, "headswitch: %ld\t\t# microseconds\n", - (long) lp->d_headswitch); - (void) fprintf(f, "track-to-track seek: %ld\t# microseconds\n", - (long) lp->d_trkseek); - (void) fprintf(f, "drivedata: "); + (void)fprintf(f, " badsect"); + (void)fprintf(f, "\n"); + (void)fprintf(f, "bytes/sector: %ld\n", (long) lp->d_secsize); + (void)fprintf(f, "sectors/track: %ld\n", (long) lp->d_nsectors); + (void)fprintf(f, "tracks/cylinder: %ld\n", (long) lp->d_ntracks); + (void)fprintf(f, "sectors/cylinder: %ld\n", (long) lp->d_secpercyl); + (void)fprintf(f, "cylinders: %ld\n", (long) lp->d_ncylinders); + (void)fprintf(f, "total sectors: %ld\n", (long) lp->d_secperunit); + (void)fprintf(f, "rpm: %ld\n", (long) lp->d_rpm); + (void)fprintf(f, "interleave: %ld\n", (long) lp->d_interleave); + (void)fprintf(f, "trackskew: %ld\n", (long) lp->d_trackskew); + (void)fprintf(f, "cylinderskew: %ld\n", (long) lp->d_cylskew); + (void)fprintf(f, "headswitch: %ld\t\t# microseconds\n", + (long)lp->d_headswitch); + (void)fprintf(f, "track-to-track seek: %ld\t# microseconds\n", + (long)lp->d_trkseek); + (void)fprintf(f, "drivedata: "); for (i = NDDATA - 1; i >= 0; i--) if (lp->d_drivedata[i]) break; if (i < 0) i = 0; for (j = 0; j <= i; j++) - (void) fprintf(f, "%d ", lp->d_drivedata[j]); - (void) fprintf(f, "\n\n"); - (void) fflush(f); + (void)fprintf(f, "%d ", lp->d_drivedata[j]); + (void)fprintf(f, "\n\n"); + (void)fflush(f); +} + +void +showpartition(FILE *f, struct disklabel *lp, int i, int ctsformat) +{ + struct partition *pp = lp->d_partitions + i; + if (pp->p_size == 0) + return; + + if (ctsformat && lp->d_secpercyl && lp->d_nsectors) { + char sbuf[64], obuf[64]; + + (void)snprintf(sbuf, sizeof(sbuf), "%d/%d/%d", + pp->p_size/lp->d_secpercyl, + (pp->p_size%lp->d_secpercyl) / lp->d_nsectors, + pp->p_size%lp->d_nsectors); + + (void)snprintf(obuf, sizeof(obuf), "%d/%d/%d", + pp->p_offset/lp->d_secpercyl, + (pp->p_offset%lp->d_secpercyl) / lp->d_nsectors, + pp->p_offset%lp->d_nsectors); + + (void)fprintf(f, " %c: %9s %9s ", + 'a' + i, sbuf, obuf); + } else { + (void)fprintf(f, " %c: %9d %9d ", 'a' + i, + pp->p_size, pp->p_offset); + } + + if ((unsigned) pp->p_fstype < FSMAXTYPES) + (void)fprintf(f, "%10.10s", fstypenames[pp->p_fstype]); + else + (void)fprintf(f, "%10d", pp->p_fstype); + + switch (pp->p_fstype) { + case FS_UNUSED: /* XXX */ + (void)fprintf(f, " %5d %5d %5.5s ", + pp->p_fsize, pp->p_fsize * pp->p_frag, ""); + break; + + case FS_BSDFFS: + case FS_ADOS: + (void)fprintf(f, " %5d %5d %5d ", + pp->p_fsize, pp->p_fsize * pp->p_frag, pp->p_cpg); + break; + + case FS_BSDLFS: + (void)fprintf(f, " %5d %5d %5d ", + pp->p_fsize, pp->p_fsize * pp->p_frag, pp->p_sgs); + break; + + case FS_EX2FS: + (void)fprintf(f, " %5d %5d ", + pp->p_fsize, pp->p_fsize * pp->p_frag); + break; + + default: + (void)fprintf(f, "%20.20s", ""); + break; + } + if (lp->d_secpercyl != 0) { + (void)fprintf(f, " # (Cyl. %4d", + pp->p_offset / lp->d_secpercyl); + + if (pp->p_offset % lp->d_secpercyl) + putc('*', f); + else + putc(' ', f); + + (void)fprintf(f, "- %d", + (pp->p_offset + + pp->p_size + lp->d_secpercyl - 1) / + lp->d_secpercyl - 1); + + if ((pp->p_offset + pp->p_size) % lp->d_secpercyl) + putc('*', f); + + (void)fprintf(f, ")\n"); + } else + (void)fprintf(f, "\n"); } void showpartitions(FILE *f, struct disklabel *lp, int ctsformat) { int i; - struct partition *pp; - (void) fprintf(f, "%d partitions:\n", lp->d_npartitions); - (void) fprintf(f, - "# size offset fstype [fsize bsize cpg/sgs]\n"); - pp = lp->d_partitions; - for (i = 0; i < lp->d_npartitions; i++, pp++) { - if (pp->p_size) { - if (ctsformat && lp->d_secpercyl && lp->d_nsectors) { - char sbuf[32], obuf[32]; - sprintf(sbuf, "%d/%d/%d", - pp->p_size/lp->d_secpercyl, - (pp->p_size%lp->d_secpercyl) / - lp->d_nsectors, - pp->p_size%lp->d_nsectors); + (void)fprintf(f, "%d partitions:\n", lp->d_npartitions); + (void)fprintf(f, + "# size offset fstype [fsize bsize cpg/sgs]\n"); - sprintf(obuf, "%d/%d/%d", - pp->p_offset/lp->d_secpercyl, - (pp->p_offset%lp->d_secpercyl) / - lp->d_nsectors, - pp->p_offset%lp->d_nsectors); - (void) fprintf(f, " %c: %8s %8s ", - 'a' + i, sbuf, obuf); - } else - (void) fprintf(f, " %c: %8d %8d ", 'a' + i, - pp->p_size, pp->p_offset); - if ((unsigned) pp->p_fstype < FSMAXTYPES) - (void) fprintf(f, "%10.10s", - fstypenames[pp->p_fstype]); - else - (void) fprintf(f, "%10d", pp->p_fstype); - switch (pp->p_fstype) { - - case FS_UNUSED: /* XXX */ - (void) fprintf(f, " %5d %5d %5.5s ", - pp->p_fsize, pp->p_fsize * pp->p_frag, ""); - break; - - case FS_BSDFFS: - case FS_ADOS: - (void) fprintf(f, " %5d %5d %5d ", - pp->p_fsize, pp->p_fsize * pp->p_frag, - pp->p_cpg); - break; - - case FS_BSDLFS: - (void) fprintf(f, " %5d %5d %5d ", - pp->p_fsize, pp->p_fsize * pp->p_frag, - pp->p_sgs); - break; - - case FS_EX2FS: - (void) fprintf(f, " %5d %5d ", - pp->p_fsize, pp->p_fsize * pp->p_frag); - break; - - default: - (void) fprintf(f, "%22.22s", ""); - break; - } - if (lp->d_secpercyl != 0) { - (void) fprintf(f, " # (Cyl. %4d", - pp->p_offset / lp->d_secpercyl); - if (pp->p_offset % lp->d_secpercyl) - putc('*', f); - else - putc(' ', f); - (void) fprintf(f, "- %d", - (pp->p_offset + - pp->p_size + lp->d_secpercyl - 1) / - lp->d_secpercyl - 1); - if ((pp->p_offset + pp->p_size) - % lp->d_secpercyl) - putc('*', f); - (void) fprintf(f, ")\n"); - } else - (void) fprintf(f, "\n"); - } - } - (void) fflush(f); + for (i = 0; i < lp->d_npartitions; i++) + showpartition(f, lp, i, ctsformat); + (void)fflush(f); }