From: Ari Sundholm <[email protected]>
bloat-o-meter:
function old new delta
truncate_main - 178 +178
.rodata 154411 154462 +51
packed_usage 30197 30214 +17
applet_names 2483 2492 +9
applet_main 2888 2896 +8
applet_nameofs 722 724 +2
------------------------------------------------------------------------------
(add/remove: 2/0 grow/shrink: 5/0 up/down: 265/0) Total: 265 bytes
v2: Make dd and truncate share a common suffix struct.
v3: Split suffix struct move into separate commit, make applet NOFORK, adjust
error message.
v4: Use bb_error_msg_and_die() instead of bb_error_msg() + xfunc_die().
v5: Add correct bloat-o-meter data.
v6: Use opt_complementary to check the presence of filenames, juggle the flags
to xopen() and shorten an error message.
v7: Do not die on first file on which ftruncate() fails.
Signed-off-by: Ari Sundholm <[email protected]>
---
coreutils/truncate.c | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 72 insertions(+)
create mode 100644 coreutils/truncate.c
diff --git a/coreutils/truncate.c b/coreutils/truncate.c
new file mode 100644
index 0000000..d5b9b6a
--- /dev/null
+++ b/coreutils/truncate.c
@@ -0,0 +1,72 @@
+/*
+ * Mini truncate implementation for busybox
+ *
+ * Copyright (C) 2015 by Ari Sundholm <[email protected]>
+ *
+ * Licensed under GPLv2 or later, see file LICENSE in this source tree.
+ */
+
+//config:config TRUNCATE
+//config: bool "truncate"
+//config: default y
+//config: help
+//config: truncate truncates files to a given size. If a file does
+//config: not exist, it is created unless told otherwise.
+
+//kbuild:lib-$(CONFIG_TRUNCATE) += truncate.o
+//applet:IF_TRUNCATE(APPLET_NOFORK(truncate, truncate, BB_DIR_USR_BIN,
BB_SUID_DROP, truncate))
+
+//usage:#define truncate_trivial_usage
+//usage: "[-c] -s SIZE FILE..."
+//usage:#define truncate_full_usage "\n\n"
+//usage: "Truncate FILEs to the given size.\n"
+//usage: "\n -c Do not create any files."
+//usage: "\n -s SIZE Truncate to SIZE."
+//usage:
+//usage:#define truncate_example_usage
+//usage: "$ truncate -s 1G foo"
+
+#include "libbb.h"
+
+#if ENABLE_LFS
+# define XATOU_SFX xatoull_sfx
+#else
+# define XATOU_SFX xatoul_sfx
+#endif
+
+/* This is a NOFORK applet. Be very careful! */
+
+int truncate_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
+int truncate_main(int argc UNUSED_PARAM, char **argv) {
+ unsigned opts;
+ int flags = O_RDWR;
+ int ret = EXIT_SUCCESS;
+ char *size_str;
+ off_t size;
+
+ enum {
+ OPT_NOCREATE = (1 << 0),
+ OPT_SIZE = (1 << 1),
+ };
+
+ opt_complementary = "s:-1";
+ opts = getopt32(argv, "cs:", &size_str);
+
+ if (!(opts & OPT_NOCREATE))
+ flags |= O_CREAT;
+
+ size = XATOU_SFX(size_str, cwbkMG_suffixes);
+
+ argv += optind;
+ while (*argv) {
+ int fd = xopen(*argv, flags);
+ if (ftruncate(fd, size) == -1) {
+ bb_perror_msg("%s: ftruncate", *argv);
+ ret = EXIT_FAILURE;
+ }
+ xclose(fd);
+ ++argv;
+ }
+
+ return ret;
+}
--
1.9.1
_______________________________________________
busybox mailing list
[email protected]
http://lists.busybox.net/mailman/listinfo/busybox