parted: Modernize check_parttype() with table-driven mapping

Signed-off-by: Susant Sahani <ssahani@redhat.com>
This commit is contained in:
Susant Sahani
2025-11-20 20:28:37 +05:30
committed by rwmjones
parent 18d9769dc3
commit 3940b79041

View File

@@ -47,25 +47,30 @@
static const char * static const char *
check_parttype (const char *parttype) check_parttype (const char *parttype)
{ {
/* Check and translate parttype. */ static const struct {
if (STREQ (parttype, "aix") || const char *input; /* what the user is allowed to type */
STREQ (parttype, "amiga") || const char *canonical; /* what we return / what parted expects */
STREQ (parttype, "bsd") || } map[] = {
STREQ (parttype, "dasd") || { "aix", "aix" },
STREQ (parttype, "dvh") || { "amiga", "amiga" }, { "rdb", "amiga" },
STREQ (parttype, "gpt") || { "bsd", "bsd" },
STREQ (parttype, "mac") || { "dasd", "dasd" },
STREQ (parttype, "msdos") || { "dvh", "dvh" },
STREQ (parttype, "pc98") || { "gpt", "gpt" }, { "efi", "gpt" },
STREQ (parttype, "sun")) { "mac", "mac" },
return parttype; { "msdos", "msdos" }, { "mbr", "msdos" },
else if (STREQ (parttype, "rdb")) { "pc98", "pc98" },
return "amiga"; { "sun", "sun" },
else if (STREQ (parttype, "efi")) };
return "gpt";
else if (STREQ (parttype, "mbr")) if (parttype == NULL)
return "msdos"; return NULL;
else
for (size_t i = 0; i < sizeof(map) / sizeof(map[0]); ++i) {
if (STREQ(parttype, map[i].input))
return map[i].canonical;
}
return NULL; return NULL;
} }