From fbb9a05e83f4af8b4aa99c82c6fbad0849afd9a0 Mon Sep 17 00:00:00 2001 From: =?utf-8?q?P=C3=A1draig=20Brady?=
Date: Sun, 29 Jun 2008 01:55:03 +0100 Subject: [PATCH] truncate: Ignore whitespace in --size parameters. Without this `truncate -s '> -1' file` would truncate file to 0, and `truncate -s " +1" would truncate a file to 1 byte. src/truncate.c: For --size parameter, skip leading whitespace and any whitespace after one of the relative modifiers so that the presence of the +/- modifiers can be checked for reliably. tests/misc/truncate-parameters: Add tests for spaces in --size parameter --- 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..d08c9b2 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 +[ $(du -b file | cut -f1) -eq 2 ] || fail=1 + (exit $fail); exit $fail -- 1.5.3.6