Clean up compatibility definitions.
[git.git] / git-compat-util.h
1 #ifndef GIT_COMPAT_UTIL_H
2 #define GIT_COMPAT_UTIL_H
3
4 #include <unistd.h>
5 #include <stdio.h>
6 #include <sys/stat.h>
7 #include <fcntl.h>
8 #include <stddef.h>
9 #include <stdlib.h>
10 #include <stdarg.h>
11 #include <string.h>
12 #include <errno.h>
13 #include <limits.h>
14 #include <sys/param.h>
15 #include <netinet/in.h>
16 #include <sys/types.h>
17 #include <dirent.h>
18
19 #ifdef __GNUC__
20 #define NORETURN __attribute__((__noreturn__))
21 #else
22 #define NORETURN
23 #ifndef __attribute__
24 #define __attribute__(x)
25 #endif
26 #endif
27
28 /* General helper functions */
29 extern void usage(const char *err) NORETURN;
30 extern void die(const char *err, ...) NORETURN __attribute__((format (printf, 1, 2)));
31 extern int error(const char *err, ...) __attribute__((format (printf, 1, 2)));
32
33 #ifdef NO_MMAP
34
35 #ifndef PROT_READ
36 #define PROT_READ 1
37 #define PROT_WRITE 2
38 #define MAP_PRIVATE 1
39 #define MAP_FAILED ((void*)-1)
40 #endif
41
42 #define mmap gitfakemmap
43 #define munmap gitfakemunmap
44 extern void *gitfakemmap(void *start, size_t length, int prot , int flags, int fd, off_t offset);
45 extern int gitfakemunmap(void *start, size_t length);
46
47 #else /* NO_MMAP */
48
49 #include <sys/mman.h>
50
51 #endif /* NO_MMAP */
52
53 #ifdef NO_SETENV
54 #define setenv gitsetenv
55 extern int gitsetenv(const char *, const char *, int);
56 #endif
57
58 #ifdef NO_STRCASESTR
59 #define strcasestr gitstrcasestr
60 extern char *gitstrcasestr(const char *haystack, const char *needle);
61 #endif
62
63 static inline void *xmalloc(size_t size)
64 {
65         void *ret = malloc(size);
66         if (!ret)
67                 die("Out of memory, malloc failed");
68         return ret;
69 }
70
71 static inline void *xrealloc(void *ptr, size_t size)
72 {
73         void *ret = realloc(ptr, size);
74         if (!ret)
75                 die("Out of memory, realloc failed");
76         return ret;
77 }
78
79 static inline void *xcalloc(size_t nmemb, size_t size)
80 {
81         void *ret = calloc(nmemb, size);
82         if (!ret)
83                 die("Out of memory, calloc failed");
84         return ret;
85 }
86
87 /* Sane ctype - no locale, and works with signed chars */
88 #undef isspace
89 #undef isdigit
90 #undef isalpha
91 #undef isalnum
92 #undef tolower
93 #undef toupper
94 extern unsigned char sane_ctype[256];
95 #define GIT_SPACE 0x01
96 #define GIT_DIGIT 0x02
97 #define GIT_ALPHA 0x04
98 #define sane_istest(x,mask) ((sane_ctype[(unsigned char)(x)] & (mask)) != 0)
99 #define isspace(x) sane_istest(x,GIT_SPACE)
100 #define isdigit(x) sane_istest(x,GIT_DIGIT)
101 #define isalpha(x) sane_istest(x,GIT_ALPHA)
102 #define isalnum(x) sane_istest(x,GIT_ALPHA | GIT_DIGIT)
103 #define tolower(x) sane_case((unsigned char)(x), 0x20)
104 #define toupper(x) sane_case((unsigned char)(x), 0)
105
106 static inline int sane_case(int x, int high)
107 {
108         if (sane_istest(x, GIT_ALPHA))
109                 x = (x & ~0x20) | high;
110         return x;
111 }
112
113 #endif