summaryrefslogtreecommitdiff
path: root/sbin
diff options
context:
space:
mode:
authorKenneth R Westerback <krw@cvs.openbsd.org>2021-09-13 15:07:52 +0000
committerKenneth R Westerback <krw@cvs.openbsd.org>2021-09-13 15:07:52 +0000
commitc5cc15330274cbb5c71b2d2038b29a24f635392c (patch)
tree5a342913b825becca0818940448a259639d2f663 /sbin
parentc1320929a0d0f2c4a48e865447e8ac90d2b855c8 (diff)
disk.dk_size can't be 0 as we errx() if that happens during
DISK_open(). So eliminate pointless check when printing geometry. Replace unit_lookup() with units_size(), reducing four conversion dances to one. Return pointer to the unit_type used in the conversion. unit_types[] is now needed only in misc.c. Fewer variables make for cleaner logic. No intentional functional change.
Diffstat (limited to 'sbin')
-rw-r--r--sbin/fdisk/disk.c29
-rw-r--r--sbin/fdisk/gpt.c30
-rw-r--r--sbin/fdisk/misc.c18
-rw-r--r--sbin/fdisk/misc.h6
-rw-r--r--sbin/fdisk/part.c17
5 files changed, 45 insertions, 55 deletions
diff --git a/sbin/fdisk/disk.c b/sbin/fdisk/disk.c
index 17d0ace4821..6819518cf60 100644
--- a/sbin/fdisk/disk.c
+++ b/sbin/fdisk/disk.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: disk.c,v 1.72 2021/09/12 16:36:52 krw Exp $ */
+/* $OpenBSD: disk.c,v 1.73 2021/09/13 15:07:51 krw Exp $ */
/*
* Copyright (c) 1997 Tobias Weingartner
@@ -99,23 +99,16 @@ DISK_open(const char *name, const int oflags)
void
DISK_printgeometry(const char *units)
{
- const int secsize = dl.d_secsize;
- double size;
- int i;
-
- i = unit_lookup(units);
- size = disk.dk_size;
- if (unit_types[i].ut_conversion != 0)
- size = (size * secsize) / unit_types[i].ut_conversion;
- printf("Disk: %s\t", disk.dk_name);
- if (disk.dk_size) {
- printf("geometry: %d/%d/%d [%.0f ", disk.dk_cylinders,
- disk.dk_heads, disk.dk_sectors, size);
- if (unit_types[i].ut_conversion == 0 && secsize != DEV_BSIZE)
- printf("%d-byte ", secsize);
- printf("%s]\n", unit_types[i].ut_lname);
- } else
- printf("geometry: <none>\n");
+ const struct unit_type *ut;
+ const int secsize = dl.d_secsize;
+ double size;
+
+ size = units_size(units, disk.dk_size, &ut);
+ printf("Disk: %s\tgeometry: %d/%d/%d [%.0f ", disk.dk_name,
+ disk.dk_cylinders, disk.dk_heads, disk.dk_sectors, size);
+ if (ut->ut_conversion == 0 && secsize != DEV_BSIZE)
+ printf("%d-byte ", secsize);
+ printf("%s]\n", ut->ut_lname);
}
/*
diff --git a/sbin/fdisk/gpt.c b/sbin/fdisk/gpt.c
index 0130b73ee11..cf562f2fb6a 100644
--- a/sbin/fdisk/gpt.c
+++ b/sbin/fdisk/gpt.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: gpt.c,v 1.52 2021/09/12 16:36:52 krw Exp $ */
+/* $OpenBSD: gpt.c,v 1.53 2021/09/13 15:07:51 krw Exp $ */
/*
* Copyright (c) 2015 Markus Muller <mmu@grummel.net>
* Copyright (c) 2015 Kenneth R Westerback <krw@openbsd.org>
@@ -296,11 +296,12 @@ GPT_read(const int which)
void
GPT_print(const char *units, const int verbosity)
{
- const int secsize = dl.d_secsize;
+ const struct unit_type *ut;
struct uuid guid;
+ const int secsize = dl.d_secsize;
char *guidstr = NULL;
double size;
- int i, u, status;
+ int i, status;
#ifdef DEBUG
char *p;
@@ -338,17 +339,13 @@ GPT_print(const char *units, const int verbosity)
printf("\n");
#endif /* DEBUG */
- u = unit_lookup(units);
- size = DL_GETDSIZE(&dl);
- if (unit_types[u].ut_conversion != 0)
- size = (size * secsize) / unit_types[u].ut_conversion;
+ size = units_size(units, DL_GETDSIZE(&dl), &ut);
printf("Disk: %s Usable LBA: %llu to %llu [%.0f ",
disk.dk_name, letoh64(gh.gh_lba_start), letoh64(gh.gh_lba_end),
size);
-
- if (unit_types[u].ut_conversion == 0 && secsize != DEV_BSIZE)
+ if (ut->ut_conversion == 0 && secsize != DEV_BSIZE)
printf("%d-byte ", secsize);
- printf("%s]\n", unit_types[u].ut_lname);
+ printf("%s]\n", ut->ut_lname);
if (verbosity == VERBOSE) {
printf("GUID: ");
@@ -383,22 +380,21 @@ GPT_print_parthdr(const int verbosity)
void
GPT_print_part(const int n, const char *units, const int verbosity)
{
+ const struct unit_type *ut;
struct uuid guid;
struct gpt_partition *partn = &gp[n];
char *guidstr = NULL;
- const int secsize = dl.d_secsize;
double size;
- int u, status;
+ uint64_t sectors;
+ int status;
uuid_dec_le(&partn->gp_type, &guid);
- u = unit_lookup(units);
- size = letoh64(partn->gp_lba_end) - letoh64(partn->gp_lba_start) + 1;
- if (unit_types[u].ut_conversion != 0)
- size = (size * secsize) / unit_types[u].ut_conversion;
+ sectors = letoh64(partn->gp_lba_end) - letoh64(partn->gp_lba_start) + 1;
+ size = units_size(units, sectors, &ut);
printf("%c%3d: %-36s [%12lld: %12.0f%s]\n",
(letoh64(partn->gp_attrs) & GPTDOSACTIVE)?'*':' ', n,
PRT_uuid_to_typename(&guid), letoh64(partn->gp_lba_start),
- size, unit_types[u].ut_abbr);
+ size, ut->ut_abbr);
if (verbosity == VERBOSE) {
uuid_dec_le(&partn->gp_guid, &guid);
diff --git a/sbin/fdisk/misc.c b/sbin/fdisk/misc.c
index 38d5f98029d..a1c8c59802e 100644
--- a/sbin/fdisk/misc.c
+++ b/sbin/fdisk/misc.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: misc.c,v 1.85 2021/09/12 16:36:52 krw Exp $ */
+/* $OpenBSD: misc.c,v 1.86 2021/09/13 15:07:51 krw Exp $ */
/*
* Copyright (c) 1997 Tobias Weingartner
@@ -40,17 +40,25 @@ const struct unit_type unit_types[] = {
};
#define SECTORS 1
-int
-unit_lookup(const char *units)
+double
+units_size(const char *units, const uint64_t sectors,
+ const struct unit_type **ut)
{
+ double size;
unsigned int i;
+ *ut = &unit_types[SECTORS];
+ size = sectors;
+
for (i = 0; i < nitems(unit_types); i++) {
if (strncasecmp(unit_types[i].ut_abbr, units, 1) == 0)
- return i;
+ *ut = &unit_types[i];
}
- return SECTORS;
+ if ((*ut)->ut_conversion == 0)
+ return size;
+ else
+ return (size * dl.d_secsize) / (*ut)->ut_conversion;
}
void
diff --git a/sbin/fdisk/misc.h b/sbin/fdisk/misc.h
index 7b3b5b26932..e956467d8ee 100644
--- a/sbin/fdisk/misc.h
+++ b/sbin/fdisk/misc.h
@@ -1,4 +1,4 @@
-/* $OpenBSD: misc.h,v 1.45 2021/09/12 16:36:52 krw Exp $ */
+/* $OpenBSD: misc.h,v 1.46 2021/09/13 15:07:51 krw Exp $ */
/*
* Copyright (c) 1997 Tobias Weingartner
@@ -24,7 +24,6 @@ struct unit_type {
int64_t ut_conversion;
char *ut_lname;
};
-extern const struct unit_type unit_types[];
#ifndef nitems
#define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
@@ -34,7 +33,8 @@ extern const struct unit_type unit_types[];
#define UNTRIMMED 0
#define TRIMMED 1
-int unit_lookup(const char *);
+double units_size(const char *, const uint64_t,
+ const struct unit_type **);
void string_from_line(char *, const size_t, const int);
int ask_yn(const char *);
int hex_octet(char *);
diff --git a/sbin/fdisk/part.c b/sbin/fdisk/part.c
index f085cf3e146..ee1c55ea1d7 100644
--- a/sbin/fdisk/part.c
+++ b/sbin/fdisk/part.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: part.c,v 1.106 2021/09/12 16:36:52 krw Exp $ */
+/* $OpenBSD: part.c,v 1.107 2021/09/13 15:07:51 krw Exp $ */
/*
* Copyright (c) 1997 Tobias Weingartner
@@ -363,11 +363,8 @@ PRT_make(const struct prt *prt, const uint64_t lba_self, const uint64_t lba_firs
void
PRT_print(const int num, const struct prt *prt, const char *units)
{
- const int secsize = dl.d_secsize;
- double size;
- int i;
-
- i = unit_lookup(units);
+ const struct unit_type *ut;
+ double size;
if (prt == NULL) {
printf(" Starting Ending "
@@ -377,18 +374,14 @@ PRT_print(const int num, const struct prt *prt, const char *units)
printf("---------------------------------------"
"----------------------------------------\n");
} else {
- size = prt->prt_ns;
- if (unit_types[i].ut_conversion != 0)
- size = (size * secsize) / unit_types[i].ut_conversion;
+ size = units_size(units, prt->prt_ns, &ut);
printf("%c%1d: %.2X %6u %3u %3u - %6u %3u %3u "
"[%12llu:%12.0f%s] %s\n",
(prt->prt_flag == DOSACTIVE)?'*':' ',
num, prt->prt_id,
prt->prt_scyl, prt->prt_shead, prt->prt_ssect,
prt->prt_ecyl, prt->prt_ehead, prt->prt_esect,
- prt->prt_bs, size,
- unit_types[i].ut_abbr,
- ascii_id(prt->prt_id));
+ prt->prt_bs, size, ut->ut_abbr, ascii_id(prt->prt_id));
}
}