util-linux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 3/6] fdisk: refactor -s option
@ 2012-05-20 16:11 Davidlohr Bueso
  2012-05-21  9:27 ` Karel Zak
  0 siblings, 1 reply; 3+ messages in thread
From: Davidlohr Bueso @ 2012-05-20 16:11 UTC (permalink / raw)
  To: Karel Zak, Petr Uzel; +Cc: util-linux

From: Davidlohr Bueso <dave@gnu.org>

Signed-off-by: Davidlohr Bueso <dave@gnu.org>
---
 fdisk/fdisk.c |   41 ++++++++++++++++++++++-------------------
 1 files changed, 22 insertions(+), 19 deletions(-)

diff --git a/fdisk/fdisk.c b/fdisk/fdisk.c
index 9387795..08afb45 100644
--- a/fdisk/fdisk.c
+++ b/fdisk/fdisk.c
@@ -2095,10 +2095,22 @@ static void command_prompt(void)
 	}
 }
 
+static unsigned long long get_dev_sz(char *dev)
+{
+	int fd;
+	unsigned long long size;
+	
+	if ((fd = open(dev, O_RDONLY)) < 0)
+		err(EXIT_FAILURE, _("unable to open %s"), dev);
+	if (blkdev_get_sectors(fd, &size) == -1)
+		fatal(ioctl_error);
+	close(fd);
+	return size/2;
+}
+
 int main(int argc, char **argv)
 {
-	int j, c;
-	int optl = 0, opts = 0;
+	int c, optl = 0, opts = 0;
 
 	setlocale(LC_ALL, "");
 	bindtextdomain(PACKAGE, LOCALEDIR);
@@ -2182,27 +2194,18 @@ int main(int argc, char **argv)
 	}
 
 	if (opts) {
-		unsigned long long size;
-
-		nowarn = 1;
-
-		opts = argc - optind;
-		if (opts <= 0)
+		/* print partition size for one or more devices */
+		int i, ndevs = argc - optind;
+		if (ndevs <= 0)
 			usage(stderr);
 
-		for (j = optind; j < argc; j++) {
-			disk_device = argv[j];
-			if ((fd = open(disk_device, O_RDONLY)) < 0)
-				err(EXIT_FAILURE, _("unable to open %s"), disk_device);
-			if (blkdev_get_sectors(fd, &size) == -1)
-				fatal(ioctl_error);
-			close(fd);
-			if (opts == 1)
-				printf("%llu\n", size/2);
+		for (i = optind; i < argc; i++) {
+			if (ndevs == 1)
+				printf("%llu\n", get_dev_sz(argv[i]));
 			else
-				printf("%s: %llu\n", argv[j], size/2);
+				printf("%s: %llu\n", argv[i], get_dev_sz(argv[i]));
 		}
-		exit(0);
+		exit(EXIT_SUCCESS);
 	}
 
 	if (argc-optind == 1)
-- 
1.7.4.1

^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH 3/6] fdisk: refactor -s option
  2012-05-20 16:11 [PATCH 3/6] fdisk: refactor -s option Davidlohr Bueso
@ 2012-05-21  9:27 ` Karel Zak
  2012-05-21 19:23   ` Davidlohr Bueso
  0 siblings, 1 reply; 3+ messages in thread
From: Karel Zak @ 2012-05-21  9:27 UTC (permalink / raw)
  To: Davidlohr Bueso; +Cc: Petr Uzel, util-linux

On Sun, May 20, 2012 at 06:11:29PM +0200, Davidlohr Bueso wrote:
> +static unsigned long long get_dev_sz(char *dev)

It's number of blocks (1024-bytes) rather than size, what about

    get_dev_blocks()

or so...

    Karel

-- 
 Karel Zak  <kzak@redhat.com>
 http://karelzak.blogspot.com

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH 3/6] fdisk: refactor -s option
  2012-05-21  9:27 ` Karel Zak
@ 2012-05-21 19:23   ` Davidlohr Bueso
  0 siblings, 0 replies; 3+ messages in thread
From: Davidlohr Bueso @ 2012-05-21 19:23 UTC (permalink / raw)
  To: Karel Zak; +Cc: Petr Uzel, util-linux

On Mon, 2012-05-21 at 11:27 +0200, Karel Zak wrote:
> On Sun, May 20, 2012 at 06:11:29PM +0200, Davidlohr Bueso wrote:
> > +static unsigned long long get_dev_sz(char *dev)
> 
> It's number of blocks (1024-bytes) rather than size, what about
> 
>     get_dev_blocks()
> 
> or so...

From: Davidlohr Bueso <dave@gnu.org>
Date: Mon, 21 May 2012 21:22:37 +0200
Subject: [PATCH] fdisk: refactor -s option

Signed-off-by: Davidlohr Bueso <dave@gnu.org>
---
 fdisk/fdisk.c |   41 ++++++++++++++++++++++-------------------
 1 files changed, 22 insertions(+), 19 deletions(-)

diff --git a/fdisk/fdisk.c b/fdisk/fdisk.c
index 9387795..9788e5c 100644
--- a/fdisk/fdisk.c
+++ b/fdisk/fdisk.c
@@ -2095,10 +2095,22 @@ static void command_prompt(void)
 	}
 }
 
+static unsigned long long get_dev_blocks(char *dev)
+{
+	int fd;
+	unsigned long long size;
+	
+	if ((fd = open(dev, O_RDONLY)) < 0)
+		err(EXIT_FAILURE, _("unable to open %s"), dev);
+	if (blkdev_get_sectors(fd, &size) == -1)
+		fatal(ioctl_error);
+	close(fd);
+	return size/2;
+}
+
 int main(int argc, char **argv)
 {
-	int j, c;
-	int optl = 0, opts = 0;
+	int c, optl = 0, opts = 0;
 
 	setlocale(LC_ALL, "");
 	bindtextdomain(PACKAGE, LOCALEDIR);
@@ -2182,27 +2194,18 @@ int main(int argc, char **argv)
 	}
 
 	if (opts) {
-		unsigned long long size;
-
-		nowarn = 1;
-
-		opts = argc - optind;
-		if (opts <= 0)
+		/* print partition size for one or more devices */
+		int i, ndevs = argc - optind;
+		if (ndevs <= 0)
 			usage(stderr);
 
-		for (j = optind; j < argc; j++) {
-			disk_device = argv[j];
-			if ((fd = open(disk_device, O_RDONLY)) < 0)
-				err(EXIT_FAILURE, _("unable to open %s"), disk_device);
-			if (blkdev_get_sectors(fd, &size) == -1)
-				fatal(ioctl_error);
-			close(fd);
-			if (opts == 1)
-				printf("%llu\n", size/2);
+		for (i = optind; i < argc; i++) {
+			if (ndevs == 1)
+				printf("%llu\n", get_dev_blocks(argv[i]));
 			else
-				printf("%s: %llu\n", argv[j], size/2);
+				printf("%s: %llu\n", argv[i], get_dev_blocks(argv[i]));
 		}
-		exit(0);
+		exit(EXIT_SUCCESS);
 	}
 
 	if (argc-optind == 1)
-- 
1.7.4.1




^ permalink raw reply related	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2012-05-21 19:23 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2012-05-20 16:11 [PATCH 3/6] fdisk: refactor -s option Davidlohr Bueso
2012-05-21  9:27 ` Karel Zak
2012-05-21 19:23   ` Davidlohr Bueso

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).