]> git.ipfire.org Git - thirdparty/git.git/blame - git-compat-util.h
git-status: use ls-files --others --directory for untracked list.
[thirdparty/git.git] / git-compat-util.h
CommitLineData
4050c0df
JH
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 */
29extern void usage(const char *err) NORETURN;
30extern void die(const char *err, ...) NORETURN __attribute__((format (printf, 1, 2)));
31extern 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
44extern void *gitfakemmap(void *start, size_t length, int prot , int flags, int fd, off_t offset);
45extern 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
55extern int gitsetenv(const char *, const char *, int);
56#endif
57
58#ifdef NO_STRCASESTR
59#define strcasestr gitstrcasestr
60extern char *gitstrcasestr(const char *haystack, const char *needle);
61#endif
62
63static inline void *xmalloc(size_t size)
64{
65 void *ret = malloc(size);
4e7a2ecc
JH
66 if (!ret && !size)
67 ret = malloc(1);
4050c0df
JH
68 if (!ret)
69 die("Out of memory, malloc failed");
70 return ret;
71}
72
73static inline void *xrealloc(void *ptr, size_t size)
74{
75 void *ret = realloc(ptr, size);
4e7a2ecc
JH
76 if (!ret && !size)
77 ret = realloc(ptr, 1);
4050c0df
JH
78 if (!ret)
79 die("Out of memory, realloc failed");
80 return ret;
81}
82
83static inline void *xcalloc(size_t nmemb, size_t size)
84{
85 void *ret = calloc(nmemb, size);
4e7a2ecc
JH
86 if (!ret && (!nmemb || !size))
87 ret = calloc(1, 1);
4050c0df
JH
88 if (!ret)
89 die("Out of memory, calloc failed");
90 return ret;
91}
92
1c15afb9
JH
93static inline ssize_t xread(int fd, void *buf, size_t len)
94{
95 ssize_t nr;
96 while (1) {
97 nr = read(fd, buf, len);
98 if ((nr < 0) && (errno == EAGAIN || errno == EINTR))
99 continue;
100 return nr;
101 }
102}
103
104static inline ssize_t xwrite(int fd, const void *buf, size_t len)
105{
106 ssize_t nr;
107 while (1) {
108 nr = write(fd, buf, len);
109 if ((nr < 0) && (errno == EAGAIN || errno == EINTR))
110 continue;
111 return nr;
112 }
113}
114
4050c0df
JH
115/* Sane ctype - no locale, and works with signed chars */
116#undef isspace
117#undef isdigit
118#undef isalpha
119#undef isalnum
120#undef tolower
121#undef toupper
122extern unsigned char sane_ctype[256];
123#define GIT_SPACE 0x01
124#define GIT_DIGIT 0x02
125#define GIT_ALPHA 0x04
126#define sane_istest(x,mask) ((sane_ctype[(unsigned char)(x)] & (mask)) != 0)
127#define isspace(x) sane_istest(x,GIT_SPACE)
128#define isdigit(x) sane_istest(x,GIT_DIGIT)
129#define isalpha(x) sane_istest(x,GIT_ALPHA)
130#define isalnum(x) sane_istest(x,GIT_ALPHA | GIT_DIGIT)
131#define tolower(x) sane_case((unsigned char)(x), 0x20)
132#define toupper(x) sane_case((unsigned char)(x), 0)
133
134static inline int sane_case(int x, int high)
135{
136 if (sane_istest(x, GIT_ALPHA))
137 x = (x & ~0x20) | high;
138 return x;
139}
140
252fef71
MA
141#ifndef MAXPATHLEN
142#define MAXPATHLEN 256
143#endif
4050c0df 144#endif