From 3a9241add947cb6d24b5de7a8927517426a78795 Mon Sep 17 00:00:00 2001 From: Rob Landley Date: Sat, 25 Aug 2012 14:25:22 -0500 Subject: Move commands into "posix", "lsb", and "other" menus/directories. --- toys/other/truncate.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 toys/other/truncate.c (limited to 'toys/other/truncate.c') diff --git a/toys/other/truncate.c b/toys/other/truncate.c new file mode 100644 index 00000000..dccd3215 --- /dev/null +++ b/toys/other/truncate.c @@ -0,0 +1,47 @@ +/* vi: set sw=4 ts=4: + * + * truncate.c - set file length, extending sparsely if necessary + * + * Copyright 2011 Rob Landley + * + * Not in SUSv4 + +USE_TRUNCATE(NEWTOY(truncate, "<1s#|c", TOYFLAG_BIN)) + +config TRUNCATE + bool "truncate" + default y + help + usage: truncate [-c] -s file... + Set length of file(s), extending sparsely if necessary. + + -c Don't create file if it doesn't exist. + -s New size +*/ + +#include "toys.h" + +DEFINE_GLOBALS( + long size; +) + +#define TT this.truncate + +static void do_truncate(int fd, char *name) +{ + if (fd<0) return; + if (ftruncate(fd, TT.size)) { + perror_msg("failed to set '%s' to '%ld'", name, TT.size); + toys.exitval = EXIT_FAILURE; + } +} + +void truncate_main(void) +{ + int cr = !(toys.optflags&1); + + // Create files with mask rwrwrw. + // Nonexistent files are only an error if we're supposed to create them. + loopfiles_rw(toys.optargs, O_WRONLY|(cr ? O_CREAT : 0), 0666, cr, + do_truncate); +} -- cgit v1.2.3