diff options
Diffstat (limited to 'git-compat-util.h')
-rw-r--r-- | git-compat-util.h | 164 |
1 files changed, 150 insertions, 14 deletions
diff --git a/git-compat-util.h b/git-compat-util.h index 44890d5b18..4fe10cc146 100644 --- a/git-compat-util.h +++ b/git-compat-util.h @@ -3,6 +3,23 @@ #define _FILE_OFFSET_BITS 64 + +/* Derived from Linux "Features Test Macro" header + * Convenience macros to test the versions of gcc (or + * a compatible compiler). + * Use them like this: + * #if GIT_GNUC_PREREQ (2,8) + * ... code requiring gcc 2.8 or later ... + * #endif +*/ +#if defined(__GNUC__) && defined(__GNUC_MINOR__) +# define GIT_GNUC_PREREQ(maj, min) \ + ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min)) +#else + #define GIT_GNUC_PREREQ(maj, min) 0 +#endif + + #ifndef FLEX_ARRAY /* * See if our compiler is known to support flexible array members. @@ -25,7 +42,40 @@ #endif #endif -#define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0])) + +/* + * BUILD_ASSERT_OR_ZERO - assert a build-time dependency, as an expression. + * @cond: the compile-time condition which must be true. + * + * Your compile will fail if the condition isn't true, or can't be evaluated + * by the compiler. This can be used in an expression: its value is "0". + * + * Example: + * #define foo_to_char(foo) \ + * ((char *)(foo) \ + * + BUILD_ASSERT_OR_ZERO(offsetof(struct foo, string) == 0)) + */ +#define BUILD_ASSERT_OR_ZERO(cond) \ + (sizeof(char [1 - 2*!(cond)]) - 1) + +#if GIT_GNUC_PREREQ(3, 1) + /* &arr[0] degrades to a pointer: a different type from an array */ +# define BARF_UNLESS_AN_ARRAY(arr) \ + BUILD_ASSERT_OR_ZERO(!__builtin_types_compatible_p(__typeof__(arr), \ + __typeof__(&(arr)[0]))) +#else +# define BARF_UNLESS_AN_ARRAY(arr) 0 +#endif +/* + * ARRAY_SIZE - get the number of elements in a visible array + * <at> x: the array whose size you want. + * + * This does not work on pointers, or arrays declared as [], or + * function parameters. With correct compiler support, such usage + * will cause a build error (see the build_assert_or_zero macro). + */ +#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]) + BARF_UNLESS_AN_ARRAY(x)) + #define bitsizeof(x) (CHAR_BIT * sizeof(x)) #define maximum_signed_value_of_type(a) \ @@ -75,7 +125,8 @@ # endif #elif !defined(__APPLE__) && !defined(__FreeBSD__) && !defined(__USLC__) && \ !defined(_M_UNIX) && !defined(__sgi) && !defined(__DragonFly__) && \ - !defined(__TANDEM) && !defined(__QNX__) && !defined(__MirBSD__) + !defined(__TANDEM) && !defined(__QNX__) && !defined(__MirBSD__) && \ + !defined(__CYGWIN__) #define _XOPEN_SOURCE 600 /* glibc2 and AIX 5.3L need 500, OpenBSD needs 600 for S_ISLNK() */ #define _XOPEN_SOURCE_EXTENDED 1 /* AIX 5.3L needs this */ #endif @@ -126,6 +177,9 @@ #else #include <poll.h> #endif +#ifdef HAVE_BSD_SYSCTL +#include <sys/sysctl.h> +#endif #if defined(__MINGW32__) /* pull in Windows compatibility stuff */ @@ -133,6 +187,7 @@ #elif defined(_MSC_VER) #include "compat/msvc.h" #else +#include <sys/utsname.h> #include <sys/wait.h> #include <sys/resource.h> #include <sys/socket.h> @@ -163,16 +218,10 @@ typedef long intptr_t; typedef unsigned long uintptr_t; #endif -#if defined(__CYGWIN__) -#undef _XOPEN_SOURCE -#include <grp.h> -#define _XOPEN_SOURCE 600 -#else #undef _ALL_SOURCE /* AIX 5.3L defines a struct list with _ALL_SOURCE. */ #include <grp.h> #define _ALL_SOURCE 1 #endif -#endif /* used on Mac OS X */ #ifdef PRECOMPOSE_UNICODE @@ -211,8 +260,18 @@ extern char *gitbasename(char *); #endif #ifndef NO_OPENSSL +#ifdef __APPLE__ +#define __AVAILABILITY_MACROS_USES_AVAILABILITY 0 +#include <AvailabilityMacros.h> +#undef DEPRECATED_ATTRIBUTE +#define DEPRECATED_ATTRIBUTE +#undef __AVAILABILITY_MACROS_USES_AVAILABILITY +#endif #include <openssl/ssl.h> #include <openssl/err.h> +#ifdef NO_HMAC_CTX_CLEANUP +#define HMAC_CTX_cleanup HMAC_cleanup +#endif #endif /* On most systems <netdb.h> would have given us this, but @@ -237,6 +296,10 @@ extern char *gitbasename(char *); #define PRIuMAX "llu" #endif +#ifndef SCNuMAX +#define SCNuMAX PRIuMAX +#endif + #ifndef PRIu32 #define PRIu32 "u" #endif @@ -326,9 +389,10 @@ static inline char *git_find_last_dir_sep(const char *path) #include "wildmatch.h" +struct strbuf; + /* General helper functions */ extern void vreportf(const char *prefix, const char *err, va_list params); -extern void vwritef(int fd, const char *prefix, const char *err, va_list params); extern NORETURN void usage(const char *err); extern NORETURN void usagef(const char *err, ...) __attribute__((format (printf, 1, 2))); extern NORETURN void die(const char *err, ...) __attribute__((format (printf, 1, 2))); @@ -364,6 +428,7 @@ static inline int const_error(void) extern void set_die_routine(NORETURN_PTR void (*routine)(const char *err, va_list params)); extern void set_error_routine(void (*routine)(const char *err, va_list params)); extern void set_die_is_recursing_routine(int (*routine)(void)); +extern void set_error_handle(FILE *); extern int starts_with(const char *str, const char *prefix); @@ -472,8 +537,42 @@ extern int git_munmap(void *start, size_t length); #define on_disk_bytes(st) ((st).st_blocks * 512) #endif +#ifdef NEEDS_MODE_TRANSLATION +#undef S_IFMT +#undef S_IFREG +#undef S_IFDIR +#undef S_IFLNK +#undef S_IFBLK +#undef S_IFCHR +#undef S_IFIFO +#undef S_IFSOCK +#define S_IFMT 0170000 +#define S_IFREG 0100000 +#define S_IFDIR 0040000 +#define S_IFLNK 0120000 +#define S_IFBLK 0060000 +#define S_IFCHR 0020000 +#define S_IFIFO 0010000 +#define S_IFSOCK 0140000 +#ifdef stat +#undef stat +#endif +#define stat(path, buf) git_stat(path, buf) +extern int git_stat(const char *, struct stat *); +#ifdef fstat +#undef fstat +#endif +#define fstat(fd, buf) git_fstat(fd, buf) +extern int git_fstat(int, struct stat *); +#ifdef lstat +#undef lstat +#endif +#define lstat(path, buf) git_lstat(path, buf) +extern int git_lstat(const char *, struct stat *); +#endif + #define DEFAULT_PACKED_GIT_LIMIT \ - ((1024L * 1024L) * (sizeof(void*) >= 8 ? 8192 : 256)) + ((1024L * 1024L) * (size_t)(sizeof(void*) >= 8 ? 8192 : 256)) #ifdef NO_PREAD #define pread git_pread @@ -594,6 +693,11 @@ int inet_pton(int af, const char *src, void *dst); const char *inet_ntop(int af, const void *src, char *dst, size_t size); #endif +#ifdef NO_PTHREADS +#define atexit git_atexit +extern int git_atexit(void (*handler)(void)); +#endif + extern void release_pack_memory(size_t); typedef void (*try_to_free_t)(size_t); @@ -616,10 +720,13 @@ extern char *xstrndup(const char *str, size_t len); extern void *xrealloc(void *ptr, size_t size); extern void *xcalloc(size_t nmemb, size_t size); extern void *xmmap(void *start, size_t length, int prot, int flags, int fd, off_t offset); +extern void *xmmap_gently(void *start, size_t length, int prot, int flags, int fd, off_t offset); +extern int xopen(const char *path, int flags, ...); extern ssize_t xread(int fd, void *buf, size_t len); extern ssize_t xwrite(int fd, const void *buf, size_t len); extern ssize_t xpread(int fd, void *buf, size_t len, off_t offset); extern int xdup(int fd); +extern FILE *xfopen(const char *path, const char *mode); extern FILE *xfdopen(int fd, const char *mode); extern int xmkstemp(char *template); extern int xmkstemp_mode(char *template, int mode); @@ -629,6 +736,11 @@ extern char *xgetcwd(void); #define REALLOC_ARRAY(x, alloc) (x) = xrealloc((x), (alloc) * sizeof(*(x))) +static inline char *xstrdup_or_null(const char *str) +{ + return str ? xstrdup(str) : NULL; +} + static inline size_t xsize_t(off_t len) { if (len > (size_t) len) @@ -637,7 +749,7 @@ static inline size_t xsize_t(off_t len) } /* in ctype.c, for kwset users */ -extern const char tolower_trans_tbl[256]; +extern const unsigned char tolower_trans_tbl[256]; /* Sane ctype - no locale, and works with signed chars */ #undef isascii @@ -771,17 +883,27 @@ void git_qsort(void *base, size_t nmemb, size_t size, #endif #endif -#if defined(__GNUC__) || (_MSC_VER >= 1400) +#if defined(__GNUC__) || (_MSC_VER >= 1400) || defined(__C99_MACRO_WITH_VA_ARGS) #define HAVE_VARIADIC_MACROS 1 #endif /* * Preserves errno, prints a message, but gives no warning for ENOENT. - * Always returns the return value of unlink(2). + * Returns 0 on success, which includes trying to unlink an object that does + * not exist. */ int unlink_or_warn(const char *path); + /* + * Tries to unlink file. Returns 0 if unlink succeeded + * or the file already didn't exist. Returns -1 and + * appends a message to err suitable for + * 'error("%s", err->buf)' on error. + */ +int unlink_or_msg(const char *file, struct strbuf *err); /* - * Likewise for rmdir(2). + * Preserves errno, prints a message, but gives no warning for ENOENT. + * Returns 0 on success, which includes trying to remove a directory that does + * not exist. */ int rmdir_or_warn(const char *path); /* @@ -811,4 +933,18 @@ struct tm *git_gmtime_r(const time_t *, struct tm *); #define gmtime_r git_gmtime_r #endif +#if !defined(USE_PARENS_AROUND_GETTEXT_N) && defined(__GNUC__) +#define USE_PARENS_AROUND_GETTEXT_N 1 +#endif + +#ifndef SHELL_PATH +# define SHELL_PATH "/bin/sh" +#endif + +#ifndef _POSIX_THREAD_SAFE_FUNCTIONS +#define flockfile(fh) +#define funlockfile(fh) +#define getc_unlocked(fh) getc(fh) +#endif + #endif |