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>
This commit is contained in:
Karel Zak 2016-08-31 15:51:11 +02:00
parent b54439cbad
commit ad8cd66adf
1 changed files with 26 additions and 14 deletions

View 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;