From d5bee4bb59b4e78cdc85800b75f15d70def79c4a Mon Sep 17 00:00:00 2001 From: Karel Zak Date: Tue, 9 Sep 2014 13:36:16 +0200 Subject: [PATCH] libfdisk: cleanup script size= code Signed-off-by: Karel Zak --- disk-utils/sfdisk.c | 14 ++++++++++---- libfdisk/src/script.c | 34 ++++++++++++++++++++++------------ 2 files changed, 32 insertions(+), 16 deletions(-) diff --git a/disk-utils/sfdisk.c b/disk-utils/sfdisk.c index 5381d43dc2..d9608d770f 100644 --- a/disk-utils/sfdisk.c +++ b/disk-utils/sfdisk.c @@ -386,10 +386,15 @@ static int command_fdisk(struct sfdisk *sf, int argc, char **argv) fdisk_script_set_header(dp, "label", label); - printf(_("\nInput in the following format; absent fields get a default value.\n" - " \n" - "Usually you only need to specify and \n\n")); + fputc('\n', stdout); + printf(_("Input in the following format; absent fields get a default value.\n" + " \n")); + fputc('\n', stdout); + printf(_("If the size is specified by {K,M,G,T,P,E,Z,Y} then\n" + "it's interpreted as size in bytes rather then in sectors.\n")); + + fputc('\n', stdout); if (!fdisk_has_label(sf->cxt)) printf(_("sfdisk is going to create a new '%s' disk label.\n" "Use 'label: ' before you define a first partition\n" @@ -416,6 +421,7 @@ static int command_fdisk(struct sfdisk *sf, int argc, char **argv) rc = fdisk_script_read_line(dp, stdin, buf, sizeof(buf)); if (rc == 1) { /* end of file */ + printf(_("Done.\n")); rc = 0; break; } else if (rc < 0) { @@ -455,7 +461,7 @@ static int command_fdisk(struct sfdisk *sf, int argc, char **argv) } else if (pa) /* error, drop partition from script */ fdisk_table_remove_partition(tb, pa); } else - printf(_("OK\n")); /* probably added script header */ + printf(_("\nScript header accepted.\n")); } while (1); diff --git a/libfdisk/src/script.c b/libfdisk/src/script.c index e0e2d5387c..ebfaf61a53 100644 --- a/libfdisk/src/script.c +++ b/libfdisk/src/script.c @@ -466,7 +466,7 @@ static char *next_separator(char *s) return NULL; } -static int next_number(char **s, uint64_t *num) +static int next_number(char **s, uint64_t *num, int *power) { char *end = NULL; int rc; @@ -482,7 +482,7 @@ static int next_number(char **s, uint64_t *num) if (end) *end = '\0'; - rc = strtosize(*s, (uintmax_t *) num); + rc = parse_size(*s, (uintmax_t *) num, power); if (end) *s = ++end; else @@ -588,21 +588,27 @@ static int parse_script_line(struct fdisk_script *dp, char *s) if (!*p) break; + DBG(SCRIPT, ul_debugobj(dp, " parsing '%s'", p)); + if (!strncasecmp(p, "start=", 6)) { p += 6; - rc = next_number(&p, &num); + rc = next_number(&p, &num, NULL); if (!rc) fdisk_partition_set_start(pa, num); } else if (!strncasecmp(p, "size=", 5)) { - p += 5; - rc = next_number(&p, &num); - if (!rc) - fdisk_partition_set_size(pa, num / dp->cxt->sector_size); + int pow = 0; + p += 5; + rc = next_number(&p, &num, &pow); + if (!rc) { + if (pow) + num /= dp->cxt->sector_size; + fdisk_partition_set_size(pa, num); + } } else if (!strncasecmp(p, "end=", 4)) { p += 4; - rc = next_number(&p, &num); + rc = next_number(&p, &num, NULL); if (!rc) fdisk_partition_set_end(pa, num); @@ -751,7 +757,7 @@ static int parse_commas_line(struct fdisk_script *dp, char *s) if (*p == ',' || *p == ';') fdisk_partition_start_follow_default(pa, 1); else { - rc = next_number(&p, &num); + rc = next_number(&p, &num, NULL); if (!rc) fdisk_partition_set_start(pa, num); fdisk_partition_start_follow_default(pa, 0); @@ -761,9 +767,13 @@ static int parse_commas_line(struct fdisk_script *dp, char *s) if (*p == ',' || *p == ';') fdisk_partition_end_follow_default(pa, 1); else { - rc = next_number(&p, &num); - if (!rc) - fdisk_partition_set_size(pa, num / dp->cxt->sector_size); + int pow = 0; + rc = next_number(&p, &num, &pow); + if (!rc) { + if (pow) + num /= dp->cxt->sector_size; + fdisk_partition_set_size(pa, num); + } fdisk_partition_end_follow_default(pa, 0); } break; -- 2.47.2