/* * $Id: zerofile.c,v 1.5 2013/01/09 04:48:36 grog Exp $ * * Create a file with zero content. Continue until the file system is full or * the optionally specified number of bytes have been written. * * This seemingly useless program makes the file system more easily compressible * for a physical backup. */ #include #include #include #include #include #include #include #include #include #define MANYMANY 131072 #ifndef O_LARGEFILE /* This *horrible* Linux kludge */ #ifdef linux /* ... which Linux even hides */ #define O_LARGEFILE 0100000 #else #define O_LARGEFILE 0 #endif #endif int main (int argc, char *argv []) { char *buf; int myfile; int count; long size; buf = malloc (MANYMANY); memset (buf, 0, MANYMANY); if ((argc > 3) || (argc < 2)) { fprintf (stderr, "Usage:\n\t%s filename [size in MB]\n", argv [0]); exit (1); } if (argc == 3) size = atol (argv [2]) * 8; /* number of I/O transfers */ else size = INT_MAX; myfile = open (argv [1], O_CREAT | O_RDWR | O_LARGEFILE, 0666); if (myfile < 0) { fprintf (stderr, "Can't open %s: %s (%d)\n", argv [1], strerror (errno), errno); exit (1); } while (size--) { count = write (myfile, buf, MANYMANY); if (count < 0) { fprintf (stderr, "Can't write to %s: %s (%d)\n", argv [1], strerror (errno), errno ); exit (1); } else if (count == 0) exit (0); } /* NOTREACHED */ return 0; }