]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
sfdisk: avoid unneeded empty lines with '--list-free'
authorWolfram Sang <wsa@kernel.org>
Mon, 11 May 2020 16:10:48 +0000 (18:10 +0200)
committerKarel Zak <kzak@redhat.com>
Tue, 12 May 2020 08:09:46 +0000 (10:09 +0200)
Similar to commit 4a52959d1 ("(s)fdisk: avoid unneeded empty lines with
'--list'"), there were also two superfluous empty lines when /dev/sr0
didn't contain a medium. Refactor the '--list-free' code the same way as
in the mentioned commit.

Signed-off-by: Wolfram Sang <wsa@kernel.org>
disk-utils/fdisk-list.c
disk-utils/fdisk-list.h
disk-utils/sfdisk.c

index 78e17a97fad1e5ef90220de307cdfb09c3f3bb29..8cc116281983644fe19347679a5a153523d1b2fa 100644 (file)
@@ -383,7 +383,8 @@ int print_device_pt(struct fdisk_context *cxt, char *device, int warnme,
        return 0;
 }
 
-int print_device_freespace(struct fdisk_context *cxt, char *device, int warnme)
+int print_device_freespace(struct fdisk_context *cxt, char *device, int warnme,
+                          int seperator)
 {
        if (fdisk_assign_device(cxt, device, 1) != 0) { /* read-only */
                if (warnme || errno == EACCES)
@@ -391,6 +392,9 @@ int print_device_freespace(struct fdisk_context *cxt, char *device, int warnme)
                return -1;
        }
 
+       if (seperator)
+               fputs("\n\n", stdout);
+
        list_freespace(cxt);
        fdisk_deassign_device(cxt, 1);
        return 0;
@@ -412,15 +416,13 @@ void print_all_devices_pt(struct fdisk_context *cxt, int verify)
 void print_all_devices_freespace(struct fdisk_context *cxt)
 {
        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_freespace(cxt, dev, 0) == 0)
-                       ct++;
+               print_device_freespace(cxt, dev, 0, sep);
                free(dev);
+               sep = 1;
        }
 }
 
index 47518c4987304852c00ea9a7c8128caafeb54215..a31ab0a7e981f9a7e4b25b87b1c07a60dd496ec7 100644 (file)
@@ -8,7 +8,7 @@ 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, int seperator);
-extern int print_device_freespace(struct fdisk_context *cxt, char *device, int warnme);
+extern int print_device_freespace(struct fdisk_context *cxt, char *device, int warnme, int seperator);
 
 extern void print_all_devices_pt(struct fdisk_context *cxt, int verify);
 extern void print_all_devices_freespace(struct fdisk_context *cxt);
index 6299cb49c7559de4d34684967364800ff7873f86..e0c25fde76aa1986bccbdf177cc2eb473075df4b 100644 (file)
@@ -673,15 +673,11 @@ static int command_list_freespace(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_freespace(sf->cxt, argv[i], 1) != 0)
+               for (i = 0; i < argc; i++)
+                       if (print_device_freespace(sf->cxt, argv[i], 1, i) != 0)
                                fail++;
-                       ct++;
-               }
        } else
                print_all_devices_freespace(sf->cxt);