view toys/other/truncate.c @ 788:a94ca6ee780f

Silence deeply stupid gcc warning. (First non-declaration line of function: if (file) ffd = open(); at end of of function: if (file) close(ffd); "file" is an argument to the function and nothing else assigns to it. gcc warning on that close, "ffd may be used uninitialized!" _HOW_?)
author Rob Landley <rob@landley.net>
date Mon, 14 Jan 2013 05:25:19 -0600
parents 6cc69be43c42
children e5b52720f539
line wrap: on
line source

/* truncate.c - set file length, extending sparsely if necessary
 *
 * Copyright 2011 Rob Landley <rob@landley.net>

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
*/

#define FOR_truncate
#include "toys.h"

GLOBALS(
  long size;
)

static void do_truncate(int fd, char *name)
{
  if (fd<0) return;
  if (ftruncate(fd, TT.size)) perror_msg("'%s' to '%ld'", name, TT.size);
}

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);
}