[PATCH] Compilation: zero-length array declaration.
[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
4050c0df
JH
12#include <unistd.h>
13#include <stdio.h>
14#include <sys/stat.h>
15#include <fcntl.h>
16#include <stddef.h>
17#include <stdlib.h>
18#include <stdarg.h>
19#include <string.h>
20#include <errno.h>
21#include <limits.h>
22#include <sys/param.h>
23#include <netinet/in.h>
24#include <sys/types.h>
25#include <dirent.h>
26
27#ifdef __GNUC__
28#define NORETURN __attribute__((__noreturn__))
29#else
30#define NORETURN
31#ifndef __attribute__
32#define __attribute__(x)
33#endif
34#endif
35
36/* General helper functions */
37extern void usage(const char *err) NORETURN;
38extern void die(const char *err, ...) NORETURN __attribute__((format (printf, 1, 2)));
39extern int error(const char *err, ...) __attribute__((format (printf, 1, 2)));
40
41#ifdef NO_MMAP
42
43#ifndef PROT_READ
44#define PROT_READ 1
45#define PROT_WRITE 2
46#define MAP_PRIVATE 1
47#define MAP_FAILED ((void*)-1)
48#endif
49
50#define mmap gitfakemmap
51#define munmap gitfakemunmap
52extern void *gitfakemmap(void *start, size_t length, int prot , int flags, int fd, off_t offset);
53extern int gitfakemunmap(void *start, size_t length);
54
55#else /* NO_MMAP */
56
57#include <sys/mman.h>
58
59#endif /* NO_MMAP */
60
61#ifdef NO_SETENV
62#define setenv gitsetenv
63extern int gitsetenv(const char *, const char *, int);
64#endif
65
66#ifdef NO_STRCASESTR
67#define strcasestr gitstrcasestr
68extern char *gitstrcasestr(const char *haystack, const char *needle);
69#endif
70
71static inline void *xmalloc(size_t size)
72{
73 void *ret = malloc(size);
4e7a2ecc
JH
74 if (!ret && !size)
75 ret = malloc(1);
4050c0df
JH
76 if (!ret)
77 die("Out of memory, malloc failed");
78 return ret;
79}
80
81static inline void *xrealloc(void *ptr, size_t size)
82{
83 void *ret = realloc(ptr, size);
4e7a2ecc
JH
84 if (!ret && !size)
85 ret = realloc(ptr, 1);
4050c0df
JH
86 if (!ret)
87 die("Out of memory, realloc failed");
88 return ret;
89}
90
91static inline void *xcalloc(size_t nmemb, size_t size)
92{
93 void *ret = calloc(nmemb, size);
4e7a2ecc
JH
94 if (!ret && (!nmemb || !size))
95 ret = calloc(1, 1);
4050c0df
JH
96 if (!ret)
97 die("Out of memory, calloc failed");
98 return ret;
99}
100
1c15afb9
JH
101static inline ssize_t xread(int fd, void *buf, size_t len)
102{
103 ssize_t nr;
104 while (1) {
105 nr = read(fd, buf, len);
106 if ((nr < 0) && (errno == EAGAIN || errno == EINTR))
107 continue;
108 return nr;
109 }
110}
111
112static inline ssize_t xwrite(int fd, const void *buf, size_t len)
113{
114 ssize_t nr;
115 while (1) {
116 nr = write(fd, buf, len);
117 if ((nr < 0) && (errno == EAGAIN || errno == EINTR))
118 continue;
119 return nr;
120 }
121}
122
4050c0df
JH
123/* Sane ctype - no locale, and works with signed chars */
124#undef isspace
125#undef isdigit
126#undef isalpha
127#undef isalnum
128#undef tolower
129#undef toupper
130extern unsigned char sane_ctype[256];
131#define GIT_SPACE 0x01
132#define GIT_DIGIT 0x02
133#define GIT_ALPHA 0x04
134#define sane_istest(x,mask) ((sane_ctype[(unsigned char)(x)] & (mask)) != 0)
135#define isspace(x) sane_istest(x,GIT_SPACE)
136#define isdigit(x) sane_istest(x,GIT_DIGIT)
137#define isalpha(x) sane_istest(x,GIT_ALPHA)
138#define isalnum(x) sane_istest(x,GIT_ALPHA | GIT_DIGIT)
139#define tolower(x) sane_case((unsigned char)(x), 0x20)
140#define toupper(x) sane_case((unsigned char)(x), 0)
141
142static inline int sane_case(int x, int high)
143{
144 if (sane_istest(x, GIT_ALPHA))
145 x = (x & ~0x20) | high;
146 return x;
147}
148
252fef71
MA
149#ifndef MAXPATHLEN
150#define MAXPATHLEN 256
151#endif
4050c0df 152#endif