#ifndef FCNTL_H
#define FCNTL_H

/*
**          $VER: fcntl.h 1.3 (18.09.98)
**             Includes Release 45.00
**
**     Copyright © 1996/99 by CyberdyneSystems
**
**            written by Matthias Henze
**               All Rights Reserved
*/

#ifndef STORMAMIGA_H
  #include <stormamiga.h>
#endif
#ifndef SYS_TYPES_H
  #include <sys/types.h>
#endif

#ifdef __cplusplus
  extern "C" {
#endif

/*
 * File status flags: these are used by open.
 * They are also used (indirectly) in the kernel file structure f_flags,
 * which is a superset of the open/fcntl flags.  Open flags and f_flags
 * are inter-convertible using OFLAGS(fflags) and FFLAGS(oflags).
 * Open/fcntl flags begin with O_; kernel-internal flags begin with F.
 */
/* open-only flags */
#define O_RDONLY        0x0000          /* open for reading only */
#define O_WRONLY        0x0001          /* open for writing only */
#define O_RDWR          0x0002          /* open for reading and writing */
#define O_ACCMODE       0x0003          /* mask for above modes */

#define FREAD           0x0001
#define FWRITE          0x0002
#define O_NONBLOCK      0x0004          /* no delay */
#define O_APPEND        0x0100          /* set append mode */
#ifndef _POSIX_SOURCE
  #define O_SHLOCK        0x0           /* open with shared file lock */
  #define O_EXLOCK        0x0           /* open with exclusive file lock */
  #define O_ASYNC         0x0040        /* signal pgrp when data ready */
  #define O_FSYNC         0x2000        /* synchronous writes */
  #define O_CASE          0x1000        /* open file using a case-sensitive filename */
#endif
#define O_CREAT         0x0200          /* create if nonexistant */
#define O_TRUNC         0x0400          /* truncate to zero length */
#define O_EXCL          0x0800          /* error if already exists */
#define FEXTOPEN        0x0010          /* don't close with DOS Close command */
#define FUNLINK         0x0080          /* unlink file when closed */
#define FMARK           0x0             /* mark during gc() */
#define FDEFER          0x0             /* defer for next gc pass */
#define FHASLOCK        0x0             /* descriptor holds advisory lock */

/* defined by POSIX 1003.1; BSD default, so no bit required */
#define O_NOCTTY        0               /* don't assign controlling terminal */

/*
 * The O_* flags used to have only F* names, which were used in the kernel
 * and by fcntl.  We retain the F* names for the kernel f_flags field
 * and for backward compatibility for fcntl.
 */
#ifndef _POSIX_SOURCE
  #define FAPPEND         O_APPEND        /* kernel/compat */
  #define FASYNC          O_ASYNC         /* kernel/compat */
  #define FFSYNC          O_FSYNC         /* kernel */
  #define FNONBLOCK       O_NONBLOCK      /* kernel */
  #define FNDELAY         O_NONBLOCK      /* compat */
  #define FNBIO           O_NONBLOCK      /* compat */
  #define O_NDELAY        O_NONBLOCK      /* compat */
#endif

#define FOPEN           (-1)
/* convert from open() flags to/from fflags; convert O_RD/WR to FREAD/FWRITE */
#define FFLAGS(oflags)  ((oflags) + 1)
#define OFLAGS(fflags)  ((fflags) - 1)

/* bits to save after open */
#define FMASK           (FREAD|FWRITE|FAPPEND|FASYNC|FFSYNC|FNONBLOCK)
/* bits settable by fcntl(F_SETFL, ...) */
#define FCNTLFLAGS      (FAPPEND|FASYNC|FFSYNC|FNONBLOCK)


/* file descriptor flags (F_GETFD, F_SETFD) */
#define FD_CLOEXEC      1               /* close-on-exec flag */

int     creat (cchar *, mode_t);
int     open  (cchar *, int, ...);

#ifdef __cplusplus
  }
#endif

#ifdef STORMAMIGA_UNIXPATH
  __inline int creat_u (cchar *path, mode_t mode)
  { return creat       (path, mode);}

  __inline int open_u  (cchar *path, int flags)
  { return open        (path, flags); }

  #define creat(path, mode) creat_u(UnixToAmigaPath(file), mode)
  #define open(path, flags) open_u(UnixToAmigaPath(file), flags)
#endif

#endif /* FCNTL_H */
