git-compat-util.hon commit configure: test whether -lresolv is needed (8fccb00)
   1#ifndef GIT_COMPAT_UTIL_H
   2#define GIT_COMPAT_UTIL_H
   3
   4#define _FILE_OFFSET_BITS 64
   5
   6#ifndef FLEX_ARRAY
   7/*
   8 * See if our compiler is known to support flexible array members.
   9 */
  10#if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) && (!defined(__SUNPRO_C) || (__SUNPRO_C > 0x580))
  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
  25#endif
  26#endif
  27
  28#define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))
  29
  30#ifdef __GNUC__
  31#define TYPEOF(x) (__typeof__(x))
  32#else
  33#define TYPEOF(x)
  34#endif
  35
  36#define MSB(x, bits) ((x) & TYPEOF(x)(~0ULL << (sizeof(x) * 8 - (bits))))
  37#define HAS_MULTI_BITS(i)  ((i) & ((i) - 1))  /* checks if an integer has more than 1 bit set */
  38
  39/* Approximation of the length of the decimal representation of this type. */
  40#define decimal_length(x)       ((int)(sizeof(x) * 2.56 + 0.5) + 1)
  41
  42#if defined(__sun__)
  43 /*
  44  * On Solaris, when _XOPEN_EXTENDED is set, its header file
  45  * forces the programs to be XPG4v2, defeating any _XOPEN_SOURCE
  46  * setting to say we are XPG5 or XPG6.  Also on Solaris,
  47  * XPG6 programs must be compiled with a c99 compiler, while
  48  * non XPG6 programs must be compiled with a pre-c99 compiler.
  49  */
  50# if __STDC_VERSION__ - 0 >= 199901L
  51# define _XOPEN_SOURCE 600
  52# else
  53# define _XOPEN_SOURCE 500
  54# endif
  55#elif !defined(__APPLE__) && !defined(__FreeBSD__)  && !defined(__USLC__) && !defined(_M_UNIX)
  56#define _XOPEN_SOURCE 600 /* glibc2 and AIX 5.3L need 500, OpenBSD needs 600 for S_ISLNK() */
  57#define _XOPEN_SOURCE_EXTENDED 1 /* AIX 5.3L needs this */
  58#endif
  59#define _ALL_SOURCE 1
  60#define _GNU_SOURCE 1
  61#define _BSD_SOURCE 1
  62#define _NETBSD_SOURCE 1
  63
  64#include <unistd.h>
  65#include <stdio.h>
  66#include <sys/stat.h>
  67#include <fcntl.h>
  68#include <stddef.h>
  69#include <stdlib.h>
  70#include <stdarg.h>
  71#include <string.h>
  72#include <errno.h>
  73#include <limits.h>
  74#include <sys/param.h>
  75#include <sys/types.h>
  76#include <dirent.h>
  77#include <sys/time.h>
  78#include <time.h>
  79#include <signal.h>
  80#include <fnmatch.h>
  81#include <assert.h>
  82#include <regex.h>
  83#include <utime.h>
  84#ifndef __MINGW32__
  85#include <sys/wait.h>
  86#include <sys/poll.h>
  87#include <sys/socket.h>
  88#include <sys/ioctl.h>
  89#ifndef NO_SYS_SELECT_H
  90#include <sys/select.h>
  91#endif
  92#include <netinet/in.h>
  93#include <netinet/tcp.h>
  94#include <arpa/inet.h>
  95#include <netdb.h>
  96#include <pwd.h>
  97#include <inttypes.h>
  98#if defined(__CYGWIN__)
  99#undef _XOPEN_SOURCE
 100#include <grp.h>
 101#define _XOPEN_SOURCE 600
 102#include "compat/cygwin.h"
 103#else
 104#undef _ALL_SOURCE /* AIX 5.3L defines a struct list with _ALL_SOURCE. */
 105#include <grp.h>
 106#define _ALL_SOURCE 1
 107#endif
 108#else   /* __MINGW32__ */
 109/* pull in Windows compatibility stuff */
 110#include "compat/mingw.h"
 111#endif  /* __MINGW32__ */
 112
 113#ifndef NO_ICONV
 114#include <iconv.h>
 115#endif
 116
 117#ifndef NO_OPENSSL
 118#include <openssl/ssl.h>
 119#include <openssl/err.h>
 120#endif
 121
 122/* On most systems <limits.h> would have given us this, but
 123 * not on some systems (e.g. GNU/Hurd).
 124 */
 125#ifndef PATH_MAX
 126#define PATH_MAX 4096
 127#endif
 128
 129#ifndef PRIuMAX
 130#define PRIuMAX "llu"
 131#endif
 132
 133#ifndef PRIu32
 134#define PRIu32 "u"
 135#endif
 136
 137#ifndef PRIx32
 138#define PRIx32 "x"
 139#endif
 140
 141#ifndef PATH_SEP
 142#define PATH_SEP ':'
 143#endif
 144
 145#ifndef STRIP_EXTENSION
 146#define STRIP_EXTENSION ""
 147#endif
 148
 149#ifndef has_dos_drive_prefix
 150#define has_dos_drive_prefix(path) 0
 151#endif
 152
 153#ifndef is_dir_sep
 154#define is_dir_sep(c) ((c) == '/')
 155#endif
 156
 157#ifdef __GNUC__
 158#define NORETURN __attribute__((__noreturn__))
 159#else
 160#define NORETURN
 161#ifndef __attribute__
 162#define __attribute__(x)
 163#endif
 164#endif
 165
 166/* General helper functions */
 167extern void usage(const char *err) NORETURN;
 168extern void die(const char *err, ...) NORETURN __attribute__((format (printf, 1, 2)));
 169extern int error(const char *err, ...) __attribute__((format (printf, 1, 2)));
 170extern void warning(const char *err, ...) __attribute__((format (printf, 1, 2)));
 171
 172extern void set_die_routine(void (*routine)(const char *err, va_list params) NORETURN);
 173
 174extern int prefixcmp(const char *str, const char *prefix);
 175extern time_t tm_to_time_t(const struct tm *tm);
 176
 177static inline const char *skip_prefix(const char *str, const char *prefix)
 178{
 179        size_t len = strlen(prefix);
 180        return strncmp(str, prefix, len) ? NULL : str + len;
 181}
 182
 183#if defined(NO_MMAP) || defined(USE_WIN32_MMAP)
 184
 185#ifndef PROT_READ
 186#define PROT_READ 1
 187#define PROT_WRITE 2
 188#define MAP_PRIVATE 1
 189#define MAP_FAILED ((void*)-1)
 190#endif
 191
 192#define mmap git_mmap
 193#define munmap git_munmap
 194extern void *git_mmap(void *start, size_t length, int prot, int flags, int fd, off_t offset);
 195extern int git_munmap(void *start, size_t length);
 196
 197#else /* NO_MMAP || USE_WIN32_MMAP */
 198
 199#include <sys/mman.h>
 200
 201#endif /* NO_MMAP || USE_WIN32_MMAP */
 202
 203#ifdef NO_MMAP
 204
 205/* This value must be multiple of (pagesize * 2) */
 206#define DEFAULT_PACKED_GIT_WINDOW_SIZE (1 * 1024 * 1024)
 207
 208#else /* NO_MMAP */
 209
 210/* This value must be multiple of (pagesize * 2) */
 211#define DEFAULT_PACKED_GIT_WINDOW_SIZE \
 212        (sizeof(void*) >= 8 \
 213                ?  1 * 1024 * 1024 * 1024 \
 214                : 32 * 1024 * 1024)
 215
 216#endif /* NO_MMAP */
 217
 218#ifdef NO_ST_BLOCKS_IN_STRUCT_STAT
 219#define on_disk_bytes(st) ((st).st_size)
 220#else
 221#define on_disk_bytes(st) ((st).st_blocks * 512)
 222#endif
 223
 224#define DEFAULT_PACKED_GIT_LIMIT \
 225        ((1024L * 1024L) * (sizeof(void*) >= 8 ? 8192 : 256))
 226
 227#ifdef NO_PREAD
 228#define pread git_pread
 229extern ssize_t git_pread(int fd, void *buf, size_t count, off_t offset);
 230#endif
 231/*
 232 * Forward decl that will remind us if its twin in cache.h changes.
 233 * This function is used in compat/pread.c.  But we can't include
 234 * cache.h there.
 235 */
 236extern ssize_t read_in_full(int fd, void *buf, size_t count);
 237
 238#ifdef NO_SETENV
 239#define setenv gitsetenv
 240extern int gitsetenv(const char *, const char *, int);
 241#endif
 242
 243#ifdef NO_MKDTEMP
 244#define mkdtemp gitmkdtemp
 245extern char *gitmkdtemp(char *);
 246#endif
 247
 248#ifdef NO_UNSETENV
 249#define unsetenv gitunsetenv
 250extern void gitunsetenv(const char *);
 251#endif
 252
 253#ifdef NO_STRCASESTR
 254#define strcasestr gitstrcasestr
 255extern char *gitstrcasestr(const char *haystack, const char *needle);
 256#endif
 257
 258#ifdef NO_STRLCPY
 259#define strlcpy gitstrlcpy
 260extern size_t gitstrlcpy(char *, const char *, size_t);
 261#endif
 262
 263#ifdef NO_STRTOUMAX
 264#define strtoumax gitstrtoumax
 265extern uintmax_t gitstrtoumax(const char *, char **, int);
 266#endif
 267
 268#ifdef NO_HSTRERROR
 269#define hstrerror githstrerror
 270extern const char *githstrerror(int herror);
 271#endif
 272
 273#ifdef NO_MEMMEM
 274#define memmem gitmemmem
 275void *gitmemmem(const void *haystack, size_t haystacklen,
 276                const void *needle, size_t needlelen);
 277#endif
 278
 279#ifdef FREAD_READS_DIRECTORIES
 280#ifdef fopen
 281#undef fopen
 282#endif
 283#define fopen(a,b) git_fopen(a,b)
 284extern FILE *git_fopen(const char*, const char*);
 285#endif
 286
 287#ifdef SNPRINTF_RETURNS_BOGUS
 288#define snprintf git_snprintf
 289extern int git_snprintf(char *str, size_t maxsize,
 290                        const char *format, ...);
 291#define vsnprintf git_vsnprintf
 292extern int git_vsnprintf(char *str, size_t maxsize,
 293                         const char *format, va_list ap);
 294#endif
 295
 296#ifdef __GLIBC_PREREQ
 297#if __GLIBC_PREREQ(2, 1)
 298#define HAVE_STRCHRNUL
 299#endif
 300#endif
 301
 302#ifndef HAVE_STRCHRNUL
 303#define strchrnul gitstrchrnul
 304static inline char *gitstrchrnul(const char *s, int c)
 305{
 306        while (*s && *s != c)
 307                s++;
 308        return (char *)s;
 309}
 310#endif
 311
 312extern void release_pack_memory(size_t, int);
 313
 314extern char *xstrdup(const char *str);
 315extern void *xmalloc(size_t size);
 316extern void *xmemdupz(const void *data, size_t len);
 317extern char *xstrndup(const char *str, size_t len);
 318extern void *xrealloc(void *ptr, size_t size);
 319extern void *xcalloc(size_t nmemb, size_t size);
 320extern void *xmmap(void *start, size_t length, int prot, int flags, int fd, off_t offset);
 321extern ssize_t xread(int fd, void *buf, size_t len);
 322extern ssize_t xwrite(int fd, const void *buf, size_t len);
 323extern int xdup(int fd);
 324extern FILE *xfdopen(int fd, const char *mode);
 325extern int xmkstemp(char *template);
 326extern int odb_mkstemp(char *template, size_t limit, const char *pattern);
 327extern int odb_pack_keep(char *name, size_t namesz, unsigned char *sha1);
 328
 329static inline size_t xsize_t(off_t len)
 330{
 331        return (size_t)len;
 332}
 333
 334static inline int has_extension(const char *filename, const char *ext)
 335{
 336        size_t len = strlen(filename);
 337        size_t extlen = strlen(ext);
 338        return len > extlen && !memcmp(filename + len - extlen, ext, extlen);
 339}
 340
 341/* Sane ctype - no locale, and works with signed chars */
 342#undef isascii
 343#undef isspace
 344#undef isdigit
 345#undef isalpha
 346#undef isalnum
 347#undef tolower
 348#undef toupper
 349extern unsigned char sane_ctype[256];
 350#define GIT_SPACE 0x01
 351#define GIT_DIGIT 0x02
 352#define GIT_ALPHA 0x04
 353#define GIT_GLOB_SPECIAL 0x08
 354#define GIT_REGEX_SPECIAL 0x10
 355#define sane_istest(x,mask) ((sane_ctype[(unsigned char)(x)] & (mask)) != 0)
 356#define isascii(x) (((x) & ~0x7f) == 0)
 357#define isspace(x) sane_istest(x,GIT_SPACE)
 358#define isdigit(x) sane_istest(x,GIT_DIGIT)
 359#define isalpha(x) sane_istest(x,GIT_ALPHA)
 360#define isalnum(x) sane_istest(x,GIT_ALPHA | GIT_DIGIT)
 361#define is_glob_special(x) sane_istest(x,GIT_GLOB_SPECIAL)
 362#define is_regex_special(x) sane_istest(x,GIT_GLOB_SPECIAL | GIT_REGEX_SPECIAL)
 363#define tolower(x) sane_case((unsigned char)(x), 0x20)
 364#define toupper(x) sane_case((unsigned char)(x), 0)
 365
 366static inline int sane_case(int x, int high)
 367{
 368        if (sane_istest(x, GIT_ALPHA))
 369                x = (x & ~0x20) | high;
 370        return x;
 371}
 372
 373static inline int strtoul_ui(char const *s, int base, unsigned int *result)
 374{
 375        unsigned long ul;
 376        char *p;
 377
 378        errno = 0;
 379        ul = strtoul(s, &p, base);
 380        if (errno || *p || p == s || (unsigned int) ul != ul)
 381                return -1;
 382        *result = ul;
 383        return 0;
 384}
 385
 386static inline int strtol_i(char const *s, int base, int *result)
 387{
 388        long ul;
 389        char *p;
 390
 391        errno = 0;
 392        ul = strtol(s, &p, base);
 393        if (errno || *p || p == s || (int) ul != ul)
 394                return -1;
 395        *result = ul;
 396        return 0;
 397}
 398
 399#ifdef INTERNAL_QSORT
 400void git_qsort(void *base, size_t nmemb, size_t size,
 401               int(*compar)(const void *, const void *));
 402#define qsort git_qsort
 403#endif
 404
 405#ifndef DIR_HAS_BSD_GROUP_SEMANTICS
 406# define FORCE_DIR_SET_GID S_ISGID
 407#else
 408# define FORCE_DIR_SET_GID 0
 409#endif
 410
 411#ifdef NO_NSEC
 412#undef USE_NSEC
 413#define ST_CTIME_NSEC(st) 0
 414#define ST_MTIME_NSEC(st) 0
 415#else
 416#ifdef USE_ST_TIMESPEC
 417#define ST_CTIME_NSEC(st) ((unsigned int)((st).st_ctimespec.tv_nsec))
 418#define ST_MTIME_NSEC(st) ((unsigned int)((st).st_mtimespec.tv_nsec))
 419#else
 420#define ST_CTIME_NSEC(st) ((unsigned int)((st).st_ctim.tv_nsec))
 421#define ST_MTIME_NSEC(st) ((unsigned int)((st).st_mtim.tv_nsec))
 422#endif
 423#endif
 424
 425#ifdef UNRELIABLE_FSTAT
 426#define fstat_is_reliable() 0
 427#else
 428#define fstat_is_reliable() 1
 429#endif
 430
 431/*
 432 * Preserves errno, prints a message, but gives no warning for ENOENT.
 433 * Always returns the return value of unlink(2).
 434 */
 435int unlink_or_warn(const char *path);
 436
 437#endif