and crc32().
No functional change.
-/* $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
#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;
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;
+}
-/* $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
#include <sys/param.h> /* DEV_BSIZE */
#include <sys/disklabel.h>
+#include <ctype.h>
#include <err.h>
#include <fcntl.h>
#include <paths.h>
uint8_t b_type;
int A_flag, y_flag;
+void parse_b(const char *, uint32_t *, uint32_t *, uint8_t *);
+
static void
usage(void)
{
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;
+}
-/* $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 <mmu@grummel.net>
* Copyright (c) 2015 Kenneth R Westerback <krw@openbsd.org>
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)
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;
+}
-/* $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
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)
{
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)
{
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;
-}
-/* $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
/* 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 */
-/* $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
int modified;
+void ask_cmd(char **, char **);
+
void
USER_edit(off_t offset, off_t reloff)
{
}
} 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];
+}