blob: 0daa341734f93ef0f09c9abc6bb75ffd37bc30ec [file] [log] [blame]
Ingo Molnar07800602009-04-20 15:00:56 +02001#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)
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
Ingo Molnar07800602009-04-20 15:00:56 +020042#define _ALL_SOURCE 1
43#define _GNU_SOURCE 1
44#define _BSD_SOURCE 1
45
46#include <unistd.h>
47#include <stdio.h>
48#include <sys/stat.h>
Jason Baronf6bdafe2009-07-21 12:20:22 -040049#include <sys/statfs.h>
Ingo Molnar07800602009-04-20 15:00:56 +020050#include <fcntl.h>
51#include <stddef.h>
52#include <stdlib.h>
53#include <stdarg.h>
54#include <string.h>
55#include <errno.h>
56#include <limits.h>
57#include <sys/param.h>
58#include <sys/types.h>
59#include <dirent.h>
60#include <sys/time.h>
61#include <time.h>
62#include <signal.h>
63#include <fnmatch.h>
64#include <assert.h>
65#include <regex.h>
66#include <utime.h>
Ingo Molnar07800602009-04-20 15:00:56 +020067#include <sys/wait.h>
68#include <sys/poll.h>
69#include <sys/socket.h>
70#include <sys/ioctl.h>
71#ifndef NO_SYS_SELECT_H
72#include <sys/select.h>
73#endif
74#include <netinet/in.h>
75#include <netinet/tcp.h>
76#include <arpa/inet.h>
77#include <netdb.h>
78#include <pwd.h>
79#include <inttypes.h>
Jason Baronf6bdafe2009-07-21 12:20:22 -040080#include "../../../include/linux/magic.h"
Ingo Molnar07800602009-04-20 15:00:56 +020081
Frederic Weisbecker1fe2c102009-08-12 10:19:53 +020082
Ingo Molnar07800602009-04-20 15:00:56 +020083#ifndef NO_ICONV
84#include <iconv.h>
85#endif
86
Ingo Molnar07800602009-04-20 15:00:56 +020087/* On most systems <limits.h> would have given us this, but
88 * not on some systems (e.g. GNU/Hurd).
89 */
90#ifndef PATH_MAX
91#define PATH_MAX 4096
92#endif
93
94#ifndef PRIuMAX
95#define PRIuMAX "llu"
96#endif
97
98#ifndef PRIu32
99#define PRIu32 "u"
100#endif
101
102#ifndef PRIx32
103#define PRIx32 "x"
104#endif
105
106#ifndef PATH_SEP
107#define PATH_SEP ':'
108#endif
109
110#ifndef STRIP_EXTENSION
111#define STRIP_EXTENSION ""
112#endif
113
114#ifndef has_dos_drive_prefix
115#define has_dos_drive_prefix(path) 0
116#endif
117
118#ifndef is_dir_sep
119#define is_dir_sep(c) ((c) == '/')
120#endif
121
122#ifdef __GNUC__
123#define NORETURN __attribute__((__noreturn__))
124#else
125#define NORETURN
126#ifndef __attribute__
127#define __attribute__(x)
128#endif
129#endif
130
131/* General helper functions */
132extern void usage(const char *err) NORETURN;
133extern void die(const char *err, ...) NORETURN __attribute__((format (printf, 1, 2)));
134extern int error(const char *err, ...) __attribute__((format (printf, 1, 2)));
135extern void warning(const char *err, ...) __attribute__((format (printf, 1, 2)));
136
Masami Hiramatsu97698332009-10-16 20:08:18 -0400137#include "../../../include/linux/stringify.h"
138
139#define DIE_IF(cnd) \
140 do { if (cnd) \
141 die(" at (" __FILE__ ":" __stringify(__LINE__) "): " \
142 __stringify(cnd) "\n"); \
143 } while (0)
144
145
Ingo Molnar07800602009-04-20 15:00:56 +0200146extern void set_die_routine(void (*routine)(const char *err, va_list params) NORETURN);
147
148extern int prefixcmp(const char *str, const char *prefix);
149extern time_t tm_to_time_t(const struct tm *tm);
150
151static inline const char *skip_prefix(const char *str, const char *prefix)
152{
153 size_t len = strlen(prefix);
154 return strncmp(str, prefix, len) ? NULL : str + len;
155}
156
157#if defined(NO_MMAP) || defined(USE_WIN32_MMAP)
158
159#ifndef PROT_READ
160#define PROT_READ 1
161#define PROT_WRITE 2
162#define MAP_PRIVATE 1
163#define MAP_FAILED ((void*)-1)
164#endif
165
166#define mmap git_mmap
167#define munmap git_munmap
168extern void *git_mmap(void *start, size_t length, int prot, int flags, int fd, off_t offset);
169extern int git_munmap(void *start, size_t length);
170
171#else /* NO_MMAP || USE_WIN32_MMAP */
172
173#include <sys/mman.h>
174
175#endif /* NO_MMAP || USE_WIN32_MMAP */
176
177#ifdef NO_MMAP
178
179/* This value must be multiple of (pagesize * 2) */
180#define DEFAULT_PACKED_GIT_WINDOW_SIZE (1 * 1024 * 1024)
181
182#else /* NO_MMAP */
183
184/* This value must be multiple of (pagesize * 2) */
185#define DEFAULT_PACKED_GIT_WINDOW_SIZE \
186 (sizeof(void*) >= 8 \
187 ? 1 * 1024 * 1024 * 1024 \
188 : 32 * 1024 * 1024)
189
190#endif /* NO_MMAP */
191
192#ifdef NO_ST_BLOCKS_IN_STRUCT_STAT
193#define on_disk_bytes(st) ((st).st_size)
194#else
195#define on_disk_bytes(st) ((st).st_blocks * 512)
196#endif
197
198#define DEFAULT_PACKED_GIT_LIMIT \
199 ((1024L * 1024L) * (sizeof(void*) >= 8 ? 8192 : 256))
200
201#ifdef NO_PREAD
202#define pread git_pread
203extern ssize_t git_pread(int fd, void *buf, size_t count, off_t offset);
204#endif
205/*
206 * Forward decl that will remind us if its twin in cache.h changes.
207 * This function is used in compat/pread.c. But we can't include
208 * cache.h there.
209 */
210extern ssize_t read_in_full(int fd, void *buf, size_t count);
211
212#ifdef NO_SETENV
213#define setenv gitsetenv
214extern int gitsetenv(const char *, const char *, int);
215#endif
216
217#ifdef NO_MKDTEMP
218#define mkdtemp gitmkdtemp
219extern char *gitmkdtemp(char *);
220#endif
221
222#ifdef NO_UNSETENV
223#define unsetenv gitunsetenv
224extern void gitunsetenv(const char *);
225#endif
226
227#ifdef NO_STRCASESTR
228#define strcasestr gitstrcasestr
229extern char *gitstrcasestr(const char *haystack, const char *needle);
230#endif
231
232#ifdef NO_STRLCPY
233#define strlcpy gitstrlcpy
234extern size_t gitstrlcpy(char *, const char *, size_t);
235#endif
236
237#ifdef NO_STRTOUMAX
238#define strtoumax gitstrtoumax
239extern uintmax_t gitstrtoumax(const char *, char **, int);
240#endif
241
242#ifdef NO_HSTRERROR
243#define hstrerror githstrerror
244extern const char *githstrerror(int herror);
245#endif
246
247#ifdef NO_MEMMEM
248#define memmem gitmemmem
249void *gitmemmem(const void *haystack, size_t haystacklen,
250 const void *needle, size_t needlelen);
251#endif
252
253#ifdef FREAD_READS_DIRECTORIES
254#ifdef fopen
255#undef fopen
256#endif
257#define fopen(a,b) git_fopen(a,b)
258extern FILE *git_fopen(const char*, const char*);
259#endif
260
261#ifdef SNPRINTF_RETURNS_BOGUS
262#define snprintf git_snprintf
263extern int git_snprintf(char *str, size_t maxsize,
264 const char *format, ...);
265#define vsnprintf git_vsnprintf
266extern int git_vsnprintf(char *str, size_t maxsize,
267 const char *format, va_list ap);
268#endif
269
270#ifdef __GLIBC_PREREQ
271#if __GLIBC_PREREQ(2, 1)
272#define HAVE_STRCHRNUL
273#endif
274#endif
275
276#ifndef HAVE_STRCHRNUL
277#define strchrnul gitstrchrnul
278static inline char *gitstrchrnul(const char *s, int c)
279{
280 while (*s && *s != c)
281 s++;
282 return (char *)s;
283}
284#endif
285
Ingo Molnar6f06ccb2009-04-20 15:22:22 +0200286/*
287 * Wrappers:
288 */
289extern char *xstrdup(const char *str);
290extern void *xmalloc(size_t size);
291extern void *xmemdupz(const void *data, size_t len);
292extern char *xstrndup(const char *str, size_t len);
293extern void *xrealloc(void *ptr, size_t size);
294extern void *xcalloc(size_t nmemb, size_t size);
295extern void *xmmap(void *start, size_t length, int prot, int flags, int fd, off_t offset);
296extern ssize_t xread(int fd, void *buf, size_t len);
297extern ssize_t xwrite(int fd, const void *buf, size_t len);
298extern int xdup(int fd);
299extern FILE *xfdopen(int fd, const char *mode);
Ingo Molnar16f762a2009-05-27 09:10:38 +0200300extern int xmkstemp(char *template);
301
Ingo Molnar07800602009-04-20 15:00:56 +0200302static inline size_t xsize_t(off_t len)
303{
304 return (size_t)len;
305}
306
307static inline int has_extension(const char *filename, const char *ext)
308{
309 size_t len = strlen(filename);
310 size_t extlen = strlen(ext);
311 return len > extlen && !memcmp(filename + len - extlen, ext, extlen);
312}
313
314/* Sane ctype - no locale, and works with signed chars */
315#undef isascii
316#undef isspace
317#undef isdigit
318#undef isalpha
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200319#undef isprint
Ingo Molnar07800602009-04-20 15:00:56 +0200320#undef isalnum
321#undef tolower
322#undef toupper
323extern unsigned char sane_ctype[256];
Peter Zijlstraa73c7d82009-06-18 09:44:20 +0200324#define GIT_SPACE 0x01
325#define GIT_DIGIT 0x02
326#define GIT_ALPHA 0x04
327#define GIT_GLOB_SPECIAL 0x08
328#define GIT_REGEX_SPECIAL 0x10
329#define GIT_PRINT_EXTRA 0x20
330#define GIT_PRINT 0x3E
Ingo Molnar07800602009-04-20 15:00:56 +0200331#define sane_istest(x,mask) ((sane_ctype[(unsigned char)(x)] & (mask)) != 0)
332#define isascii(x) (((x) & ~0x7f) == 0)
333#define isspace(x) sane_istest(x,GIT_SPACE)
334#define isdigit(x) sane_istest(x,GIT_DIGIT)
335#define isalpha(x) sane_istest(x,GIT_ALPHA)
336#define isalnum(x) sane_istest(x,GIT_ALPHA | GIT_DIGIT)
Peter Zijlstraa73c7d82009-06-18 09:44:20 +0200337#define isprint(x) sane_istest(x,GIT_PRINT)
Ingo Molnar07800602009-04-20 15:00:56 +0200338#define is_glob_special(x) sane_istest(x,GIT_GLOB_SPECIAL)
339#define is_regex_special(x) sane_istest(x,GIT_GLOB_SPECIAL | GIT_REGEX_SPECIAL)
340#define tolower(x) sane_case((unsigned char)(x), 0x20)
341#define toupper(x) sane_case((unsigned char)(x), 0)
342
343static inline int sane_case(int x, int high)
344{
345 if (sane_istest(x, GIT_ALPHA))
346 x = (x & ~0x20) | high;
347 return x;
348}
349
350static inline int strtoul_ui(char const *s, int base, unsigned int *result)
351{
352 unsigned long ul;
353 char *p;
354
355 errno = 0;
356 ul = strtoul(s, &p, base);
357 if (errno || *p || p == s || (unsigned int) ul != ul)
358 return -1;
359 *result = ul;
360 return 0;
361}
362
363static inline int strtol_i(char const *s, int base, int *result)
364{
365 long ul;
366 char *p;
367
368 errno = 0;
369 ul = strtol(s, &p, base);
370 if (errno || *p || p == s || (int) ul != ul)
371 return -1;
372 *result = ul;
373 return 0;
374}
375
376#ifdef INTERNAL_QSORT
377void git_qsort(void *base, size_t nmemb, size_t size,
378 int(*compar)(const void *, const void *));
379#define qsort git_qsort
380#endif
381
382#ifndef DIR_HAS_BSD_GROUP_SEMANTICS
383# define FORCE_DIR_SET_GID S_ISGID
384#else
385# define FORCE_DIR_SET_GID 0
386#endif
387
388#ifdef NO_NSEC
389#undef USE_NSEC
390#define ST_CTIME_NSEC(st) 0
391#define ST_MTIME_NSEC(st) 0
392#else
393#ifdef USE_ST_TIMESPEC
394#define ST_CTIME_NSEC(st) ((unsigned int)((st).st_ctimespec.tv_nsec))
395#define ST_MTIME_NSEC(st) ((unsigned int)((st).st_mtimespec.tv_nsec))
396#else
397#define ST_CTIME_NSEC(st) ((unsigned int)((st).st_ctim.tv_nsec))
398#define ST_MTIME_NSEC(st) ((unsigned int)((st).st_mtim.tv_nsec))
399#endif
400#endif
401
402#endif