util.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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. #if !defined(__APPLE__) && !defined(__FreeBSD__) && !defined(__USLC__) && !defined(_M_UNIX)
  35. #define _XOPEN_SOURCE 600 /* glibc2 and AIX 5.3L need 500, OpenBSD needs 600 for S_ISLNK() */
  36. #define _XOPEN_SOURCE_EXTENDED 1 /* AIX 5.3L needs this */
  37. #endif
  38. #define _ALL_SOURCE 1
  39. #define _GNU_SOURCE 1
  40. #define _BSD_SOURCE 1
  41. #include <unistd.h>
  42. #include <stdio.h>
  43. #include <sys/stat.h>
  44. #include <fcntl.h>
  45. #include <stddef.h>
  46. #include <stdlib.h>
  47. #include <stdarg.h>
  48. #include <string.h>
  49. #include <errno.h>
  50. #include <limits.h>
  51. #include <sys/param.h>
  52. #include <sys/types.h>
  53. #include <dirent.h>
  54. #include <sys/time.h>
  55. #include <time.h>
  56. #include <signal.h>
  57. #include <fnmatch.h>
  58. #include <assert.h>
  59. #include <regex.h>
  60. #include <utime.h>
  61. #ifndef __MINGW32__
  62. #include <sys/wait.h>
  63. #include <sys/poll.h>
  64. #include <sys/socket.h>
  65. #include <sys/ioctl.h>
  66. #ifndef NO_SYS_SELECT_H
  67. #include <sys/select.h>
  68. #endif
  69. #include <netinet/in.h>
  70. #include <netinet/tcp.h>
  71. #include <arpa/inet.h>
  72. #include <netdb.h>
  73. #include <pwd.h>
  74. #include <inttypes.h>
  75. #if defined(__CYGWIN__)
  76. #undef _XOPEN_SOURCE
  77. #include <grp.h>
  78. #define _XOPEN_SOURCE 600
  79. #include "compat/cygwin.h"
  80. #else
  81. #undef _ALL_SOURCE /* AIX 5.3L defines a struct list with _ALL_SOURCE. */
  82. #include <grp.h>
  83. #define _ALL_SOURCE 1
  84. #endif
  85. #else /* __MINGW32__ */
  86. /* pull in Windows compatibility stuff */
  87. #include "compat/mingw.h"
  88. #endif /* __MINGW32__ */
  89. #ifndef NO_ICONV
  90. #include <iconv.h>
  91. #endif
  92. /* On most systems <limits.h> would have given us this, but
  93. * not on some systems (e.g. GNU/Hurd).
  94. */
  95. #ifndef PATH_MAX
  96. #define PATH_MAX 4096
  97. #endif
  98. #ifndef PRIuMAX
  99. #define PRIuMAX "llu"
  100. #endif
  101. #ifndef PRIu32
  102. #define PRIu32 "u"
  103. #endif
  104. #ifndef PRIx32
  105. #define PRIx32 "x"
  106. #endif
  107. #ifndef PATH_SEP
  108. #define PATH_SEP ':'
  109. #endif
  110. #ifndef STRIP_EXTENSION
  111. #define STRIP_EXTENSION ""
  112. #endif
  113. #ifndef has_dos_drive_prefix
  114. #define has_dos_drive_prefix(path) 0
  115. #endif
  116. #ifndef is_dir_sep
  117. #define is_dir_sep(c) ((c) == '/')
  118. #endif
  119. #ifdef __GNUC__
  120. #define NORETURN __attribute__((__noreturn__))
  121. #else
  122. #define NORETURN
  123. #ifndef __attribute__
  124. #define __attribute__(x)
  125. #endif
  126. #endif
  127. /* General helper functions */
  128. extern void usage(const char *err) NORETURN;
  129. extern void die(const char *err, ...) NORETURN __attribute__((format (printf, 1, 2)));
  130. extern int error(const char *err, ...) __attribute__((format (printf, 1, 2)));
  131. extern void warning(const char *err, ...) __attribute__((format (printf, 1, 2)));
  132. extern void set_die_routine(void (*routine)(const char *err, va_list params) NORETURN);
  133. extern int prefixcmp(const char *str, const char *prefix);
  134. extern time_t tm_to_time_t(const struct tm *tm);
  135. static inline const char *skip_prefix(const char *str, const char *prefix)
  136. {
  137. size_t len = strlen(prefix);
  138. return strncmp(str, prefix, len) ? NULL : str + len;
  139. }
  140. #if defined(NO_MMAP) || defined(USE_WIN32_MMAP)
  141. #ifndef PROT_READ
  142. #define PROT_READ 1
  143. #define PROT_WRITE 2
  144. #define MAP_PRIVATE 1
  145. #define MAP_FAILED ((void*)-1)
  146. #endif
  147. #define mmap git_mmap
  148. #define munmap git_munmap
  149. extern void *git_mmap(void *start, size_t length, int prot, int flags, int fd, off_t offset);
  150. extern int git_munmap(void *start, size_t length);
  151. #else /* NO_MMAP || USE_WIN32_MMAP */
  152. #include <sys/mman.h>
  153. #endif /* NO_MMAP || USE_WIN32_MMAP */
  154. #ifdef NO_MMAP
  155. /* This value must be multiple of (pagesize * 2) */
  156. #define DEFAULT_PACKED_GIT_WINDOW_SIZE (1 * 1024 * 1024)
  157. #else /* NO_MMAP */
  158. /* This value must be multiple of (pagesize * 2) */
  159. #define DEFAULT_PACKED_GIT_WINDOW_SIZE \
  160. (sizeof(void*) >= 8 \
  161. ? 1 * 1024 * 1024 * 1024 \
  162. : 32 * 1024 * 1024)
  163. #endif /* NO_MMAP */
  164. #ifdef NO_ST_BLOCKS_IN_STRUCT_STAT
  165. #define on_disk_bytes(st) ((st).st_size)
  166. #else
  167. #define on_disk_bytes(st) ((st).st_blocks * 512)
  168. #endif
  169. #define DEFAULT_PACKED_GIT_LIMIT \
  170. ((1024L * 1024L) * (sizeof(void*) >= 8 ? 8192 : 256))
  171. #ifdef NO_PREAD
  172. #define pread git_pread
  173. extern ssize_t git_pread(int fd, void *buf, size_t count, off_t offset);
  174. #endif
  175. /*
  176. * Forward decl that will remind us if its twin in cache.h changes.
  177. * This function is used in compat/pread.c. But we can't include
  178. * cache.h there.
  179. */
  180. extern ssize_t read_in_full(int fd, void *buf, size_t count);
  181. #ifdef NO_SETENV
  182. #define setenv gitsetenv
  183. extern int gitsetenv(const char *, const char *, int);
  184. #endif
  185. #ifdef NO_MKDTEMP
  186. #define mkdtemp gitmkdtemp
  187. extern char *gitmkdtemp(char *);
  188. #endif
  189. #ifdef NO_UNSETENV
  190. #define unsetenv gitunsetenv
  191. extern void gitunsetenv(const char *);
  192. #endif
  193. #ifdef NO_STRCASESTR
  194. #define strcasestr gitstrcasestr
  195. extern char *gitstrcasestr(const char *haystack, const char *needle);
  196. #endif
  197. #ifdef NO_STRLCPY
  198. #define strlcpy gitstrlcpy
  199. extern size_t gitstrlcpy(char *, const char *, size_t);
  200. #endif
  201. #ifdef NO_STRTOUMAX
  202. #define strtoumax gitstrtoumax
  203. extern uintmax_t gitstrtoumax(const char *, char **, int);
  204. #endif
  205. #ifdef NO_HSTRERROR
  206. #define hstrerror githstrerror
  207. extern const char *githstrerror(int herror);
  208. #endif
  209. #ifdef NO_MEMMEM
  210. #define memmem gitmemmem
  211. void *gitmemmem(const void *haystack, size_t haystacklen,
  212. const void *needle, size_t needlelen);
  213. #endif
  214. #ifdef FREAD_READS_DIRECTORIES
  215. #ifdef fopen
  216. #undef fopen
  217. #endif
  218. #define fopen(a,b) git_fopen(a,b)
  219. extern FILE *git_fopen(const char*, const char*);
  220. #endif
  221. #ifdef SNPRINTF_RETURNS_BOGUS
  222. #define snprintf git_snprintf
  223. extern int git_snprintf(char *str, size_t maxsize,
  224. const char *format, ...);
  225. #define vsnprintf git_vsnprintf
  226. extern int git_vsnprintf(char *str, size_t maxsize,
  227. const char *format, va_list ap);
  228. #endif
  229. #ifdef __GLIBC_PREREQ
  230. #if __GLIBC_PREREQ(2, 1)
  231. #define HAVE_STRCHRNUL
  232. #endif
  233. #endif
  234. #ifndef HAVE_STRCHRNUL
  235. #define strchrnul gitstrchrnul
  236. static inline char *gitstrchrnul(const char *s, int c)
  237. {
  238. while (*s && *s != c)
  239. s++;
  240. return (char *)s;
  241. }
  242. #endif
  243. /*
  244. * Wrappers:
  245. */
  246. extern char *xstrdup(const char *str);
  247. extern void *xmalloc(size_t size);
  248. extern void *xmemdupz(const void *data, size_t len);
  249. extern char *xstrndup(const char *str, size_t len);
  250. extern void *xrealloc(void *ptr, size_t size);
  251. extern void *xcalloc(size_t nmemb, size_t size);
  252. extern void *xmmap(void *start, size_t length, int prot, int flags, int fd, off_t offset);
  253. extern ssize_t xread(int fd, void *buf, size_t len);
  254. extern ssize_t xwrite(int fd, const void *buf, size_t len);
  255. extern int xdup(int fd);
  256. extern FILE *xfdopen(int fd, const char *mode);
  257. extern int xmkstemp(char *template);
  258. static inline size_t xsize_t(off_t len)
  259. {
  260. return (size_t)len;
  261. }
  262. static inline int has_extension(const char *filename, const char *ext)
  263. {
  264. size_t len = strlen(filename);
  265. size_t extlen = strlen(ext);
  266. return len > extlen && !memcmp(filename + len - extlen, ext, extlen);
  267. }
  268. /* Sane ctype - no locale, and works with signed chars */
  269. #undef isascii
  270. #undef isspace
  271. #undef isdigit
  272. #undef isalpha
  273. #undef isalnum
  274. #undef tolower
  275. #undef toupper
  276. extern unsigned char sane_ctype[256];
  277. #define GIT_SPACE 0x01
  278. #define GIT_DIGIT 0x02
  279. #define GIT_ALPHA 0x04
  280. #define GIT_GLOB_SPECIAL 0x08
  281. #define GIT_REGEX_SPECIAL 0x10
  282. #define GIT_PRINT_EXTRA 0x20
  283. #define GIT_PRINT 0x3E
  284. #define sane_istest(x,mask) ((sane_ctype[(unsigned char)(x)] & (mask)) != 0)
  285. #define isascii(x) (((x) & ~0x7f) == 0)
  286. #define isspace(x) sane_istest(x,GIT_SPACE)
  287. #define isdigit(x) sane_istest(x,GIT_DIGIT)
  288. #define isalpha(x) sane_istest(x,GIT_ALPHA)
  289. #define isalnum(x) sane_istest(x,GIT_ALPHA | GIT_DIGIT)
  290. #define isprint(x) sane_istest(x,GIT_PRINT)
  291. #define is_glob_special(x) sane_istest(x,GIT_GLOB_SPECIAL)
  292. #define is_regex_special(x) sane_istest(x,GIT_GLOB_SPECIAL | GIT_REGEX_SPECIAL)
  293. #define tolower(x) sane_case((unsigned char)(x), 0x20)
  294. #define toupper(x) sane_case((unsigned char)(x), 0)
  295. static inline int sane_case(int x, int high)
  296. {
  297. if (sane_istest(x, GIT_ALPHA))
  298. x = (x & ~0x20) | high;
  299. return x;
  300. }
  301. static inline int strtoul_ui(char const *s, int base, unsigned int *result)
  302. {
  303. unsigned long ul;
  304. char *p;
  305. errno = 0;
  306. ul = strtoul(s, &p, base);
  307. if (errno || *p || p == s || (unsigned int) ul != ul)
  308. return -1;
  309. *result = ul;
  310. return 0;
  311. }
  312. static inline int strtol_i(char const *s, int base, int *result)
  313. {
  314. long ul;
  315. char *p;
  316. errno = 0;
  317. ul = strtol(s, &p, base);
  318. if (errno || *p || p == s || (int) ul != ul)
  319. return -1;
  320. *result = ul;
  321. return 0;
  322. }
  323. #ifdef INTERNAL_QSORT
  324. void git_qsort(void *base, size_t nmemb, size_t size,
  325. int(*compar)(const void *, const void *));
  326. #define qsort git_qsort
  327. #endif
  328. #ifndef DIR_HAS_BSD_GROUP_SEMANTICS
  329. # define FORCE_DIR_SET_GID S_ISGID
  330. #else
  331. # define FORCE_DIR_SET_GID 0
  332. #endif
  333. #ifdef NO_NSEC
  334. #undef USE_NSEC
  335. #define ST_CTIME_NSEC(st) 0
  336. #define ST_MTIME_NSEC(st) 0
  337. #else
  338. #ifdef USE_ST_TIMESPEC
  339. #define ST_CTIME_NSEC(st) ((unsigned int)((st).st_ctimespec.tv_nsec))
  340. #define ST_MTIME_NSEC(st) ((unsigned int)((st).st_mtimespec.tv_nsec))
  341. #else
  342. #define ST_CTIME_NSEC(st) ((unsigned int)((st).st_ctim.tv_nsec))
  343. #define ST_MTIME_NSEC(st) ((unsigned int)((st).st_mtim.tv_nsec))
  344. #endif
  345. #endif
  346. #endif