From 543185b3236db8b50fff43d0cadc28a0b1184d51 Mon Sep 17 00:00:00 2001 From: krw Date: Sun, 11 Jul 2021 13:51:42 +0000 Subject: [PATCH] Move ask_cmd(), ask_num(), ask_pid(), ask_string(), parse_b() and crc32(). No functional change. --- sbin/fdisk/cmd.c | 107 +++++++++++++++++++++++-- sbin/fdisk/fdisk.c | 55 ++++++++++++- sbin/fdisk/gpt.c | 34 +++++++- sbin/fdisk/misc.c | 194 +-------------------------------------------- sbin/fdisk/misc.h | 8 +- sbin/fdisk/user.c | 24 +++++- 6 files changed, 213 insertions(+), 209 deletions(-) diff --git a/sbin/fdisk/cmd.c b/sbin/fdisk/cmd.c index 62913b9c386..5ecc2f49413 100644 --- a/sbin/fdisk/cmd.c +++ b/sbin/fdisk/cmd.c @@ -1,4 +1,4 @@ -/* $OpenBSD: cmd.c,v 1.120 2021/07/11 13:38:27 krw Exp $ */ +/* $OpenBSD: cmd.c,v 1.121 2021/07/11 13:51:42 krw Exp $ */ /* * Copyright (c) 1997 Tobias Weingartner @@ -35,11 +35,15 @@ #include "user.h" #include "cmd.h" -int gedit(int); -int edit(int, struct mbr *); -int gsetpid(int); -int setpid(int, struct mbr *); -int parsepn(char *); +int gedit(int); +int edit(int, struct mbr *); +int gsetpid(int); +int setpid(int, struct mbr *); +int parsepn(char *); + +int ask_num(const char *, int, int, int); +int ask_pid(int, struct uuid *); +char *ask_string(const char *, const char *); extern const unsigned char manpage[]; extern const int manpage_sz; @@ -604,3 +608,94 @@ Xmanual(char *args, struct mbr *mbr) return CMD_CONT; } + +int +ask_num(const char *str, int dflt, int low, int high) +{ + char lbuf[100]; + const char *errstr; + int num; + + if (dflt < low) + dflt = low; + else if (dflt > high) + dflt = high; + + do { + printf("%s [%d - %d]: [%d] ", str, low, high, dflt); + + if (string_from_line(lbuf, sizeof(lbuf))) + errx(1, "eof"); + + if (lbuf[0] == '\0') { + num = dflt; + errstr = NULL; + } else { + num = (int)strtonum(lbuf, low, high, &errstr); + if (errstr) + printf("%s is %s: %s.\n", str, errstr, lbuf); + } + } while (errstr); + + return num; +} + +int +ask_pid(int dflt, struct uuid *guid) +{ + char lbuf[100], *cp; + int num = -1, status; + + do { + printf("Partition id ('0' to disable) [01 - FF]: [%X] ", dflt); + printf("(? for help) "); + + if (string_from_line(lbuf, sizeof(lbuf))) + errx(1, "eof"); + + if (lbuf[0] == '?') { + PRT_printall(); + continue; + } + + if (guid && strlen(lbuf) == UUID_STR_LEN) { + uuid_from_string(lbuf, guid, &status); + if (status == uuid_s_ok) + return 0x100; + } + + /* Convert */ + cp = lbuf; + num = strtol(lbuf, &cp, 16); + + /* 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; +} + +char * +ask_string(const char *prompt, const char *oval) +{ + static char buf[UUID_STR_LEN + 1]; + + buf[0] = '\0'; + printf("%s: [%s] ", prompt, oval ? oval : ""); + if (string_from_line(buf, sizeof(buf))) + errx(1, "eof"); + + if (buf[0] == '\0' && oval) + strlcpy(buf, oval, sizeof(buf)); + + return buf; +} diff --git a/sbin/fdisk/fdisk.c b/sbin/fdisk/fdisk.c index cd2855cd3eb..2c507b3f7f0 100644 --- a/sbin/fdisk/fdisk.c +++ b/sbin/fdisk/fdisk.c @@ -1,4 +1,4 @@ -/* $OpenBSD: fdisk.c,v 1.116 2021/07/11 13:38:27 krw Exp $ */ +/* $OpenBSD: fdisk.c,v 1.117 2021/07/11 13:51:42 krw Exp $ */ /* * Copyright (c) 1997 Tobias Weingartner @@ -19,6 +19,7 @@ #include /* DEV_BSIZE */ #include +#include #include #include #include @@ -45,6 +46,8 @@ uint32_t b_sectors, b_offset; uint8_t b_type; int A_flag, y_flag; +void parse_b(const char *, uint32_t *, uint32_t *, uint8_t *); + static void usage(void) { @@ -252,3 +255,53 @@ done: return 0; } + +void +parse_b(const char *arg, uint32_t *blocks, uint32_t *offset, uint8_t *type) +{ + const char *errstr; + char *poffset, *ptype; + uint32_t blockcount, blockoffset; + uint8_t partitiontype; + + blockoffset = BLOCKALIGNMENT; + partitiontype = DOSPTYP_EFISYS; + ptype = NULL; + + /* First number: # of 512-byte blocks in boot partition. */ + poffset = strchr(arg, '@'); + if (poffset != NULL) + *poffset++ = '\0'; + if (poffset != NULL) { + ptype = strchr(poffset, ':'); + if (ptype != NULL) + *ptype++ = '\0'; + } + + blockcount = strtonum(arg, BLOCKALIGNMENT, UINT32_MAX, &errstr); + if (errstr) + errx(1, "Block argument %s [%u..%u].", errstr, BLOCKALIGNMENT, + UINT32_MAX); + + if (poffset == NULL) + goto done; + + /* Second number: # of 512-byte blocks to offset partition start. */ + blockoffset = strtonum(poffset, BLOCKALIGNMENT, UINT32_MAX, &errstr); + if (errstr) + errx(1, "Block offset argument %s [%u..%u].", errstr, + BLOCKALIGNMENT, UINT32_MAX); + + 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); + + done: + *blocks = blockcount; + *offset = blockoffset; + *type = partitiontype; +} diff --git a/sbin/fdisk/gpt.c b/sbin/fdisk/gpt.c index 81839b8564f..cd36d06ea35 100644 --- a/sbin/fdisk/gpt.c +++ b/sbin/fdisk/gpt.c @@ -1,4 +1,4 @@ -/* $OpenBSD: gpt.c,v 1.36 2021/07/11 13:38:27 krw Exp $ */ +/* $OpenBSD: gpt.c,v 1.37 2021/07/11 13:51:42 krw Exp $ */ /* * Copyright (c) 2015 Markus Muller * Copyright (c) 2015 Kenneth R Westerback @@ -50,6 +50,7 @@ int get_header(off_t); int get_partition_table(void); int init_gh(void); int init_gp(int, uint32_t); +uint32_t crc32(const u_char *, const uint32_t); int get_header(off_t where) @@ -713,3 +714,34 @@ GPT_get_lba_end(unsigned int pn) return 0; } + +/* + * Adapted from Hacker's Delight crc32b(). + * + * To quote http://www.hackersdelight.org/permissions.htm : + * + * "You are free to use, copy, and distribute any of the code on + * this web site, whether modified by you or not. You need not give + * attribution. This includes the algorithms (some of which appear + * in Hacker's Delight), the Hacker's Assistant, and any code submitted + * by readers. Submitters implicitly agree to this." + */ +uint32_t +crc32(const u_char *buf, const uint32_t size) +{ + int j; + uint32_t i, byte, crc, mask; + + crc = 0xFFFFFFFF; + + for (i = 0; i < size; i++) { + byte = buf[i]; /* Get next byte. */ + crc = crc ^ byte; + for (j = 7; j >= 0; j--) { /* Do eight times. */ + mask = -(crc & 1); + crc = (crc >> 1) ^ (0xEDB88320 & mask); + } + } + + return ~crc; +} diff --git a/sbin/fdisk/misc.c b/sbin/fdisk/misc.c index 2de384b9e40..26fe3e1ad17 100644 --- a/sbin/fdisk/misc.c +++ b/sbin/fdisk/misc.c @@ -1,4 +1,4 @@ -/* $OpenBSD: misc.c,v 1.71 2021/07/11 13:38:27 krw Exp $ */ +/* $OpenBSD: misc.c,v 1.72 2021/07/11 13:51:42 krw Exp $ */ /* * Copyright (c) 1997 Tobias Weingartner @@ -81,101 +81,6 @@ string_from_line(char *buf, size_t buflen) return 0; } -void -ask_cmd(char **cmd, char **arg) -{ - static char lbuf[100]; - size_t cmdstart, cmdend, argstart; - - /* Get NUL terminated string from stdin. */ - if (string_from_line(lbuf, sizeof(lbuf))) - errx(1, "eof"); - - cmdstart = strspn(lbuf, " \t"); - cmdend = cmdstart + strcspn(&lbuf[cmdstart], " \t"); - argstart = cmdend + strspn(&lbuf[cmdend], " \t"); - - /* *cmd and *arg may be set to point at final NUL! */ - *cmd = &lbuf[cmdstart]; - lbuf[cmdend] = '\0'; - *arg = &lbuf[argstart]; -} - -int -ask_num(const char *str, int dflt, int low, int high) -{ - char lbuf[100]; - const char *errstr; - int num; - - if (dflt < low) - dflt = low; - else if (dflt > high) - dflt = high; - - do { - printf("%s [%d - %d]: [%d] ", str, low, high, dflt); - - if (string_from_line(lbuf, sizeof(lbuf))) - errx(1, "eof"); - - if (lbuf[0] == '\0') { - num = dflt; - errstr = NULL; - } else { - num = (int)strtonum(lbuf, low, high, &errstr); - if (errstr) - printf("%s is %s: %s.\n", str, errstr, lbuf); - } - } while (errstr); - - return num; -} - -int -ask_pid(int dflt, struct uuid *guid) -{ - char lbuf[100], *cp; - int num = -1, status; - - do { - printf("Partition id ('0' to disable) [01 - FF]: [%X] ", dflt); - printf("(? for help) "); - - if (string_from_line(lbuf, sizeof(lbuf))) - errx(1, "eof"); - - if (lbuf[0] == '?') { - PRT_printall(); - continue; - } - - if (guid && strlen(lbuf) == UUID_STR_LEN) { - uuid_from_string(lbuf, guid, &status); - if (status == uuid_s_ok) - return 0x100; - } - - /* Convert */ - cp = lbuf; - num = strtol(lbuf, &cp, 16); - - /* 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; -} - int ask_yn(const char *str) { @@ -322,53 +227,6 @@ getuint64(char *prompt, uint64_t oval, uint64_t minval, uint64_t maxval) return (uint64_t)d; } -char * -ask_string(const char *prompt, const char *oval) -{ - static char buf[UUID_STR_LEN + 1]; - - buf[0] = '\0'; - printf("%s: [%s] ", prompt, oval ? oval : ""); - if (string_from_line(buf, sizeof(buf))) - errx(1, "eof"); - - if (buf[0] == '\0' && oval) - strlcpy(buf, oval, sizeof(buf)); - - return buf; -} - -/* - * Adapted from Hacker's Delight crc32b(). - * - * To quote http://www.hackersdelight.org/permissions.htm : - * - * "You are free to use, copy, and distribute any of the code on - * this web site, whether modified by you or not. You need not give - * attribution. This includes the algorithms (some of which appear - * in Hacker's Delight), the Hacker's Assistant, and any code submitted - * by readers. Submitters implicitly agree to this." - */ -uint32_t -crc32(const u_char *buf, const uint32_t size) -{ - int j; - uint32_t i, byte, crc, mask; - - crc = 0xFFFFFFFF; - - for (i = 0; i < size; i++) { - byte = buf[i]; /* Get next byte. */ - crc = crc ^ byte; - for (j = 7; j >= 0; j--) { /* Do eight times. */ - mask = -(crc & 1); - crc = (crc >> 1) ^ (0xEDB88320 & mask); - } - } - - return ~crc; -} - char * utf16le_to_string(const uint16_t *utf) { @@ -402,53 +260,3 @@ string_to_utf16le(const char *ch) return utf; } - -void -parse_b(const char *arg, uint32_t *blocks, uint32_t *offset, uint8_t *type) -{ - const char *errstr; - char *poffset, *ptype; - uint32_t blockcount, blockoffset; - uint8_t partitiontype; - - blockoffset = BLOCKALIGNMENT; - partitiontype = DOSPTYP_EFISYS; - ptype = NULL; - - /* First number: # of 512-byte blocks in boot partition. */ - poffset = strchr(arg, '@'); - if (poffset != NULL) - *poffset++ = '\0'; - if (poffset != NULL) { - ptype = strchr(poffset, ':'); - if (ptype != NULL) - *ptype++ = '\0'; - } - - blockcount = strtonum(arg, BLOCKALIGNMENT, UINT32_MAX, &errstr); - if (errstr) - errx(1, "Block argument %s [%u..%u].", errstr, BLOCKALIGNMENT, - UINT32_MAX); - - if (poffset == NULL) - goto done; - - /* Second number: # of 512-byte blocks to offset partition start. */ - blockoffset = strtonum(poffset, BLOCKALIGNMENT, UINT32_MAX, &errstr); - if (errstr) - errx(1, "Block offset argument %s [%u..%u].", errstr, - BLOCKALIGNMENT, UINT32_MAX); - - 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); - - done: - *blocks = blockcount; - *offset = blockoffset; - *type = partitiontype; -} diff --git a/sbin/fdisk/misc.h b/sbin/fdisk/misc.h index f5f8c8e2a23..648936856b2 100644 --- a/sbin/fdisk/misc.h +++ b/sbin/fdisk/misc.h @@ -1,4 +1,4 @@ -/* $OpenBSD: misc.h,v 1.37 2021/07/11 13:23:18 krw Exp $ */ +/* $OpenBSD: misc.h,v 1.38 2021/07/11 13:51:42 krw Exp $ */ /* * Copyright (c) 1997 Tobias Weingartner @@ -30,15 +30,9 @@ extern struct unit_type unit_types[]; /* Prototypes */ int unit_lookup(char *); int string_from_line(char *, size_t); -void ask_cmd(char **, char **); -int ask_num(const char *, int, int, int); -int ask_pid(int, struct uuid *); -char *ask_string(const char *, const char *); int ask_yn(const char *); uint64_t getuint64(char *, uint64_t, uint64_t, uint64_t); -uint32_t crc32(const u_char *, const uint32_t); char *utf16le_to_string(const uint16_t *); uint16_t *string_to_utf16le(const char *); -void parse_b(const char *, uint32_t *, uint32_t *, uint8_t *); #endif /* _MISC_H */ diff --git a/sbin/fdisk/user.c b/sbin/fdisk/user.c index 3a045725bdf..0c9621d6daa 100644 --- a/sbin/fdisk/user.c +++ b/sbin/fdisk/user.c @@ -1,4 +1,4 @@ -/* $OpenBSD: user.c,v 1.56 2021/07/11 13:38:27 krw Exp $ */ +/* $OpenBSD: user.c,v 1.57 2021/07/11 13:51:42 krw Exp $ */ /* * Copyright (c) 1997 Tobias Weingartner @@ -56,6 +56,8 @@ struct cmd cmd_table[] = { int modified; +void ask_cmd(char **, char **); + void USER_edit(off_t offset, off_t reloff) { @@ -198,3 +200,23 @@ USER_print_disk(int verbosity) } } while (offset); } + +void +ask_cmd(char **cmd, char **arg) +{ + static char lbuf[100]; + size_t cmdstart, cmdend, argstart; + + /* Get NUL terminated string from stdin. */ + if (string_from_line(lbuf, sizeof(lbuf))) + errx(1, "eof"); + + cmdstart = strspn(lbuf, " \t"); + cmdend = cmdstart + strcspn(&lbuf[cmdstart], " \t"); + argstart = cmdend + strspn(&lbuf[cmdend], " \t"); + + /* *cmd and *arg may be set to point at final NUL! */ + *cmd = &lbuf[cmdstart]; + lbuf[cmdend] = '\0'; + *arg = &lbuf[argstart]; +} -- 2.20.1