/* 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; but breaks
in fseek (EOVERFLOW) on 32-bit Solaris.  The fix is to 
use the fseeko/ftello interfaces (see large_file_stdio_o.c)
*/
#define _FILE_OFFSET_BITS 64  /* enable large file support  */
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.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;
	FILE *f=fopen(fName,"wb");
	if (f==NULL) hitError("Error in fopen of " fName);
	while (nCM<60) {
		int r;
		r=fwrite(&c,sizeof(c),1,f);
		if (r!=1) hitError("Error in fwrite");
		nCM++;
		r=fseek(f,cm,SEEK_CUR);
		if (r!=0) hitError("Error in fseek");
	}
	finish("Large files work fine");
}
/*<@>
<@> ******** Program output: ********
<@> Large files work fine: wrote 6.0 gig file
<@> */
