]> git.ipfire.org Git - thirdparty/git.git/blame - git-compat-util.h
repo-config: Fix late-night bug
[thirdparty/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
43#ifdef NO_MMAP
44
45#ifndef PROT_READ
46#define PROT_READ 1
47#define PROT_WRITE 2
48#define MAP_PRIVATE 1
49#define MAP_FAILED ((void*)-1)
50#endif
51
52#define mmap gitfakemmap
53#define munmap gitfakemunmap
54extern void *gitfakemmap(void *start, size_t length, int prot , int flags, int fd, off_t offset);
55extern int gitfakemunmap(void *start, size_t length);
56
57#else /* NO_MMAP */
58
59#include <sys/mman.h>
60
61#endif /* NO_MMAP */
62
63#ifdef NO_SETENV
64#define setenv gitsetenv
65extern int gitsetenv(const char *, const char *, int);
66#endif
67
731043fd
JR
68#ifdef NO_UNSETENV
69#define unsetenv gitunsetenv
70extern void gitunsetenv(const char *);
71#endif
72
4050c0df
JH
73#ifdef NO_STRCASESTR
74#define strcasestr gitstrcasestr
75extern char *gitstrcasestr(const char *haystack, const char *needle);
76#endif
77
78static inline void *xmalloc(size_t size)
79{
80 void *ret = malloc(size);
4e7a2ecc
JH
81 if (!ret && !size)
82 ret = malloc(1);
4050c0df
JH
83 if (!ret)
84 die("Out of memory, malloc failed");
85 return ret;
86}
87
88static inline void *xrealloc(void *ptr, size_t size)
89{
90 void *ret = realloc(ptr, size);
4e7a2ecc
JH
91 if (!ret && !size)
92 ret = realloc(ptr, 1);
4050c0df
JH
93 if (!ret)
94 die("Out of memory, realloc failed");
95 return ret;
96}
97
98static inline void *xcalloc(size_t nmemb, size_t size)
99{
100 void *ret = calloc(nmemb, size);
4e7a2ecc
JH
101 if (!ret && (!nmemb || !size))
102 ret = calloc(1, 1);
4050c0df
JH
103 if (!ret)
104 die("Out of memory, calloc failed");
105 return ret;
106}
107
1c15afb9
JH
108static inline ssize_t xread(int fd, void *buf, size_t len)
109{
110 ssize_t nr;
111 while (1) {
112 nr = read(fd, buf, len);
113 if ((nr < 0) && (errno == EAGAIN || errno == EINTR))
114 continue;
115 return nr;
116 }
117}
118
119static inline ssize_t xwrite(int fd, const void *buf, size_t len)
120{
121 ssize_t nr;
122 while (1) {
123 nr = write(fd, buf, len);
124 if ((nr < 0) && (errno == EAGAIN || errno == EINTR))
125 continue;
126 return nr;
127 }
128}
129
4050c0df
JH
130/* Sane ctype - no locale, and works with signed chars */
131#undef isspace
132#undef isdigit
133#undef isalpha
134#undef isalnum
135#undef tolower
136#undef toupper
137extern unsigned char sane_ctype[256];
138#define GIT_SPACE 0x01
139#define GIT_DIGIT 0x02
140#define GIT_ALPHA 0x04
141#define sane_istest(x,mask) ((sane_ctype[(unsigned char)(x)] & (mask)) != 0)
142#define isspace(x) sane_istest(x,GIT_SPACE)
143#define isdigit(x) sane_istest(x,GIT_DIGIT)
144#define isalpha(x) sane_istest(x,GIT_ALPHA)
145#define isalnum(x) sane_istest(x,GIT_ALPHA | GIT_DIGIT)
146#define tolower(x) sane_case((unsigned char)(x), 0x20)
147#define toupper(x) sane_case((unsigned char)(x), 0)
148
149static inline int sane_case(int x, int high)
150{
151 if (sane_istest(x, GIT_ALPHA))
152 x = (x & ~0x20) | high;
153 return x;
154}
155
252fef71
MA
156#ifndef MAXPATHLEN
157#define MAXPATHLEN 256
158#endif
4050c0df 159#endif