util.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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 _GNU_SOURCE 1
  36. #define _BSD_SOURCE 1
  37. #define HAS_BOOL
  38. #include <unistd.h>
  39. #include <stdio.h>
  40. #include <sys/stat.h>
  41. #include <sys/statfs.h>
  42. #include <fcntl.h>
  43. #include <stdbool.h>
  44. #include <stddef.h>
  45. #include <stdlib.h>
  46. #include <stdarg.h>
  47. #include <string.h>
  48. #include <errno.h>
  49. #include <limits.h>
  50. #include <sys/param.h>
  51. #include <sys/types.h>
  52. #include <dirent.h>
  53. #include <sys/time.h>
  54. #include <time.h>
  55. #include <signal.h>
  56. #include <fnmatch.h>
  57. #include <assert.h>
  58. #include <regex.h>
  59. #include <utime.h>
  60. #include <sys/wait.h>
  61. #include <sys/poll.h>
  62. #include <sys/socket.h>
  63. #include <sys/ioctl.h>
  64. #ifndef NO_SYS_SELECT_H
  65. #include <sys/select.h>
  66. #endif
  67. #include <netinet/in.h>
  68. #include <netinet/tcp.h>
  69. #include <arpa/inet.h>
  70. #include <netdb.h>
  71. #include <pwd.h>
  72. #include <inttypes.h>
  73. #include "../../../include/linux/magic.h"
  74. #include "types.h"
  75. #include <sys/ttydefaults.h>
  76. #ifndef NO_ICONV
  77. #include <iconv.h>
  78. #endif
  79. extern const char *graph_line;
  80. extern const char *graph_dotted_line;
  81. /* On most systems <limits.h> would have given us this, but
  82. * not on some systems (e.g. GNU/Hurd).
  83. */
  84. #ifndef PATH_MAX
  85. #define PATH_MAX 4096
  86. #endif
  87. #ifndef PRIuMAX
  88. #define PRIuMAX "llu"
  89. #endif
  90. #ifndef PRIu32
  91. #define PRIu32 "u"
  92. #endif
  93. #ifndef PRIx32
  94. #define PRIx32 "x"
  95. #endif
  96. #ifndef PATH_SEP
  97. #define PATH_SEP ':'
  98. #endif
  99. #ifndef STRIP_EXTENSION
  100. #define STRIP_EXTENSION ""
  101. #endif
  102. #ifndef has_dos_drive_prefix
  103. #define has_dos_drive_prefix(path) 0
  104. #endif
  105. #ifndef is_dir_sep
  106. #define is_dir_sep(c) ((c) == '/')
  107. #endif
  108. #ifdef __GNUC__
  109. #define NORETURN __attribute__((__noreturn__))
  110. #else
  111. #define NORETURN
  112. #ifndef __attribute__
  113. #define __attribute__(x)
  114. #endif
  115. #endif
  116. /* General helper functions */
  117. extern void usage(const char *err) NORETURN;
  118. extern void die(const char *err, ...) NORETURN __attribute__((format (printf, 1, 2)));
  119. extern int error(const char *err, ...) __attribute__((format (printf, 1, 2)));
  120. extern void warning(const char *err, ...) __attribute__((format (printf, 1, 2)));
  121. #include "../../../include/linux/stringify.h"
  122. #define DIE_IF(cnd) \
  123. do { if (cnd) \
  124. die(" at (" __FILE__ ":" __stringify(__LINE__) "): " \
  125. __stringify(cnd) "\n"); \
  126. } while (0)
  127. extern void set_die_routine(void (*routine)(const char *err, va_list params) NORETURN);
  128. extern int prefixcmp(const char *str, const char *prefix);
  129. static inline const char *skip_prefix(const char *str, const char *prefix)
  130. {
  131. size_t len = strlen(prefix);
  132. return strncmp(str, prefix, len) ? NULL : str + len;
  133. }
  134. #ifdef __GLIBC_PREREQ
  135. #if __GLIBC_PREREQ(2, 1)
  136. #define HAVE_STRCHRNUL
  137. #endif
  138. #endif
  139. #ifndef HAVE_STRCHRNUL
  140. #define strchrnul gitstrchrnul
  141. static inline char *gitstrchrnul(const char *s, int c)
  142. {
  143. while (*s && *s != c)
  144. s++;
  145. return (char *)s;
  146. }
  147. #endif
  148. /*
  149. * Wrappers:
  150. */
  151. extern char *xstrdup(const char *str);
  152. extern void *xrealloc(void *ptr, size_t size) __attribute__((weak));
  153. static inline void *zalloc(size_t size)
  154. {
  155. return calloc(1, size);
  156. }
  157. static inline int has_extension(const char *filename, const char *ext)
  158. {
  159. size_t len = strlen(filename);
  160. size_t extlen = strlen(ext);
  161. return len > extlen && !memcmp(filename + len - extlen, ext, extlen);
  162. }
  163. /* Sane ctype - no locale, and works with signed chars */
  164. #undef isascii
  165. #undef isspace
  166. #undef isdigit
  167. #undef isxdigit
  168. #undef isalpha
  169. #undef isprint
  170. #undef isalnum
  171. #undef tolower
  172. #undef toupper
  173. extern unsigned char sane_ctype[256];
  174. #define GIT_SPACE 0x01
  175. #define GIT_DIGIT 0x02
  176. #define GIT_ALPHA 0x04
  177. #define GIT_GLOB_SPECIAL 0x08
  178. #define GIT_REGEX_SPECIAL 0x10
  179. #define GIT_PRINT_EXTRA 0x20
  180. #define GIT_PRINT 0x3E
  181. #define sane_istest(x,mask) ((sane_ctype[(unsigned char)(x)] & (mask)) != 0)
  182. #define isascii(x) (((x) & ~0x7f) == 0)
  183. #define isspace(x) sane_istest(x,GIT_SPACE)
  184. #define isdigit(x) sane_istest(x,GIT_DIGIT)
  185. #define isxdigit(x) \
  186. (sane_istest(toupper(x), GIT_ALPHA | GIT_DIGIT) && toupper(x) < 'G')
  187. #define isalpha(x) sane_istest(x,GIT_ALPHA)
  188. #define isalnum(x) sane_istest(x,GIT_ALPHA | GIT_DIGIT)
  189. #define isprint(x) sane_istest(x,GIT_PRINT)
  190. #define tolower(x) sane_case((unsigned char)(x), 0x20)
  191. #define toupper(x) sane_case((unsigned char)(x), 0)
  192. static inline int sane_case(int x, int high)
  193. {
  194. if (sane_istest(x, GIT_ALPHA))
  195. x = (x & ~0x20) | high;
  196. return x;
  197. }
  198. #ifndef DIR_HAS_BSD_GROUP_SEMANTICS
  199. # define FORCE_DIR_SET_GID S_ISGID
  200. #else
  201. # define FORCE_DIR_SET_GID 0
  202. #endif
  203. #ifdef NO_NSEC
  204. #undef USE_NSEC
  205. #define ST_CTIME_NSEC(st) 0
  206. #define ST_MTIME_NSEC(st) 0
  207. #else
  208. #ifdef USE_ST_TIMESPEC
  209. #define ST_CTIME_NSEC(st) ((unsigned int)((st).st_ctimespec.tv_nsec))
  210. #define ST_MTIME_NSEC(st) ((unsigned int)((st).st_mtimespec.tv_nsec))
  211. #else
  212. #define ST_CTIME_NSEC(st) ((unsigned int)((st).st_ctim.tv_nsec))
  213. #define ST_MTIME_NSEC(st) ((unsigned int)((st).st_mtim.tv_nsec))
  214. #endif
  215. #endif
  216. int mkdir_p(char *path, mode_t mode);
  217. int copyfile(const char *from, const char *to);
  218. s64 perf_atoll(const char *str);
  219. char **argv_split(const char *str, int *argcp);
  220. void argv_free(char **argv);
  221. bool strglobmatch(const char *str, const char *pat);
  222. bool strlazymatch(const char *str, const char *pat);
  223. unsigned long convert_unit(unsigned long value, char *unit);
  224. #ifndef ESC
  225. #define ESC 27
  226. #endif
  227. static inline bool is_exit_key(int key)
  228. {
  229. char up;
  230. if (key == CTRL('c') || key == ESC)
  231. return true;
  232. up = toupper(key);
  233. return up == 'Q';
  234. }
  235. #define _STR(x) #x
  236. #define STR(x) _STR(x)
  237. #endif