Compat.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #ifndef HEADER_Compat
  2. #define HEADER_Compat
  3. /*
  4. htop - Compat.h
  5. (C) 2020 htop dev team
  6. Released under the GNU GPLv2+, see the COPYING file
  7. in the source distribution for its full text.
  8. */
  9. #include <assert.h> // IWYU pragma: keep
  10. #include <fcntl.h>
  11. #include <stddef.h> // IWYU pragma: keep
  12. #include <unistd.h>
  13. #include <sys/stat.h> // IWYU pragma: keep
  14. int Compat_faccessat(int dirfd,
  15. const char* pathname,
  16. int mode,
  17. int flags);
  18. int Compat_fstatat(int dirfd,
  19. const char* dirpath,
  20. const char* pathname,
  21. struct stat* statbuf,
  22. int flags);
  23. #ifdef HAVE_OPENAT
  24. typedef int openat_arg_t;
  25. static inline void Compat_openatArgClose(openat_arg_t dirfd) {
  26. close(dirfd);
  27. }
  28. static inline int Compat_openat(openat_arg_t dirfd, const char* pathname, int flags) {
  29. return openat(dirfd, pathname, flags);
  30. }
  31. #else /* HAVE_OPENAT */
  32. typedef const char* openat_arg_t;
  33. static inline void Compat_openatArgClose(openat_arg_t dirpath) {
  34. (void)dirpath;
  35. }
  36. int Compat_openat(openat_arg_t dirpath, const char* pathname, int flags);
  37. #endif /* HAVE_OPENAT */
  38. ssize_t Compat_readlinkat(int dirfd,
  39. const char* dirpath,
  40. const char* pathname,
  41. char* buf,
  42. size_t bufsize);
  43. ssize_t Compat_readlink(openat_arg_t dirfd,
  44. const char* pathname,
  45. char* buf,
  46. size_t bufsize);
  47. /*
  48. * static_assert() hack for pre-C11
  49. * TODO: drop after moving to -std=c11 or newer
  50. */
  51. /* C11 guarantees _Static_assert is a keyword */
  52. #if (defined __STDC_VERSION__ ? __STDC_VERSION__ : 0) < 201112L
  53. # if !defined(_Static_assert)
  54. # define _Static_assert(expr, msg) \
  55. extern int (*__Static_assert_function (void)) \
  56. [!!sizeof (struct { int __error_if_negative: (expr) ? 2 : -1; })]
  57. # endif
  58. #endif
  59. /* C23 guarantees static_assert is a keyword or a macro */
  60. #if (defined __STDC_VERSION__ ? __STDC_VERSION__ : 0) < 202311L
  61. # if !defined(static_assert)
  62. # define static_assert(expr, msg) _Static_assert(expr, msg)
  63. # endif
  64. #endif
  65. #endif /* HEADER_Compat */