xref: /minix3/external/bsd/tmux/dist/compat/openat.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /* Id */
2*0a6a1f1dSLionel Sambuc 
3*0a6a1f1dSLionel Sambuc /*
4*0a6a1f1dSLionel Sambuc  * Copyright (c) 2013 Nicholas Marriott <nicm@users.sourceforge.net>
5*0a6a1f1dSLionel Sambuc  *
6*0a6a1f1dSLionel Sambuc  * Permission to use, copy, modify, and distribute this software for any
7*0a6a1f1dSLionel Sambuc  * purpose with or without fee is hereby granted, provided that the above
8*0a6a1f1dSLionel Sambuc  * copyright notice and this permission notice appear in all copies.
9*0a6a1f1dSLionel Sambuc  *
10*0a6a1f1dSLionel Sambuc  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11*0a6a1f1dSLionel Sambuc  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12*0a6a1f1dSLionel Sambuc  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13*0a6a1f1dSLionel Sambuc  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14*0a6a1f1dSLionel Sambuc  * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
15*0a6a1f1dSLionel Sambuc  * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
16*0a6a1f1dSLionel Sambuc  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*0a6a1f1dSLionel Sambuc  */
18*0a6a1f1dSLionel Sambuc 
19*0a6a1f1dSLionel Sambuc #include <errno.h>
20*0a6a1f1dSLionel Sambuc #include <fcntl.h>
21*0a6a1f1dSLionel Sambuc #include <stdarg.h>
22*0a6a1f1dSLionel Sambuc #include <unistd.h>
23*0a6a1f1dSLionel Sambuc 
24*0a6a1f1dSLionel Sambuc #include "tmux.h"
25*0a6a1f1dSLionel Sambuc 
26*0a6a1f1dSLionel Sambuc int
openat(int fd,const char * path,int flags,...)27*0a6a1f1dSLionel Sambuc openat(int fd, const char *path, int flags, ...)
28*0a6a1f1dSLionel Sambuc {
29*0a6a1f1dSLionel Sambuc 	mode_t	mode;
30*0a6a1f1dSLionel Sambuc 	va_list ap;
31*0a6a1f1dSLionel Sambuc 	int	dotfd, retval, saved_errno;
32*0a6a1f1dSLionel Sambuc 
33*0a6a1f1dSLionel Sambuc 	if (flags & O_CREAT) {
34*0a6a1f1dSLionel Sambuc 		va_start(ap, flags);
35*0a6a1f1dSLionel Sambuc 		mode = va_arg(ap, mode_t);
36*0a6a1f1dSLionel Sambuc 		va_end(ap);
37*0a6a1f1dSLionel Sambuc 	} else
38*0a6a1f1dSLionel Sambuc 		mode = 0;
39*0a6a1f1dSLionel Sambuc 
40*0a6a1f1dSLionel Sambuc 	dotfd = -1;
41*0a6a1f1dSLionel Sambuc 	if (fd != AT_FDCWD) {
42*0a6a1f1dSLionel Sambuc 		dotfd = open(".", O_RDONLY);
43*0a6a1f1dSLionel Sambuc 		if (dotfd == -1)
44*0a6a1f1dSLionel Sambuc 			return (-1);
45*0a6a1f1dSLionel Sambuc 		if (fchdir(fd) != 0)
46*0a6a1f1dSLionel Sambuc 			return (-1);
47*0a6a1f1dSLionel Sambuc 	}
48*0a6a1f1dSLionel Sambuc 
49*0a6a1f1dSLionel Sambuc 	retval = open(path, flags, mode);
50*0a6a1f1dSLionel Sambuc 
51*0a6a1f1dSLionel Sambuc 	if (dotfd != -1) {
52*0a6a1f1dSLionel Sambuc 		if (fchdir(dotfd) != 0) {
53*0a6a1f1dSLionel Sambuc 			saved_errno = errno;
54*0a6a1f1dSLionel Sambuc 			close(retval);
55*0a6a1f1dSLionel Sambuc 			close(dotfd);
56*0a6a1f1dSLionel Sambuc 			errno = saved_errno;
57*0a6a1f1dSLionel Sambuc 			return (-1);
58*0a6a1f1dSLionel Sambuc 		}
59*0a6a1f1dSLionel Sambuc 		close(dotfd);
60*0a6a1f1dSLionel Sambuc 	}
61*0a6a1f1dSLionel Sambuc 
62*0a6a1f1dSLionel Sambuc 	return (retval);
63*0a6a1f1dSLionel Sambuc }
64