util-linux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Wolfram Sang <wsa@kernel.org>
To: util-linux@vger.kernel.org
Cc: Wolfram Sang <wsa@kernel.org>
Subject: [PATCH] (s)fdisk: avoid unneeded empty lines with '--list'
Date: Wed,  6 May 2020 12:39:21 +0200	[thread overview]
Message-ID: <20200506103921.2689-1-wsa@kernel.org> (raw)

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


             reply	other threads:[~2020-05-06 10:39 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-05-06 10:39 Wolfram Sang [this message]
2020-05-11  9:45 ` [PATCH] (s)fdisk: avoid unneeded empty lines with '--list' Karel Zak

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200506103921.2689-1-wsa@kernel.org \
    --to=wsa@kernel.org \
    --cc=util-linux@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).