Document git-grep -[Hh]
[git/git.git] / git-compat-util.h
CommitLineData
4050c0df
JH
1#ifndef GIT_COMPAT_UTIL_H
2#define GIT_COMPAT_UTIL_H
3
8f1d2e6f
JH
4#ifndef FLEX_ARRAY
5#if defined(__GNUC__) && (__GNUC__ < 3)
6#define FLEX_ARRAY 0
7#else
8#define FLEX_ARRAY /* empty */
9#endif
10#endif
11
b4f2a6ac
JH
12#define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))
13
4050c0df
JH
14#include <unistd.h>
15#include <stdio.h>
16#include <sys/stat.h>
17#include <fcntl.h>
18#include <stddef.h>
19#include <stdlib.h>
20#include <stdarg.h>
21#include <string.h>
22#include <errno.h>
23#include <limits.h>
24#include <sys/param.h>
25#include <netinet/in.h>
26#include <sys/types.h>
27#include <dirent.h>
28
29#ifdef __GNUC__
30#define NORETURN __attribute__((__noreturn__))
31#else
32#define NORETURN
33#ifndef __attribute__
34#define __attribute__(x)
35#endif
36#endif
37
38/* General helper functions */
39extern void usage(const char *err) NORETURN;
40extern void die(const char *err, ...) NORETURN __attribute__((format (printf, 1, 2)));
41extern int error(const char *err, ...) __attribute__((format (printf, 1, 2)));
42
39a3f5ea
PB
43extern void set_usage_routine(void (*routine)(const char *err) NORETURN);
44extern void set_die_routine(void (*routine)(const char *err, va_list params) NORETURN);
45extern void set_error_routine(void (*routine)(const char *err, va_list params));
46
4050c0df
JH
47#ifdef NO_MMAP
48
49#ifndef PROT_READ
50#define PROT_READ 1
51#define PROT_WRITE 2
52#define MAP_PRIVATE 1
53#define MAP_FAILED ((void*)-1)
54#endif
55
56#define mmap gitfakemmap
57#define munmap gitfakemunmap
58extern void *gitfakemmap(void *start, size_t length, int prot , int flags, int fd, off_t offset);
59extern int gitfakemunmap(void *start, size_t length);
60
61#else /* NO_MMAP */
62
63#include <sys/mman.h>
64
65#endif /* NO_MMAP */
66
67#ifdef NO_SETENV
68#define setenv gitsetenv
69extern int gitsetenv(const char *, const char *, int);
70#endif
71
731043fd
JR
72#ifdef NO_UNSETENV
73#define unsetenv gitunsetenv
74extern void gitunsetenv(const char *);
75#endif
76
4050c0df
JH
77#ifdef NO_STRCASESTR
78#define strcasestr gitstrcasestr
79extern char *gitstrcasestr(const char *haystack, const char *needle);
80#endif
81
817151e6
PE
82#ifdef NO_STRLCPY
83#define strlcpy gitstrlcpy
84extern size_t gitstrlcpy(char *, const char *, size_t);
85#endif
86
9befac47
SP
87static inline char* xstrdup(const char *str)
88{
89 char *ret = strdup(str);
90 if (!ret)
91 die("Out of memory, strdup failed");
92 return ret;
93}
94
4050c0df
JH
95static inline void *xmalloc(size_t size)
96{
97 void *ret = malloc(size);
4e7a2ecc
JH
98 if (!ret && !size)
99 ret = malloc(1);
4050c0df
JH
100 if (!ret)
101 die("Out of memory, malloc failed");
aa5481c1
JH
102#ifdef XMALLOC_POISON
103 memset(ret, 0xA5, size);
104#endif
4050c0df
JH
105 return ret;
106}
107
108static inline void *xrealloc(void *ptr, size_t size)
109{
110 void *ret = realloc(ptr, size);
4e7a2ecc
JH
111 if (!ret && !size)
112 ret = realloc(ptr, 1);
4050c0df
JH
113 if (!ret)
114 die("Out of memory, realloc failed");
115 return ret;
116}
117
118static inline void *xcalloc(size_t nmemb, size_t size)
119{
120 void *ret = calloc(nmemb, size);
4e7a2ecc
JH
121 if (!ret && (!nmemb || !size))
122 ret = calloc(1, 1);
4050c0df
JH
123 if (!ret)
124 die("Out of memory, calloc failed");
125 return ret;
126}
127
1c15afb9
JH
128static inline ssize_t xread(int fd, void *buf, size_t len)
129{
130 ssize_t nr;
131 while (1) {
132 nr = read(fd, buf, len);
133 if ((nr < 0) && (errno == EAGAIN || errno == EINTR))
134 continue;
135 return nr;
136 }
137}
138
139static inline ssize_t xwrite(int fd, const void *buf, size_t len)
140{
141 ssize_t nr;
142 while (1) {
143 nr = write(fd, buf, len);
144 if ((nr < 0) && (errno == EAGAIN || errno == EINTR))
145 continue;
146 return nr;
147 }
148}
149
5bb1cda5 150static inline int has_extension(const char *filename, const char *ext)
83a2b841 151{
5bb1cda5
RS
152 size_t len = strlen(filename);
153 size_t extlen = strlen(ext);
83a2b841
RS
154 return len > extlen && !memcmp(filename + len - extlen, ext, extlen);
155}
156
4050c0df
JH
157/* Sane ctype - no locale, and works with signed chars */
158#undef isspace
159#undef isdigit
160#undef isalpha
161#undef isalnum
162#undef tolower
163#undef toupper
164extern unsigned char sane_ctype[256];
165#define GIT_SPACE 0x01
166#define GIT_DIGIT 0x02
167#define GIT_ALPHA 0x04
168#define sane_istest(x,mask) ((sane_ctype[(unsigned char)(x)] & (mask)) != 0)
169#define isspace(x) sane_istest(x,GIT_SPACE)
170#define isdigit(x) sane_istest(x,GIT_DIGIT)
171#define isalpha(x) sane_istest(x,GIT_ALPHA)
172#define isalnum(x) sane_istest(x,GIT_ALPHA | GIT_DIGIT)
173#define tolower(x) sane_case((unsigned char)(x), 0x20)
174#define toupper(x) sane_case((unsigned char)(x), 0)
175
176static inline int sane_case(int x, int high)
177{
178 if (sane_istest(x, GIT_ALPHA))
179 x = (x & ~0x20) | high;
180 return x;
181}
182
183#endif