]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
sfdisk: make non-interactive output more readable
authorKarel Zak <kzak@redhat.com>
Wed, 31 Aug 2016 13:51:11 +0000 (15:51 +0200)
committerKarel Zak <kzak@redhat.com>
Wed, 31 Aug 2016 13:51:11 +0000 (15:51 +0200)
 # echo -e ',1M\n,2M' | sfdisk /dev/sdc

Old version:

  >>> Created a new DOS disklabel with disk identifier 0x8fc7d065.
  Created a new partition 1 of type 'Linux' and of size 1 MiB.
  /dev/sdc2: Created a new partition 2 of type 'Linux' and of size 2 MiB.
  /dev/sdc3:

New version:

  >>> Created a new DOS disklabel with disk identifier 0x9afe17c0.
 /dev/sdc1: Created a new partition 1 of type 'Linux' and of size 1 MiB.
 /dev/sdc2: Created a new partition 2 of type 'Linux' and of size 2 MiB.
 /dev/sdc3: Done.

Addresses: https://github.com/karelzak/util-linux/issues/337
Signed-off-by: Karel Zak <kzak@redhat.com>
disk-utils/sfdisk.c

index 6613a4fd960ba72667aa49a4165d150a0c756cac..a97c5854db442d1e185b7db0c990abe44eb4a085 100644 (file)
@@ -1472,6 +1472,25 @@ done:
        return rc;
 }
 
+static void refresh_prompt_buffer(struct sfdisk *sf, const char *devname,
+                                 size_t next_partno, int created)
+{
+       if (created) {
+               char *partname = fdisk_partname(devname, next_partno + 1);
+               if (!partname)
+                       err(EXIT_FAILURE, _("failed to allocate partition name"));
+
+               if (!sf->prompt || !startswith(sf->prompt, partname)) {
+                       free(sf->prompt);
+                       xasprintf(&sf->prompt,"%s: ", partname);
+               }
+               free(partname);
+       } else if (!sf->prompt || !startswith(sf->prompt, SFDISK_PROMPT)) {
+               free(sf->prompt);
+               sf->prompt = xstrdup(SFDISK_PROMPT);
+       }
+}
+
 /*
  * sfdisk <device> [[-N] <partno>]
  *
@@ -1639,20 +1658,7 @@ static int command_fdisk(struct sfdisk *sf, int argc, char **argv)
                        break;
                }
 
-               if (created) {
-                       char *partname = fdisk_partname(devname, next_partno + 1);
-                       if (!partname)
-                               err(EXIT_FAILURE, _("failed to allocate partition name"));
-                       if (!sf->prompt || !startswith(sf->prompt, partname)) {
-                               free(sf->prompt);
-                               xasprintf(&sf->prompt,"%s: ", partname);
-                       }
-                       free(partname);
-               } else if (!sf->prompt || !startswith(sf->prompt, SFDISK_PROMPT)) {
-                       free(sf->prompt);
-                       sf->prompt = xstrdup(SFDISK_PROMPT);
-               }
-
+               refresh_prompt_buffer(sf, devname, next_partno, created);
                if (sf->prompt && (sf->interactive || !sf->quiet)) {
 #ifndef HAVE_LIBREADLINE
                        fputs(sf->prompt, stdout);
@@ -1672,6 +1678,7 @@ static int command_fdisk(struct sfdisk *sf, int argc, char **argv)
                        continue;
                } else if (rc == 1) {
                        rc = SFDISK_DONE_EOF;
+                       fputs(_("Done.\n"), stdout);
                        break;
                }
 
@@ -1700,6 +1707,11 @@ static int command_fdisk(struct sfdisk *sf, int argc, char **argv)
                                rc = rc == 0 ? SFDISK_DONE_ASK : SFDISK_DONE_ABORT;
                                break;
                        } else if (!rc) {               /* add partition */
+                               if (!sf->interactive &&
+                                   (!sf->prompt || startswith(sf->prompt, SFDISK_PROMPT))) {
+                                       refresh_prompt_buffer(sf, devname, next_partno, created);
+                                       fputs(sf->prompt, stdout);
+                               }
                                rc = fdisk_add_partition(sf->cxt, pa, &cur_partno);
                                if (rc) {
                                        errno = -rc;