]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
libfdisk: cleanup script size= code
authorKarel Zak <kzak@redhat.com>
Tue, 9 Sep 2014 11:36:16 +0000 (13:36 +0200)
committerKarel Zak <kzak@redhat.com>
Tue, 7 Oct 2014 12:55:31 +0000 (14:55 +0200)
Signed-off-by: Karel Zak <kzak@redhat.com>
disk-utils/sfdisk.c
libfdisk/src/script.c

index 5381d43dc2f57f9a39fa56d5222daee7e7f0725c..d9608d770fad6841775debae387ce91a73b04778 100644 (file)
@@ -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"
-                "<start> <size> <type [uuid, hex or E,S,L,X]> <bootable [-,*]>\n"
-                "Usually you only need to specify <start> and <size>\n\n"));
+       fputc('\n', stdout);
+       printf(_("Input in the following format; absent fields get a default value.\n"
+                "<start> <size> <type [uuid, hex or E,S,L,X]> <bootable [-,*]>\n"));
 
+       fputc('\n', stdout);
+       printf(_("If the size is specified by <number>{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: <name>' 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);
 
 
index e0e2d5387c0a1cbd30b3591bc0c133ddf36b6e9d..ebfaf61a53f78f447efdba9804b6848afbad7f62 100644 (file)
@@ -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;