/*
 * $Header: /smsa/bsd:include/fcntl.h:bsd  1.7  $
 * $Source: /smsa/bsd:include/fcntl.h: $
 *
 * Copyright (c) 1988,1990 Acorn Computers Ltd., Cambridge, England
 * Copyright (c) 1983 Regents of the University of California.
 * All rights reserved.  The Berkeley software License Agreement
 * specifies the terms and conditions for redistribution.
 *
 */
#ifndef __fcntl_h
#define __fcntl_h

#ifdef	_BSD_C
#ifndef __sys_types_h
#include "sys/types.h"
#endif
#endif

/* fcntl(2) requests */
#define	F_DUPFD		0	/* Duplicate fildes */
#define	F_GETFD		1	/* Get fildes flags */
#define	F_SETFD		2	/* Set fildes flags */
#define	F_GETFL		3	/* Get file flags */
#define	F_SETFL		4	/* Set file flags */
#define	F_GETOWN	5	/* Get owner (BSD) */
#define F_SETOWN	6	/* Set owner (BSD) */
#define F_GETLK		7	/* Get record-locking information */
#define F_SETLK		8	/* Set or Clear a record-lock (Non-Blocking) */
#define F_SETLKW	9	/* Set or Clear a record-lock (Blocking) */

/* fcntl() flag for F_[GS]ETFD */
#define	FD_CLOEXEC	1	/* value for close-on-exec flag  */

/* fcntl() flags (l_type field of flock structure) */
#define F_RDLCK		1       /* read lock */
#define F_WRLCK		2       /* write lock */
#define F_UNLCK		3       /* remove lock(s) */

/*
 * The following underlying bit-positions form a complete set of file
 * status flags.  Each falls into exactly one of four categories:
 * (1) initialised by open(), fixed until close()
 * (2) initialised by open(), modifiable by fcntl()
 * (3) used only internally in the kernel
 * (4) optionally set for open(), and pertaining only to the open operation
 */
#define _F_READ		0		/* (1) */
#define _F_WRITE	1		/* (1) */
#define _F_NDELAY	2		/* (2) (obsolete) */
#define _F_APPEND	3		/* (2) */
#define _F_MARK		4		/* (3) */
#define _F_DEFER	5		/* (3) */
#define _F_ASYNC	6		/* (2) */
#define _F_SHLOCK	7		/* (3) */
#define _F_EXLOCK	8		/* (3) */
#define _F_CREAT	9		/* (4) */
#define _F_TRUNC	10		/* (4) */
#define _F_EXCL		11		/* (4) */
#define _F_NOCTTY	12		/* (4) */
#define _F_SYNC		13		/* (2) */
#define _F_NONBLOCK	14		/* (2) */

#define  _F__RW_OFFSET	1		/* kernel internal value */


/* Flags applicable to open */
#define	O_CREAT		(1 << _F_CREAT)
#define O_EXCL		(1 << _F_EXCL)
#define O_NOCTTY	(1 << _F_NOCTTY)
#define O_TRUNC		(1 << _F_TRUNC)
/* Flags settable on open and fcntl */
#define O_APPEND	(1 << _F_APPEND)
#ifndef	_POSIX_SOURCE
#define O_NDELAY	(1 << _F_NDELAY)	/* obsolete */
#endif
#define O_NONBLOCK	(1 << _F_NONBLOCK)	/* new */
#ifndef	_POSIX_SOURCE
#define O_SYNC		(1 << _F_SYNC)
#endif
#if !defined(_XOPEN_SOURCE) && !defined(_POSIX_SOURCE)
#define O_ASYNC		(1 << _F_ASYNC)		/* non-X/Open */
#endif

/*
 * File access modes for open() calls. The following 3 are funny because
 * for historical reasons, open flags are known: 0 = read, 1 = write,
 * 2 = read/write.
 */
#define O_ACCMODE	(1 << _F_READ | 1 << _F_WRITE)
#define	O_RDONLY	((1 << _F_READ)  - _F__RW_OFFSET)
#define	O_WRONLY	((1 << _F_WRITE) - _F__RW_OFFSET)
#define	O_RDWR		((1 << _F_READ | 1 << _F_WRITE) - _F__RW_OFFSET)


#ifdef	_BSD_C
/* Kernel names for flags; BSD toolkit code also uses some of these. */
#define FNDELAY		(1 << _F_NDELAY)
#define FAPPEND		(1 << _F_APPEND)
#define FASYNC		(1 << _F_ASYNC)
#define FCREAT		(1 << _F_CREAT)
#define FTRUNC		(1 << _F_TRUNC)
#define FEXCL		(1 << _F_EXCL)
#define FNOCTTY		(1 << _F_NOCTTY)
#define FSYNC		(1 << _F_SYNC)
#define FNONBLOCK	(1 << _F_NONBLOCK)

/* access(2) requests: these live in <unistd.h> for POSIX,SVID,X/Open */
#define	F_OK		0	/* does file exist */
#define	X_OK		1	/* is it executable by caller */
#define	W_OK		2	/* writable by caller */
#define	R_OK		4	/* readable by caller */

/* System-V record-locking options */
/* lockf(2) requests. These live in <unistd.h> for SVID */
#define F_ULOCK 	0       /* Unlock a previously locked region */
#define F_LOCK  	1       /* Lock a region for exclusive use */ 
#define F_TLOCK 	2       /* Test and lock a region for exclusive use */
#define F_TEST  	3       /* Test a region for other processes locks */

#endif/*_BSD_C*/


/* file segment locking set data type - information passed to system by user */
struct flock {
        short   l_type;		/* F_RDLCK, F_WRLCK, or F_UNLCK */
        short   l_whence;	/* flag to choose starting offset */
        off_t   l_start;	/* relative offset, in bytes */
        off_t   l_len;          /* length, in bytes; 0 means lock to EOF */
        pid_t   l_pid;		/* returned with F_GETLK */
        short   l_xxx;		/* reserved for future use */
};

#ifndef	_KERNEL
extern int creat (const char * /*path*/, int /*mode_t mode*/);
extern int fcntl (int /*fildes*/, int /*cmd*/, ...);
extern int open (const char * /*path*/, int /*oflag*/, ...);
#endif

#endif/*__fcntl_h*/

/* EOF fcntl.h */
