2020-10-26 20:16:43 +00:00
|
|
|
/*
|
|
|
|
htop - Compat.c
|
2020-12-04 12:42:00 +00:00
|
|
|
(C) 2020 htop dev team
|
2021-09-22 09:33:00 +00:00
|
|
|
Released under the GNU GPLv2+, see the COPYING file
|
2020-10-26 20:16:43 +00:00
|
|
|
in the source distribution for its full text.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h" // IWYU pragma: keep
|
|
|
|
|
2020-11-18 13:26:30 +00:00
|
|
|
#include "Compat.h"
|
|
|
|
|
2020-11-07 21:51:46 +00:00
|
|
|
#include <errno.h>
|
2020-11-18 13:26:30 +00:00
|
|
|
#include <fcntl.h> // IWYU pragma: keep
|
2020-10-26 20:16:43 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/stat.h>
|
2020-11-18 13:26:30 +00:00
|
|
|
#include <sys/types.h> // IWYU pragma: keep
|
2020-10-26 20:16:43 +00:00
|
|
|
|
2020-11-18 13:26:30 +00:00
|
|
|
#include "XUtils.h" // IWYU pragma: keep
|
2020-10-26 20:16:43 +00:00
|
|
|
|
|
|
|
|
2020-11-07 21:51:46 +00:00
|
|
|
int Compat_faccessat(int dirfd,
|
|
|
|
const char* pathname,
|
|
|
|
int mode,
|
|
|
|
int flags) {
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
#ifdef HAVE_FACCESSAT
|
|
|
|
|
|
|
|
// Implementation note: AT_SYMLINK_NOFOLLOW unsupported on FreeBSD, fallback to lstat in that case
|
|
|
|
|
|
|
|
errno = 0;
|
|
|
|
|
|
|
|
ret = faccessat(dirfd, pathname, mode, flags);
|
|
|
|
if (!ret || errno != EINVAL)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Error out on unsupported configurations
|
2021-03-21 20:25:28 +00:00
|
|
|
if (dirfd != (int)AT_FDCWD || mode != F_OK) {
|
2020-11-07 21:51:46 +00:00
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fallback to stat(2)/lstat(2) depending on flags
|
|
|
|
struct stat statinfo;
|
2021-11-04 19:40:39 +00:00
|
|
|
if (flags) {
|
2020-11-07 21:51:46 +00:00
|
|
|
ret = lstat(pathname, &statinfo);
|
|
|
|
} else {
|
|
|
|
ret = stat(pathname, &statinfo);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-10-26 20:16:43 +00:00
|
|
|
int Compat_fstatat(int dirfd,
|
|
|
|
const char* dirpath,
|
|
|
|
const char* pathname,
|
|
|
|
struct stat* statbuf,
|
|
|
|
int flags) {
|
|
|
|
|
|
|
|
#ifdef HAVE_FSTATAT
|
|
|
|
|
|
|
|
(void)dirpath;
|
|
|
|
|
|
|
|
return fstatat(dirfd, pathname, statbuf, flags);
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
(void)dirfd;
|
|
|
|
|
|
|
|
char path[4096];
|
|
|
|
xSnprintf(path, sizeof(path), "%s/%s", dirpath, pathname);
|
|
|
|
|
|
|
|
if (flags & AT_SYMLINK_NOFOLLOW)
|
|
|
|
return lstat(path, statbuf);
|
|
|
|
|
|
|
|
return stat(path, statbuf);
|
|
|
|
|
|
|
|
#endif
|
|
|
|
}
|
2020-10-29 22:02:57 +00:00
|
|
|
|
2020-11-20 16:50:34 +00:00
|
|
|
#ifndef HAVE_OPENAT
|
|
|
|
|
|
|
|
int Compat_openat(const char* dirpath,
|
|
|
|
const char* pathname,
|
|
|
|
int flags) {
|
|
|
|
|
|
|
|
char path[4096];
|
|
|
|
xSnprintf(path, sizeof(path), "%s/%s", dirpath, pathname);
|
|
|
|
|
|
|
|
return open(path, flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* !HAVE_OPENAT */
|
|
|
|
|
2020-11-24 16:40:00 +00:00
|
|
|
ssize_t Compat_readlinkat(int dirfd,
|
|
|
|
const char* dirpath,
|
|
|
|
const char* pathname,
|
|
|
|
char* buf,
|
|
|
|
size_t bufsize) {
|
2020-10-29 22:02:57 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_READLINKAT
|
|
|
|
|
|
|
|
(void)dirpath;
|
|
|
|
|
|
|
|
return readlinkat(dirfd, pathname, buf, bufsize);
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
(void)dirfd;
|
|
|
|
|
|
|
|
char path[4096];
|
|
|
|
xSnprintf(path, sizeof(path), "%s/%s", dirpath, pathname);
|
|
|
|
|
|
|
|
return readlink(path, buf, bufsize);
|
|
|
|
|
|
|
|
#endif
|
|
|
|
}
|