#ifndef __CCUSTIO_H
#define __CCUSTIO_H

#ifndef DOS
#include        <unistd.h>
#endif
#include    <errno.h>

#ifndef __CCOMMON_H
#include        "ccommon.h"
#endif
#ifndef __GENCONST_H
#include        "genconst.h"
#endif

#define BlockLenIO      512
#define Valid                           0
#define Deleted                 1

typedef char                    *FileName;
typedef char                    *RecType;
typedef long                    RecNoType;
typedef struct  {
  int                                   F;
  FILE                                  *Fd;
  int                                   IOR;
  int                                   LenRec;
  int                                   BaseFil;
  unsigned                      LockMode;
  RecNoType             lpos;
  PathSt                                name;
  /* @(!) 2.3.01.325 */
  RecNoType       lkpos;
  /* @(:) 2.3.01.325 */
} SecDef ;

#ifdef DOS
#define TESTLOCK(x)             (/*test_share() && */ ((x) == EACCES || (x) == EAGAIN))
#define DEADLOCK(x)             (/*test_share() && */ (x) == EDEADLOCK)
#else   
#define TESTLOCK(x)             (((x) == EACCES) || ((x) == EAGAIN) || ((x) == ENOLCK))
#define DEADLOCK(x)             (((x) == EDEADLOCK) || ((x) == EDEADLK))
#endif
#define BLOCKEXT(len,rec)       (((((RecNoType) (len)) * ((RecNoType) (rec))) / BlockLenIO) + 1)

/* @(!) 2.3.01.325 */
#ifdef DOS
#define ASPETTA(x)              (sleep_dos(x))
#else   
#define ASPETTA(x)              (sleep(x))
#endif
/* @(:) 2.3.01.325 */


#ifdef __cplusplus
extern "C" {
#endif
  void CLockRec(SecDef *, RecNoType, unsigned);
  void CVerify(SecDef *,char *);
  void COpen(SecDef *,char *, unsigned, unsigned, unsigned);
  void CCreate(SecDef *,char *,unsigned ,unsigned ,RecNoType);
  void CChsize(SecDef *,char *,unsigned ,unsigned ,RecNoType);
  void CClose(SecDef *);
  void CDelete(SecDef *,char *);
  void CRead(SecDef *,RecType,RecNoType ,unsigned );
  void CWrite(SecDef *,RecType,RecNoType ,unsigned );
  void IDeleteRec(RecType);
  void IRecallRec(RecType);
  BOOLEAN IRecIsDeleted(RecType);
  BOOLEAN ITestLock(int );
  /* @(!) 2.3.01.325 modificata int excllock(char *,BOOLEAN);*/ 
  /* @(!) 2.3.01.325 modificata int exclunlock(char *,BOOLEAN);*/ 
  int excllock(char *,BOOLEAN);
  int exclunlock(char *,BOOLEAN);
  int sleep_dos(int);
  /* @(:) 2.3.01.325 */
  BOOLEAN test_share(void);
#ifdef __cplusplus
};
#endif
#endif /* __CCUSTIO_H */