#ifndef AST_MAP_FSAT_H
#define AST_MAP_FSAT_H 1
/***********************************************************************
*                                                                      *
*               This software is part of the ast package               *
*             Copyright (c) 2012 AT&T Intellectual Property            *
*                      and is licensed under the                       *
*                 Eclipse Public License, Version 1.0                  *
*                    by AT&T Intellectual Property                     *
*                                                                      *
*                A copy of the License is available at                 *
*          http://www.eclipse.org/org/documents/epl-v10.html           *
*         (with md5 checksum b35adb5213ca9657e911e9befb180842)         *
*                                                                      *
*              Information and Software Systems Research               *
*                            AT&T Research                             *
*                           Florham Park NJ                            *
*                                                                      *
*               Roland Mainz <roland.mainz@nrubsig.org>                *
*                                                                      *
***********************************************************************/

/*
 * This header defines provide an easy way for legacy applications
 * to use the new POSIX |*at()|-APIs.
 *
 * This is mainly intended for operating systems which already map
 * the old calls to the new |*at()| calls, therefore the macros
 * below bypass a lot of wrapper code in libc.
 * The other usage is _testing_ the code in libc and the |*at())
 * emulation code in libast
 */

#ifdef AT_FDCWD
/* note: |AT_CWD| must always be a negative number < -64 */
#	define AST_AT_FDCWD		(AT_FDCWD)

/* |open()|-flags */
#	define AST_O_DIRECTORY		(O_DIRECTORY)
#if defined(O_SEARCH)
#	define AST_O_SEARCH		(O_SEARCH)
#elif defined(O_PATH)
#	define AST_O_SEARCH		(O_PATH)
#endif

/* extra |*at()| flags */
#	define AST_AT_REMOVEDIR		(AT_REMOVEDIR)
#	define AST_AT_SYMLINK_NOFOLLOW	(AT_SYMLINK_NOFOLLOW)
#	define AST_AT_EACCESS		(AT_EACCESS)

#else /* AT_FDCWD */

/* note: |AT_CWD| must always be a negative number < -64 */
#	define AST_AT_FDCWD		(-129)

/* |open()|-flags */
#ifdef O_DIRECTORY
#	define AST_O_DIRECTORY		(O_DIRECTORY)
#endif
#if defined(O_SEARCH)
#	define AST_O_SEARCH		(O_SEARCH)
#elif defined(O_PATH)
#	define AST_O_SEARCH		(O_PATH)
#endif

/* extra |*at()| flags */
#	define AST_AT_REMOVEDIR		(0x01)
#	define AST_AT_SYMLINK_NOFOLLOW	(0x02)
#	define AST_AT_EACCESS		(0x04)

#endif /* AT_FDCWD */

/*
 * Consumers can define |AST_AT_GET_FDCWD| to define a function
 * to obtain the current cwd fd
 */
#ifndef AST_AT_GET_FDCWD
#define AST_AT_GET_FDCWD AST_AT_FDCWD
#endif

#ifdef AST_MAP_FS_TO_FSAT
/*
 * |open()| and |creat() require C99 __VA_ARGS__
 *
 * gcc supports this since gcc3.0
 *
 * See http://web.archive.org/web/20101030023231/http://blog.mellenthin.de/archives/2010/10/26/portable-__va_args__-macros-for-linux-hp-ux-solaris-and-aix/
 * for possible workarounds for pre-C99 compilers.
 */
/* |open()| argument |flags| is part of |__VA_ARGS__| since |__VA_ARGS__| must never be empty */
#define	open(path, ...)			openat(AST_AT_GET_FDCWD, (path), __VA_ARGS__)
#define	creat(path, ...)		openat(AST_AT_GET_FDCWD, (path), O_WRONLY|O_CREAT|O_TRUNC, __VA_ARGS__)
#define	unlink(path)			unlinkat(AST_AT_GET_FDCWD, (path), 0)
#define	rmdir(path)			unlinkat(AST_AT_GET_FDCWD, (path), AT_REMOVEDIR)
#define	chown(path, uid, gid)		fchownat(AST_AT_GET_FDCWD, (path), (uid), (gid), 0)
#define	lchown(path, uid, gid)		fchownat(AST_AT_GET_FDCWD, (path), (uid), (gid), AT_SYMLINK_NOFOLLOW)
#define	fchown(fd, uid, gid)		fchownat((fd), NULL, (uid), (gid), 0)
#define	stat(path, sb)			fstatat(AST_AT_GET_FDCWD, (path), (sb), 0)
#define	lstat(path, sb)			fstatat(AST_AT_GET_FDCWD, (path), (sb), AT_SYMLINK_NOFOLLOW)
#define	fstat(fd, sb)			fstatat((fd), NULL, (sb), 0)
#define	rename(oldname, newname)	renameat(AST_AT_GET_FDCWD, (oldname), AST_AT_GET_FDCWD, (newname))
#define	access(path, amode)		faccessat(AST_AT_GET_FDCWD, (path), (amode), 0)
#define	eaccess(path, amode)		faccessat(AST_AT_GET_FDCWD, (path), (amode), AT_EACCESS)
#define	mkdir(path, amode)		mkdirat(AST_AT_GET_FDCWD, (path), (amode))
#define	mkfifo(path, amode)		mkfifoat(AST_AT_GET_FDCWD, (path), (amode))
#define	mknod(path, amode, adev)	mknodat(AST_AT_GET_FDCWD, (path), (amode), (adev))
#define	readlink(path, buf, bufsize)	readlinkat(AST_AT_GET_FDCWD, (path), (buf), (bufsize))
#define	symlink(oldpath, newpath)	symlinkat((oldpath), AST_AT_GET_FDCWD, (newpath))

