From b984fdccb546b0fe5fc685e892f55116d305a16c Mon Sep 17 00:00:00 2001 From: krw Date: Tue, 11 Apr 2023 21:14:19 +0000 Subject: [PATCH] Abstract find_[gpt|mbr]_menuitem() to simplify code and prepare for future uses. No functional change. --- sbin/fdisk/part.c | 56 ++++++++++++++++++++++++++++++++++++----------- 1 file changed, 43 insertions(+), 13 deletions(-) diff --git a/sbin/fdisk/part.c b/sbin/fdisk/part.c index 885481f4666..943158c256d 100644 --- a/sbin/fdisk/part.c +++ b/sbin/fdisk/part.c @@ -1,4 +1,4 @@ -/* $OpenBSD: part.c,v 1.157 2023/04/11 17:26:59 krw Exp $ */ +/* $OpenBSD: part.c,v 1.158 2023/04/11 21:14:19 krw Exp $ */ /* * Copyright (c) 1997 Tobias Weingartner @@ -732,11 +732,13 @@ const struct menu_item menu_items[] = { }; const struct gpt_type *find_gpt_type(const struct uuid *); +const struct menu_item *find_gpt_menuitem(const struct gpt_type *); const char *find_gpt_desc(const struct gpt_type *); int gpt_item(const unsigned int); int uuid_attr(const struct uuid *); const struct mbr_type *find_mbr_type(const int); +const struct menu_item *find_mbr_menuitem(const struct mbr_type *); const char *find_mbr_desc(const struct mbr_type *); int mbr_item(const unsigned int); @@ -767,24 +769,38 @@ find_gpt_type(const struct uuid *uuid) return gt; } -const char * -find_gpt_desc(const struct gpt_type *gt) +const struct menu_item * +find_gpt_menuitem(const struct gpt_type *gt) { - unsigned int i; + unsigned int i; if (gt != NULL) { - if (gt->gt_desc != NULL) - return gt->gt_desc; for (i = 0; i < nitems(menu_items); i++) { if (gpt_item(i) == 0 && strcasecmp(menu_items[i].mi_guid, gt->gt_guid) == 0) - return menu_items[i].mi_name; + return &menu_items[i]; } } return NULL; } +const char * +find_gpt_desc(const struct gpt_type *gt) +{ + const struct menu_item *mi; + + if (gt != NULL) { + if (gt->gt_desc != NULL) + return gt->gt_desc; + mi = find_gpt_menuitem(gt); + if (mi) + return mi->mi_name; + } + + return NULL; +} + int gpt_item(const unsigned int item) { @@ -819,19 +835,33 @@ find_mbr_type(const int id) return mt; } +const struct menu_item * +find_mbr_menuitem(const struct mbr_type *mt) +{ + unsigned int i; + + if (mt != NULL) { + for (i = 0; i < nitems(menu_items); i++) { + if (mbr_item(i) == 0 + && menu_items[i].mi_mbrid == mt->mt_type) + return &menu_items[i]; + } + } + + return NULL; +} + const char * find_mbr_desc(const struct mbr_type *mt) { - unsigned int i; + const struct menu_item *mi; if (mt != NULL) { if (mt->mt_desc != NULL) return mt->mt_desc; - for (i = 0; i < nitems(menu_items); i++) { - if (mbr_item(i) == 0 && - menu_items[i].mi_mbrid == mt->mt_type) - return menu_items[i].mi_name; - } + mi = find_mbr_menuitem(mt); + if (mi) + return mi->mi_name; } return NULL; -- 2.20.1