-
Notifications
You must be signed in to change notification settings - Fork 26
/
stdenv.h
327 lines (258 loc) · 5.99 KB
/
stdenv.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/* stdenv.h -- set up an environment we can use ($Revision: 1.3 $) */
#include "esconfig.h"
#ifdef HAVE_SYS_CDEFS_H
# include <sys/cdefs.h>
#endif
/*
* protect the rest of es source from the dance of the includes
*/
#if HAVE_UNISTD_H
#include <unistd.h>
#endif
#if REQUIRE_PARAM
#include <sys/param.h>
#endif
#include <string.h>
#include <stddef.h>
#if HAVE_MEMORY_H
#include <memory.h>
#endif
#if HAVE_STDARG_H
#include <stdarg.h>
#else
#include <varargs.h>
#endif
#include <errno.h>
#include <setjmp.h>
#include <signal.h>
#include <ctype.h>
/* #if REQUIRE_STAT || REQUIRE_IOCTL */
/* We need sys/types.h for the prototype of gid_t on Linux */
#include <sys/types.h>
/* #endif */
#if REQUIRE_IOCTL
#include <sys/ioctl.h>
#endif
#if REQUIRE_STAT
#include <sys/stat.h>
#endif
#if REQUIRE_DIRENT
#if HAVE_DIRENT_H
#include <dirent.h>
typedef struct dirent Dirent;
#else
#include <sys/dir.h>
typedef struct direct Dirent;
#endif
/* prototypes for XXXdir functions. comment out if necessary */
#if !HPUX
extern DIR *opendir(const char *);
extern Dirent *readdir(DIR *);
/*extern int closedir(DIR *);*/
#endif
#endif
#if REQUIRE_PWD
#include <pwd.h>
#endif
#if REQUIRE_FCNTL
#include <fcntl.h>
#endif
/* stdlib */
#ifndef Noreturn
#if __GNUC__
#define Noreturn __attribute__((__noreturn__)) void
#else
#define Noreturn void
#endif
#endif
#ifndef UNUSED
#if __GNUC__
#define UNUSED __attribute__((__unused__))
#else
#define UNUSED
#endif
#endif
#ifndef FALLTHROUGH
#if __GNUC__
#define FALLTHROUGH __attribute__((__fallthrough__))
#else
#define FALLTHROUGH (void)0
#endif
#endif
#if STDC_HEADERS
# include <stdlib.h>
#else
extern Noreturn exit(int);
extern Noreturn abort(void);
extern long strtol(const char *num, char **end, int base);
extern void *qsort(
void *base, size_t nmemb, size_t size,
int (*compar)(const void *, const void *)
);
#endif /* !STDC_HEADERS */
#if HAVE_READLINE
# include <stdio.h>
#endif
#include <sys/wait.h>
#include <time.h>
/*
* things that should be defined by header files but might not have been
*/
#ifndef offsetof
#define offsetof(t, m) ((size_t) (((char *) &((t *) 0)->m) - (char *)0))
#endif
#ifndef EOF
#define EOF (-1)
#endif
/* setjmp */
#if defined sigsetjmp || HAVE_SIGSETJMP
/* under linux, sigsetjmp and setjmp are both macros
* -- need to undef setjmp to avoid problems
*/
# ifdef setjmp
# undef setjmp
# endif
# define setjmp(buf) sigsetjmp(buf,1)
# define longjmp(x,y) siglongjmp(x,y)
# define jmp_buf sigjmp_buf
#endif
/*
* macros
*/
#define streq(s, t) (strcmp(s, t) == 0)
#define strneq(s, t, n) (strncmp(s, t, n) == 0)
#define hasprefix(s, p) strneq(s, p, (sizeof p) - 1)
#define arraysize(a) ((int) (sizeof (a) / sizeof (*a)))
#define memzero(dest, count) memset(dest, 0, count)
#define atoi(s) strtol(s, NULL, 0)
#define STMT(stmt) do { stmt; } while (0)
#define NOP do {} while (0)
#if REISER_CPP
#define CONCAT(a,b) a/**/b
#define STRING(s) "s"
#else
#define CONCAT(a,b) a ## b
#define STRING(s) #s
#endif
/*
* types we use throughout es
*/
#undef FALSE
#undef TRUE
typedef enum { FALSE, TRUE } Boolean;
#if USE_SIG_ATOMIC_T
typedef volatile sig_atomic_t Atomic;
#else
typedef volatile int Atomic;
#endif
typedef GETGROUPS_T gidset_t;
/*
* variable argument lists
*/
#if HAVE_STDARG_H
#define VARARGS , ...
#define VARARGS1(t1, v1) (t1 v1, ...)
#define VARARGS2(t1, v1, t2, v2) (t1 v1, t2 v2, ...)
#define VA_START(ap, v) va_start(ap, v)
#else /* !HAVE_STDARG_H */
#define VARARGS
#define VARARGS1(t1, v1) (v1, va_alist) t1 v1; va_dcl
#define VARARGS2(t1, v1, t2, v2) (v1, v2, va_alist) t1 v1; t2 v2; va_dcl
#define VA_START(ap, var) va_start(ap)
/* __va_* are defined by the compiler */
#define va_start(ap) __va_start(ap)
#define va_copy(dest, src) __va_copy(dest, src)
#define va_end(ap) __va_end(ap)
#endif
/*
* assertion checking
*/
#if ASSERTIONS
#define assert(expr) \
STMT( \
if (!(expr)) { \
eprint("%s:%d: assertion failed (%s)\n", \
__FILE__, __LINE__, STRING(expr)); \
abort(); \
} \
)
#else
#define assert(ignore) NOP
#endif
enum { UNREACHABLE = 0 };
#define NOTREACHED STMT(assert(UNREACHABLE))
/*
* system calls -- can we get these from some standard header uniformly?
*/
#if !HAVE_UNISTD_H
extern int chdir(const char *dirname);
extern int close(int fd);
extern int dup(int fd);
extern int dup2(int srcfd, int dstfd);
extern int execve(char *name, char **argv, char **envp);
extern int fork(void);
extern int getegid(void);
extern int geteuid(void);
extern int getpagesize(void);
extern int getpid(void);
extern int pipe(int p[2]);
extern int read(int fd, void *buf, size_t n);
extern int setpgrp(int pid, int pgrp);
extern int umask(int mask);
extern int write(int fd, const void *buf, size_t n);
#if REQUIRE_IOCTL
extern int ioctl(int fd, int cmd, void *arg);
#endif
#if REQUIRE_STAT
extern int stat(const char *, struct stat *);
#endif
#ifdef NGROUPS
extern int getgroups(int, int *);
#endif
#endif /* !HAVE_UNISTD_H */
/*
* hacks to present a standard system call interface
*/
#ifdef HAVE_SETSID
# define setpgrp(a, b) setsid()
#else
#if defined(linux) || defined(__GLIBC__)
#include "unistd.h"
#define setpgrp(a, b) setpgid(a, b)
#endif
#if sgi
#define setpgrp(a, b) BSDsetpgrp(a,b)
#endif
#if HPUX
#define setpgrp(a, b) setpgrp()
#endif
#endif
#if !HAVE_LSTAT
#define lstat stat
#endif
/*
* macros for picking apart statuses
* in general systems should have these macros defined, so this
* should all be a bunch of no-ops. the only interesting case is
* WCOREDUMP, which was only very recently standardized and is
* still spelled by some systems as WIFCORED.
*/
#ifndef WIFSIGNALED
# define WIFSIGNALED(status) (((status) & 0xff) != 0)
#endif
#ifndef WTERMSIG
# define WTERMSIG(status) ((status) & 0x7f)
#endif
#ifndef WCOREDUMP
# ifdef WIFCORED
# define WCOREDUMP(status) (WIFCORED(status))
# else
# define WCOREDUMP(status) ((status) & 0x80)
# endif
#endif
#ifndef WIFEXITED
# define WIFEXITED(status) (!WIFSIGNALED(status))
#endif
#ifndef WEXITSTATUS
# define WEXITSTATUS(status) (((status) >> 8) & 0xff)
#endif