]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
truncate: ignore whitespace in --size parameters
authorPádraig Brady <P@draigBrady.com>
Sun, 29 Jun 2008 00:55:03 +0000 (01:55 +0100)
committerJim Meyering <meyering@redhat.com>
Mon, 30 Jun 2008 08:23:17 +0000 (10:23 +0200)
Without this, `truncate -s '> -1' F` would truncate F to length 0,
and `truncate -s " +1" F` would truncate F to 1 byte.  Now, each
elicits a diagnostic.
* src/truncate.c: Skip leading white space in the --size option
argument and any white space after one of the relative modifiers,
so that the presence of a +/- modifier can be detected reliably.
* tests/misc/truncate-parameters: Add tests for the above.

src/truncate.c
tests/misc/truncate-parameters

index fd321c6c5bf068e1101bfdcb2c1ed3d46d227252..3bc52ca144ca1f8f0be8171b096780e10aee2e38 100644 (file)
@@ -286,6 +286,9 @@ main (int argc, char **argv)
           break;
 
         case 's':
+          /* skip any whitespace */
+          while (isspace (*optarg))
+            optarg++;
           switch (*optarg)
             {
             case '<':
@@ -305,6 +308,9 @@ main (int argc, char **argv)
               optarg++;
               break;
             }
+          /* skip any whitespace */
+          while (isspace (*optarg))
+            optarg++;
           if (*optarg == '+' || *optarg == '-')
             {
               if (rel_mode)
index e416831905e67c47b9cf64eb72525dc6c1d268d2..27a22a91f4f5a37fef7ecf38ab70feb73379b4c0 100755 (executable)
@@ -40,4 +40,10 @@ truncate --io-blocks --reference=file file && fail=1
 # must specify valid numbers
 truncate --size="invalid" file && fail=1
 
+# spaces not significant around size
+truncate --size="> -1" file && fail=1
+truncate --size=" >1" file || fail=1 #file now 1
+truncate --size=" +1" file || fail=1 #file now 2
+test $(stat --format %s file) = 2 || fail=1
+
 (exit $fail); exit $fail