aboutsummaryrefslogtreecommitdiffstats
path: root/disk-utils/sfdisk.c
diff options
context:
space:
mode:
authorKarel Zak <kzak@redhat.com>2016-08-31 15:51:11 +0200
committerKarel Zak <kzak@redhat.com>2016-08-31 15:51:11 +0200
commitad8cd66adf344da5b6c0ab40ee2618640f086350 (patch)
tree2104134fc180a78649abe0286428d7f7d6402d96 /disk-utils/sfdisk.c
parentb54439cbadaf77ff96baf43c83670dbe7395f95b (diff)
downloadutil-linux-ad8cd66adf344da5b6c0ab40ee2618640f086350.tar.gz
sfdisk: make non-interactive output more readable
# 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>
Diffstat (limited to 'disk-utils/sfdisk.c')
-rw-r--r--disk-utils/sfdisk.c40
1 files changed, 26 insertions, 14 deletions
diff --git a/disk-utils/sfdisk.c b/disk-utils/sfdisk.c
index 6613a4fd96..a97c5854db 100644
--- a/disk-utils/sfdisk.c
+++ b/disk-utils/sfdisk.c
@@ -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;