summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLars Wirzenius <liw@liw.fi>2009-02-24 13:52:32 +0200
committerLars Wirzenius <liw@liw.fi>2009-02-24 13:52:32 +0200
commitcb3fac19147b31394920a85fc3b2228a6bcc83dd (patch)
tree7616743fdddc5ee22aedeea25a8370191ed576b5
parent5fef3f2ae3dd27eda5130a3adc9e68b32e828d48 (diff)
parent8d95b516427eb23a89e7fea0da07dfaef22f7839 (diff)
downloadextrautils-cb3fac19147b31394920a85fc3b2228a6bcc83dd.tar.gz
Merged Mikko Rauhala's trunc command.
-rwxr-xr-xtrunc112
1 files changed, 112 insertions, 0 deletions
diff --git a/trunc b/trunc
new file mode 100755
index 0000000..6217614
--- /dev/null
+++ b/trunc
@@ -0,0 +1,112 @@
+#!/bin/sh
+
+# trunc v.1.0rc1 - Truncate a file to a given length
+#
+# Copyright Mikko Rauhala <mjr@iki.fi>, 2009
+#
+# This program is free software. It comes without any warranty, to
+# the extent permitted by applicable law. You can redistribute it
+# and/or modify it under the terms of the Do What The Fuck You Want
+# To Public License, Version 2, as published by Sam Hocevar at
+# http://sam.zoy.org/wtfpl/COPYING and reproduced here:
+#
+# DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
+# Version 2, December 2004
+#
+# Copyright (C) 2004 Sam Hocevar
+# 14 rue de Plaisance, 75014 Paris, France
+# Everyone is permitted to copy and distribute verbatim or modified
+# copies of this license document, and changing it is allowed as long
+# as the name is changed.
+#
+# DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
+# TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
+#
+# 0. You just DO WHAT THE FUCK YOU WANT TO.
+
+# README:
+
+usage()
+{
+cat 1>&2 << EOF
+Usage: trunc [-b blocksize] size file(s)
+
+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
+file was originally larger than the given size, the rest of it
+is lost. If it was smaller, the newly created part of the file
+will be filled with zeroes (sparse if supported by the system).
+If a file doesn't exist, an empty (sparse) file is created with
+the given size and name.
+
+Size is given directly to dd as a count and the optional blocksize
+as bs; refer to your system's dd documentation for possible special
+formatting options.
+
+EOF
+exit 0
+}
+
+# In practice, trunc is a small convenience wrapper for dd, which
+# does the entire actual job. The dd recipe isn't all that complicated
+# either, but hey, this is still more convenient and some of us find
+# ourselves wanting to do this from time to time.
+
+# Technically, we rely on the fact that dd per default (without
+# conv=notrunc) truncates the output file after completing its other
+# tasks. We merely tell dd to seek the output file to the given
+# position and write nothing.
+
+# History:
+
+# 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.)
+LC_ALL=C
+export LC_ALL
+
+RETVAL=0
+
+ohnoes()
+{
+ echo "trunc: $1" 1>&2
+ exit "$2"
+}
+
+if ! which dd > /dev/null
+then
+ ohnoes "dd not found (why, oh why?)" 3
+fi
+
+BS=1
+if [ "a$1" = "a-b" ]; then
+ if [ "a$2" = "a" ]; then
+ usage
+ else
+ BS="$2"
+ shift
+ shift
+ fi
+fi
+
+if [ "a$1" = a -o "a$2" = a ]; then
+ usage
+fi
+
+SIZE="$1"
+shift
+FILE="$1"
+
+while [ "a$FILE" != a ]; 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