• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #ifndef GIT_COMPAT_UTIL_H
2 #define GIT_COMPAT_UTIL_H
3 
4 /* ANDROID_CHANGE_BEGIN */
5 #ifndef __BIONIC__
6 #define _FILE_OFFSET_BITS 64
7 #endif
8 /* ANDROID_CHANGE_END */
9 
10 #ifndef FLEX_ARRAY
11 /*
12  * See if our compiler is known to support flexible array members.
13  */
14 #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)
15 # define FLEX_ARRAY /* empty */
16 #elif defined(__GNUC__)
17 # if (__GNUC__ >= 3)
18 #  define FLEX_ARRAY /* empty */
19 # else
20 #  define FLEX_ARRAY 0 /* older GNU extension */
21 # endif
22 #endif
23 
24 /*
25  * Otherwise, default to safer but a bit wasteful traditional style
26  */
27 #ifndef FLEX_ARRAY
28 # define FLEX_ARRAY 1
29 #endif
30 #endif
31 
32 #define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))
33 
34 #ifdef __GNUC__
35 #define TYPEOF(x) (__typeof__(x))
36 #else
37 #define TYPEOF(x)
38 #endif
39 
40 #define MSB(x, bits) ((x) & TYPEOF(x)(~0ULL << (sizeof(x) * 8 - (bits))))
41 #define HAS_MULTI_BITS(i)  ((i) & ((i) - 1))  /* checks if an integer has more than 1 bit set */
42 
43 /* Approximation of the length of the decimal representation of this type. */
44 #define decimal_length(x)	((int)(sizeof(x) * 2.56 + 0.5) + 1)
45 
46 #define _ALL_SOURCE 1
47 #define _GNU_SOURCE 1
48 #define _BSD_SOURCE 1
49 #define HAS_BOOL
50 
51 #include <unistd.h>
52 #include <stdio.h>
53 #include <sys/stat.h>
54 /* ANDROID_CHANGE_BEGIN */
55 #ifndef __APPLE__
56 #include <sys/statfs.h>
57 #endif
58 /* ANDROID_CHANGE_END */
59 #include <fcntl.h>
60 #include <stdbool.h>
61 #include <stddef.h>
62 #include <stdlib.h>
63 #include <stdarg.h>
64 #include <string.h>
65 #include <errno.h>
66 #include <limits.h>
67 #include <sys/param.h>
68 #include <sys/types.h>
69 /* ANDROID_CHANGE_BEGIN */
70 #ifdef __BIONIC__
71 #include <pthread.h>
72 #endif
73 /* ANDROID_CHANGE_END */
74 #include <dirent.h>
75 #include <sys/time.h>
76 #include <time.h>
77 #include <signal.h>
78 #include <fnmatch.h>
79 #include <assert.h>
80 #include <regex.h>
81 #include <utime.h>
82 #include <sys/wait.h>
83 #include <sys/poll.h>
84 #include <sys/socket.h>
85 #include <sys/ioctl.h>
86 #include <sys/select.h>
87 #include <netinet/in.h>
88 #include <netinet/tcp.h>
89 #include <arpa/inet.h>
90 #include <netdb.h>
91 #include <pwd.h>
92 #include <inttypes.h>
93 
94 /* ANDROID_CHANGE_BEGIN */
95 #ifndef __APPLE__
96 #include <linux/magic.h>
97 #endif
98 /* ANDROID_CHANGE_END */
99 
100 #include "types.h"
101 
102 /* ANDROID_CHANGE_BEGIN */
103 #ifndef __BIONIC__
104 #include <sys/ttydefaults.h>
105 #endif
106 /* ANDROID_CHANGE_END */
107 
108 
109 extern const char *graph_line;
110 extern const char *graph_dotted_line;
111 extern char buildid_dir[];
112 
113 /* On most systems <limits.h> would have given us this, but
114  * not on some systems (e.g. GNU/Hurd).
115  */
116 #ifndef PATH_MAX
117 #define PATH_MAX 4096
118 #endif
119 
120 #ifndef PRIuMAX
121 #define PRIuMAX "llu"
122 #endif
123 
124 #ifndef PRIu32
125 #define PRIu32 "u"
126 #endif
127 
128 #ifndef PRIx32
129 #define PRIx32 "x"
130 #endif
131 
132 #ifndef PATH_SEP
133 #define PATH_SEP ':'
134 #endif
135 
136 #ifndef STRIP_EXTENSION
137 #define STRIP_EXTENSION ""
138 #endif
139 
140 #ifndef has_dos_drive_prefix
141 #define has_dos_drive_prefix(path) 0
142 #endif
143 
144 #ifndef is_dir_sep
145 #define is_dir_sep(c) ((c) == '/')
146 #endif
147 
148 #ifdef __GNUC__
149 #define NORETURN __attribute__((__noreturn__))
150 #else
151 #define NORETURN
152 #ifndef __attribute__
153 #define __attribute__(x)
154 #endif
155 #endif
156 
157 /* General helper functions */
158 extern void usage(const char *err) NORETURN;
159 extern void die(const char *err, ...) NORETURN __attribute__((format (printf, 1, 2)));
160 extern int error(const char *err, ...) __attribute__((format (printf, 1, 2)));
161 extern void warning(const char *err, ...) __attribute__((format (printf, 1, 2)));
162 
163 /* ANDROID_CHANGE_BEGIN */
164 #if 0
165 #include "../../../include/linux/stringify.h"
166 #else
167 #include "util/include/linux/added/stringify.h"
168 #endif
169 /* ANDROID_CHANGE_END */
170 
171 #define DIE_IF(cnd)	\
172 	do { if (cnd)	\
173 		die(" at (" __FILE__ ":" __stringify(__LINE__) "): "	\
174 		    __stringify(cnd) "\n");				\
175 	} while (0)
176 
177 
178 extern void set_die_routine(void (*routine)(const char *err, va_list params) NORETURN);
179 
180 extern int prefixcmp(const char *str, const char *prefix);
181 extern void set_buildid_dir(void);
182 extern void disable_buildid_cache(void);
183 
skip_prefix(const char * str,const char * prefix)184 static inline const char *skip_prefix(const char *str, const char *prefix)
185 {
186 	size_t len = strlen(prefix);
187 	return strncmp(str, prefix, len) ? NULL : str + len;
188 }
189 
190 #ifdef __GLIBC_PREREQ
191 #if __GLIBC_PREREQ(2, 1)
192 #define HAVE_STRCHRNUL
193 #endif
194 #endif
195 
196 #ifndef HAVE_STRCHRNUL
197 #define strchrnul gitstrchrnul
gitstrchrnul(const char * s,int c)198 static inline char *gitstrchrnul(const char *s, int c)
199 {
200 	while (*s && *s != c)
201 		s++;
202 	return (char *)s;
203 }
204 #endif
205 
206 /*
207  * Wrappers:
208  */
209 extern char *xstrdup(const char *str);
210 extern void *xrealloc(void *ptr, size_t size) __attribute__((weak));
211 
212 
zalloc(size_t size)213 static inline void *zalloc(size_t size)
214 {
215 	return calloc(1, size);
216 }
217 
has_extension(const char * filename,const char * ext)218 static inline int has_extension(const char *filename, const char *ext)
219 {
220 	size_t len = strlen(filename);
221 	size_t extlen = strlen(ext);
222 
223 	return len > extlen && !memcmp(filename + len - extlen, ext, extlen);
224 }
225 
226 /* Sane ctype - no locale, and works with signed chars */
227 #undef isascii
228 #undef isspace
229 #undef isdigit
230 #undef isxdigit
231 #undef isalpha
232 #undef isprint
233 #undef isalnum
234 #undef tolower
235 #undef toupper
236 
237 extern unsigned char sane_ctype[256];
238 #define GIT_SPACE		0x01
239 #define GIT_DIGIT		0x02
240 #define GIT_ALPHA		0x04
241 #define GIT_GLOB_SPECIAL	0x08
242 #define GIT_REGEX_SPECIAL	0x10
243 #define GIT_PRINT_EXTRA		0x20
244 #define GIT_PRINT		0x3E
245 #define sane_istest(x,mask) ((sane_ctype[(unsigned char)(x)] & (mask)) != 0)
246 #define isascii(x) (((x) & ~0x7f) == 0)
247 #define isspace(x) sane_istest(x,GIT_SPACE)
248 #define isdigit(x) sane_istest(x,GIT_DIGIT)
249 #define isxdigit(x)	\
250 	(sane_istest(toupper(x), GIT_ALPHA | GIT_DIGIT) && toupper(x) < 'G')
251 #define isalpha(x) sane_istest(x,GIT_ALPHA)
252 #define isalnum(x) sane_istest(x,GIT_ALPHA | GIT_DIGIT)
253 #define isprint(x) sane_istest(x,GIT_PRINT)
254 #define tolower(x) sane_case((unsigned char)(x), 0x20)
255 #define toupper(x) sane_case((unsigned char)(x), 0)
256 
sane_case(int x,int high)257 static inline int sane_case(int x, int high)
258 {
259 	if (sane_istest(x, GIT_ALPHA))
260 		x = (x & ~0x20) | high;
261 	return x;
262 }
263 
264 int mkdir_p(char *path, mode_t mode);
265 int copyfile(const char *from, const char *to);
266 
267 s64 perf_atoll(const char *str);
268 char **argv_split(const char *str, int *argcp);
269 void argv_free(char **argv);
270 bool strglobmatch(const char *str, const char *pat);
271 bool strlazymatch(const char *str, const char *pat);
272 unsigned long convert_unit(unsigned long value, char *unit);
273 int readn(int fd, void *buf, size_t size);
274 
275 #define _STR(x) #x
276 #define STR(x) _STR(x)
277 
278 #endif
279