view toys/other/truncate.c @ 674:7e846e281e38

New build infrastructure to generate FLAG_ macros and TT alias, #define FOR_commandname before #including toys.h to trigger it. Rename DEFINE_GLOBALS() to just GLOBALS() (because I could never remember if it was DECLARE_GLOBALS). Convert existing commands to use new infrastructure, and replace optflag constants with FLAG_ macros where appropriate.
author Rob Landley <>
date Mon, 08 Oct 2012 00:02:30 -0500
parents 6df4ccc0acbe
children 786841fdb1e0
line wrap: on
line source

/* vi: set sw=4 ts=4:
 * truncate.c - set file length, extending sparsely if necessary
 * Copyright 2011 Rob Landley <>


	bool "truncate"
	default y
	  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

#define FOR_truncate
#include "toys.h"

	long size;

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,