Util-Linux Archive on lore.kernel.org
 help / color / Atom feed
* [PATCH] (s)fdisk: avoid unneeded empty lines with '--list'
@ 2020-05-06 10:39 Wolfram Sang
  2020-05-11  9:45 ` Karel Zak
  0 siblings, 1 reply; 2+ messages in thread
From: Wolfram Sang @ 2020-05-06 10:39 UTC (permalink / raw)
  To: util-linux; +Cc: Wolfram Sang

On my system, I got two superfluous empty lines because /dev/sr0 didn't
contain a medium. Refactor the code to handle the seperator within
print_device_pt() and print it only when assigning the device worked.
This unifies handling between print_all_devices_pt and (s)fdisk because
the latter did not consider the return code for the seperator while the
former did. Also, it saves some lines of code.

Addresses: https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=814184 (first part)
Signed-off-by: Wolfram Sang <wsa@kernel.org>
---

If you like this cleanup, I will do the same for
'print_device_freespace' and friends.

 disk-utils/fdisk-list.c | 14 ++++++++------
 disk-utils/fdisk-list.h |  2 +-
 disk-utils/fdisk.c      |  9 ++-------
 disk-utils/sfdisk.c     | 10 +++-------
 4 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/disk-utils/fdisk-list.c b/disk-utils/fdisk-list.c
index 834c7b4e9..78e17a97f 100644
--- a/disk-utils/fdisk-list.c
+++ b/disk-utils/fdisk-list.c
@@ -360,7 +360,8 @@ char *next_proc_partition(FILE **f)
 	return NULL;
 }
 
-int print_device_pt(struct fdisk_context *cxt, char *device, int warnme, int verify)
+int print_device_pt(struct fdisk_context *cxt, char *device, int warnme,
+		    int verify, int seperator)
 {
 	if (fdisk_assign_device(cxt, device, 1) != 0) {	/* read-only */
 		if (warnme || errno == EACCES)
@@ -368,6 +369,9 @@ int print_device_pt(struct fdisk_context *cxt, char *device, int warnme, int ver
 		return -1;
 	}
 
+	if (seperator)
+		fputs("\n\n", stdout);
+
 	list_disk_geometry(cxt);
 
 	if (fdisk_has_label(cxt)) {
@@ -395,15 +399,13 @@ int print_device_freespace(struct fdisk_context *cxt, char *device, int warnme)
 void print_all_devices_pt(struct fdisk_context *cxt, int verify)
 {
 	FILE *f = NULL;
-	int ct = 0;
+	int sep = 0;
 	char *dev;
 
 	while ((dev = next_proc_partition(&f))) {
-		if (ct)
-			fputs("\n\n", stdout);
-		if (print_device_pt(cxt, dev, 0, verify) == 0)
-			ct++;
+		print_device_pt(cxt, dev, 0, verify, sep);
 		free(dev);
+		sep = 1;
 	}
 }
 
diff --git a/disk-utils/fdisk-list.h b/disk-utils/fdisk-list.h
index 4ed5c256b..47518c498 100644
--- a/disk-utils/fdisk-list.h
+++ b/disk-utils/fdisk-list.h
@@ -7,7 +7,7 @@ extern void list_disk_geometry(struct fdisk_context *cxt);
 extern void list_freespace(struct fdisk_context *cxt);
 
 extern char *next_proc_partition(FILE **f);
-extern int print_device_pt(struct fdisk_context *cxt, char *device, int warnme, int verify);
+extern int print_device_pt(struct fdisk_context *cxt, char *device, int warnme, int verify, int seperator);
 extern int print_device_freespace(struct fdisk_context *cxt, char *device, int warnme);
 
 extern void print_all_devices_pt(struct fdisk_context *cxt, int verify);
diff --git a/disk-utils/fdisk.c b/disk-utils/fdisk.c
index 8a9b8cf2a..bf5307b62 100644
--- a/disk-utils/fdisk.c
+++ b/disk-utils/fdisk.c
@@ -1080,15 +1080,10 @@ int main(int argc, char **argv)
 
 		if (argc > optind) {
 			int k;
-			int ct = 0;
 
-			for (rc = 0, k = optind; k < argc; k++) {
-				if (ct)
-				    fputs("\n\n", stdout);
+			for (rc = 0, k = optind; k < argc; k++)
+				rc += print_device_pt(cxt, argv[k], 1, 0, k != optind);
 
-				rc += print_device_pt(cxt, argv[k], 1, 0);
-				ct++;
-			}
 			if (rc)
 				return EXIT_FAILURE;
 		} else
diff --git a/disk-utils/sfdisk.c b/disk-utils/sfdisk.c
index 07e8b2341..6299cb49c 100644
--- a/disk-utils/sfdisk.c
+++ b/disk-utils/sfdisk.c
@@ -653,15 +653,11 @@ static int command_list_partitions(struct sfdisk *sf, int argc, char **argv)
 	fdisk_enable_listonly(sf->cxt, 1);
 
 	if (argc) {
-		int i, ct = 0;
+		int i;
 
-		for (i = 0; i < argc; i++) {
-			if (ct)
-				fputs("\n\n", stdout);
-			if (print_device_pt(sf->cxt, argv[i], 1, sf->verify) != 0)
+		for (i = 0; i < argc; i++)
+			if (print_device_pt(sf->cxt, argv[i], 1, sf->verify, i) != 0)
 				fail++;
-			ct++;
-		}
 	} else
 		print_all_devices_pt(sf->cxt, sf->verify);
 
-- 
2.26.2


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

* Re: [PATCH] (s)fdisk: avoid unneeded empty lines with '--list'
  2020-05-06 10:39 [PATCH] (s)fdisk: avoid unneeded empty lines with '--list' Wolfram Sang
@ 2020-05-11  9:45 ` Karel Zak
  0 siblings, 0 replies; 2+ messages in thread
From: Karel Zak @ 2020-05-11  9:45 UTC (permalink / raw)
  To: Wolfram Sang; +Cc: util-linux

On Wed, May 06, 2020 at 12:39:21PM +0200, Wolfram Sang wrote:
>  disk-utils/fdisk-list.c | 14 ++++++++------
>  disk-utils/fdisk-list.h |  2 +-
>  disk-utils/fdisk.c      |  9 ++-------
>  disk-utils/sfdisk.c     | 10 +++-------
>  4 files changed, 14 insertions(+), 21 deletions(-)

Applied, thanks.

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


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

end of thread, back to index

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-05-06 10:39 [PATCH] (s)fdisk: avoid unneeded empty lines with '--list' Wolfram Sang
2020-05-11  9:45 ` Karel Zak

Util-Linux Archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/util-linux/0 util-linux/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 util-linux util-linux/ https://lore.kernel.org/util-linux \
		util-linux@vger.kernel.org
	public-inbox-index util-linux

Example config snippet for mirrors

Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/org.kernel.vger.util-linux


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git