[PATCH 08/14] fdisk: avoid code duplication

Sami Kerola
2014-07-13T20:25:27Z

To me having call to close() twice is less readable than one new
variable.

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
---
disk-utils/fdisk.c | 9 ++++-----
1 file changed, 4 insertions(+), 5 deletions(-)

diff --git a/disk-utils/fdisk.c b/disk-utils/fdisk.c
index e9b4fec..d6bb737 100644
--- a/disk-utils/fdisk.c
+++ b/disk-utils/fdisk.c
@@ -725,16 +725,15 @@ static void print_all_devices_pt(struct fdisk_context *cxt)

static sector_t get_dev_blocks(char *dev)
{
- int fd;
+ int fd, ret;
sector_t size;

if ((fd = open(dev, O_RDONLY)) < 0)
err(EXIT_FAILURE, _("cannot open %s"), dev);
- if (blkdev_get_sectors(fd, &size) == -1) {
- close(fd);
- err(EXIT_FAILURE, _("BLKGETSIZE ioctl failed on %s"), dev);
- }
+ ret = blkdev_get_sectors(fd, &size);
close(fd);
+ if (ret < 0)
+ err(EXIT_FAILURE, _("BLKGETSIZE ioctl failed on %s"), dev);
return size/2;
}

--
2.0.1