faccessat.h revision 243750
1168404Spjd/*-
2168404Spjd * Copyright (c) 2012 The FreeBSD Foundation
3168404Spjd * All rights reserved.
4168404Spjd *
5168404Spjd * This software was developed by Pawel Jakub Dawidek under sponsorship from
6168404Spjd * the FreeBSD Foundation.
7168404Spjd *
8168404Spjd * Redistribution and use in source and binary forms, with or without
9168404Spjd * modification, are permitted provided that the following conditions
10168404Spjd * are met:
11168404Spjd * 1. Redistributions of source code must retain the above copyright
12168404Spjd *    notice, this list of conditions and the following disclaimer.
13168404Spjd * 2. Redistributions in binary form must reproduce the above copyright
14168404Spjd *    notice, this list of conditions and the following disclaimer in the
15168404Spjd *    documentation and/or other materials provided with the distribution.
16168404Spjd *
17168404Spjd * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
18168404Spjd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19168404Spjd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20168404Spjd * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
21168404Spjd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22168404Spjd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23168404Spjd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24168404Spjd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25168404Spjd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26168404Spjd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27168404Spjd * SUCH DAMAGE.
28168404Spjd *
29168404Spjd * $P4: //depot/projects/trustedbsd/openbsm/bin/auditdistd/faccessat.h#1 $
30169303Spjd */
31168404Spjd
32168404Spjd#ifndef	_FACCESSAT_H_
33168404Spjd#define	_FACCESSAT_H_
34168404Spjd
35168404Spjd#include <unistd.h>
36169303Spjd
37168404Spjd#define	AT_EACCESS	0x01
38168404Spjd
39168404Spjdstatic int
40168404Spjdfaccessat(int fd, const char *path, int mode, int flag)
41168404Spjd{
42168404Spjd	int cfd, error, ret;
43168404Spjd
44168404Spjd	if (flag == AT_EACCESS) {
45168404Spjd		errno = EINVAL;
46168404Spjd		return (-1);
47168404Spjd	}
48168404Spjd
49168404Spjd	cfd = open(".", O_RDONLY | O_DIRECTORY);
50168404Spjd	if (cfd == -1)
51168404Spjd		return (-1);
52168404Spjd
53168404Spjd	if (fchdir(fd) == -1) {
54168404Spjd		error = errno;
55168404Spjd		(void)close(cfd);
56168404Spjd		errno = error;
57168404Spjd		return (-1);
58168404Spjd	}
59168404Spjd
60168404Spjd	ret = access(path, mode);
61168404Spjd
62168404Spjd	error = errno;
63168404Spjd	(void)fchdir(cfd);
64168404Spjd	(void)close(cfd);
65168404Spjd	errno = error;
66168404Spjd	return (ret);
67168404Spjd}
68168404Spjd
69168404Spjd#endif	/* !_FACCESSAT_H_ */
70168404Spjd