diff --git a/Compat.c b/Compat.c new file mode 100644 index 00000000..25aa9f73 --- /dev/null +++ b/Compat.c @@ -0,0 +1,46 @@ +/* +htop - Compat.c +(C) 2020 Christian Göttsche +Released under the GNU GPLv2, see the COPYING file +in the source distribution for its full text. +*/ + +#include "config.h" // IWYU pragma: keep + +#include +#include +#include +#include + +#include "Compat.h" +#ifndef HAVE_FSTATAT +#include "XUtils.h" +#endif + + +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 +} diff --git a/Compat.h b/Compat.h new file mode 100644 index 00000000..c9fd0e62 --- /dev/null +++ b/Compat.h @@ -0,0 +1,19 @@ +#ifndef HEADER_Compat +#define HEADER_Compat +/* +htop - Compat.h +(C) 2020 Christian Göttsche +Released under the GNU GPLv2, see the COPYING file +in the source distribution for its full text. +*/ + +#include + + +int Compat_fstatat(int dirfd, + const char* dirpath, + const char* pathname, + struct stat* statbuf, + int flags); + +#endif /* HEADER_Compat */ diff --git a/Makefile.am b/Makefile.am index d5b05d06..e7582e71 100644 --- a/Makefile.am +++ b/Makefile.am @@ -26,6 +26,7 @@ myhtopsources = \ ColorsPanel.c \ ColumnsPanel.c \ CommandScreen.c \ + Compat.c \ CPUMeter.c \ CRT.c \ DateMeter.c \ @@ -79,6 +80,7 @@ myhtopheaders = \ ColorsPanel.h \ ColumnsPanel.h \ CommandScreen.h \ + Compat.h \ DateMeter.h \ DateTimeMeter.h \ DiskIOMeter.h \ diff --git a/configure.ac b/configure.ac index 70289a78..b70a509f 100644 --- a/configure.ac +++ b/configure.ac @@ -88,7 +88,7 @@ AC_TYPE_UID_T # ---------------------------------------------------------------------- AC_FUNC_CLOSEDIR_VOID AC_FUNC_STAT -AC_CHECK_FUNCS([memmove strncasecmp strstr strdup]) +AC_CHECK_FUNCS([fstatat memmove strncasecmp strstr strdup]) save_cflags="${CFLAGS}" CFLAGS="${CFLAGS} -std=c99" diff --git a/freebsd/FreeBSDProcessList.c b/freebsd/FreeBSDProcessList.c index 4c418114..df06546c 100644 --- a/freebsd/FreeBSDProcessList.c +++ b/freebsd/FreeBSDProcessList.c @@ -22,6 +22,7 @@ in the source distribution for its full text. #include #include +#include "Compat.h" #include "CRT.h" #include "FreeBSDProcess.h" #include "Macros.h" @@ -338,7 +339,7 @@ static void FreeBSDProcessList_scanTTYs(ProcessList* pl) { continue; struct stat info; - if (fstatat(dirFd, entry->d_name, &info, 0) < 0) + if (Compat_fstatat(dirFd, "/dev", entry->d_name, &info, 0) < 0) continue; if (!S_ISCHR(info.st_mode)) @@ -365,7 +366,7 @@ err1: const struct dirent* entry; while ((entry = readdir(dirPtr))) { struct stat info; - if (fstatat(dirFd, entry->d_name, &info, 0) < 0) + if (Compat_fstatat(dirFd, "/dev/pts", entry->d_name, &info, 0) < 0) continue; if (!S_ISCHR(info.st_mode))