summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLars Wirzenius <liw@liw.fi>2009-02-24 14:49:36 +0200
committerLars Wirzenius <liw@liw.fi>2009-02-24 14:49:36 +0200
commitfca0f1229335f599176866f64b03a2df442c773c (patch)
treefe5bc36d606357d8be7663f0747e1e71cb50bc6b
parentcb3fac19147b31394920a85fc3b2228a6bcc83dd (diff)
downloadextrautils-fca0f1229335f599176866f64b03a2df442c773c.tar.gz
Updated trunc version from Mikko Rauhala.
-rw-r--r--[-rwxr-xr-x]trunc36
1 files changed, 25 insertions, 11 deletions
diff --git a/trunc b/trunc
index 6217614..51b62c4 100755..100644
--- a/trunc
+++ b/trunc
@@ -1,6 +1,6 @@
#!/bin/sh
-# trunc v.1.0rc1 - Truncate a file to a given length
+# trunc v.1.0 - Truncate a file to a given length
#
# Copyright Mikko Rauhala <mjr@iki.fi>, 2009
#
@@ -29,7 +29,8 @@
usage()
{
cat 1>&2 << EOF
-Usage: trunc [-b blocksize] size file(s)
+Usage: trunc [-b blocksize] size file(s)
+ trunc [--help|-h|--version|-v]
Files will be truncated to the given size. Size is given in bytes
by default, but another block size may also be spesified. If a
@@ -57,31 +58,47 @@ exit 0
# tasks. We merely tell dd to seek the output file to the given
# position and write nothing.
+# There are some idiosyncracies in this script; most of them can be
+# explained by me having started scripting first in the mid-90s and
+# not bothering to properly find out which of the nicer constructs
+# are bashims (which I want to avoid). Yeah, I'm a lazy bastard.
+
# History:
+# 1.0 Initial release. Slightly cleaned up from rc1 though
+# no actual bugs were found. While cleaning, added also
+# -v and -h.
# 1.0rc1 Initial release candidate. Will be named 1.0 later if
# no bugs are found.
# Make sure locale settings don't interfere. Might need to rethink and
# isolate this setting to smaller parts of the script if this ever gets
-# localized, but for now, it's okay. (Probably unnecessary here anyway.)
+# localized, but for now, it's okay. (Probably unnecessary in this
+# script anyway, I don't _think_ we're doing anything locale-spesific,
+# but I've taken this habit to be sure. Doesn't hurt.)
LC_ALL=C
export LC_ALL
-RETVAL=0
-
ohnoes()
{
echo "trunc: $1" 1>&2
exit "$2"
}
-if ! which dd > /dev/null
-then
+if ! which dd > /dev/null; then
ohnoes "dd not found (why, oh why?)" 3
fi
+if [ "a$1" = "a-h" -o "a$1" = "a--help" ]; then
+ usage
+fi
+
+if [ "a$1" = "a-v" -o "a$1" = "a--version" ]; then
+ echo "trunc 1.0 by Mikko Rauhala <mjr@iki.fi>" 1>&2
+ exit 0
+fi
+
BS=1
if [ "a$1" = "a-b" ]; then
if [ "a$2" = "a" ]; then
@@ -99,14 +116,11 @@ fi
SIZE="$1"
shift
-FILE="$1"
-while [ "a$FILE" != a ]; do
+for FILE in "$@"; do
if ! ERROR="`dd if=/dev/null of="$FILE" bs="$BS" count=0 seek="$SIZE" 2>&1`"; then
ohnoes "$ERROR" 1
fi
- shift
- FILE="$1"
done
exit 0