From 81f2740f0a58f27703345a5bc175a8d3d87eb4a2 Mon Sep 17 00:00:00 2001 From: krw Date: Wed, 25 Jan 2023 21:44:08 +0000 Subject: [PATCH] Use getpartno() in editor_delete(), enhancing getpartno() to allow '*' to select all partitions when the action is 'delete'. No intentional functional change. --- sbin/disklabel/editor.c | 34 ++++++++++++---------------------- 1 file changed, 12 insertions(+), 22 deletions(-) diff --git a/sbin/disklabel/editor.c b/sbin/disklabel/editor.c index db6e0d9dc2b..e30aaca229f 100644 --- a/sbin/disklabel/editor.c +++ b/sbin/disklabel/editor.c @@ -1,4 +1,4 @@ -/* $OpenBSD: editor.c,v 1.397 2023/01/24 15:47:10 krw Exp $ */ +/* $OpenBSD: editor.c,v 1.398 2023/01/25 21:44:08 krw Exp $ */ /* * Copyright (c) 1997-2000 Todd C. Miller @@ -904,29 +904,14 @@ editor_delete(struct disklabel *lp, const char *p) struct partition *pp; int partno; - if (p == NULL) - p = getstring("partition to delete", - "The letter of the partition to delete, a - p, or '*'.", - NULL); - if (p == NULL) + if ((partno = getpartno(lp, p, "delete")) == -1) return; - if (p[0] == '*') { + if (partno == lp->d_npartitions) { zero_partitions(lp); 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]); - return; - } - /* Really delete it (as opposed to just setting to "unused") */ memset(pp, 0, sizeof(*pp)); free(mountpoints[partno]); @@ -1010,19 +995,21 @@ getpartno(const struct disklabel *lp, const char *p, const char *action) char buf[2] = { '\0', '\0'}; const char *promptfmt = "partition to %s"; const char *helpfmt = "Partition must be between 'a' and '%c' " - "(excluding 'c').\n"; + "(excluding 'c')%s.\n"; const struct partition *pp; char *help = NULL, *prompt = NULL; unsigned char maxpart; unsigned int partno; - int add, inuse; + int add, delete, inuse; add = strcmp("add", action) == 0; + delete = strcmp("delete", action) == 0; maxpart = 'a' - 1 + (add ? MAXPARTITIONS : lp->d_npartitions); if (p == NULL) { if (asprintf(&prompt, promptfmt, action) == -1 || - asprintf(&help, helpfmt, maxpart) == -1) { + asprintf(&help, helpfmt, maxpart, delete ? ", or '*'" : "") + == -1) { fprintf(stderr, "Unable to build prompt or help\n"); goto done; } @@ -1048,8 +1035,11 @@ getpartno(const struct disklabel *lp, const char *p, const char *action) goto done; } + if (delete && strlen(p) == 1 && *p == '*') + return (lp->d_npartitions); + if (strlen(p) > 1 || *p < 'a' || *p > maxpart || *p == 'c') { - fprintf(stderr, helpfmt, maxpart); + fprintf(stderr, helpfmt, maxpart, delete ? ", or '*'" : ""); goto done; } -- 2.20.1