From bbf01054bf943db4394027d77915f9625ebde81e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20G=C3=B6ttsche?= Date: Mon, 26 Oct 2020 21:16:43 +0100 Subject: [PATCH] Add compat wrapper for fstatat --- Compat.c | 46 ++++++++++++++++++++++++++++++++++++ Compat.h | 19 +++++++++++++++ Makefile.am | 2 ++ configure.ac | 2 +- freebsd/FreeBSDProcessList.c | 5 ++-- 5 files changed, 71 insertions(+), 3 deletions(-) create mode 100644 Compat.c create mode 100644 Compat.h 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))