From ad8cd66adf344da5b6c0ab40ee2618640f086350 Mon Sep 17 00:00:00 2001 From: Karel Zak Date: Wed, 31 Aug 2016 15:51:11 +0200 Subject: [PATCH] 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 --- disk-utils/sfdisk.c | 40 ++++++++++++++++++++++++++-------------- 1 file 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 [[-N] ] * @@ -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; -- 2.47.3