/* Orion Lawlor's Short UNIX Examples, olawlor@acm.org 2004/4/19

Tests if fopen/fwrite/fseek can build a large (>2gb) file.
This seems to work properly under 32-bit Linux and Solaris.

Large files seem to be broken if:
  - You leave off the _FILE_OFFSET_BITS define below AND
     you are on a 32-bit machine.
  - Your filesystem doesn't support it (e.g., NFS v2)
*/
#define _FILE_OFFSET_BITS 64  /* enable large file support  */
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>

#ifndef fName
#define fName "large_file_test.out"
#endif

int nCM=0; /* count of 100's of megs we've written */
void finish(const char *where) {
	printf("%s: wrote %.1f gig file\n",where,0.1*nCM);
	unlink(fName);
	exit(0);
}
void hitError(const char *where) { perror(where); finish(where); }

int main() {
	char c=0; /* The byte we're writing */
	int cm=100*1024*1024;
	int f=open(fName,O_CREAT|O_TRUNC|O_RDWR,00700);
	if (f<0) hitError("Error in open of "fName);
	while (nCM<60) {
		int r;
		r=write(f,&c,sizeof(c));
		if (r!=sizeof(c)) hitError("Error in write");
		nCM++;
		r=lseek(f,cm,SEEK_CUR);
		if (r==-1) hitError("Error in lseek");
	}
	finish("Large files work fine");
}
/*<@>
<@> ******** Program output: ********
<@> Large files work fine: wrote 6.0 gig file
<@> */
