summaryrefslogtreecommitdiff
path: root/sbin/fdisk
diff options
context:
space:
mode:
authorRay Lai <ray@cvs.openbsd.org>2006-05-29 05:09:37 +0000
committerRay Lai <ray@cvs.openbsd.org>2006-05-29 05:09:37 +0000
commit48ca5a4d3d37598ebd1cae7304116db9fdac8203 (patch)
tree68995b314850fcc0c306cf4502af857753e5d937 /sbin/fdisk
parentc6e4dc78ab29fab11e263b608249ce3d38c67839 (diff)
Change MBR_read() and MBR_write() to return -1 on error and set
errno = EIO for short reads. This makes it easier to check for read and write errors. Much patient tutoring by weingart@, deraadt@, and cloder@. Correctly use ssize_t to store read(2) and write(2) return values. No functional change, since MBR_read() and MBR_write() return values aren't currently checked. OK deraadt@
Diffstat (limited to 'sbin/fdisk')
-rw-r--r--sbin/fdisk/mbr.c29
1 files changed, 20 insertions, 9 deletions
diff --git a/sbin/fdisk/mbr.c b/sbin/fdisk/mbr.c
index aef5452b579..a1af4508de7 100644
--- a/sbin/fdisk/mbr.c
+++ b/sbin/fdisk/mbr.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: mbr.c,v 1.21 2004/09/18 23:22:05 deraadt Exp $ */
+/* $OpenBSD: mbr.c,v 1.22 2006/05/29 05:09:36 ray Exp $ */
/*
* Copyright (c) 1997 Tobias Weingartner
@@ -26,6 +26,7 @@
*/
#include <err.h>
+#include <errno.h>
#include <util.h>
#include <stdio.h>
#include <unistd.h>
@@ -134,15 +135,20 @@ int
MBR_read(int fd, off_t where, char *buf)
{
off_t off;
- int len;
+ ssize_t len;
where *= DEV_BSIZE;
off = lseek(fd, where, SEEK_SET);
if (off != where)
- return (off);
+ return (-1);
len = read(fd, buf, DEV_BSIZE);
- if (len != DEV_BSIZE)
- return (len);
+ if (len == -1)
+ return (-1);
+ if (len != DEV_BSIZE) {
+ /* short read */
+ errno = EIO;
+ return (-1);
+ }
return (0);
}
@@ -150,15 +156,20 @@ int
MBR_write(int fd, off_t where, char *buf)
{
off_t off;
- int len;
+ ssize_t len;
where *= DEV_BSIZE;
off = lseek(fd, where, SEEK_SET);
if (off != where)
- return (off);
+ return (-1);
len = write(fd, buf, DEV_BSIZE);
- if (len != DEV_BSIZE)
- return (len);
+ if (len == -1)
+ return (-1);
+ if (len != DEV_BSIZE) {
+ /* short write */
+ errno = EIO;
+ return (-1);
+ }
(void) ioctl(fd, DIOCRLDINFO, 0);
return (0);
}