From: krw Date: Mon, 19 Jul 2021 19:46:20 +0000 (+0000) Subject: Abstract reading the built-in MBR or MBR file into X-Git-Url: http://artulab.com/gitweb/?a=commitdiff_plain;h=62b9ed031fdaa36937f171ab329351784a6a0301;p=openbsd Abstract reading the built-in MBR or MBR file into a separate function. No functional change. --- diff --git a/sbin/fdisk/fdisk.c b/sbin/fdisk/fdisk.c index b92c4e12268..efd9f79e63e 100644 --- a/sbin/fdisk/fdisk.c +++ b/sbin/fdisk/fdisk.c @@ -1,4 +1,4 @@ -/* $OpenBSD: fdisk.c,v 1.126 2021/07/18 15:28:37 krw Exp $ */ +/* $OpenBSD: fdisk.c,v 1.127 2021/07/19 19:46:20 krw Exp $ */ /* * Copyright (c) 1997 Tobias Weingartner @@ -45,6 +45,7 @@ static unsigned char builtin_mbr[] = { int A_flag, y_flag; void parse_bootprt(const char *); +void get_default_mbr(const char *, struct mbr *); static void usage(void) @@ -61,15 +62,13 @@ usage(void) int main(int argc, char *argv[]) { - struct dos_mbr dos_mbr; struct mbr mbr; #ifdef HAS_MBR char *mbrfile = _PATH_MBR; #else char *mbrfile = NULL; #endif - ssize_t len; - int ch, fd, efi, error; + int ch, error; int e_flag = 0, g_flag = 0, i_flag = 0, u_flag = 0; int verbosity = TERSE; int oflags = O_RDONLY; @@ -163,31 +162,11 @@ main(int argc, char *argv[]) if (pledge("stdio rpath wpath disklabel proc exec", NULL) == -1) err(1, "pledge"); + get_default_mbr(mbrfile, &initial_mbr); error = MBR_read(0, 0, &mbr); if (error) errx(1, "Can't read MBR!"); - /* Create initial/default MBR. */ - if (mbrfile == NULL) { - memcpy(&dos_mbr, builtin_mbr, sizeof(dos_mbr)); - } else { - fd = open(mbrfile, O_RDONLY); - if (fd == -1) { - warn("%s", mbrfile); - warnx("using builtin MBR"); - memcpy(&dos_mbr, builtin_mbr, sizeof(dos_mbr)); - } else { - len = read(fd, &dos_mbr, sizeof(dos_mbr)); - close(fd); - if (len == -1) - err(1, "Unable to read MBR from '%s'", mbrfile); - else if (len != sizeof(dos_mbr)) - errx(1, "Unable to read complete MBR from '%s'", - mbrfile); - } - } - MBR_parse(&dos_mbr, 0, 0, &initial_mbr); - query = NULL; if (A_flag) { if (GPT_read(ANYGPT)) @@ -273,3 +252,32 @@ parse_bootprt(const char *arg) disk.dk_bootprt.prt_bs = blockoffset; disk.dk_bootprt.prt_id = partitiontype; } + +void +get_default_mbr(const char *mbrfile, struct mbr *mbr) +{ + struct dos_mbr dos_mbr; + ssize_t len; + int fd; + + if (mbrfile == NULL) { + memcpy(&dos_mbr, builtin_mbr, sizeof(dos_mbr)); + } else { + fd = open(mbrfile, O_RDONLY); + if (fd == -1) { + warn("%s", mbrfile); + warnx("using builtin MBR"); + memcpy(&dos_mbr, builtin_mbr, sizeof(dos_mbr)); + } else { + len = read(fd, &dos_mbr, sizeof(dos_mbr)); + close(fd); + if (len == -1) + err(1, "Unable to read MBR from '%s'", mbrfile); + else if (len != sizeof(dos_mbr)) + errx(1, "Unable to read complete MBR from '%s'", + mbrfile); + } + } + + MBR_parse(&dos_mbr, 0, 0, mbr); +}