From 3bbc645f32a7ebeacdaa36b168febf6e9e0c69ad Mon Sep 17 00:00:00 2001 From: krw Date: Mon, 10 May 2021 17:16:01 +0000 Subject: [PATCH] Replace some magic numbers with #define's. Rename GPT_get_gpt() to GPT_read() to match MBR_read(). No intentional functional change. --- sbin/fdisk/cmd.c | 10 +++++----- sbin/fdisk/fdisk.c | 8 ++++---- sbin/fdisk/gpt.c | 32 ++++++++++++++------------------ sbin/fdisk/gpt.h | 11 +++++++++-- sbin/fdisk/user.c | 15 ++++++++------- 5 files changed, 40 insertions(+), 36 deletions(-) diff --git a/sbin/fdisk/cmd.c b/sbin/fdisk/cmd.c index 70a1c34d38b..0ee22c37c3b 100644 --- a/sbin/fdisk/cmd.c +++ b/sbin/fdisk/cmd.c @@ -1,4 +1,4 @@ -/* $OpenBSD: cmd.c,v 1.101 2021/05/07 22:15:13 krw Exp $ */ +/* $OpenBSD: cmd.c,v 1.102 2021/05/10 17:16:01 krw Exp $ */ /* * Copyright (c) 1997 Tobias Weingartner @@ -65,7 +65,7 @@ Xreinit(char *args, struct mbr *mbr) if (dogpt) { MBR_init_GPT(mbr); GPT_init(); - GPT_print("s", 0); + GPT_print("s", TERSE); } else { memset(&gh, 0, sizeof(gh)); MBR_init(mbr); @@ -297,8 +297,8 @@ Xgsetpid(char *args) gg = &gp[pn]; /* Print out current table entry */ - GPT_print_parthdr(0); - GPT_print_part(pn, "s", 0); + GPT_print_parthdr(TERSE); + GPT_print_part(pn, "s", TERSE); /* Ask for partition type or GUID. */ uuid_dec_le(&gg->gp_type, &guid); @@ -395,7 +395,7 @@ Xprint(char *args, struct mbr *mbr) { if (MBR_protective_mbr(mbr) == 0 && letoh64(gh.gh_sig) == GPTSIGNATURE) - GPT_print(args, 1); + GPT_print(args, VERBOSE); else MBR_print(mbr, args); diff --git a/sbin/fdisk/fdisk.c b/sbin/fdisk/fdisk.c index f84ad6985aa..55ad12d7eee 100644 --- a/sbin/fdisk/fdisk.c +++ b/sbin/fdisk/fdisk.c @@ -1,4 +1,4 @@ -/* $OpenBSD: fdisk.c,v 1.105 2021/05/07 22:15:13 krw Exp $ */ +/* $OpenBSD: fdisk.c,v 1.106 2021/05/10 17:16:01 krw Exp $ */ /* * Copyright (c) 1997 Tobias Weingartner @@ -73,7 +73,7 @@ main(int argc, char *argv[]) ssize_t len; int ch, fd, error; int e_flag = 0, g_flag = 0, i_flag = 0, u_flag = 0; - int verbosity = 0; + int verbosity = TERSE; int c_arg = 0, h_arg = 0, s_arg = 0; uint32_t l_arg = 0; char *query; @@ -146,7 +146,7 @@ main(int argc, char *argv[]) y_flag = 1; break; case 'v': - verbosity++; + verbosity = VERBOSE; break; default: usage(); @@ -176,7 +176,7 @@ main(int argc, char *argv[]) /* Get the GPT if present. Either primary or secondary is ok. */ if (MBR_protective_mbr(&mbr) == 0) - GPT_get_gpt(0); + GPT_read(ANYGPT); if (!(i_flag || u_flag || e_flag)) { if (pledge("stdio", NULL) == -1) diff --git a/sbin/fdisk/gpt.c b/sbin/fdisk/gpt.c index 7a15a0d11a3..113ec5648be 100644 --- a/sbin/fdisk/gpt.c +++ b/sbin/fdisk/gpt.c @@ -1,4 +1,4 @@ -/* $OpenBSD: gpt.c,v 1.13 2021/05/07 22:15:13 krw Exp $ */ +/* $OpenBSD: gpt.c,v 1.14 2021/05/10 17:16:01 krw Exp $ */ /* * Copyright (c) 2015 Markus Muller * Copyright (c) 2015 Kenneth R Westerback @@ -192,28 +192,24 @@ GPT_get_partition_table(off_t where) } void -GPT_get_gpt(int which) +GPT_read(int which) { - int privalid, altvalid; + int valid; - /* - * primary header && primary partition table || - * alt header && alt partition table - */ - privalid = GPT_get_header(GPTSECTOR); - if (privalid == 0) - privalid = GPT_get_partition_table(gh.gh_part_lba); - if (which == 1 || (which == 0 && privalid == 0)) + valid = GPT_get_header(GPTSECTOR); + if (valid == 0) + valid = GPT_get_partition_table(gh.gh_part_lba); + if (which == PRIMARYGPT || (which == ANYGPT && valid == 0)) return; /* No valid GPT found. Zap any artifacts. */ memset(&gh, 0, sizeof(gh)); memset(&gp, 0, sizeof(gp)); - altvalid = GPT_get_header(DL_GETDSIZE(&dl) - 1); - if (altvalid == 0) - altvalid = GPT_get_partition_table(gh.gh_part_lba); - if (which == 2 || altvalid == 0) + valid = GPT_get_header(DL_GETDSIZE(&dl) - 1); + if (valid == 0) + valid = GPT_get_partition_table(gh.gh_part_lba); + if (which == SECONDARYGPT || valid == 0) return; /* No valid GPT found. Zap any artifacts. */ @@ -239,7 +235,7 @@ GPT_print(char *units, int verbosity) printf("%d-byte ", secsize); printf("%s]\n", unit_types[u].lname); - if (verbosity) { + if (verbosity == VERBOSE) { printf("GUID: "); uuid_dec_le(&gh.gh_guid, &guid); uuid_to_string(&guid, &guidstr, &status); @@ -264,7 +260,7 @@ GPT_print_parthdr(int verbosity) { printf(" #: type " " [ start: size ]\n"); - if (verbosity) + if (verbosity == VERBOSE) printf(" guid name\n"); printf("--------------------------------------------------------" "----------------\n"); @@ -289,7 +285,7 @@ GPT_print_part(int n, char *units, int verbosity) PRT_uuid_to_typename(&guid), letoh64(partn->gp_lba_start), size, unit_types[u].abbr); - if (verbosity) { + if (verbosity == VERBOSE) { uuid_dec_le(&partn->gp_guid, &guid); uuid_to_string(&guid, &guidstr, &status); if (status != uuid_s_ok) diff --git a/sbin/fdisk/gpt.h b/sbin/fdisk/gpt.h index e6f29f06085..19c8bcfe3f5 100644 --- a/sbin/fdisk/gpt.h +++ b/sbin/fdisk/gpt.h @@ -1,4 +1,4 @@ -/* $OpenBSD: gpt.h,v 1.8 2021/05/06 22:26:14 krw Exp $ */ +/* $OpenBSD: gpt.h,v 1.9 2021/05/10 17:16:01 krw Exp $ */ /* * Copyright (c) 2015 Markus Muller * Copyright (c) 2015 Kenneth R Westerback @@ -16,7 +16,7 @@ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ -void GPT_get_gpt(int); +void GPT_read(int); int GPT_get_header(off_t); int GPT_get_partition_table(off_t); int GPT_get_lba_start(unsigned int); @@ -30,3 +30,10 @@ void GPT_print_parthdr(int); extern struct gpt_header gh; extern struct gpt_partition gp[NGPTPARTITIONS]; + +#define ANYGPT 0 +#define PRIMARYGPT 1 +#define SECONDARYGPT 2 + +#define TERSE 0 +#define VERBOSE 1 diff --git a/sbin/fdisk/user.c b/sbin/fdisk/user.c index b1cf5538541..409ff366711 100644 --- a/sbin/fdisk/user.c +++ b/sbin/fdisk/user.c @@ -1,4 +1,4 @@ -/* $OpenBSD: user.c,v 1.52 2019/04/29 18:54:12 krw Exp $ */ +/* $OpenBSD: user.c,v 1.53 2021/05/10 17:16:01 krw Exp $ */ /* * Copyright (c) 1997 Tobias Weingartner @@ -80,7 +80,7 @@ USER_edit(off_t offset, off_t reloff) memset(&gh, 0, sizeof(gh)); memset(&gp, 0, sizeof(gp)); if (MBR_protective_mbr(&mbr) == 0) - GPT_get_gpt(0); + GPT_read(ANYGPT); } printf("Enter 'help' for information\n"); @@ -153,19 +153,20 @@ USER_print_disk(int verbosity) break; MBR_parse(&dos_mbr, offset, firstoff, &mbr); if (offset == 0) { - if (verbosity || MBR_protective_mbr(&mbr) == 0) { - if (verbosity) { + if (verbosity == VERBOSE || MBR_protective_mbr(&mbr) == + 0) { + if (verbosity == VERBOSE) { printf("Primary GPT:\n"); - GPT_get_gpt(1); /* Get Primary */ + GPT_read(PRIMARYGPT); } if (letoh64(gh.gh_sig) == GPTSIGNATURE) GPT_print("s", verbosity); else printf("\tNot Found\n"); - if (verbosity) { + if (verbosity == VERBOSE) { printf("\n"); printf("Secondary GPT:\n"); - GPT_get_gpt(2); /* Get Secondary */ + GPT_read(SECONDARYGPT); if (letoh64(gh.gh_sig) == GPTSIGNATURE) GPT_print("s", verbosity); else -- 2.20.1