Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sudo-project
GitHub Repository: sudo-project/sudo
Path: blob/main/lib/util/fstatat.c
1532 views
1
/*
2
* SPDX-License-Identifier: ISC
3
*
4
* Copyright (c) 2019-2020 Todd C. Miller <[email protected]>
5
*
6
* Permission to use, copy, modify, and distribute this software for any
7
* purpose with or without fee is hereby granted, provided that the above
8
* copyright notice and this permission notice appear in all copies.
9
*
10
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17
*/
18
19
#include <config.h>
20
21
#include <sys/stat.h>
22
23
#include <fcntl.h>
24
#include <unistd.h>
25
26
#include <sudo_compat.h>
27
28
#ifndef HAVE_FSTATAT
29
int
30
sudo_fstatat(int dfd, const char * restrict path, struct stat * restrict sb, int flag)
31
{
32
int odfd, ret = -1;
33
34
if (dfd == (int)AT_FDCWD) {
35
if (ISSET(flag, AT_SYMLINK_NOFOLLOW))
36
return lstat(path, sb);
37
else
38
return stat(path, sb);
39
}
40
41
/* Save cwd (cannot use O_PATH on older Linux kernels). */
42
if ((odfd = open(".", O_RDONLY|O_DIRECTORY)) == -1)
43
goto done;
44
45
if (fchdir(dfd) == -1)
46
goto done;
47
48
if (ISSET(flag, AT_SYMLINK_NOFOLLOW))
49
ret = lstat(path, sb);
50
else
51
ret = stat(path, sb);
52
53
/* Restore cwd */
54
if (fchdir(odfd) == -1) {
55
/* Should not happen */
56
ret = -1;
57
}
58
59
done:
60
if (odfd != -1)
61
close(odfd);
62
63
return ret;
64
}
65
#endif /* HAVE_FSTATAT */
66
67