diff options
author | Kenneth R Westerback <krw@cvs.openbsd.org> | 2023-01-21 16:20:26 +0000 |
---|---|---|
committer | Kenneth R Westerback <krw@cvs.openbsd.org> | 2023-01-21 16:20:26 +0000 |
commit | 276ab4743706d64e032a6868dbf7a0bf5a988b88 (patch) | |
tree | 827a8758c80adbdab65fba3baee829b6b4853908 /sbin | |
parent | 294d0e91b021f67710859d090ab67fa13da2df06 (diff) |
editor_resize(), editor_change(), editor_modify() and
editor_name() use identical logic to choose a partition to act
on, with some minor verbiage differences.
Extract getpartno() to contain/tighten this logic and shrink the
editor_* functions by using it.
No intentional functional change but for verbiage tweaks.
Diffstat (limited to 'sbin')
-rw-r--r-- | sbin/disklabel/editor.c | 118 |
1 files changed, 50 insertions, 68 deletions
diff --git a/sbin/disklabel/editor.c b/sbin/disklabel/editor.c index b584c558d14..cd1a13e8019 100644 --- a/sbin/disklabel/editor.c +++ b/sbin/disklabel/editor.c @@ -1,4 +1,4 @@ -/* $OpenBSD: editor.c,v 1.394 2023/01/20 17:47:07 krw Exp $ */ +/* $OpenBSD: editor.c,v 1.395 2023/01/21 16:20:25 krw Exp $ */ /* * Copyright (c) 1997-2000 Todd C. Miller <millert@openbsd.org> @@ -149,6 +149,7 @@ char *getstring(const char *, const char *, const char *); u_int64_t getuint64(const struct disklabel *, char *, char *, u_int64_t, u_int64_t, int *); u_int64_t getnumber(const char *, const char *, u_int32_t, u_int32_t); +int getpartno(const struct disklabel *, const char *, const char *); int has_overlap(struct disklabel *); int partition_cmp(const void *, const void *); const struct partition **sort_partitions(const struct disklabel *, int); @@ -706,25 +707,11 @@ editor_resize(struct disklabel *lp, const char *p) label = *lp; - /* Change which partition? */ - if (p == NULL) - p = getstring("partition to resize", - "The letter of the partition to name, a - p.", NULL); - if (p == NULL) + if ((partno = getpartno(&label, p, "resize")) == -1) return; - partno = p[0] - 'a'; - if (partno < 0 || partno == RAW_PART || partno >= lp->d_npartitions) { - fprintf(stderr, "Partition must be between 'a' and '%c' " - "(excluding 'c').\n", 'a' + lp->d_npartitions - 1); - return; - } pp = &label.d_partitions[partno]; sz = DL_GETPSIZE(pp); - if (sz == 0) { - fputs("No such partition\n", stderr); - return; - } if (pp->p_fstype != FS_BSDFFS && pp->p_fstype != FS_SWAP) { fputs("Cannot resize spoofed partition\n", stderr); return; @@ -902,27 +889,10 @@ editor_add(struct disklabel *lp, const char *p) void editor_name(const struct disklabel *lp, const char *p) { - const struct partition *pp; int partno; - /* Change which partition? */ - if (p == NULL) - p = getstring("partition to name", - "The letter of the partition to name, a - p.", NULL); - if (p == NULL) - return; - partno = p[0] - 'a'; - if (partno < 0 || partno == RAW_PART || partno >= lp->d_npartitions) { - fprintf(stderr, "Partition must be between 'a' and '%c' " - "(excluding 'c').\n", 'a' + lp->d_npartitions - 1); + if ((partno = getpartno(lp, p, "name")) == -1) return; - } - pp = &lp->d_partitions[partno]; - - if (pp->p_fstype == FS_UNUSED && DL_GETPSIZE(pp) == 0) { - fprintf(stderr, "Partition '%c' is not in use.\n", p[0]); - return; - } get_mp(lp, partno); } @@ -936,25 +906,10 @@ editor_modify(struct disklabel *lp, const char *p) struct partition opp, *pp; int partno; - /* Change which partition? */ - if (p == NULL) - p = getstring("partition to modify", - "The letter of the partition to modify, a - p.", NULL); - if (p == NULL) - return; - partno = p[0] - 'a'; - if (partno < 0 || partno == RAW_PART || partno >= lp->d_npartitions) { - fprintf(stderr, "Partition must be between 'a' and '%c' " - "(excluding 'c').\n", 'a' + lp->d_npartitions - 1); - return; - } - pp = &lp->d_partitions[partno]; - - if (pp->p_fstype == FS_UNUSED && DL_GETPSIZE(pp) == 0) { - fprintf(stderr, "Partition '%c' is not in use.\n", p[0]); + if ((partno = getpartno(lp, p, "modify")) == -1) return; - } + pp = &lp->d_partitions[partno]; opp = *pp; if (get_offset(lp, partno) == 0 && @@ -1017,27 +972,13 @@ editor_change(struct disklabel *lp, const char *p) struct partition *pp; int partno; - if (p == NULL) - p = getstring("partition to change size", - "The letter of the partition to change size, a - p.", NULL); - if (p == NULL) - return; - partno = p[0] - 'a'; - if (partno < 0 || partno == RAW_PART || partno >= lp->d_npartitions) { - fprintf(stderr, "Partition must be between 'a' and '%c' " - "(excluding 'c').\n", 'a' + lp->d_npartitions - 1); - return; - } - pp = &lp->d_partitions[partno]; - - if (DL_GETPSIZE(pp) == 0) { - fprintf(stderr, "Partition '%c' is not in use.\n", p[0]); + if ((partno = getpartno(lp, p, "change size")) == -1) return; - } + pp = &lp->d_partitions[partno]; printf("Partition %c is currently %llu sectors in size, and can have " "a maximum\nsize of %llu sectors.\n", - p[0], DL_GETPSIZE(pp), max_partition_size(lp, partno)); + 'a' + partno, DL_GETPSIZE(pp), max_partition_size(lp, partno)); /* Get new size */ get_size(lp, partno); @@ -1093,6 +1034,47 @@ getstring(const char *prompt, const char *helpstring, const char *oval) return (&buf[0]); } +int +getpartno(const struct disklabel *lp, const char *id, const char *action) +{ + const char *promptfmt = "partition to %s"; + const char *helpfmt = "Partition must be between 'a' and '%c' " + "(excluding 'c').\n"; + const struct partition *pp; + const char *p; + char *help = NULL, *prompt = NULL; + const unsigned char maxpart = 'a' + lp->d_npartitions - 1; + unsigned int partno; + + p = id; + if (p == NULL) { + if (asprintf(&prompt, promptfmt, action) == -1 || + asprintf(&help, helpfmt, maxpart) == -1) + fprintf(stderr, "Unable to build prompt or help\n"); + else + p = getstring(prompt, help, NULL); + free(prompt); + free(help); + if (p == NULL || *p == '\0') + goto done; + } + + if (strlen(p) > 1 || *p < 'a' || *p >= maxpart || *p == 'c') { + fprintf(stderr, helpfmt, maxpart); + goto done; + } + + partno = *p - 'a'; + pp = &lp->d_partitions[partno]; + if (DL_GETPSIZE(pp) > 0 && pp->p_fstype != FS_UNUSED) + return (partno); + + fprintf(stderr, "Partition '%c' is not in use.\n", *p); + + done: + return (-1); +} + /* * Returns * 0 .. CMD_ABORTED - 1 ==> valid value |