util.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. #ifndef GIT_COMPAT_UTIL_H
  2. #define GIT_COMPAT_UTIL_H
  3. #define _FILE_OFFSET_BITS 64
  4. #ifndef FLEX_ARRAY
  5. /*
  6. * See if our compiler is known to support flexible array members.
  7. */
  8. #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)
  9. # define FLEX_ARRAY /* empty */
  10. #elif defined(__GNUC__)
  11. # if (__GNUC__ >= 3)
  12. # define FLEX_ARRAY /* empty */
  13. # else
  14. # define FLEX_ARRAY 0 /* older GNU extension */
  15. # endif
  16. #endif
  17. /*
  18. * Otherwise, default to safer but a bit wasteful traditional style
  19. */
  20. #ifndef FLEX_ARRAY
  21. # define FLEX_ARRAY 1
  22. #endif
  23. #endif
  24. #define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))
  25. #ifdef __GNUC__
  26. #define TYPEOF(x) (__typeof__(x))
  27. #else
  28. #define TYPEOF(x)
  29. #endif
  30. #define MSB(x, bits) ((x) & TYPEOF(x)(~0ULL << (sizeof(x) * 8 - (bits))))
  31. #define HAS_MULTI_BITS(i) ((i) & ((i) - 1)) /* checks if an integer has more than 1 bit set */
  32. /* Approximation of the length of the decimal representation of this type. */
  33. #define decimal_length(x) ((int)(sizeof(x) * 2.56 + 0.5) + 1)
  34. #define _ALL_SOURCE 1
  35. #define _BSD_SOURCE 1
  36. #define HAS_BOOL
  37. #include <unistd.h>
  38. #include <stdio.h>
  39. #include <sys/stat.h>
  40. #include <sys/statfs.h>
  41. #include <fcntl.h>
  42. #include <stdbool.h>
  43. #include <stddef.h>
  44. #include <stdlib.h>
  45. #include <stdarg.h>
  46. #include <string.h>
  47. #include <errno.h>
  48. #include <limits.h>
  49. #include <sys/param.h>
  50. #include <sys/types.h>
  51. #include <dirent.h>
  52. #include <sys/time.h>
  53. #include <time.h>
  54. #include <signal.h>
  55. #include <fnmatch.h>
  56. #include <assert.h>
  57. #include <regex.h>
  58. #include <utime.h>
  59. #include <sys/wait.h>
  60. #include <sys/poll.h>
  61. #include <sys/socket.h>
  62. #include <sys/ioctl.h>
  63. #include <inttypes.h>
  64. #include <linux/magic.h>
  65. #include "types.h"
  66. #include <sys/ttydefaults.h>
  67. extern const char *graph_line;
  68. extern const char *graph_dotted_line;
  69. extern char buildid_dir[];
  70. /* On most systems <limits.h> would have given us this, but
  71. * not on some systems (e.g. GNU/Hurd).
  72. */
  73. #ifndef PATH_MAX
  74. #define PATH_MAX 4096
  75. #endif
  76. #ifndef PRIuMAX
  77. #define PRIuMAX "llu"
  78. #endif
  79. #ifndef PRIu32
  80. #define PRIu32 "u"
  81. #endif
  82. #ifndef PRIx32
  83. #define PRIx32 "x"
  84. #endif
  85. #ifndef PATH_SEP
  86. #define PATH_SEP ':'
  87. #endif
  88. #ifndef STRIP_EXTENSION
  89. #define STRIP_EXTENSION ""
  90. #endif
  91. #ifndef has_dos_drive_prefix
  92. #define has_dos_drive_prefix(path) 0
  93. #endif
  94. #ifndef is_dir_sep
  95. #define is_dir_sep(c) ((c) == '/')
  96. #endif
  97. #ifdef __GNUC__
  98. #define NORETURN __attribute__((__noreturn__))
  99. #else
  100. #define NORETURN
  101. #ifndef __attribute__
  102. #define __attribute__(x)
  103. #endif
  104. #endif
  105. /* General helper functions */
  106. extern void usage(const char *err) NORETURN;
  107. extern void die(const char *err, ...) NORETURN __attribute__((format (printf, 1, 2)));
  108. extern int error(const char *err, ...) __attribute__((format (printf, 1, 2)));
  109. extern void warning(const char *err, ...) __attribute__((format (printf, 1, 2)));
  110. #include "../../../include/linux/stringify.h"
  111. #define DIE_IF(cnd) \
  112. do { if (cnd) \
  113. die(" at (" __FILE__ ":" __stringify(__LINE__) "): " \
  114. __stringify(cnd) "\n"); \
  115. } while (0)
  116. extern void set_die_routine(void (*routine)(const char *err, va_list params) NORETURN);
  117. extern int prefixcmp(const char *str, const char *prefix);
  118. extern void set_buildid_dir(void);
  119. extern void disable_buildid_cache(void);
  120. static inline const char *skip_prefix(const char *str, const char *prefix)
  121. {
  122. size_t len = strlen(prefix);
  123. return strncmp(str, prefix, len) ? NULL : str + len;
  124. }
  125. #ifdef __GLIBC_PREREQ
  126. #if __GLIBC_PREREQ(2, 1)
  127. #define HAVE_STRCHRNUL
  128. #endif
  129. #endif
  130. #ifndef HAVE_STRCHRNUL
  131. #define strchrnul gitstrchrnul
  132. static inline char *gitstrchrnul(const char *s, int c)
  133. {
  134. while (*s && *s != c)
  135. s++;
  136. return (char *)s;
  137. }
  138. #endif
  139. /*
  140. * Wrappers:
  141. */
  142. extern char *xstrdup(const char *str);
  143. extern void *xrealloc(void *ptr, size_t size) __attribute__((weak));
  144. static inline void *zalloc(size_t size)
  145. {
  146. return calloc(1, size);
  147. }
  148. static inline int has_extension(const char *filename, const char *ext)
  149. {
  150. size_t len = strlen(filename);
  151. size_t extlen = strlen(ext);
  152. return len > extlen && !memcmp(filename + len - extlen, ext, extlen);
  153. }
  154. /* Sane ctype - no locale, and works with signed chars */
  155. #undef isascii
  156. #undef isspace
  157. #undef isdigit
  158. #undef isxdigit
  159. #undef isalpha
  160. #undef isprint
  161. #undef isalnum
  162. #undef islower
  163. #undef isupper
  164. #undef tolower
  165. #undef toupper
  166. extern unsigned char sane_ctype[256];
  167. #define GIT_SPACE 0x01
  168. #define GIT_DIGIT 0x02
  169. #define GIT_ALPHA 0x04
  170. #define GIT_GLOB_SPECIAL 0x08
  171. #define GIT_REGEX_SPECIAL 0x10
  172. #define GIT_PRINT_EXTRA 0x20
  173. #define GIT_PRINT 0x3E
  174. #define sane_istest(x,mask) ((sane_ctype[(unsigned char)(x)] & (mask)) != 0)
  175. #define isascii(x) (((x) & ~0x7f) == 0)
  176. #define isspace(x) sane_istest(x,GIT_SPACE)
  177. #define isdigit(x) sane_istest(x,GIT_DIGIT)
  178. #define isxdigit(x) \
  179. (sane_istest(toupper(x), GIT_ALPHA | GIT_DIGIT) && toupper(x) < 'G')
  180. #define isalpha(x) sane_istest(x,GIT_ALPHA)
  181. #define isalnum(x) sane_istest(x,GIT_ALPHA | GIT_DIGIT)
  182. #define isprint(x) sane_istest(x,GIT_PRINT)
  183. #define islower(x) (sane_istest(x,GIT_ALPHA) && sane_istest(x,0x20))
  184. #define isupper(x) (sane_istest(x,GIT_ALPHA) && !sane_istest(x,0x20))
  185. #define tolower(x) sane_case((unsigned char)(x), 0x20)
  186. #define toupper(x) sane_case((unsigned char)(x), 0)
  187. static inline int sane_case(int x, int high)
  188. {
  189. if (sane_istest(x, GIT_ALPHA))
  190. x = (x & ~0x20) | high;
  191. return x;
  192. }
  193. int mkdir_p(char *path, mode_t mode);
  194. int copyfile(const char *from, const char *to);
  195. s64 perf_atoll(const char *str);
  196. char **argv_split(const char *str, int *argcp);
  197. void argv_free(char **argv);
  198. bool strglobmatch(const char *str, const char *pat);
  199. bool strlazymatch(const char *str, const char *pat);
  200. int strtailcmp(const char *s1, const char *s2);
  201. unsigned long convert_unit(unsigned long value, char *unit);
  202. int readn(int fd, void *buf, size_t size);
  203. struct perf_event_attr;
  204. void event_attr_init(struct perf_event_attr *attr);
  205. #define _STR(x) #x
  206. #define STR(x) _STR(x)
  207. /*
  208. * Determine whether some value is a power of two, where zero is
  209. * *not* considered a power of two.
  210. */
  211. static inline __attribute__((const))
  212. bool is_power_of_2(unsigned long n)
  213. {
  214. return (n != 0 && ((n & (n - 1)) == 0));
  215. }
  216. size_t hex_width(u64 v);
  217. char *rtrim(char *s);
  218. void dump_stack(void);
  219. #endif