bug-coreutils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [PATCH] truncate: Ignore whitespace in --size parameters


From: Jim Meyering
Subject: Re: [PATCH] truncate: Ignore whitespace in --size parameters
Date: Mon, 30 Jun 2008 10:58:21 +0200

Pádraig Brady <address@hidden> wrote:
...
> Well found! That space would mean that
> `truncate -s '> -1' file` would truncate file to 0!
>
> It also means that the more likely command of
> `truncate -s " +1" would also truncate a file to 1,
> rather than extending the size of file by 1 byte.
>
> patch attached.

Thanks for the patch!
I've applied it, along with some adjustments:

[Use "test ...", not "[ ... ]", because I prefer the former ;-)
 But seriously, there's less syntax with the former, and it's good
 to be consistent with the style of the many other shell scripts.
 Personally, I might have acquired the habit by writing autoconf
 macros, where '[' is an m4 quote character.  Or maybe it started
 because '[' was not a built-in on some old shells, while "test" was.

 Use test's =/!= operators, rather than -eq/-ne, unless it's important
 to allow e.g., 09 -eq 9 or '   42   ' -eq 42

 I prefer to use "stat" over a du|cut pipeline. ]

diff --git a/tests/misc/truncate-parameters b/tests/misc/truncate-parameters
index d08c9b2..27a22a9 100755
--- a/tests/misc/truncate-parameters
+++ b/tests/misc/truncate-parameters
@@ -44,6 +44,6 @@ truncate --size="invalid" file && fail=1
 truncate --size="> -1" file && fail=1
 truncate --size=" >1" file || fail=1 #file now 1
 truncate --size=" +1" file || fail=1 #file now 2
-[ $(du -b file | cut -f1) -eq 2 ] || fail=1
+test $(stat --format %s file) = 2 || fail=1

 (exit $fail); exit $fail

Here's the revised change set:
[note the added "* " before each file name, as well as
 blank line after one-line summary]

>From 760bc6f7e73014e934a744a9d46ea8dbf5ba25c8 Mon Sep 17 00:00:00 2001
From: =?utf-8?q?P=C3=A1draig=20Brady?= <address@hidden>
Date: Sun, 29 Jun 2008 01:55:03 +0100
Subject: [PATCH] truncate: ignore whitespace in --size parameters

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                 |    6 ++++++
 tests/misc/truncate-parameters |    6 ++++++
 2 files changed, 12 insertions(+), 0 deletions(-)

diff --git a/src/truncate.c b/src/truncate.c
index fd321c6..3bc52ca 100644
--- a/src/truncate.c
+++ b/src/truncate.c
@@ -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)
diff --git a/tests/misc/truncate-parameters b/tests/misc/truncate-parameters
index e416831..27a22a9 100755
--- a/tests/misc/truncate-parameters
+++ b/tests/misc/truncate-parameters
@@ -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
--
1.5.6.1.104.g2ff9




reply via email to

[Prev in Thread] Current Thread [Next in Thread]