summaryrefslogtreecommitdiff
path: root/src/compat.h
blob: 65a0c8eb15da49fc753cafa14c58d35a8c3f789b (plain)
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
/*
 * AUTHOR
 * N. Nielsen
 *
 * LICENSE
 * This software is in the public domain.
 *
 * The software is provided "as is", without warranty of any kind,
 * express or implied, including but not limited to the warranties
 * of merchantability, fitness for a particular purpose, and
 * noninfringement. In no event shall the author(s) be liable for any
 * claim, damages, or other liability, whether in an action of
 * contract, tort, or otherwise, arising from, out of, or in connection
 * with the software or the use or other dealings in the software.
 *
 * SUPPORT
 * Send bug reports to: <nielsen@memberwebs.com>
 */

#ifndef _COMPAT_H_
#define _COMPAT_H_

/* Force use of win32 configuration if compiling there */
#ifdef _WIN32
#include "config.win32.h"
#else
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#endif

#include <sys/types.h>

#ifndef MAX_PATH
#ifdef _MAX_PATH
#define MAX_PATH _MAX_PATH
#else
#define MAX_PATH 256
#endif
#endif

#ifndef HAVE_STDARG_H
#error ERROR: Must have a working stdarg.h header
#else
#include <stdarg.h>
#endif

#ifndef HAVE_GETCWD
#ifdef _WIN32
#include <direct.h>
#define getcwd _getcwd
#define HAVE_GETCWD
#else
#error ERROR: Must have a working 'getcwd' function
#endif
#endif

#ifndef HAVE_CHDIR
#ifdef _WIN32
#include <direct.h>
#define chdir _chdir
#define HAVE_CHDIR
#else
#error ERROR: Must have a working 'chdir' function
#endif
#endif

#ifndef NULL
#define NULL	(void*)0
#endif

#ifndef HAVE_BOOL
typedef unsigned char bool;
#define false	0x00
#define true	0x01
#endif

#ifndef HAVE_BYTE
typedef unsigned char byte;
#endif

#ifndef HAVE_UINT
typedef unsigned int uint;
#endif

#ifdef HAVE_STDINT_H
#include <stdint.h>
#endif

#ifndef HAVE_UINT64
  #ifdef HAVE_UINT64_T
    typedef uint64_t uint64;
  #else
    #ifdef _WIN32
      typedef unsigned __int64 uint64;
    #else
      #error ERROR: Must have a compiler that can handle 64 bit integers
    #endif
  #endif
#endif

#ifndef HAVE_INT64
  #ifdef HAVE_INT64_T
    typedef int64_t int64;
  #else
    #ifdef _WIN32
      typedef signed __int64 int64;
    #else
      #error ERROR: Must have a compiler that can handle 64 bit integers
    #endif
  #endif
#endif

#ifndef HAVE_UINT32
  #ifdef HAVE_UINT32_T
    typedef uint32_t uint32;
  #else
    #ifdef _WIN32
      typedef unsigned __int32 uint32;
    #else
      #error ERROR: Couldnt determine a 32 bit integer
    #endif
  #endif
#endif

#ifndef HAVE_INT32
  #ifdef HAVE_INT32_T
    typedef int32_t int32;
  #else
    #ifdef _WIN32
      typedef signed __int32 int32;
    #else
      #error ERROR: Couldnt determine a 32 bit integer
    #endif
  #endif
#endif

#ifndef HAVE_UINT16
  #ifdef HAVE_UINT16_T
    typedef uint16_t uint16;
  #else
    #ifdef _WIN32
      typedef unsigned __int16 uint16;
    #else
      #error ERROR: Couldnt determine a 16 bit integer
    #endif
  #endif
#endif

#ifndef HAVE_INT16
  #ifdef HAVE_INT16_T
    typedef int16_t int16;
  #else
    #ifdef _WIN32
      typedef signed __int16 int16;
    #else
      #error ERROR: Couldnt determine a 16 bit integer
    #endif
  #endif
#endif


#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif

#ifndef HAVE_GETOPT
extern char* optarg;
extern int optind, opterr, optopt;
int getopt(int nargc, char* const* nargv, const char* ostr);
#endif

