XUtils.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. #ifndef HEADER_XUtils
  2. #define HEADER_XUtils
  3. /*
  4. htop - StringUtils.h
  5. (C) 2004-2011 Hisham H. Muhammad
  6. (C) 2020-2023 htop dev team
  7. Released under the GNU GPLv2+, see the COPYING file
  8. in the source distribution for its full text.
  9. */
  10. // IWYU pragma: no_include "config.h"
  11. #ifndef PACKAGE
  12. // strchrnul() needs _GNU_SOURCE defined, see PR #1337 for details
  13. #error "Must have #include \"config.h\" line at the top of the file that includes these XUtils helper functions"
  14. #endif
  15. #include <dirent.h>
  16. #include <stdbool.h>
  17. #include <stddef.h> // IWYU pragma: keep
  18. #include <stdio.h>
  19. #include <stdlib.h> // IWYU pragma: keep
  20. #include <string.h> // IWYU pragma: keep
  21. #include "Compat.h"
  22. #include "Macros.h"
  23. ATTR_NORETURN
  24. void fail(void);
  25. ATTR_RETNONNULL ATTR_MALLOC ATTR_ALLOC_SIZE1(1)
  26. void* xMalloc(size_t size);
  27. ATTR_RETNONNULL ATTR_MALLOC ATTR_ALLOC_SIZE2(1, 2)
  28. void* xMallocArray(size_t nmemb, size_t size);
  29. ATTR_RETNONNULL ATTR_MALLOC ATTR_ALLOC_SIZE2(1, 2)
  30. void* xCalloc(size_t nmemb, size_t size);
  31. ATTR_RETNONNULL ATTR_ALLOC_SIZE1(2)
  32. void* xRealloc(void* ptr, size_t size);
  33. ATTR_RETNONNULL ATTR_ALLOC_SIZE2(2, 3)
  34. void* xReallocArray(void* ptr, size_t nmemb, size_t size);
  35. ATTR_RETNONNULL ATTR_ALLOC_SIZE2(3, 4)
  36. void* xReallocArrayZero(void* ptr, size_t prevmemb, size_t newmemb, size_t size);
  37. /*
  38. * String_startsWith gives better performance if strlen(match) can be computed
  39. * at compile time (e.g. when they are immutable string literals). :)
  40. */
  41. ATTR_NONNULL
  42. static inline bool String_startsWith(const char* s, const char* match) {
  43. return strncmp(s, match, strlen(match)) == 0;
  44. }
  45. bool String_contains_i(const char* s1, const char* s2, bool multi);
  46. ATTR_NONNULL
  47. static inline bool String_eq(const char* s1, const char* s2) {
  48. return strcmp(s1, s2) == 0;
  49. }
  50. static inline bool String_eq_nullable(const char* s1, const char* s2) {
  51. if (s1 == s2)
  52. return true;
  53. if (s1 && s2)
  54. return String_eq(s1, s2);
  55. return false;
  56. }
  57. ATTR_NONNULL ATTR_RETNONNULL ATTR_MALLOC
  58. char* String_cat(const char* s1, const char* s2);
  59. ATTR_NONNULL ATTR_RETNONNULL ATTR_MALLOC
  60. char* String_trim(const char* in);
  61. ATTR_NONNULL_N(1) ATTR_RETNONNULL
  62. char** String_split(const char* s, char sep, size_t* n);
  63. void String_freeArray(char** s);
  64. ATTR_NONNULL ATTR_MALLOC
  65. char* String_readLine(FILE* fp);
  66. ATTR_NONNULL ATTR_RETNONNULL
  67. static inline char* String_strchrnul(const char* s, int c) {
  68. #ifdef HAVE_STRCHRNUL
  69. return strchrnul(s, c);
  70. #else
  71. char* result = strchr(s, c);
  72. if (result)
  73. return result;
  74. return strchr(s, '\0');
  75. #endif
  76. }
  77. /* Always null-terminates dest. Caller must pass a strictly positive size. */
  78. ATTR_NONNULL ATTR_ACCESS3_W(1, 3) ATTR_ACCESS3_R(2, 3)
  79. size_t String_safeStrncpy(char* restrict dest, const char* restrict src, size_t size);
  80. ATTR_FORMAT(printf, 2, 3) ATTR_NONNULL_N(1, 2)
  81. int xAsprintf(char** strp, const char* fmt, ...);
  82. ATTR_FORMAT(printf, 3, 4) ATTR_NONNULL_N(1, 3) ATTR_ACCESS3_W(1, 2)
  83. int xSnprintf(char* buf, size_t len, const char* fmt, ...);
  84. ATTR_NONNULL ATTR_RETNONNULL ATTR_MALLOC
  85. char* xStrdup(const char* str);
  86. ATTR_NONNULL
  87. void free_and_xStrdup(char** ptr, const char* str);
  88. ATTR_NONNULL ATTR_RETNONNULL ATTR_MALLOC ATTR_ACCESS3_R(1, 2)
  89. char* xStrndup(const char* str, size_t len);
  90. ATTR_NONNULL ATTR_ACCESS3_W(2, 3)
  91. ssize_t xReadfile(const char* pathname, void* buffer, size_t count);
  92. ATTR_NONNULL ATTR_ACCESS3_W(3, 4)
  93. ssize_t xReadfileat(openat_arg_t dirfd, const char* pathname, void* buffer, size_t count);
  94. ATTR_NONNULL ATTR_ACCESS3_R(2, 3)
  95. ssize_t full_write(int fd, const void* buf, size_t count);
  96. ATTR_NONNULL
  97. static inline ssize_t full_write_str(int fd, const char* str) {
  98. return full_write(fd, str, strlen(str));
  99. }
  100. /* Compares floating point values for ordering data entries. In this function,
  101. NaN is considered "less than" any other floating point value (regardless of
  102. sign), and two NaNs are considered "equal" regardless of payload. */
  103. int compareRealNumbers(double a, double b);
  104. /* Computes the sum of all positive floating point values in an array.
  105. NaN values in the array are skipped. The returned sum will always be
  106. nonnegative. */
  107. ATTR_NONNULL ATTR_ACCESS3_R(1, 2)
  108. double sumPositiveValues(const double* array, size_t count);
  109. /* Returns the number of trailing zero bits */
  110. #if defined(HAVE_BUILTIN_CTZ)
  111. static inline unsigned int countTrailingZeros(unsigned int x) {
  112. return !x ? 32 : __builtin_ctz(x);
  113. }
  114. #else
  115. unsigned int countTrailingZeros(unsigned int x);
  116. #endif
  117. /* IEC unit prefixes */
  118. static const char unitPrefixes[] = { 'K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y', 'R', 'Q' };
  119. static inline bool skipEndOfLine(FILE* fp) {
  120. char buffer[1024];
  121. while (fgets(buffer, sizeof(buffer), fp)) {
  122. if (strchr(buffer, '\n')) {
  123. return true;
  124. }
  125. }
  126. return false;
  127. }
  128. static inline int xDirfd(DIR* dirp) {
  129. int r = dirfd(dirp);
  130. assert(r >= 0);
  131. return r;
  132. }
  133. #endif