]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
sfdisk: introduce get_ull() function
authorPetr Uzel <petr.uzel@suse.cz>
Mon, 19 Sep 2011 13:29:13 +0000 (15:29 +0200)
committerKarel Zak <kzak@redhat.com>
Tue, 27 Sep 2011 13:24:02 +0000 (15:24 +0200)
Signed-off-by: Petr Uzel <petr.uzel@suse.cz>
fdisk/sfdisk.c

index 9c94185bdd9488a2678d046238a60945ae29e147..fe5190835797ed5ea27590f25d737b7b72f19bbd 100644 (file)
@@ -1861,6 +1861,44 @@ get_ul(char *u, unsigned long *up, unsigned long def, int base) {
     return 0;
 }
 
+
+/* read a number, use default if absent */
+/* a sign gives an offset from the default */
+static int
+get_ull(char *u, unsigned long long *up, unsigned long long def, int base) {
+    char *nu;
+    int sign = 0;
+    unsigned long long val;
+
+    if (*u == '+') {
+       sign = 1;
+       u++;
+    } else if (*u == '-') {
+       sign = -1;
+       u++;
+    }
+    if (*u) {
+       errno = 0;
+       val = strtoull(u, &nu, base);
+       if (errno == ERANGE) {
+           do_warn(_("number too big\n"));
+           return -1;
+       }
+       if (*nu) {
+           do_warn(_("trailing junk after number\n"));
+           return -1;
+       }
+       if (sign == 1)
+           val = def + val;
+       else if (sign == -1)
+           val = def - val;
+       *up = val;
+    } else
+       *up = def;
+    return 0;
+}
+
+
 /* There are two common ways to structure extended partitions:
    as nested boxes, and as a chain. Sometimes the partitions
    must be given in order. Sometimes all logical partitions