#else
#ifdef AST_MAP_FS_TO_FSAT_EMULATION

/* |open()| argument |flags| is part of |__VA_ARGS__| since |__VA_ARGS__| must never be empty */
#define	open(path, ...)			ast_openat(AST_AT_GET_FDCWD, (path), __VA_ARGS__)
#define	creat(path, ...)		ast_openat(AST_AT_GET_FDCWD, (path), O_WRONLY|O_CREAT|O_TRUNC, __VA_ARGS__)
#define	unlink(path)			ast_unlinkat(AST_AT_GET_FDCWD, (path), 0)
#define	rmdir(path)			ast_unlinkat(AST_AT_GET_FDCWD, (path), AT_REMOVEDIR)
#define	chown(path, uid, gid)		ast_fchownat(AST_AT_GET_FDCWD, (path), (uid), (gid), 0)
#define	lchown(path, uid, gid)		ast_fchownat(AST_AT_GET_FDCWD, (path), (uid), (gid), AT_SYMLINK_NOFOLLOW)
#define	fchown(fd, uid, gid)		ast_fchownat((fd), NULL, (uid), (gid), 0)
#define	stat(path, sb)			ast_fstatat(AST_AT_GET_FDCWD, (path), (sb), 0)
#define	lstat(path, sb)			ast_fstatat(AST_AT_GET_FDCWD, (path), (sb), AT_SYMLINK_NOFOLLOW)
#define	fstat(fd, sb)			ast_fstatat((fd), NULL, (sb), 0)
#define	rename(oldname, newname)	ast_renameat(AST_AT_GET_FDCWD, (oldname), AST_AT_GET_FDCWD, (newname))
#define	access(path, amode)		ast_faccessat(AST_AT_GET_FDCWD, (path), (amode), 0)
#define	eaccess(path, amode)		ast_faccessat(AST_AT_GET_FDCWD, (path), (amode), AT_EACCESS)
#define	mkdir(path, amode)		ast_mkdirat(AST_AT_GET_FDCWD, (path), (amode))
#define	mkfifo(path, amode)		ast_mkfifoat(AST_AT_GET_FDCWD, (path), (amode))
#define	mknod(path, amode, adev)	ast_mknodat(AST_AT_GET_FDCWD, (path), (amode), (adev))
#define	readlink(path, buf, bufsize)	ast_readlinkat(AST_AT_GET_FDCWD, (path), (buf), (bufsize))
#define	symlink(oldpath, newpath)	ast_symlinkat((oldpath), AST_AT_GET_FDCWD, (newpath))

#endif /* AST_MAP_FS_TO_FSAT_EMULATION */
#endif /* AST_MAP_FS_TO_FSAT */

/* prototypes */
extern int ast_openat (int dirfd, const char *pathname, int flags, ...);
extern int ast_close (int fd);
extern int ast_fchdir (int fd);
extern int ast_chdir (const char *path);
extern int ast_unlinkat (int dirfd, const char *pathname, int flags);
extern int ast_fchownat (int dirfd, const char *pathname, uid_t owner, gid_t group, int flags);
extern int ast_fstatat (int dirfd, const char *pathname, struct stat *buf, int flags);
extern int ast_renameat (int olddirfd, const char *oldpath, int newdirfd, const char *newpath);
extern int ast_faccessat (int dirfd, const char *pathname, int mode, int flags);
extern int ast_mkdirat (int dirfd, const char *pathname, mode_t mode);
extern int ast_mkfifoat (int dirfd, const char *pathname, mode_t mode);
extern int ast_mknodat (int dirfd, const char *pathname, mode_t mode, dev_t dev);
extern int ast_utimensat (int dirfd, const char *pathname, const struct timespec *timespec, int flags);
extern int ast_symlinkat(const char *oldpath, int newdirfd, const char *newpath);
extern int ast_readlinkat(int dirfd, const char *pathname, char *buf, size_t bufsiz);

#endif /* !AST_MAP_FSAT_H */
