Listing 1: A sparse file example
*
* cc -o example1 example1.c
* ./example1
*
*/
#include "fcntl.h"
#include "unistd.h"
#include "sys/types.h"
main()
{
int F;
/* One byte at offset zero. */
F = open("file0", O_WRONLY|O_CREAT);
write(F, "x", 1);
close(F);
/* One byte at offset 9,999. */
F = open("file9999", O_WRONLY|O_CREAT);
lseek(F, 9999l, SEEK_SET);
write(F, "x", 1);
close(F);
/* If you are using tmpfs (/tmp comes out of your
* swap space), these results will be different!
*/
F = open("/tmp/file0", O_WRONLY|O_CREAT);
write(F, "x", 1);
close(F);
F = open("/tmp/file9999", O_WRONLY|O_CREAT);
lseek(F, 9999l, SEEK_SET);
write(F, "x", 1);
close(F);
}
|