unlinkat.h revision 243750
1158115Sume/*-
2158115Sume * Copyright (c) 2012 The FreeBSD Foundation
3158115Sume * All rights reserved.
4158115Sume *
5158115Sume * This software was developed by Pawel Jakub Dawidek under sponsorship from
6158115Sume * the FreeBSD Foundation.
7158115Sume *
8158115Sume * Redistribution and use in source and binary forms, with or without
9158115Sume * modification, are permitted provided that the following conditions
10158115Sume * are met:
11158115Sume * 1. Redistributions of source code must retain the above copyright
12158115Sume *    notice, this list of conditions and the following disclaimer.
13158115Sume * 2. Redistributions in binary form must reproduce the above copyright
14158115Sume *    notice, this list of conditions and the following disclaimer in the
15158115Sume *    documentation and/or other materials provided with the distribution.
16158115Sume *
17158115Sume * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
18158115Sume * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19158115Sume * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20158115Sume * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
21158115Sume * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22158115Sume * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23158115Sume * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24158115Sume * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25158115Sume * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26158115Sume * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27158115Sume * SUCH DAMAGE.
28158115Sume *
29158115Sume * $P4: //depot/projects/trustedbsd/openbsm/bin/auditdistd/unlinkat.h#1 $
30158115Sume */
31158115Sume
32194093Sdes#ifndef	_UNLINKAT_H_
33158115Sume#define	_UNLINKAT_H_
34158115Sume
35158115Sume#include <fcntl.h>
36158115Sume#include <unistd.h>
37158115Sume
38158115Sume#define	AT_REMOVEDIR	0x01
39194091Sdes
40158115Sumestatic int
41158115Sumeunlinkat(int fd, const char *path, int flag)
42158115Sume{
43158115Sume	int cfd, error, ret;
44158115Sume
45158115Sume	cfd = open(".", O_RDONLY | O_DIRECTORY);
46158115Sume	if (cfd == -1)
47158115Sume		return (-1);
48158115Sume
49158115Sume	if (fchdir(fd) == -1) {
50158115Sume		error = errno;
51158115Sume		(void)close(cfd);
52158115Sume		errno = error;
53158115Sume		return (-1);
54194091Sdes	}
55158115Sume
56158115Sume	if (flag == AT_REMOVEDIR)
57158115Sume		ret = rmdir(path);
58158115Sume	else
59158115Sume		ret = unlink(path);
60158115Sume
61158115Sume	error = errno;
62158115Sume	(void)fchdir(cfd);
63158115Sume	(void)close(cfd);
64158115Sume	errno = error;
65158115Sume	return (ret);
66158115Sume}
67158115Sume
68194091Sdes#endif	/* !_UNLINKAT_H_ */
69158115Sume