/* IBM_PROLOG_BEGIN_TAG                                                   */
/* This is an automatically generated prolog.                             */
/*                                                                        */
/* bos720 src/bos/usr/ccs/lib/libmio/samples/example.c 1.3                */
/*                                                                        */
/* Licensed Materials - Property of IBM                                   */
/*                                                                        */
/* Restricted Materials of IBM                                            */
/*                                                                        */
/* COPYRIGHT International Business Machines Corp. 2005,2006              */
/* All Rights Reserved                                                    */
/*                                                                        */
/* US Government Users Restricted Rights - Use, duplication or            */
/* disclosure restricted by GSA ADP Schedule Contract with IBM Corp.      */
/*                                                                        */
/* IBM_PROLOG_END_TAG                                                     */
static char sccsid[] = "@(#)52	1.3	src/bos/usr/ccs/lib/libmio/samples/example.c, libmio, bos720 1/3/06 08:42:22";
#define _LARGE_FILES
#include <fcntl.h>
#include <stdio.h>
#include <errno.h>

#define USE_MIO_DEFINES
#include <libmio.h>

/* Note that we define open64, lseek64, ftruncate64, and not the
 * open, lseek, and ftruncate that are used in the code.  This is
 * because libmio.h defines _LARGE_FILES which forces <fcntl.h> to
 * redefine open, lseek, and ftruncate as open64, lseek64, and 
 * ftruncate64
 */

#define RECSIZE 16384
#define NREC    100

main(int argc, char **argv)
{
int i, fd, status ;
char *name ;
char *buffer ;
int64 ret64 ;

   if( argc < 2 ){
      fprintf(stderr,"Usage : example file_name\n");
      exit(-1);
   }
   name = argv[1] ;

   buffer = (char *)malloc(RECSIZE);
   memset( buffer, 0, RECSIZE ) ;

   fd = open(name, O_RDWR|O_TRUNC|O_CREAT, 0640 ) ;
   if( fd < 0 ){
      fprintf(stderr,"Unable to open file %s errno=%d\n",name,errno);
      exit(-1);
   }

/* write the file */
   for(i=0;i<NREC;i++){
      status = write( fd, buffer, RECSIZE ) ;
   }

/* read the file forwards */
   ret64 = lseek(fd, 0, SEEK_SET ) ;
   for(i=0;i<NREC;i++){
      status = read( fd, buffer, RECSIZE ) ;
   }

/* read the file backwards */
   for(i=0;i<NREC;i++){
      ret64 = lseek(fd, (NREC-i-1)*RECSIZE, SEEK_SET ) ;
      status = read( fd, buffer, RECSIZE ) ;
   }

/* truncate the file back to 0 bytes*/
   status = ftruncate( fd, 0 ) ;

   free(buffer);

/* close the file */
   status = close(fd);
}
