Junio C Hamano | 4050c0d | 2005-12-05 11:54:29 -0800 | [diff] [blame] | 1 | #ifndef GIT_COMPAT_UTIL_H |
| 2 | #define GIT_COMPAT_UTIL_H |
| 3 | |
Martin Waitz | b97e911 | 2007-02-17 10:13:10 +0100 | [diff] [blame] | 4 | #define _FILE_OFFSET_BITS 64 |
| 5 | |
Junio C Hamano | 8f1d2e6 | 2006-01-07 01:33:54 -0800 | [diff] [blame] | 6 | #ifndef FLEX_ARRAY |
Junio C Hamano | 8e97399 | 2007-11-20 12:08:06 -0800 | [diff] [blame] | 7 | /* |
| 8 | * See if our compiler is known to support flexible array members. |
| 9 | */ |
Brandon Casey | 203ee91 | 2009-06-08 18:53:48 -0500 | [diff] [blame] | 10 | #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) && (!defined(__SUNPRO_C) || (__SUNPRO_C > 0x580)) |
Junio C Hamano | 8e97399 | 2007-11-20 12:08:06 -0800 | [diff] [blame] | 11 | # define FLEX_ARRAY /* empty */ |
| 12 | #elif defined(__GNUC__) |
| 13 | # if (__GNUC__ >= 3) |
| 14 | # define FLEX_ARRAY /* empty */ |
| 15 | # else |
| 16 | # define FLEX_ARRAY 0 /* older GNU extension */ |
| 17 | # endif |
| 18 | #endif |
| 19 | |
| 20 | /* |
| 21 | * Otherwise, default to safer but a bit wasteful traditional style |
| 22 | */ |
| 23 | #ifndef FLEX_ARRAY |
| 24 | # define FLEX_ARRAY 1 |
Junio C Hamano | 8f1d2e6 | 2006-01-07 01:33:54 -0800 | [diff] [blame] | 25 | #endif |
| 26 | #endif |
| 27 | |
Junio C Hamano | b4f2a6a | 2006-03-09 11:58:05 -0800 | [diff] [blame] | 28 | #define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0])) |
Pierre Habouzit | f630cfd | 2009-07-22 23:34:34 +0200 | [diff] [blame] | 29 | #define bitsizeof(x) (CHAR_BIT * sizeof(x)) |
Junio C Hamano | b4f2a6a | 2006-03-09 11:58:05 -0800 | [diff] [blame] | 30 | |
Erik Faye-Lund | c03c831 | 2010-10-05 09:24:10 +0200 | [diff] [blame] | 31 | #define maximum_signed_value_of_type(a) \ |
| 32 | (INTMAX_MAX >> (bitsizeof(intmax_t) - bitsizeof(a))) |
| 33 | |
Jonathan Nieder | 1368f65 | 2010-10-10 21:59:26 -0500 | [diff] [blame] | 34 | #define maximum_unsigned_value_of_type(a) \ |
| 35 | (UINTMAX_MAX >> (bitsizeof(uintmax_t) - bitsizeof(a))) |
| 36 | |
Erik Faye-Lund | c03c831 | 2010-10-05 09:24:10 +0200 | [diff] [blame] | 37 | /* |
| 38 | * Signed integer overflow is undefined in C, so here's a helper macro |
| 39 | * to detect if the sum of two integers will overflow. |
| 40 | * |
| 41 | * Requires: a >= 0, typeof(a) equals typeof(b) |
| 42 | */ |
| 43 | #define signed_add_overflows(a, b) \ |
| 44 | ((b) > maximum_signed_value_of_type(a) - (a)) |
| 45 | |
Jonathan Nieder | 1368f65 | 2010-10-10 21:59:26 -0500 | [diff] [blame] | 46 | #define unsigned_add_overflows(a, b) \ |
| 47 | ((b) > maximum_unsigned_value_of_type(a) - (a)) |
| 48 | |
Nicolas Pitre | 8723f21 | 2007-04-09 01:06:29 -0400 | [diff] [blame] | 49 | #ifdef __GNUC__ |
| 50 | #define TYPEOF(x) (__typeof__(x)) |
| 51 | #else |
| 52 | #define TYPEOF(x) |
| 53 | #endif |
| 54 | |
Pierre Habouzit | f630cfd | 2009-07-22 23:34:34 +0200 | [diff] [blame] | 55 | #define MSB(x, bits) ((x) & TYPEOF(x)(~0ULL << (bitsizeof(x) - (bits)))) |
Pierre Habouzit | db7244b | 2007-11-07 11:20:27 +0100 | [diff] [blame] | 56 | #define HAS_MULTI_BITS(i) ((i) & ((i) - 1)) /* checks if an integer has more than 1 bit set */ |
Nicolas Pitre | 8723f21 | 2007-04-09 01:06:29 -0400 | [diff] [blame] | 57 | |
Pierre Habouzit | 98cb6f3 | 2009-07-22 23:34:35 +0200 | [diff] [blame] | 58 | #define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d)) |
| 59 | |
Andy Whitcroft | cf606e3 | 2007-05-15 17:33:25 +0100 | [diff] [blame] | 60 | /* Approximation of the length of the decimal representation of this type. */ |
| 61 | #define decimal_length(x) ((int)(sizeof(x) * 2.56 + 0.5) + 1) |
| 62 | |
Brandon Casey | 4cb18a4 | 2009-06-05 18:36:13 -0500 | [diff] [blame] | 63 | #if defined(__sun__) |
| 64 | /* |
| 65 | * On Solaris, when _XOPEN_EXTENDED is set, its header file |
| 66 | * forces the programs to be XPG4v2, defeating any _XOPEN_SOURCE |
| 67 | * setting to say we are XPG5 or XPG6. Also on Solaris, |
| 68 | * XPG6 programs must be compiled with a c99 compiler, while |
| 69 | * non XPG6 programs must be compiled with a pre-c99 compiler. |
| 70 | */ |
| 71 | # if __STDC_VERSION__ - 0 >= 199901L |
| 72 | # define _XOPEN_SOURCE 600 |
| 73 | # else |
| 74 | # define _XOPEN_SOURCE 500 |
| 75 | # endif |
YONETANI Tomokazu | 6555b19 | 2010-04-02 16:52:09 +0900 | [diff] [blame] | 76 | #elif !defined(__APPLE__) && !defined(__FreeBSD__) && !defined(__USLC__) && \ |
Joachim Schmitz | 6c10990 | 2012-09-19 12:03:30 +0200 | [diff] [blame] | 77 | !defined(_M_UNIX) && !defined(__sgi) && !defined(__DragonFly__) && \ |
Benny Siegert | 92164af | 2013-12-31 14:36:45 +0000 | [diff] [blame] | 78 | !defined(__TANDEM) && !defined(__QNX__) && !defined(__MirBSD__) |
Junio C Hamano | 8502357 | 2006-12-19 14:34:12 -0800 | [diff] [blame] | 79 | #define _XOPEN_SOURCE 600 /* glibc2 and AIX 5.3L need 500, OpenBSD needs 600 for S_ISLNK() */ |
| 80 | #define _XOPEN_SOURCE_EXTENDED 1 /* AIX 5.3L needs this */ |
Terje Sten Bjerkseth | c902c9a | 2006-12-20 17:32:21 -0800 | [diff] [blame] | 81 | #endif |
Jason Riedy | fb95220 | 2007-01-15 17:34:49 -0800 | [diff] [blame] | 82 | #define _ALL_SOURCE 1 |
| 83 | #define _GNU_SOURCE 1 |
| 84 | #define _BSD_SOURCE 1 |
Sergey Senozhatsky | f978a99 | 2014-09-14 14:33:35 +0900 | [diff] [blame] | 85 | #define _DEFAULT_SOURCE 1 |
Patrick Welche | 9a695fb | 2009-04-26 14:49:00 +0100 | [diff] [blame] | 86 | #define _NETBSD_SOURCE 1 |
Brandon Casey | 9398b85 | 2009-07-10 12:10:44 -0500 | [diff] [blame] | 87 | #define _SGI_SOURCE 1 |
Junio C Hamano | 8502357 | 2006-12-19 14:34:12 -0800 | [diff] [blame] | 88 | |
Jonathan Nieder | 380395d | 2013-05-02 20:26:08 +0100 | [diff] [blame] | 89 | #if defined(WIN32) && !defined(__CYGWIN__) /* Both MinGW and MSVC */ |
Sebastian Schuberth | 8453c12 | 2013-09-11 18:06:31 +0200 | [diff] [blame] | 90 | # if defined (_MSC_VER) && !defined(_WIN32_WINNT) |
Ramsay Jones | 41f2999 | 2013-01-31 18:28:35 +0000 | [diff] [blame] | 91 | # define _WIN32_WINNT 0x0502 |
| 92 | # endif |
Marius Storm-Olsen | 435bdf8 | 2009-09-16 10:20:26 +0200 | [diff] [blame] | 93 | #define WIN32_LEAN_AND_MEAN /* stops windows.h including winsock.h */ |
| 94 | #include <winsock2.h> |
| 95 | #include <windows.h> |
Jonathan Nieder | 380395d | 2013-05-02 20:26:08 +0100 | [diff] [blame] | 96 | #define GIT_WINDOWS_NATIVE |
Marius Storm-Olsen | 435bdf8 | 2009-09-16 10:20:26 +0200 | [diff] [blame] | 97 | #endif |
| 98 | |
Junio C Hamano | 4050c0d | 2005-12-05 11:54:29 -0800 | [diff] [blame] | 99 | #include <unistd.h> |
| 100 | #include <stdio.h> |
| 101 | #include <sys/stat.h> |
| 102 | #include <fcntl.h> |
| 103 | #include <stddef.h> |
| 104 | #include <stdlib.h> |
| 105 | #include <stdarg.h> |
| 106 | #include <string.h> |
David Michael | b3e103d | 2012-12-14 14:57:01 -0500 | [diff] [blame] | 107 | #ifdef HAVE_STRINGS_H |
Joachim Schmitz | 6c10990 | 2012-09-19 12:03:30 +0200 | [diff] [blame] | 108 | #include <strings.h> /* for strcasecmp() */ |
| 109 | #endif |
Junio C Hamano | 4050c0d | 2005-12-05 11:54:29 -0800 | [diff] [blame] | 110 | #include <errno.h> |
| 111 | #include <limits.h> |
Junio C Hamano | b2d05e0 | 2012-12-18 09:35:33 -0800 | [diff] [blame] | 112 | #ifdef NEEDS_SYS_PARAM_H |
Junio C Hamano | 4050c0d | 2005-12-05 11:54:29 -0800 | [diff] [blame] | 113 | #include <sys/param.h> |
David Michael | 6ede720 | 2012-12-14 14:56:58 -0500 | [diff] [blame] | 114 | #endif |
Junio C Hamano | 4050c0d | 2005-12-05 11:54:29 -0800 | [diff] [blame] | 115 | #include <sys/types.h> |
| 116 | #include <dirent.h> |
Junio C Hamano | 8502357 | 2006-12-19 14:34:12 -0800 | [diff] [blame] | 117 | #include <sys/time.h> |
| 118 | #include <time.h> |
| 119 | #include <signal.h> |
Johannes Sixt | f4626df | 2007-12-01 21:24:59 +0100 | [diff] [blame] | 120 | #include <assert.h> |
| 121 | #include <regex.h> |
| 122 | #include <utime.h> |
Mike Pape | 088d880 | 2010-11-04 02:35:10 +0100 | [diff] [blame] | 123 | #include <syslog.h> |
Markus Duft | 2844923 | 2010-10-27 10:39:52 +0200 | [diff] [blame] | 124 | #ifndef NO_SYS_POLL_H |
Erik Faye-Lund | fdc1211 | 2010-11-04 02:35:21 +0100 | [diff] [blame] | 125 | #include <sys/poll.h> |
Markus Duft | 2844923 | 2010-10-27 10:39:52 +0200 | [diff] [blame] | 126 | #else |
| 127 | #include <poll.h> |
| 128 | #endif |
Ramsay Jones | 2f0aaaf | 2013-05-30 00:53:28 +0100 | [diff] [blame] | 129 | |
Vincent van Ravesteijn | cfc755d | 2011-10-31 20:12:42 +0100 | [diff] [blame] | 130 | #if defined(__MINGW32__) |
| 131 | /* pull in Windows compatibility stuff */ |
| 132 | #include "compat/mingw.h" |
| 133 | #elif defined(_MSC_VER) |
| 134 | #include "compat/msvc.h" |
| 135 | #else |
Johannes Sixt | f4626df | 2007-12-01 21:24:59 +0100 | [diff] [blame] | 136 | #include <sys/wait.h> |
Jonathan Nieder | ebae9ff | 2011-03-18 15:23:52 -0500 | [diff] [blame] | 137 | #include <sys/resource.h> |
Junio C Hamano | 8502357 | 2006-12-19 14:34:12 -0800 | [diff] [blame] | 138 | #include <sys/socket.h> |
Johannes Sixt | 80bbe72 | 2007-11-13 21:05:01 +0100 | [diff] [blame] | 139 | #include <sys/ioctl.h> |
Nguyễn Thái Ngọc Duy | eb80042 | 2010-01-11 17:41:01 +0700 | [diff] [blame] | 140 | #include <termios.h> |
Robert Schiele | 2600973 | 2008-01-24 19:34:46 +0100 | [diff] [blame] | 141 | #ifndef NO_SYS_SELECT_H |
Johannes Sixt | 80bbe72 | 2007-11-13 21:05:01 +0100 | [diff] [blame] | 142 | #include <sys/select.h> |
Robert Schiele | 2600973 | 2008-01-24 19:34:46 +0100 | [diff] [blame] | 143 | #endif |
Junio C Hamano | 8502357 | 2006-12-19 14:34:12 -0800 | [diff] [blame] | 144 | #include <netinet/in.h> |
| 145 | #include <netinet/tcp.h> |
| 146 | #include <arpa/inet.h> |
| 147 | #include <netdb.h> |
| 148 | #include <pwd.h> |
Jeff King | e277097 | 2011-12-10 05:34:14 -0500 | [diff] [blame] | 149 | #include <sys/un.h> |
Markus Duft | 2844923 | 2010-10-27 10:39:52 +0200 | [diff] [blame] | 150 | #ifndef NO_INTTYPES_H |
Jason Riedy | 007e2ba | 2007-01-25 13:11:40 -0800 | [diff] [blame] | 151 | #include <inttypes.h> |
Markus Duft | 2844923 | 2010-10-27 10:39:52 +0200 | [diff] [blame] | 152 | #else |
| 153 | #include <stdint.h> |
| 154 | #endif |
Joachim Schmitz | 6c10990 | 2012-09-19 12:03:30 +0200 | [diff] [blame] | 155 | #ifdef NO_INTPTR_T |
| 156 | /* |
| 157 | * On I16LP32, ILP32 and LP64 "long" is the save bet, however |
| 158 | * on LLP86, IL33LLP64 and P64 it needs to be "long long", |
| 159 | * while on IP16 and IP16L32 it is "int" (resp. "short") |
| 160 | * Size needs to match (or exceed) 'sizeof(void *)'. |
| 161 | * We can't take "long long" here as not everybody has it. |
| 162 | */ |
| 163 | typedef long intptr_t; |
| 164 | typedef unsigned long uintptr_t; |
| 165 | #endif |
Ramsay Jones | 41b2001 | 2007-03-03 18:28:52 +0000 | [diff] [blame] | 166 | #if defined(__CYGWIN__) |
| 167 | #undef _XOPEN_SOURCE |
| 168 | #include <grp.h> |
| 169 | #define _XOPEN_SOURCE 600 |
| 170 | #else |
Jason Riedy | fb95220 | 2007-01-15 17:34:49 -0800 | [diff] [blame] | 171 | #undef _ALL_SOURCE /* AIX 5.3L defines a struct list with _ALL_SOURCE. */ |
Junio C Hamano | 8502357 | 2006-12-19 14:34:12 -0800 | [diff] [blame] | 172 | #include <grp.h> |
Jason Riedy | fb95220 | 2007-01-15 17:34:49 -0800 | [diff] [blame] | 173 | #define _ALL_SOURCE 1 |
Ramsay Jones | 41b2001 | 2007-03-03 18:28:52 +0000 | [diff] [blame] | 174 | #endif |
Frank Li | d75f8e6 | 2009-09-16 10:20:25 +0200 | [diff] [blame] | 175 | #endif |
Junio C Hamano | 8502357 | 2006-12-19 14:34:12 -0800 | [diff] [blame] | 176 | |
Torsten Bögershausen | 76759c7 | 2012-07-08 15:50:25 +0200 | [diff] [blame] | 177 | /* used on Mac OS X */ |
| 178 | #ifdef PRECOMPOSE_UNICODE |
| 179 | #include "compat/precompose_utf8.h" |
| 180 | #else |
| 181 | #define precompose_str(in,i_nfd2nfc) |
| 182 | #define precompose_argv(c,v) |
| 183 | #define probe_utf8_pathname_composition(a,b) |
| 184 | #endif |
| 185 | |
Joachim Schmitz | 0539ecf | 2012-08-24 12:31:03 +0200 | [diff] [blame] | 186 | #ifdef MKDIR_WO_TRAILING_SLASH |
| 187 | #define mkdir(a,b) compat_mkdir_wo_trailing_slash((a),(b)) |
| 188 | extern int compat_mkdir_wo_trailing_slash(const char*, mode_t); |
| 189 | #endif |
| 190 | |
Joachim Schmitz | 7f9e848 | 2012-09-08 18:54:34 +0200 | [diff] [blame] | 191 | #ifdef NO_STRUCT_ITIMERVAL |
| 192 | struct itimerval { |
| 193 | struct timeval it_interval; |
| 194 | struct timeval it_value; |
Jonas 'Sortie' Termansen | 981ff52 | 2014-08-29 09:42:33 -0700 | [diff] [blame] | 195 | }; |
Joachim Schmitz | 7f9e848 | 2012-09-08 18:54:34 +0200 | [diff] [blame] | 196 | #endif |
| 197 | |
| 198 | #ifdef NO_SETITIMER |
| 199 | #define setitimer(which,value,ovalue) |
| 200 | #endif |
| 201 | |
David Aguilar | e1c0688 | 2009-05-31 01:35:51 -0700 | [diff] [blame] | 202 | #ifndef NO_LIBGEN_H |
| 203 | #include <libgen.h> |
| 204 | #else |
| 205 | #define basename gitbasename |
| 206 | extern char *gitbasename(char *); |
| 207 | #endif |
| 208 | |
Junio C Hamano | 8502357 | 2006-12-19 14:34:12 -0800 | [diff] [blame] | 209 | #ifndef NO_ICONV |
| 210 | #include <iconv.h> |
| 211 | #endif |
Junio C Hamano | 4050c0d | 2005-12-05 11:54:29 -0800 | [diff] [blame] | 212 | |
Robert Shearman | 684ec6c | 2008-07-09 22:29:00 +0100 | [diff] [blame] | 213 | #ifndef NO_OPENSSL |
| 214 | #include <openssl/ssl.h> |
| 215 | #include <openssl/err.h> |
| 216 | #endif |
| 217 | |
David Michael | 3b130ade | 2013-02-25 14:30:19 -0500 | [diff] [blame] | 218 | /* On most systems <netdb.h> would have given us this, but |
| 219 | * not on some systems (e.g. z/OS). |
| 220 | */ |
| 221 | #ifndef NI_MAXHOST |
| 222 | #define NI_MAXHOST 1025 |
| 223 | #endif |
| 224 | |
| 225 | #ifndef NI_MAXSERV |
| 226 | #define NI_MAXSERV 32 |
| 227 | #endif |
| 228 | |
Junio C Hamano | d0c2449 | 2006-09-15 22:47:21 -0700 | [diff] [blame] | 229 | /* On most systems <limits.h> would have given us this, but |
| 230 | * not on some systems (e.g. GNU/Hurd). |
| 231 | */ |
| 232 | #ifndef PATH_MAX |
| 233 | #define PATH_MAX 4096 |
| 234 | #endif |
| 235 | |
Shawn O. Pearce | c4001d9 | 2007-03-06 20:44:30 -0500 | [diff] [blame] | 236 | #ifndef PRIuMAX |
| 237 | #define PRIuMAX "llu" |
| 238 | #endif |
| 239 | |
Johannes Sixt | 607bb3f | 2008-07-09 22:38:14 +0200 | [diff] [blame] | 240 | #ifndef PRIu32 |
| 241 | #define PRIu32 "u" |
| 242 | #endif |
| 243 | |
| 244 | #ifndef PRIx32 |
| 245 | #define PRIx32 "x" |
| 246 | #endif |
| 247 | |
Ramsay Jones | 5418d96 | 2010-09-09 18:24:06 +0100 | [diff] [blame] | 248 | #ifndef PRIo32 |
| 249 | #define PRIo32 "o" |
| 250 | #endif |
| 251 | |
Johannes Sixt | 80ba074 | 2007-12-03 21:55:57 +0100 | [diff] [blame] | 252 | #ifndef PATH_SEP |
| 253 | #define PATH_SEP ':' |
| 254 | #endif |
| 255 | |
Chris Webb | cb6a22c | 2010-04-13 10:07:13 +0100 | [diff] [blame] | 256 | #ifdef HAVE_PATHS_H |
| 257 | #include <paths.h> |
| 258 | #endif |
| 259 | #ifndef _PATH_DEFPATH |
| 260 | #define _PATH_DEFPATH "/usr/local/bin:/usr/bin:/bin" |
| 261 | #endif |
| 262 | |
Johannes Sixt | 23326d1 | 2007-12-08 20:57:25 +0100 | [diff] [blame] | 263 | #ifndef STRIP_EXTENSION |
| 264 | #define STRIP_EXTENSION "" |
| 265 | #endif |
| 266 | |
Johannes Sixt | 25fe217 | 2008-03-05 21:51:27 +0100 | [diff] [blame] | 267 | #ifndef has_dos_drive_prefix |
René Scharfe | bf72834 | 2014-08-16 23:48:33 +0200 | [diff] [blame] | 268 | static inline int git_has_dos_drive_prefix(const char *path) |
| 269 | { |
| 270 | return 0; |
| 271 | } |
| 272 | #define has_dos_drive_prefix git_has_dos_drive_prefix |
Cezary Zawadka | c2369bd | 2010-07-13 16:17:43 +0200 | [diff] [blame] | 273 | #endif |
| 274 | |
Johannes Sixt | 25fe217 | 2008-03-05 21:51:27 +0100 | [diff] [blame] | 275 | #ifndef is_dir_sep |
René Scharfe | bf72834 | 2014-08-16 23:48:33 +0200 | [diff] [blame] | 276 | static inline int git_is_dir_sep(int c) |
| 277 | { |
| 278 | return c == '/'; |
| 279 | } |
| 280 | #define is_dir_sep git_is_dir_sep |
| 281 | #endif |
| 282 | |
| 283 | #ifndef offset_1st_component |
| 284 | static inline int git_offset_1st_component(const char *path) |
| 285 | { |
| 286 | return is_dir_sep(path[0]); |
| 287 | } |
| 288 | #define offset_1st_component git_offset_1st_component |
Johannes Sixt | 25fe217 | 2008-03-05 21:51:27 +0100 | [diff] [blame] | 289 | #endif |
| 290 | |
Theo Niessink | d1c6925 | 2011-05-27 18:00:39 +0200 | [diff] [blame] | 291 | #ifndef find_last_dir_sep |
René Scharfe | bf72834 | 2014-08-16 23:48:33 +0200 | [diff] [blame] | 292 | static inline char *git_find_last_dir_sep(const char *path) |
| 293 | { |
| 294 | return strrchr(path, '/'); |
| 295 | } |
| 296 | #define find_last_dir_sep git_find_last_dir_sep |
Theo Niessink | d1c6925 | 2011-05-27 18:00:39 +0200 | [diff] [blame] | 297 | #endif |
| 298 | |
Ramkumar Ramachandra | e4ac953 | 2011-11-15 23:01:09 +0530 | [diff] [blame] | 299 | #if defined(__HP_cc) && (__HP_cc >= 61000) |
Michal Rokos | b6ab349 | 2011-03-07 13:13:15 +0100 | [diff] [blame] | 300 | #define NORETURN __attribute__((noreturn)) |
| 301 | #define NORETURN_PTR |
Junio C Hamano | 6520c84 | 2011-06-18 18:07:03 -0700 | [diff] [blame] | 302 | #elif defined(__GNUC__) && !defined(NO_NORETURN) |
Junio C Hamano | 4050c0d | 2005-12-05 11:54:29 -0800 | [diff] [blame] | 303 | #define NORETURN __attribute__((__noreturn__)) |
Erik Faye-Lund | 18660bc | 2009-09-30 18:05:50 +0000 | [diff] [blame] | 304 | #define NORETURN_PTR __attribute__((__noreturn__)) |
Ramsay Jones | aba7dea | 2010-01-20 19:45:12 +0000 | [diff] [blame] | 305 | #elif defined(_MSC_VER) |
| 306 | #define NORETURN __declspec(noreturn) |
| 307 | #define NORETURN_PTR |
Junio C Hamano | 4050c0d | 2005-12-05 11:54:29 -0800 | [diff] [blame] | 308 | #else |
| 309 | #define NORETURN |
Erik Faye-Lund | 18660bc | 2009-09-30 18:05:50 +0000 | [diff] [blame] | 310 | #define NORETURN_PTR |
Andi Kleen | 8cd7ebc | 2014-07-04 16:43:49 -0700 | [diff] [blame] | 311 | #ifndef __GNUC__ |
Junio C Hamano | 4050c0d | 2005-12-05 11:54:29 -0800 | [diff] [blame] | 312 | #ifndef __attribute__ |
| 313 | #define __attribute__(x) |
| 314 | #endif |
| 315 | #endif |
Andi Kleen | 8cd7ebc | 2014-07-04 16:43:49 -0700 | [diff] [blame] | 316 | #endif |
Junio C Hamano | 4050c0d | 2005-12-05 11:54:29 -0800 | [diff] [blame] | 317 | |
Ramsay Jones | 9fe3edc | 2013-07-18 21:02:12 +0100 | [diff] [blame] | 318 | /* The sentinel attribute is valid from gcc version 4.0 */ |
| 319 | #if defined(__GNUC__) && (__GNUC__ >= 4) |
| 320 | #define LAST_ARG_MUST_BE_NULL __attribute__((sentinel)) |
| 321 | #else |
| 322 | #define LAST_ARG_MUST_BE_NULL |
| 323 | #endif |
| 324 | |
Nicolas Pitre | 51ea551 | 2009-08-18 15:26:55 -0400 | [diff] [blame] | 325 | #include "compat/bswap.h" |
| 326 | |
Nguyễn Thái Ngọc Duy | cebcab1 | 2013-01-01 09:44:11 +0700 | [diff] [blame] | 327 | #include "wildmatch.h" |
Nguyễn Thái Ngọc Duy | cebcab1 | 2013-01-01 09:44:11 +0700 | [diff] [blame] | 328 | |
Ronnie Sahlberg | 9ccc0c0 | 2014-07-16 11:20:36 -0700 | [diff] [blame] | 329 | struct strbuf; |
| 330 | |
Junio C Hamano | 4050c0d | 2005-12-05 11:54:29 -0800 | [diff] [blame] | 331 | /* General helper functions */ |
Johannes Sixt | ebaa79f | 2010-03-06 16:40:39 +0100 | [diff] [blame] | 332 | extern void vreportf(const char *prefix, const char *err, va_list params); |
Clemens Buchacher | 3bc4181 | 2011-07-27 23:32:34 +0200 | [diff] [blame] | 333 | extern void vwritef(int fd, const char *prefix, const char *err, va_list params); |
Erik Faye-Lund | a4f3131 | 2009-09-30 18:05:49 +0000 | [diff] [blame] | 334 | extern NORETURN void usage(const char *err); |
Jonathan Nieder | 64b1cb7 | 2009-11-09 09:05:02 -0600 | [diff] [blame] | 335 | extern NORETURN void usagef(const char *err, ...) __attribute__((format (printf, 1, 2))); |
Erik Faye-Lund | a4f3131 | 2009-09-30 18:05:49 +0000 | [diff] [blame] | 336 | extern NORETURN void die(const char *err, ...) __attribute__((format (printf, 1, 2))); |
| 337 | extern NORETURN void die_errno(const char *err, ...) __attribute__((format (printf, 1, 2))); |
Junio C Hamano | 4050c0d | 2005-12-05 11:54:29 -0800 | [diff] [blame] | 338 | extern int error(const char *err, ...) __attribute__((format (printf, 1, 2))); |
Theodore Ts'o | 46efd2d | 2007-03-30 19:07:05 -0400 | [diff] [blame] | 339 | extern void warning(const char *err, ...) __attribute__((format (printf, 1, 2))); |
Junio C Hamano | 4050c0d | 2005-12-05 11:54:29 -0800 | [diff] [blame] | 340 | |
Brian Gernhardt | f2be034 | 2013-08-05 11:59:23 -0400 | [diff] [blame] | 341 | #ifndef NO_OPENSSL |
| 342 | #ifdef APPLE_COMMON_CRYPTO |
| 343 | #include "compat/apple-common-crypto.h" |
| 344 | #else |
| 345 | #include <openssl/evp.h> |
| 346 | #include <openssl/hmac.h> |
| 347 | #endif /* APPLE_COMMON_CRYPTO */ |
| 348 | #include <openssl/x509v3.h> |
| 349 | #endif /* NO_OPENSSL */ |
| 350 | |
Jeff King | e208f9c | 2012-12-15 12:37:36 -0500 | [diff] [blame] | 351 | /* |
| 352 | * Let callers be aware of the constant return value; this can help |
Matt Kraai | 9798f7e | 2013-02-08 07:09:28 -0800 | [diff] [blame] | 353 | * gcc with -Wuninitialized analysis. We restrict this trick to gcc, though, |
| 354 | * because some compilers may not support variadic macros. Since we're only |
| 355 | * trying to help gcc, anyway, it's OK; other compilers will fall back to |
| 356 | * using the function as usual. |
Jeff King | e208f9c | 2012-12-15 12:37:36 -0500 | [diff] [blame] | 357 | */ |
Jeff King | ff0a80a | 2014-05-06 11:17:50 -0400 | [diff] [blame] | 358 | #if defined(__GNUC__) |
Jeff King | 87fe5df | 2014-05-06 11:14:42 -0400 | [diff] [blame] | 359 | static inline int const_error(void) |
| 360 | { |
| 361 | return -1; |
| 362 | } |
| 363 | #define error(...) (error(__VA_ARGS__), const_error()) |
Jeff King | e208f9c | 2012-12-15 12:37:36 -0500 | [diff] [blame] | 364 | #endif |
| 365 | |
Erik Faye-Lund | 18660bc | 2009-09-30 18:05:50 +0000 | [diff] [blame] | 366 | extern void set_die_routine(NORETURN_PTR void (*routine)(const char *err, va_list params)); |
Clemens Buchacher | 3bc4181 | 2011-07-27 23:32:34 +0200 | [diff] [blame] | 367 | extern void set_error_routine(void (*routine)(const char *err, va_list params)); |
Jeff King | c19a490 | 2013-04-16 15:46:22 -0400 | [diff] [blame] | 368 | extern void set_die_is_recursing_routine(int (*routine)(void)); |
Petr Baudis | 39a3f5e | 2006-06-24 04:34:38 +0200 | [diff] [blame] | 369 | |
Christian Couder | 9566231 | 2013-12-01 08:49:16 +0100 | [diff] [blame] | 370 | extern int starts_with(const char *str, const char *prefix); |
Junio C Hamano | 698a68b | 2008-01-03 01:23:12 -0800 | [diff] [blame] | 371 | |
Jeff King | cf4fff5 | 2014-06-18 15:44:19 -0400 | [diff] [blame] | 372 | /* |
| 373 | * If the string "str" begins with the string found in "prefix", return 1. |
| 374 | * The "out" parameter is set to "str + strlen(prefix)" (i.e., to the point in |
| 375 | * the string right after the prefix). |
| 376 | * |
| 377 | * Otherwise, return 0 and leave "out" untouched. |
| 378 | * |
| 379 | * Examples: |
| 380 | * |
| 381 | * [extract branch name, fail if not a branch] |
| 382 | * if (!skip_prefix(ref, "refs/heads/", &branch) |
| 383 | * return -1; |
| 384 | * |
| 385 | * [skip prefix if present, otherwise use whole string] |
| 386 | * skip_prefix(name, "refs/heads/", &name); |
| 387 | */ |
| 388 | static inline int skip_prefix(const char *str, const char *prefix, |
| 389 | const char **out) |
Miklos Vajna | fbca583 | 2008-06-27 18:21:56 +0200 | [diff] [blame] | 390 | { |
David Kastrup | ba399c4 | 2014-03-04 00:22:15 +0100 | [diff] [blame] | 391 | do { |
Jeff King | cf4fff5 | 2014-06-18 15:44:19 -0400 | [diff] [blame] | 392 | if (!*prefix) { |
| 393 | *out = str; |
| 394 | return 1; |
| 395 | } |
David Kastrup | ba399c4 | 2014-03-04 00:22:15 +0100 | [diff] [blame] | 396 | } while (*str++ == *prefix++); |
Jeff King | cf4fff5 | 2014-06-18 15:44:19 -0400 | [diff] [blame] | 397 | return 0; |
Miklos Vajna | fbca583 | 2008-06-27 18:21:56 +0200 | [diff] [blame] | 398 | } |
| 399 | |
Jeff King | 35480f0 | 2014-06-30 12:57:51 -0400 | [diff] [blame] | 400 | /* |
| 401 | * If buf ends with suffix, return 1 and subtract the length of the suffix |
| 402 | * from *len. Otherwise, return 0 and leave *len untouched. |
| 403 | */ |
| 404 | static inline int strip_suffix_mem(const char *buf, size_t *len, |
| 405 | const char *suffix) |
| 406 | { |
| 407 | size_t suflen = strlen(suffix); |
| 408 | if (*len < suflen || memcmp(buf + (*len - suflen), suffix, suflen)) |
| 409 | return 0; |
| 410 | *len -= suflen; |
| 411 | return 1; |
| 412 | } |
| 413 | |
| 414 | /* |
| 415 | * If str ends with suffix, return 1 and set *len to the size of the string |
| 416 | * without the suffix. Otherwise, return 0 and set *len to the size of the |
| 417 | * string. |
| 418 | * |
| 419 | * Note that we do _not_ NUL-terminate str to the new length. |
| 420 | */ |
| 421 | static inline int strip_suffix(const char *str, const char *suffix, size_t *len) |
| 422 | { |
| 423 | *len = strlen(str); |
| 424 | return strip_suffix_mem(str, len, suffix); |
| 425 | } |
| 426 | |
Jeff King | f52a35f | 2014-06-30 12:58:08 -0400 | [diff] [blame] | 427 | static inline int ends_with(const char *str, const char *suffix) |
| 428 | { |
| 429 | size_t len; |
| 430 | return strip_suffix(str, suffix, &len); |
| 431 | } |
| 432 | |
Janos Laube | b130a72 | 2009-03-13 16:50:45 +0100 | [diff] [blame] | 433 | #if defined(NO_MMAP) || defined(USE_WIN32_MMAP) |
Junio C Hamano | 4050c0d | 2005-12-05 11:54:29 -0800 | [diff] [blame] | 434 | |
| 435 | #ifndef PROT_READ |
| 436 | #define PROT_READ 1 |
| 437 | #define PROT_WRITE 2 |
| 438 | #define MAP_PRIVATE 1 |
Junio C Hamano | 4050c0d | 2005-12-05 11:54:29 -0800 | [diff] [blame] | 439 | #endif |
| 440 | |
Shawn O. Pearce | d677912 | 2006-12-24 00:45:37 -0500 | [diff] [blame] | 441 | #define mmap git_mmap |
| 442 | #define munmap git_munmap |
| 443 | extern void *git_mmap(void *start, size_t length, int prot, int flags, int fd, off_t offset); |
| 444 | extern int git_munmap(void *start, size_t length); |
Junio C Hamano | 4050c0d | 2005-12-05 11:54:29 -0800 | [diff] [blame] | 445 | |
Janos Laube | b130a72 | 2009-03-13 16:50:45 +0100 | [diff] [blame] | 446 | #else /* NO_MMAP || USE_WIN32_MMAP */ |
| 447 | |
| 448 | #include <sys/mman.h> |
| 449 | |
| 450 | #endif /* NO_MMAP || USE_WIN32_MMAP */ |
| 451 | |
| 452 | #ifdef NO_MMAP |
| 453 | |
Junio C Hamano | 5faaf24 | 2007-02-14 13:20:41 -0800 | [diff] [blame] | 454 | /* This value must be multiple of (pagesize * 2) */ |
Shawn O. Pearce | 8c82534 | 2006-12-24 00:46:13 -0500 | [diff] [blame] | 455 | #define DEFAULT_PACKED_GIT_WINDOW_SIZE (1 * 1024 * 1024) |
| 456 | |
Junio C Hamano | 4050c0d | 2005-12-05 11:54:29 -0800 | [diff] [blame] | 457 | #else /* NO_MMAP */ |
| 458 | |
Junio C Hamano | 5faaf24 | 2007-02-14 13:20:41 -0800 | [diff] [blame] | 459 | /* This value must be multiple of (pagesize * 2) */ |
Shawn O. Pearce | 22bac0e | 2007-01-04 22:28:08 -0500 | [diff] [blame] | 460 | #define DEFAULT_PACKED_GIT_WINDOW_SIZE \ |
| 461 | (sizeof(void*) >= 8 \ |
| 462 | ? 1 * 1024 * 1024 * 1024 \ |
| 463 | : 32 * 1024 * 1024) |
Junio C Hamano | 4050c0d | 2005-12-05 11:54:29 -0800 | [diff] [blame] | 464 | |
| 465 | #endif /* NO_MMAP */ |
| 466 | |
Gary V. Vaughan | fcf3a21 | 2010-05-14 09:31:39 +0000 | [diff] [blame] | 467 | #ifndef MAP_FAILED |
| 468 | #define MAP_FAILED ((void *)-1) |
| 469 | #endif |
| 470 | |
Junio C Hamano | fdb2a2a | 2008-08-18 21:57:16 +0200 | [diff] [blame] | 471 | #ifdef NO_ST_BLOCKS_IN_STRUCT_STAT |
| 472 | #define on_disk_bytes(st) ((st).st_size) |
| 473 | #else |
| 474 | #define on_disk_bytes(st) ((st).st_blocks * 512) |
| 475 | #endif |
| 476 | |
Shawn O. Pearce | 22bac0e | 2007-01-04 22:28:08 -0500 | [diff] [blame] | 477 | #define DEFAULT_PACKED_GIT_LIMIT \ |
Junio C Hamano | ecaebf4 | 2007-01-07 00:11:11 -0800 | [diff] [blame] | 478 | ((1024L * 1024L) * (sizeof(void*) >= 8 ? 8192 : 256)) |
Shawn O. Pearce | 8c82534 | 2006-12-24 00:46:13 -0500 | [diff] [blame] | 479 | |
Stefan-W. Hahn | 6900679 | 2007-01-09 22:04:12 +0100 | [diff] [blame] | 480 | #ifdef NO_PREAD |
| 481 | #define pread git_pread |
| 482 | extern ssize_t git_pread(int fd, void *buf, size_t count, off_t offset); |
| 483 | #endif |
Steffen Prohaska | 14086b0 | 2007-11-17 20:48:14 +0100 | [diff] [blame] | 484 | /* |
| 485 | * Forward decl that will remind us if its twin in cache.h changes. |
| 486 | * This function is used in compat/pread.c. But we can't include |
| 487 | * cache.h there. |
| 488 | */ |
| 489 | extern ssize_t read_in_full(int fd, void *buf, size_t count); |
Stefan-W. Hahn | 6900679 | 2007-01-09 22:04:12 +0100 | [diff] [blame] | 490 | |
Junio C Hamano | 4050c0d | 2005-12-05 11:54:29 -0800 | [diff] [blame] | 491 | #ifdef NO_SETENV |
| 492 | #define setenv gitsetenv |
| 493 | extern int gitsetenv(const char *, const char *, int); |
| 494 | #endif |
| 495 | |
Shawn O. Pearce | ca5bb5d | 2007-10-20 16:03:49 -0400 | [diff] [blame] | 496 | #ifdef NO_MKDTEMP |
| 497 | #define mkdtemp gitmkdtemp |
| 498 | extern char *gitmkdtemp(char *); |
| 499 | #endif |
| 500 | |
David Aguilar | 0620b39 | 2009-05-31 01:35:50 -0700 | [diff] [blame] | 501 | #ifdef NO_MKSTEMPS |
| 502 | #define mkstemps gitmkstemps |
| 503 | extern int gitmkstemps(char *, int); |
| 504 | #endif |
| 505 | |
Jason Riedy | 731043f | 2006-01-25 12:38:36 -0800 | [diff] [blame] | 506 | #ifdef NO_UNSETENV |
| 507 | #define unsetenv gitunsetenv |
| 508 | extern void gitunsetenv(const char *); |
| 509 | #endif |
| 510 | |
Junio C Hamano | 4050c0d | 2005-12-05 11:54:29 -0800 | [diff] [blame] | 511 | #ifdef NO_STRCASESTR |
| 512 | #define strcasestr gitstrcasestr |
| 513 | extern char *gitstrcasestr(const char *haystack, const char *needle); |
| 514 | #endif |
| 515 | |
Peter Eriksen | 817151e | 2006-06-24 16:01:25 +0200 | [diff] [blame] | 516 | #ifdef NO_STRLCPY |
| 517 | #define strlcpy gitstrlcpy |
| 518 | extern size_t gitstrlcpy(char *, const char *, size_t); |
| 519 | #endif |
| 520 | |
Jason Riedy | bc6b4f5 | 2007-02-19 16:22:56 -0800 | [diff] [blame] | 521 | #ifdef NO_STRTOUMAX |
| 522 | #define strtoumax gitstrtoumax |
| 523 | extern uintmax_t gitstrtoumax(const char *, char **, int); |
Johannes Sixt | 97000ba | 2011-11-05 16:37:34 +0100 | [diff] [blame] | 524 | #define strtoimax gitstrtoimax |
| 525 | extern intmax_t gitstrtoimax(const char *, char **, int); |
Jason Riedy | bc6b4f5 | 2007-02-19 16:22:56 -0800 | [diff] [blame] | 526 | #endif |
| 527 | |
Alex Riesen | fa0c87c | 2007-06-13 20:54:32 +0200 | [diff] [blame] | 528 | #ifdef NO_HSTRERROR |
| 529 | #define hstrerror githstrerror |
| 530 | extern const char *githstrerror(int herror); |
| 531 | #endif |
| 532 | |
René Scharfe | b21b9f1 | 2007-09-07 00:32:54 +0200 | [diff] [blame] | 533 | #ifdef NO_MEMMEM |
| 534 | #define memmem gitmemmem |
| 535 | void *gitmemmem(const void *haystack, size_t haystacklen, |
| 536 | const void *needle, size_t needlelen); |
| 537 | #endif |
| 538 | |
Matt Kraai | 40036be | 2012-12-18 14:03:55 -0800 | [diff] [blame] | 539 | #ifdef NO_GETPAGESIZE |
| 540 | #define getpagesize() sysconf(_SC_PAGESIZE) |
| 541 | #endif |
| 542 | |
Brandon Casey | cba2252 | 2008-02-08 20:32:47 -0600 | [diff] [blame] | 543 | #ifdef FREAD_READS_DIRECTORIES |
Johannes Sixt | c5445fe | 2008-05-08 09:34:49 +0200 | [diff] [blame] | 544 | #ifdef fopen |
| 545 | #undef fopen |
| 546 | #endif |
Brandon Casey | cba2252 | 2008-02-08 20:32:47 -0600 | [diff] [blame] | 547 | #define fopen(a,b) git_fopen(a,b) |
| 548 | extern FILE *git_fopen(const char*, const char*); |
| 549 | #endif |
| 550 | |
Michal Rokos | c4582f9 | 2008-03-05 16:46:13 +0100 | [diff] [blame] | 551 | #ifdef SNPRINTF_RETURNS_BOGUS |
Benoit Sigoure | ab03803 | 2014-01-30 22:25:12 -0800 | [diff] [blame] | 552 | #ifdef snprintf |
| 553 | #undef snprintf |
| 554 | #endif |
Michal Rokos | c4582f9 | 2008-03-05 16:46:13 +0100 | [diff] [blame] | 555 | #define snprintf git_snprintf |
| 556 | extern int git_snprintf(char *str, size_t maxsize, |
| 557 | const char *format, ...); |
Benoit Sigoure | ab03803 | 2014-01-30 22:25:12 -0800 | [diff] [blame] | 558 | #ifdef vsnprintf |
| 559 | #undef vsnprintf |
| 560 | #endif |
Michal Rokos | c4582f9 | 2008-03-05 16:46:13 +0100 | [diff] [blame] | 561 | #define vsnprintf git_vsnprintf |
| 562 | extern int git_vsnprintf(char *str, size_t maxsize, |
| 563 | const char *format, va_list ap); |
| 564 | #endif |
| 565 | |
Johannes Sixt | 726c8ef | 2007-11-12 11:09:05 +0100 | [diff] [blame] | 566 | #ifdef __GLIBC_PREREQ |
| 567 | #if __GLIBC_PREREQ(2, 1) |
| 568 | #define HAVE_STRCHRNUL |
Jonathan Nieder | 137c6ea | 2010-03-20 19:43:32 -0500 | [diff] [blame] | 569 | #define HAVE_MEMPCPY |
Johannes Sixt | 726c8ef | 2007-11-12 11:09:05 +0100 | [diff] [blame] | 570 | #endif |
| 571 | #endif |
| 572 | |
| 573 | #ifndef HAVE_STRCHRNUL |
René Scharfe | 659c69c | 2007-11-09 01:49:36 +0100 | [diff] [blame] | 574 | #define strchrnul gitstrchrnul |
Andreas Ericsson | 9e79f00 | 2007-11-10 12:55:48 +0100 | [diff] [blame] | 575 | static inline char *gitstrchrnul(const char *s, int c) |
| 576 | { |
| 577 | while (*s && *s != c) |
| 578 | s++; |
| 579 | return (char *)s; |
| 580 | } |
René Scharfe | 659c69c | 2007-11-09 01:49:36 +0100 | [diff] [blame] | 581 | #endif |
| 582 | |
Jonathan Nieder | 137c6ea | 2010-03-20 19:43:32 -0500 | [diff] [blame] | 583 | #ifndef HAVE_MEMPCPY |
| 584 | #define mempcpy gitmempcpy |
| 585 | static inline void *gitmempcpy(void *dest, const void *src, size_t n) |
| 586 | { |
| 587 | return (char *)memcpy(dest, src, n) + n; |
| 588 | } |
| 589 | #endif |
| 590 | |
Mike Pape | da523cc | 2010-11-04 02:35:11 +0100 | [diff] [blame] | 591 | #ifdef NO_INET_PTON |
| 592 | int inet_pton(int af, const char *src, void *dst); |
| 593 | #endif |
| 594 | |
| 595 | #ifdef NO_INET_NTOP |
| 596 | const char *inet_ntop(int af, const void *src, char *dst, size_t size); |
| 597 | #endif |
| 598 | |
Etienne Buira | 0f4b6db | 2014-10-18 14:31:15 +0200 | [diff] [blame] | 599 | #ifdef NO_PTHREADS |
| 600 | #define atexit git_atexit |
| 601 | extern int git_atexit(void (*handler)(void)); |
| 602 | #endif |
| 603 | |
Brandon Casey | 7c3ecb3 | 2013-07-31 12:51:37 -0700 | [diff] [blame] | 604 | extern void release_pack_memory(size_t); |
Shawn O. Pearce | 97bfeb3 | 2006-12-24 00:47:19 -0500 | [diff] [blame] | 605 | |
Johannes Sixt | 851c34b | 2010-05-08 17:13:49 +0200 | [diff] [blame] | 606 | typedef void (*try_to_free_t)(size_t); |
| 607 | extern try_to_free_t set_try_to_free_routine(try_to_free_t); |
Nicolas Pitre | a9a7463 | 2010-03-24 16:22:34 -0400 | [diff] [blame] | 608 | |
Kirill Smelkov | 61f76a3 | 2014-03-27 18:22:50 +0400 | [diff] [blame] | 609 | #ifdef HAVE_ALLOCA_H |
| 610 | # include <alloca.h> |
| 611 | # define xalloca(size) (alloca(size)) |
| 612 | # define xalloca_free(p) do {} while (0) |
| 613 | #else |
| 614 | # define xalloca(size) (xmalloc(size)) |
| 615 | # define xalloca_free(p) (free(p)) |
| 616 | #endif |
Linus Torvalds | 112db55 | 2008-06-22 12:19:25 -0700 | [diff] [blame] | 617 | extern char *xstrdup(const char *str); |
| 618 | extern void *xmalloc(size_t size); |
Ilari Liusvaara | 5bf9219 | 2010-01-26 20:24:12 +0200 | [diff] [blame] | 619 | extern void *xmallocz(size_t size); |
Nguyễn Thái Ngọc Duy | f8bb1d9 | 2014-08-16 10:08:02 +0700 | [diff] [blame] | 620 | extern void *xmallocz_gently(size_t size); |
Linus Torvalds | 112db55 | 2008-06-22 12:19:25 -0700 | [diff] [blame] | 621 | extern void *xmemdupz(const void *data, size_t len); |
| 622 | extern char *xstrndup(const char *str, size_t len); |
| 623 | extern void *xrealloc(void *ptr, size_t size); |
| 624 | extern void *xcalloc(size_t nmemb, size_t size); |
| 625 | extern void *xmmap(void *start, size_t length, int prot, int flags, int fd, off_t offset); |
| 626 | extern ssize_t xread(int fd, void *buf, size_t len); |
| 627 | extern ssize_t xwrite(int fd, const void *buf, size_t len); |
Yiannis Marangos | 9aa91af | 2014-04-10 21:54:12 +0300 | [diff] [blame] | 628 | extern ssize_t xpread(int fd, void *buf, size_t len, off_t offset); |
Linus Torvalds | 112db55 | 2008-06-22 12:19:25 -0700 | [diff] [blame] | 629 | extern int xdup(int fd); |
| 630 | extern FILE *xfdopen(int fd, const char *mode); |
| 631 | extern int xmkstemp(char *template); |
Jonathan Nieder | 463db9b | 2010-11-06 06:45:38 -0500 | [diff] [blame] | 632 | extern int xmkstemp_mode(char *template, int mode); |
Junio C Hamano | 6e180cd | 2009-02-24 23:11:29 -0800 | [diff] [blame] | 633 | extern int odb_mkstemp(char *template, size_t limit, const char *pattern); |
Nguyễn Thái Ngọc Duy | 8640d49 | 2014-03-16 20:35:00 +0700 | [diff] [blame] | 634 | extern int odb_pack_keep(char *name, size_t namesz, const unsigned char *sha1); |
René Scharfe | aa14e98 | 2014-07-28 20:29:50 +0200 | [diff] [blame] | 635 | extern char *xgetcwd(void); |
Luiz Fernando N. Capitulino | f21a47b | 2007-08-14 16:44:53 -0300 | [diff] [blame] | 636 | |
René Scharfe | 3ac22f8 | 2014-09-16 20:56:48 +0200 | [diff] [blame] | 637 | #define REALLOC_ARRAY(x, alloc) (x) = xrealloc((x), (alloc) * sizeof(*(x))) |
| 638 | |
Shawn O. Pearce | dc49cd7 | 2007-03-06 20:44:37 -0500 | [diff] [blame] | 639 | static inline size_t xsize_t(off_t len) |
| 640 | { |
Thomas Rast | 46be82d | 2010-07-28 18:36:31 +0200 | [diff] [blame] | 641 | if (len > (size_t) len) |
| 642 | die("Cannot handle files this big"); |
Shawn O. Pearce | dc49cd7 | 2007-03-06 20:44:37 -0500 | [diff] [blame] | 643 | return (size_t)len; |
| 644 | } |
| 645 | |
Ramsay Jones | f1589d1 | 2012-03-04 19:10:57 +0000 | [diff] [blame] | 646 | /* in ctype.c, for kwset users */ |
| 647 | extern const char tolower_trans_tbl[256]; |
| 648 | |
Junio C Hamano | 4050c0d | 2005-12-05 11:54:29 -0800 | [diff] [blame] | 649 | /* Sane ctype - no locale, and works with signed chars */ |
René Scharfe | c2e9364 | 2009-03-07 14:06:49 +0100 | [diff] [blame] | 650 | #undef isascii |
Junio C Hamano | 4050c0d | 2005-12-05 11:54:29 -0800 | [diff] [blame] | 651 | #undef isspace |
| 652 | #undef isdigit |
| 653 | #undef isalpha |
| 654 | #undef isalnum |
Jan H. Schönherr | 0fcec2c | 2012-10-18 16:43:32 +0200 | [diff] [blame] | 655 | #undef isprint |
Namhyung Kim | 43ccdf5 | 2012-02-10 11:13:31 +0900 | [diff] [blame] | 656 | #undef islower |
| 657 | #undef isupper |
Junio C Hamano | 4050c0d | 2005-12-05 11:54:29 -0800 | [diff] [blame] | 658 | #undef tolower |
| 659 | #undef toupper |
Nguyễn Thái Ngọc Duy | 1c149ab | 2012-10-15 13:25:51 +0700 | [diff] [blame] | 660 | #undef iscntrl |
| 661 | #undef ispunct |
| 662 | #undef isxdigit |
Junio C Hamano | 2adf724 | 2013-01-10 13:47:15 -0800 | [diff] [blame] | 663 | |
Nguyễn Thái Ngọc Duy | ca5ab7d | 2012-10-15 13:25:50 +0700 | [diff] [blame] | 664 | extern const unsigned char sane_ctype[256]; |
Junio C Hamano | 4050c0d | 2005-12-05 11:54:29 -0800 | [diff] [blame] | 665 | #define GIT_SPACE 0x01 |
| 666 | #define GIT_DIGIT 0x02 |
| 667 | #define GIT_ALPHA 0x04 |
René Scharfe | 8cc3299 | 2009-01-17 16:50:34 +0100 | [diff] [blame] | 668 | #define GIT_GLOB_SPECIAL 0x08 |
René Scharfe | f9b7cce | 2009-01-17 16:50:37 +0100 | [diff] [blame] | 669 | #define GIT_REGEX_SPECIAL 0x10 |
Junio C Hamano | 2f6c976 | 2011-04-08 16:18:46 -0700 | [diff] [blame] | 670 | #define GIT_PATHSPEC_MAGIC 0x20 |
Nguyễn Thái Ngọc Duy | 1c149ab | 2012-10-15 13:25:51 +0700 | [diff] [blame] | 671 | #define GIT_CNTRL 0x40 |
| 672 | #define GIT_PUNCT 0x80 |
Junio C Hamano | 4050c0d | 2005-12-05 11:54:29 -0800 | [diff] [blame] | 673 | #define sane_istest(x,mask) ((sane_ctype[(unsigned char)(x)] & (mask)) != 0) |
René Scharfe | c2e9364 | 2009-03-07 14:06:49 +0100 | [diff] [blame] | 674 | #define isascii(x) (((x) & ~0x7f) == 0) |
Junio C Hamano | 4050c0d | 2005-12-05 11:54:29 -0800 | [diff] [blame] | 675 | #define isspace(x) sane_istest(x,GIT_SPACE) |
| 676 | #define isdigit(x) sane_istest(x,GIT_DIGIT) |
| 677 | #define isalpha(x) sane_istest(x,GIT_ALPHA) |
| 678 | #define isalnum(x) sane_istest(x,GIT_ALPHA | GIT_DIGIT) |
Jan H. Schönherr | 0fcec2c | 2012-10-18 16:43:32 +0200 | [diff] [blame] | 679 | #define isprint(x) ((x) >= 0x20 && (x) <= 0x7e) |
Namhyung Kim | 43ccdf5 | 2012-02-10 11:13:31 +0900 | [diff] [blame] | 680 | #define islower(x) sane_iscase(x, 1) |
| 681 | #define isupper(x) sane_iscase(x, 0) |
René Scharfe | 8cc3299 | 2009-01-17 16:50:34 +0100 | [diff] [blame] | 682 | #define is_glob_special(x) sane_istest(x,GIT_GLOB_SPECIAL) |
René Scharfe | f9b7cce | 2009-01-17 16:50:37 +0100 | [diff] [blame] | 683 | #define is_regex_special(x) sane_istest(x,GIT_GLOB_SPECIAL | GIT_REGEX_SPECIAL) |
Nguyễn Thái Ngọc Duy | 1c149ab | 2012-10-15 13:25:51 +0700 | [diff] [blame] | 684 | #define iscntrl(x) (sane_istest(x,GIT_CNTRL)) |
| 685 | #define ispunct(x) sane_istest(x, GIT_PUNCT | GIT_REGEX_SPECIAL | \ |
| 686 | GIT_GLOB_SPECIAL | GIT_PATHSPEC_MAGIC) |
Jeff King | 50a7177 | 2014-10-15 18:34:05 -0400 | [diff] [blame] | 687 | #define isxdigit(x) (hexval_table[(unsigned char)(x)] != -1) |
Junio C Hamano | 4050c0d | 2005-12-05 11:54:29 -0800 | [diff] [blame] | 688 | #define tolower(x) sane_case((unsigned char)(x), 0x20) |
| 689 | #define toupper(x) sane_case((unsigned char)(x), 0) |
Junio C Hamano | 2f6c976 | 2011-04-08 16:18:46 -0700 | [diff] [blame] | 690 | #define is_pathspec_magic(x) sane_istest(x,GIT_PATHSPEC_MAGIC) |
Junio C Hamano | 4050c0d | 2005-12-05 11:54:29 -0800 | [diff] [blame] | 691 | |
| 692 | static inline int sane_case(int x, int high) |
| 693 | { |
| 694 | if (sane_istest(x, GIT_ALPHA)) |
| 695 | x = (x & ~0x20) | high; |
| 696 | return x; |
| 697 | } |
| 698 | |
Namhyung Kim | 43ccdf5 | 2012-02-10 11:13:31 +0900 | [diff] [blame] | 699 | static inline int sane_iscase(int x, int is_lower) |
| 700 | { |
| 701 | if (!sane_istest(x, GIT_ALPHA)) |
| 702 | return 0; |
| 703 | |
| 704 | if (is_lower) |
| 705 | return (x & 0x20) != 0; |
| 706 | else |
| 707 | return (x & 0x20) == 0; |
| 708 | } |
| 709 | |
Jim Meyering | 6aead43 | 2007-04-10 01:01:44 +0200 | [diff] [blame] | 710 | static inline int strtoul_ui(char const *s, int base, unsigned int *result) |
| 711 | { |
| 712 | unsigned long ul; |
| 713 | char *p; |
| 714 | |
| 715 | errno = 0; |
| 716 | ul = strtoul(s, &p, base); |
| 717 | if (errno || *p || p == s || (unsigned int) ul != ul) |
| 718 | return -1; |
| 719 | *result = ul; |
| 720 | return 0; |
| 721 | } |
| 722 | |
Junio C Hamano | 7791ecb | 2007-10-23 13:33:26 -0700 | [diff] [blame] | 723 | static inline int strtol_i(char const *s, int base, int *result) |
| 724 | { |
| 725 | long ul; |
| 726 | char *p; |
| 727 | |
| 728 | errno = 0; |
| 729 | ul = strtol(s, &p, base); |
| 730 | if (errno || *p || p == s || (int) ul != ul) |
| 731 | return -1; |
| 732 | *result = ul; |
| 733 | return 0; |
| 734 | } |
| 735 | |
Brian Downing | 43fe901 | 2008-02-05 15:10:44 -0600 | [diff] [blame] | 736 | #ifdef INTERNAL_QSORT |
| 737 | void git_qsort(void *base, size_t nmemb, size_t size, |
| 738 | int(*compar)(const void *, const void *)); |
| 739 | #define qsort git_qsort |
| 740 | #endif |
| 741 | |
Alex Riesen | 81a24b5 | 2008-03-05 00:15:39 +0100 | [diff] [blame] | 742 | #ifndef DIR_HAS_BSD_GROUP_SEMANTICS |
| 743 | # define FORCE_DIR_SET_GID S_ISGID |
| 744 | #else |
| 745 | # define FORCE_DIR_SET_GID 0 |
| 746 | #endif |
| 747 | |
Kjetil Barvik | c06ff49 | 2009-03-04 18:47:40 +0100 | [diff] [blame] | 748 | #ifdef NO_NSEC |
| 749 | #undef USE_NSEC |
| 750 | #define ST_CTIME_NSEC(st) 0 |
| 751 | #define ST_MTIME_NSEC(st) 0 |
| 752 | #else |
Brian Gernhardt | c567383 | 2009-03-08 16:04:28 -0400 | [diff] [blame] | 753 | #ifdef USE_ST_TIMESPEC |
| 754 | #define ST_CTIME_NSEC(st) ((unsigned int)((st).st_ctimespec.tv_nsec)) |
| 755 | #define ST_MTIME_NSEC(st) ((unsigned int)((st).st_mtimespec.tv_nsec)) |
| 756 | #else |
Kjetil Barvik | c06ff49 | 2009-03-04 18:47:40 +0100 | [diff] [blame] | 757 | #define ST_CTIME_NSEC(st) ((unsigned int)((st).st_ctim.tv_nsec)) |
| 758 | #define ST_MTIME_NSEC(st) ((unsigned int)((st).st_mtim.tv_nsec)) |
| 759 | #endif |
Brian Gernhardt | c567383 | 2009-03-08 16:04:28 -0400 | [diff] [blame] | 760 | #endif |
Kjetil Barvik | c06ff49 | 2009-03-04 18:47:40 +0100 | [diff] [blame] | 761 | |
Johannes Sixt | 34779c5 | 2009-04-20 10:17:00 +0200 | [diff] [blame] | 762 | #ifdef UNRELIABLE_FSTAT |
| 763 | #define fstat_is_reliable() 0 |
| 764 | #else |
| 765 | #define fstat_is_reliable() 1 |
| 766 | #endif |
| 767 | |
Jeff King | ab8632a | 2011-02-25 23:08:25 -0600 | [diff] [blame] | 768 | #ifndef va_copy |
Jonathan Nieder | 26db0f2 | 2011-03-08 02:33:44 -0600 | [diff] [blame] | 769 | /* |
| 770 | * Since an obvious implementation of va_list would be to make it a |
| 771 | * pointer into the stack frame, a simple assignment will work on |
| 772 | * many systems. But let's try to be more portable. |
| 773 | */ |
| 774 | #ifdef __va_copy |
| 775 | #define va_copy(dst, src) __va_copy(dst, src) |
| 776 | #else |
| 777 | #define va_copy(dst, src) ((dst) = (src)) |
| 778 | #endif |
Jeff King | ab8632a | 2011-02-25 23:08:25 -0600 | [diff] [blame] | 779 | #endif |
| 780 | |
David Michael | f51140c | 2014-10-26 13:33:12 -0400 | [diff] [blame] | 781 | #if defined(__GNUC__) || (_MSC_VER >= 1400) || defined(__C99_MACRO_WITH_VA_ARGS) |
Karsten Blees | e05bed9 | 2014-07-12 02:05:03 +0200 | [diff] [blame] | 782 | #define HAVE_VARIADIC_MACROS 1 |
| 783 | #endif |
| 784 | |
Alex Riesen | fc71db3 | 2009-04-29 23:21:46 +0200 | [diff] [blame] | 785 | /* |
| 786 | * Preserves errno, prints a message, but gives no warning for ENOENT. |
Ronnie Sahlberg | 1054af7 | 2014-07-16 11:01:18 -0700 | [diff] [blame] | 787 | * Returns 0 on success, which includes trying to unlink an object that does |
| 788 | * not exist. |
Alex Riesen | fc71db3 | 2009-04-29 23:21:46 +0200 | [diff] [blame] | 789 | */ |
| 790 | int unlink_or_warn(const char *path); |
Ronnie Sahlberg | 9ccc0c0 | 2014-07-16 11:20:36 -0700 | [diff] [blame] | 791 | /* |
| 792 | * Tries to unlink file. Returns 0 if unlink succeeded |
| 793 | * or the file already didn't exist. Returns -1 and |
| 794 | * appends a message to err suitable for |
| 795 | * 'error("%s", err->buf)' on error. |
| 796 | */ |
| 797 | int unlink_or_msg(const char *file, struct strbuf *err); |
Peter Collingbourne | d172329 | 2010-03-26 15:25:33 +0000 | [diff] [blame] | 798 | /* |
Ronnie Sahlberg | 1054af7 | 2014-07-16 11:01:18 -0700 | [diff] [blame] | 799 | * Preserves errno, prints a message, but gives no warning for ENOENT. |
| 800 | * Returns 0 on success, which includes trying to remove a directory that does |
| 801 | * not exist. |
Peter Collingbourne | d172329 | 2010-03-26 15:25:33 +0000 | [diff] [blame] | 802 | */ |
| 803 | int rmdir_or_warn(const char *path); |
Peter Collingbourne | 80d706a | 2010-03-26 15:25:34 +0000 | [diff] [blame] | 804 | /* |
| 805 | * Calls the correct function out of {unlink,rmdir}_or_warn based on |
| 806 | * the supplied file mode. |
| 807 | */ |
| 808 | int remove_or_warn(unsigned int mode, const char *path); |
Alex Riesen | fc71db3 | 2009-04-29 23:21:46 +0200 | [diff] [blame] | 809 | |
Jonathan Nieder | e5c52c9 | 2012-10-13 17:03:07 -0700 | [diff] [blame] | 810 | /* |
| 811 | * Call access(2), but warn for any error except "missing file" |
| 812 | * (ENOENT or ENOTDIR). |
| 813 | */ |
Jonathan Nieder | 4698c8f | 2013-04-12 14:03:18 -0700 | [diff] [blame] | 814 | #define ACCESS_EACCES_OK (1U << 0) |
| 815 | int access_or_warn(const char *path, int mode, unsigned flag); |
| 816 | int access_or_die(const char *path, int mode, unsigned flag); |
Jeff King | ba8bd83 | 2012-08-21 02:10:59 -0400 | [diff] [blame] | 817 | |
Junio C Hamano | 55b38a4 | 2012-08-21 14:52:07 -0700 | [diff] [blame] | 818 | /* Warn on an inaccessible file that ought to be accessible */ |
| 819 | void warn_on_inaccessible(const char *path); |
| 820 | |
Jeff King | 2f70587 | 2012-05-21 19:10:20 -0400 | [diff] [blame] | 821 | /* Get the passwd entry for the UID of the current process. */ |
| 822 | struct passwd *xgetpwuid_self(void); |
| 823 | |
Jeff King | 6654754 | 2014-04-01 17:28:42 -0400 | [diff] [blame] | 824 | #ifdef GMTIME_UNRELIABLE_ERRORS |
| 825 | struct tm *git_gmtime(const time_t *); |
| 826 | struct tm *git_gmtime_r(const time_t *, struct tm *); |
| 827 | #define gmtime git_gmtime |
| 828 | #define gmtime_r git_gmtime_r |
| 829 | #endif |
| 830 | |
Junio C Hamano | 4050c0d | 2005-12-05 11:54:29 -0800 | [diff] [blame] | 831 | #endif |