A gigabyte is like a megabyte, except it's bigger and better.
This adds support for g/G suffixes. It also updates and corrects the
comments in the source. Also move the goto label out of the if.
Index: args.c
===================================================================
RCS file: /cvs/src/bin/dd/args.c,v
retrieving revision 1.22
diff -u -p -r1.22 args.c
--- args.c 12 Feb 2014 01:18:36 -0000 1.22
+++ args.c 23 Mar 2014 22:16:58 -0000
@@ -312,13 +312,12 @@ f_conv(char *arg)
/*
* Convert an expression of the following forms to a size_t
- * 1) A positive decimal number.
- * 2) A positive decimal number followed by a b (mult by 512).
- * 3) A positive decimal number followed by a k (mult by 1024).
- * 4) A positive decimal number followed by a m (mult by 1048576).
- * 5) A positive decimal number followed by a w (mult by sizeof int)
- * 6) Two or more positive decimal numbers (with/without k,b or w).
- * separated by x (also * for backwards compatibility), specifying
+ * 1) A positive decimal number, optionally followed by
+ * b - multiply by 512.
+ * k, m or g - multply by 1024 each.
+ * w - multiply by sizeof int
+ * 2) Two or more of the above, separated by x
+ * (or * for backwards compatibility), specifying
* the product of the indicated values.
*/
static size_t
@@ -341,18 +340,24 @@ get_bsz(char *val)
goto erange;
++expr;
break;
- case 'k':
- case 'K':
+ case 'g':
+ case 'G':
t = num;
num *= 1024;
if (t > num)
goto erange;
- ++expr;
- break;
+ /* fallthrough */
case 'm':
case 'M':
t = num;
- num *= 1048576;
+ num *= 1024;
+ if (t > num)
+ goto erange;
+ /* fallthrough */
+ case 'k':
+ case 'K':
+ t = num;
+ num *= 1024;
if (t > num)
goto erange;
++expr;
@@ -374,23 +379,24 @@ get_bsz(char *val)
t = num;
num *= get_bsz(expr + 1);
if (t > num)
-erange: errx(1, "%s: %s", oper,
strerror(ERANGE));
+ goto erange;
break;
default:
errx(1, "%s: illegal numeric value", oper);
}
return (num);
+erange:
+ errx(1, "%s: %s", oper, strerror(ERANGE));
}
/*
* Convert an expression of the following forms to an off_t
- * 1) A positive decimal number.
- * 2) A positive decimal number followed by a b (mult by 512).
- * 3) A positive decimal number followed by a k (mult by 1024).
- * 4) A positive decimal number followed by a m (mult by 1048576).
- * 5) A positive decimal number followed by a w (mult by sizeof int)
- * 6) Two or more positive decimal numbers (with/without k,b or w).
- * separated by x (also * for backwards compatibility), specifying
+ * 1) A positive decimal number, optionally followed by
+ * b - multiply by 512.
+ * k, m or g - multply by 1024 each.
+ * w - multiply by sizeof int
+ * 2) Two or more of the above, separated by x
+ * (or * for backwards compatibility), specifying
* the product of the indicated values.
*/
static off_t
@@ -413,18 +419,24 @@ get_off(char *val)
goto erange;
++expr;
break;
- case 'k':
- case 'K':
+ case 'g':
+ case 'G':
t = num;
num *= 1024;
if (t > num)
goto erange;
- ++expr;
- break;
+ /* fallthrough */
case 'm':
case 'M':
t = num;
- num *= 1048576;
+ num *= 1024;
+ if (t > num)
+ goto erange;
+ /* fallthrough */
+ case 'k':
+ case 'K':
+ t = num;
+ num *= 1024;
if (t > num)
goto erange;
++expr;
@@ -446,10 +458,12 @@ get_off(char *val)
t = num;
num *= get_off(expr + 1);
if (t > num)
-erange: errx(1, "%s: %s", oper,
strerror(ERANGE));
+ goto erange;
break;
default:
errx(1, "%s: illegal numeric value", oper);
}
return (num);
+erange:
+ errx(1, "%s: %s", oper, strerror(ERANGE));
}
Index: dd.1
===================================================================
RCS file: /cvs/src/bin/dd/dd.1,v
retrieving revision 1.29
diff -u -p -r1.29 dd.1
--- dd.1 14 Feb 2014 17:27:58 -0000 1.29
+++ dd.1 23 Mar 2014 22:18:41 -0000
@@ -305,11 +305,13 @@ or
.Sq m
or
.Sq M ,
+.Sq g
+or
+.Sq G ,
or
.Sq w ,
-the number
-is multiplied by 512, 1024 (1K), 1048576 (1M), or the number of bytes
-in an integer, respectively.
+the number is multiplied by 512, 1024 (1K), 1048576 (1M), 1073741824 (1G),
+or the number of bytes in an integer, respectively.
Two or more numbers may be separated by an
.Sq x
to indicate a product.