/* cio.c $Revision: 4.1.0 $
/******************************************************************************/
/*                                                                            */
/*                               A I X - I / O                                */
/*                                                                            */
/*  The fast I/O calls the following C-langue primitives:                     */
/*  open, close, read, write, lseek, remove and fsync                         */
/*  This file includes the FORTRAN to C-language interfaces.                  */
/*                                                                            */
/*----------------------------------------------------------------------------*/
/*                                                                            */
/*    written by:                                                             */
/*    M.P. Fuelscher                                                          */
/*    University of Lund, Sweden, 1992                                        */
/*                                                                            */
/*----------------------------------------------------------------------------*/
/*                                                                            */
/*    history: none                                                           */
/*                                                                            */
/******************************************************************************/

#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <sys/stat.h>

/*--- c_open -----------------------------------------------------------------*/

#ifndef RS6K
#ifndef HPUX
#ifdef CRAY
#define c_open C_OPEN
#else
#ifdef LINUX
#ifdef PGF77
#define c_open c_open_
#else
#define c_open c_open__
#endif
#else
#define c_open c_open_
#endif
#endif
#endif
#endif

#ifdef DECAXP
#define INT long int
#elif  IRIX64
#define INT long int
#else
#define INT int
#endif

INT c_open(Path)
 char *Path;

{

 INT rc;
 INT oFlag;
 INT oMode;
 char fn[128];
 oFlag=O_CREAT|O_RDWR; 
#ifdef CRAY
 (void)strcpy(fn,Path);
 rc = open(fn,oFlag,0644);
#else
 oMode=S_IRUSR|S_IRGRP|S_IROTH|S_IWUSR;
 rc = open(Path,oFlag,oMode);
#endif
 if(rc<0) {
   oFlag=O_RDONLY;
#ifdef CRAY
   rc = open(fn,oFlag);
#else
   rc = open(Path,oFlag);
#endif
 }

 return rc;

}

/*--- c_close ----------------------------------------------------------------*/
#ifndef RS6K
#ifndef HPUX
#ifdef CRAY
#define c_close C_CLOSE
#else
#ifdef LINUX
#ifdef PGF77
#define c_close c_close_
#else
#define c_close c_close__
#endif
#else
#define c_close c_close_
#endif
#endif
#endif
#endif

#ifdef DECAXP
#define INT long int
#elif  IRIX64
#define INT long int
#else
#define INT int
#endif

INT c_close(FileDescriptor)
 INT *FileDescriptor;

{
 INT rc;
 rc = close(*FileDescriptor);
 return rc;
}

/*--- c_read -----------------------------------------------------------------*/

#ifndef RS6K
#ifndef HPUX
#ifdef CRAY
#define c_read C_READ
#else
#ifdef LINUX
#ifdef PGF77
#define c_read c_read_
#else
#define c_read c_read__
#endif
#else
#define c_read c_read_
#endif
#endif
#endif
#endif

#ifdef DECAXP
#define INT long int
#elif  IRIX64
#define INT long int
#else
#define INT int
#endif

INT c_read(FileDescriptor,Buffer,nBytes)
 INT *FileDescriptor;
 void *Buffer;
 INT *nBytes;

{
 INT rc;
 rc = (INT)read(*FileDescriptor,Buffer,(size_t)(*nBytes));
 return rc;
}

/*--- c_write ----------------------------------------------------------------*/

#ifndef RS6K
#ifndef HPUX
#ifdef CRAY
#define c_write C_WRITE
#else
#ifdef LINUX
#ifdef PGF77
#define c_write c_write_
#else
#define c_write c_write__
#endif
#else
#define c_write c_write_
#endif
#endif
#endif
#endif

#ifdef DECAXP
#define INT long int
#elif  IRIX64 
#define INT long int
#else
#define INT int
#endif

INT c_write(FileDescriptor,Buffer,nBytes)
 INT *FileDescriptor;
 void *Buffer;
 INT *nBytes;

{
 INT rc;
 rc = (INT)write(*FileDescriptor,Buffer,(size_t)(*nBytes));
 return rc;
}

/*--- c_lseek ----------------------------------------------------------------*/

#ifndef RS6K
#ifndef HPUX
#ifdef CRAY
#define c_lseek C_LSEEK
#else
#ifdef LINUX
#ifdef PGF77
#define c_lseek c_lseek_
#else
#define c_lseek c_lseek__
#endif
#else
#define c_lseek c_lseek_
#endif
#endif
#endif
#endif

#ifdef DECAXP
#define INT long int
#elif  IRIX64
#define INT long int
#else
#define INT int
#endif

INT c_lseek(FileDescriptor,Offset)
 INT *FileDescriptor;
 INT *Offset;

{
 INT rc;
 rc = (INT)lseek(*FileDescriptor,(off_t)(*Offset),SEEK_SET);
 return rc;
}

/*--- c_remove ---------------------------------------------------------------*/

#ifndef RS6K
#ifndef HPUX
#ifdef CRAY
#define c_remove C_REMOVE
#else
#ifdef LINUX
#ifdef PGF77
#define c_remove c_remove_
#else
#define c_remove c_remove__
#endif
#else
#define c_remove c_remove_
#endif
#endif
#endif
#endif

#ifdef DECAXP
#define INT long int
#elif  IRIX64
#define INT long int
#else
#define INT int
#endif

INT c_remove(FileName)
 char *FileName;

{
 INT rc;
 char fn[128];

#ifdef CRAY
 (void)strcpy(fn,FileName);
 rc = remove(fn);
#else
 rc = remove(FileName);
#endif
 return rc;
}

/*--- c_fsync ----------------------------------------------------------------*/

#ifndef RS6K
#ifndef HPUX
#ifdef CRAY
#define c_fsync C_FSYNC
#else
#ifdef LINUX
#ifdef PGF77
#define c_fsync c_fsync_
#else
#define c_fsync c_fsync__
#endif
#else
#define c_fsync c_fsync_
#endif
#endif
#endif
#endif

#ifdef DECAXP
#define INT long int
#elif  IRIX64 
#define INT long int
#else
#define INT int
#endif

INT c_fsync(FileDescriptor)
 INT *FileDescriptor;

{
 INT rc;
 rc = fsync(*FileDescriptor);
 return rc;
}

