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.
-/* $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
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);
}
/*
-/* $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>
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;
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: ");
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);
-/* $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
};
#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
-/* $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
int64_t ut_conversion;
char *ut_lname;
};
-extern const struct unit_type unit_types[];
#ifndef nitems
#define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
#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 *);
-/* $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
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 "
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));
}
}