diff options
-rw-r--r-- | sbin/fdisk/cmd.c | 24 | ||||
-rw-r--r-- | sbin/fdisk/disk.c | 11 | ||||
-rw-r--r-- | sbin/fdisk/disk.h | 4 | ||||
-rw-r--r-- | sbin/fdisk/mbr.c | 5 | ||||
-rw-r--r-- | sbin/fdisk/user.c | 50 |
5 files changed, 32 insertions, 62 deletions
diff --git a/sbin/fdisk/cmd.c b/sbin/fdisk/cmd.c index 6d4ee70f227..61d725ad2be 100644 --- a/sbin/fdisk/cmd.c +++ b/sbin/fdisk/cmd.c @@ -1,4 +1,4 @@ -/* $OpenBSD: cmd.c,v 1.42 2006/07/27 04:06:13 ray Exp $ */ +/* $OpenBSD: cmd.c,v 1.43 2009/02/08 18:03:18 krw Exp $ */ /* * Copyright (c) 1997 Tobias Weingartner @@ -25,6 +25,8 @@ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +#include <err.h> +#include <errno.h> #include <stdio.h> #include <ctype.h> #include <memory.h> @@ -314,18 +316,22 @@ int Xwrite(cmd_t *cmd, disk_t *disk, mbr_t *mbr, mbr_t *tt, int offset) { char mbr_buf[DEV_BSIZE]; - int fd, ret; - - ret = CMD_CONT; - - printf("Writing MBR at offset %d.\n", offset); + int fd; fd = DISK_open(disk->name, O_RDWR); MBR_make(mbr, mbr_buf); - if (MBR_write(fd, offset, mbr_buf) != -1) - ret = CMD_CLEAN; + + printf("Writing MBR at offset %d.\n", offset); + if (MBR_write(fd, offset, mbr_buf) == -1) { + int saved_errno = errno; + warn("error writing MBR"); + close(fd); + errno = saved_errno; + return (CMD_CONT); + } close(fd); - return (ret); + + return (CMD_CLEAN); } /* ARGSUSED */ diff --git a/sbin/fdisk/disk.c b/sbin/fdisk/disk.c index f4c5ef49f24..6d54aaf36d4 100644 --- a/sbin/fdisk/disk.c +++ b/sbin/fdisk/disk.c @@ -1,4 +1,4 @@ -/* $OpenBSD: disk.c,v 1.28 2007/04/27 11:42:44 krw Exp $ */ +/* $OpenBSD: disk.c,v 1.29 2009/02/08 18:03:18 krw Exp $ */ /* * Copyright (c) 1997, 2001 Tobias Weingartner @@ -57,13 +57,6 @@ DISK_open(char *disk, int mode) return (fd); } -int -DISK_close(int fd) -{ - - return (close(fd)); -} - /* Routine to go after the disklabel for geometry * information. This should work everywhere, but * in the land of PC, things are not always what @@ -94,7 +87,7 @@ DISK_getlabelmetrics(char *name) lm->size = dl.d_secperunit; unit_types[SECTORS].conversion = dl.d_secsize; } - DISK_close(fd); + close(fd); } return (lm); diff --git a/sbin/fdisk/disk.h b/sbin/fdisk/disk.h index 6785b583661..1be075d49a9 100644 --- a/sbin/fdisk/disk.h +++ b/sbin/fdisk/disk.h @@ -1,4 +1,4 @@ -/* $OpenBSD: disk.h,v 1.9 2007/04/26 22:42:11 krw Exp $ */ +/* $OpenBSD: disk.h,v 1.10 2009/02/08 18:03:18 krw Exp $ */ /* * Copyright (c) 1997 Tobias Weingartner @@ -44,9 +44,7 @@ typedef struct _disk_t { /* Prototypes */ int DISK_open(char *, int); -int DISK_close(int); int DISK_getmetrics(disk_t *, DISK_metrics *); int DISK_printmetrics(disk_t *, char *); #endif /* _DISK_H */ - diff --git a/sbin/fdisk/mbr.c b/sbin/fdisk/mbr.c index 3d18c361eb1..5fa12606c8e 100644 --- a/sbin/fdisk/mbr.c +++ b/sbin/fdisk/mbr.c @@ -1,4 +1,4 @@ -/* $OpenBSD: mbr.c,v 1.23 2006/12/10 19:19:32 krw Exp $ */ +/* $OpenBSD: mbr.c,v 1.24 2009/02/08 18:03:18 krw Exp $ */ /* * Copyright (c) 1997 Tobias Weingartner @@ -226,12 +226,11 @@ MBR_pcopy(disk_t *disk, mbr_t *mbr) fd = DISK_open(disk->name, O_RDONLY); MBR_read(fd, offset, mbr_disk); - DISK_close(fd); + close(fd); MBR_parse(disk, mbr_disk, offset, reloff, &mbrd); for (i = 0; i < NDOSPART; i++) { PRT_parse(disk, &mbr_disk[MBR_PART_OFF + MBR_PART_SIZE * i], offset, reloff, &mbr->part[i]); - PRT_print(i, &mbr->part[i], NULL); } } diff --git a/sbin/fdisk/user.c b/sbin/fdisk/user.c index 8e952af5a5d..dc889a6e553 100644 --- a/sbin/fdisk/user.c +++ b/sbin/fdisk/user.c @@ -1,4 +1,4 @@ -/* $OpenBSD: user.c,v 1.23 2006/07/27 04:06:13 ray Exp $ */ +/* $OpenBSD: user.c,v 1.24 2009/02/08 18:03:18 krw Exp $ */ /* * Copyright (c) 1997 Tobias Weingartner @@ -67,38 +67,18 @@ static cmd_table_t cmd_table[] = { int USER_init(disk_t *disk, mbr_t *tt, int preserve) { - int fd, yn; - char mbr_buf[DEV_BSIZE]; - char *msgp = "\nDo you wish to write new MBR?"; - char *msgk = "\nDo you wish to write new MBR and partition table?"; + char *query; - if (preserve) + if (preserve) { MBR_pcopy(disk, tt); - else + query = "Do you wish to write new MBR?"; + } else { MBR_init(disk, tt); + query = "Do you wish to write new MBR and partition table?"; + } - /* Write sector 0 */ - printf("\a\n" - "\t-----------------------------------------------------\n" - "\t------ ATTENTION - UPDATING MASTER BOOT RECORD ------\n" - "\t-----------------------------------------------------\n"); - if (preserve) - yn = ask_yn(msgp); - else - yn = ask_yn(msgk); - - if (yn) { - fd = DISK_open(disk->name, O_RDWR); - MBR_make(tt, mbr_buf); - if (MBR_write(fd, 0, mbr_buf) == -1) { - int saved_errno = errno; - DISK_close(fd); - errno = saved_errno; - return (-1); - } - DISK_close(fd); - } else - printf("MBR is unchanged\n"); + if (ask_yn(query)) + Xwrite(NULL, disk, tt, NULL, 0); return (0); } @@ -123,7 +103,7 @@ USER_modify(disk_t *disk, mbr_t *tt, off_t offset, off_t reloff) /* Read MBR & partition */ fd = DISK_open(disk->name, O_RDONLY); MBR_read(fd, offset, mbr_buf); - DISK_close(fd); + close(fd); /* Parse the sucker */ MBR_parse(disk, mbr_buf, offset, reloff, &mbr); @@ -171,14 +151,8 @@ again: /* Write out MBR */ if (modified) { if (st == CMD_SAVE) { - printf("Writing current MBR to disk.\n"); - fd = DISK_open(disk->name, O_RDWR); - MBR_make(&mbr, mbr_buf); - if (MBR_write(fd, offset, mbr_buf) == -1) { - warn("error writing MBR"); - close(fd); + if (Xwrite(NULL, disk, &mbr, NULL, offset) == CMD_CONT) goto again; - } close(fd); } else printf("Aborting changes to current MBR.\n"); @@ -219,6 +193,6 @@ USER_print_disk(disk_t *disk) } } while (offset); - return (DISK_close(fd)); + return (close(fd)); } |