#ifndef HAVE_ERR_H
#include <stdarg.h>
void err_set_file(void *fp);
void err_set_exit(void (*ef)(int));
void err(int eval, const char *fmt, ...);
void verr(int eval, const char *fmt, va_list ap);
void errc(int eval, int code, const char *fmt, ...);
void verrc(int eval, int code, const char *fmt, va_list ap);
void errx(int eval, const char *fmt, ...);
void verrx(int eval, const char *fmt, va_list ap);
void warn(const char *fmt, ...);
void vwarn(const char *fmt, va_list ap);
void warnc(int code, const char *fmt, ...);
void vwarnc(int code, const char *fmt, va_list ap);
void warnx(const char *fmt, ...);
void vwarnx(const char *fmt, va_list ap);
#endif

#ifndef HAVE_REALLOCF
void* reallocf(void* p, size_t sz);
#endif

#ifndef HAVE_MALLOCF
void* mallocf(size_t sz);
#endif

/* Some number conversion stuff */
#include <wchar.h>

#ifndef HAVE_ITOW
  #ifdef _WIN32
    #define itow _itow
    #define HAVE_ITOW 1
  #else
    wchar_t* itow(int v, wchar_t* s, int r);
  #endif
#endif

#ifndef HAVE_ITOA
  #ifdef _WIN32
    #define itoa _itoa
    #define HAVE_ITOA 1
  #else
    char* itoa(int v, char* s, int r);
  #endif
#endif

/*
 * Depending on the OS we use different width characters
 * for file names and file access. Before enabling wide
 * file access for an OS the printf needs to be able to
 * handle wide chars (ie: %S) and there should be wide
 * char file access functions
 */

#ifdef _WIN32

  #if SIZEOF_WHCHAR_T != 2
    #error Incompatible size of wchar_t
  #endif

  /* On windows we use UCS2 */
  typedef wchar_t fchar_t;
  #define FC_WIDE 1
  #define FC_PRINTF "%S"
#else
  /* Everywhere else we use UTF-8 */
  typedef char fchar_t;
  #undef FC_WIDE
  #define FC_PRINTF "%s"
#endif

#ifdef FC_WIDE

  /* An OS that handles wide char file access */

  #ifdef HAVE_WOPEN
    #define fc_open wopen
  #else
    #ifdef _WIN32
      #define fc_open _wopen
    #else
      #error Set for wide file access, but no wide open
    #endif
  #endif

  #ifdef HAVE_WCHDIR
    #define fc_chdir wchdir
  #else
    #ifdef _WIN32
      #define fc_chdir _wchdir
    #else
      #error Set for wide file access but no wide chdir
    #endif
  #endif

  #ifdef HAVE_WMKDIR
    #define fc_mkdir wmkdir
  #else
    #ifdef _WIN32
      #define fc_mkdir _wmkdir
    #else
      #error Set for wide file access but no wide mkdir
    #endif
  #endif

  #ifdef HAVE_WGETCWD
    #define fc_getcwd wgetcwd
  #else
    #ifdef _WIN32
      #define fc_getcwd _wgetcwd
    #else
      #error Set for wide file access but no wide getcwd
    #endif
  #endif

  #define fcscpy wcscpy
  #define fcscat wcscat
  #define fcsncpy wcsncpy
  #define fcslen wcslen
  #define fcscmp wcscmp
  #define itofc itow

  #define FC_DOT L"."

#else

  /* OSs without wide char file access */

  #define fc_open open
  #define fc_chdir chdir
  #define fc_mkdir mkdir
  #define fc_getcwd getcwd

  #define fcscpy strcpy
  #define fcsncpy strncpy
  #define fcscat strcat
  #define fcslen strlen
  #define fcscmp strcmp
  #define itofc itoa

  #define FC_DOT "."

#endif



/* 64 bit file handling stuff */

#ifndef HAVE_LSEEK64
  #ifdef _WIN32
    #define lseek64 _lseeki64
  #else
    #if SIZEOF_OFF_T == 8
      #define lseek64 lseek
    #else
      #error ERROR: Must have a working 64 bit seek function
    #endif
  #endif
#endif

#include <fcntl.h>
#ifdef O_LARGEFILE
  #define OPEN_LARGE_OPTS O_LARGEFILE
#else
  #define OPEN_LARGE_OPTS 0
#endif

#ifndef O_BINARY
  #ifdef _O_BINARY
    #define O_BINARY _O_BINARY
  #else
    #define O_BINARY 0
  #endif
#endif

#ifndef O_RDONLY
  #ifdef _O_RDONLY
    #define O_RDONLY _O_RDONLY
  #else
    #error ERROR: No O_RDONLY flag found
  #endif
#endif


#endif /* _COMPAT_H_ */