From 7696a120b453b8d8d5f5187d11baa48ad1f8e96f Mon Sep 17 00:00:00 2001 From: krw Date: Sat, 28 Aug 2021 11:55:17 +0000 Subject: [PATCH] Add hex_octet() so the strtol(..,16) dance is done in just one place. Allows single-digit partition id's in '-b' as a side benefit. --- sbin/fdisk/cmd.c | 30 ++++++++++-------------------- sbin/fdisk/fdisk.c | 11 +++++------ sbin/fdisk/misc.c | 20 +++++++++++++++++++- sbin/fdisk/misc.h | 3 ++- 4 files changed, 36 insertions(+), 28 deletions(-) diff --git a/sbin/fdisk/cmd.c b/sbin/fdisk/cmd.c index abcba16ea5e..0eb738e9771 100644 --- a/sbin/fdisk/cmd.c +++ b/sbin/fdisk/cmd.c @@ -1,4 +1,4 @@ -/* $OpenBSD: cmd.c,v 1.140 2021/08/24 12:34:04 krw Exp $ */ +/* $OpenBSD: cmd.c,v 1.141 2021/08/28 11:55:17 krw Exp $ */ /* * Copyright (c) 1997 Tobias Weingartner @@ -607,11 +607,13 @@ ask_pid(const int dflt, struct uuid *guid) char lbuf[100], *cp; int num = -1, status; - do { - printf("Partition id ('0' to disable) [01 - FF]: [%X] ", dflt); + for (;;) { + printf("Partition id ('0' to disable) [01 - FF]: [%02X] ", dflt); printf("(? for help) "); string_from_line(lbuf, sizeof(lbuf), TRIMMED); + if (strlen(lbuf) == 0) + return dflt; if (strcmp(lbuf, "?") == 0) { PRT_printall(); continue; @@ -623,24 +625,12 @@ ask_pid(const int dflt, struct uuid *guid) return 0x100; } - /* Convert */ - cp = lbuf; - num = strtol(lbuf, &cp, 16); + num = hex_octet(lbuf); + if (num != -1) + return num; - /* Make sure only number present */ - if (cp == lbuf) - num = dflt; - if (*cp != '\0') { - printf("'%s' is not a valid number.\n", lbuf); - num = -1; - } else if (num == 0) { - break; - } else if (num < 0 || num > 0xff) { - printf("'%x' is out of range.\n", num); - } - } while (num < 0 || num > 0xff); - - return num; + printf("'%s' is not a valid partition id.\n", lbuf); + } } char * diff --git a/sbin/fdisk/fdisk.c b/sbin/fdisk/fdisk.c index fa045d261ad..af57c767d15 100644 --- a/sbin/fdisk/fdisk.c +++ b/sbin/fdisk/fdisk.c @@ -1,4 +1,4 @@ -/* $OpenBSD: fdisk.c,v 1.134 2021/08/10 18:17:48 krw Exp $ */ +/* $OpenBSD: fdisk.c,v 1.135 2021/08/28 11:55:17 krw Exp $ */ /* * Copyright (c) 1997 Tobias Weingartner @@ -210,8 +210,8 @@ parse_bootprt(const char *arg) { const char *errstr; char *poffset, *ptype; + int partitiontype; uint32_t blockcount, blockoffset; - uint8_t partitiontype; blockoffset = BLOCKALIGNMENT; partitiontype = DOSPTYP_EFISYS; @@ -244,10 +244,9 @@ parse_bootprt(const char *arg) if (ptype == NULL) goto done; - if (strlen(ptype) != 2 || !(isxdigit(*ptype) && isxdigit(*(ptype + 1)))) - errx(1, "Block type is not 2 digit hex value"); - - partitiontype = strtol(ptype, NULL, 16); + partitiontype = hex_octet(ptype); + if (partitiontype == -1) + errx(1, "Block type is not a 1-2 digit hex value"); done: disk.dk_bootprt.prt_ns = blockcount; diff --git a/sbin/fdisk/misc.c b/sbin/fdisk/misc.c index 91b191f0db3..ce349826f53 100644 --- a/sbin/fdisk/misc.c +++ b/sbin/fdisk/misc.c @@ -1,4 +1,4 @@ -/* $OpenBSD: misc.c,v 1.81 2021/08/24 12:34:04 krw Exp $ */ +/* $OpenBSD: misc.c,v 1.82 2021/08/28 11:55:17 krw Exp $ */ /* * Copyright (c) 1997 Tobias Weingartner @@ -265,3 +265,21 @@ string_to_utf16le(const char *ch) return utf; } + +int +hex_octet(char *buf) +{ + char *cp; + long num; + + cp = buf; + num = strtol(buf, &cp, 16); + + if (cp == buf || *cp != '\0') + return -1; + + if (num < 0 || num > 0xff) + return -1; + + return num; +} diff --git a/sbin/fdisk/misc.h b/sbin/fdisk/misc.h index 380b512ba8a..1b82046e043 100644 --- a/sbin/fdisk/misc.h +++ b/sbin/fdisk/misc.h @@ -1,4 +1,4 @@ -/* $OpenBSD: misc.h,v 1.43 2021/08/24 12:55:06 krw Exp $ */ +/* $OpenBSD: misc.h,v 1.44 2021/08/28 11:55:17 krw Exp $ */ /* * Copyright (c) 1997 Tobias Weingartner @@ -38,6 +38,7 @@ extern struct unit_type unit_types[]; int unit_lookup(const char *); void string_from_line(char *, const size_t, const int); int ask_yn(const char *); +int hex_octet(char *); uint64_t getuint64(const char *, uint64_t, const uint64_t, const uint64_t); char *utf16le_to_string(const uint16_t *); uint16_t *string_to_utf16le(const char *); -- 2.20.1