User:Jonathans/trunc.c

totally useful. /** * trunc * truncate file to size in bytes. if negative, size will be zero. * jon@fuck.org * * usage: trunc file        --  truncate file to 0 bytes. *       trunc file [size]  --  truncate file to size bytes. */


 * 1) include 
 * 2) include 
 * 3) include 
 * 4) include 
 * 5) include 
 * 6) include 

int main (int argc, char **argv) { long int desired_size = 0; struct stat s;

if (argc < 2) {   printf("trunc -- truncate a file to a size in bytes.\n"); printf("usage: trunc file [size]\n"); printf(" file  :  path to a regular file\n"); printf(" size  :  length of file after truncating, default zero.\n"); return 0; }

if (argc >= 3) {   desired_size = strtol(argv[2], NULL, 10); if (desired_size < 0) desired_size = 0; }

if (0 != stat(argv[1], &s)) {   perror("stat"); return 1; }

if (!S_ISREG(s.st_mode)) {   fprintf(stderr, "trunc: %s is not a regular file\n", argv[1]); return 1; }

if (0 != truncate(argv[1], desired_size)) {   perror("truncate"); return 1; }

return 0; }