]> granicus.if.org Git - postgresql/blob - src/include/c.h
Move WIN32_ONLY_COMPILER define from c.h to win32.h because it was being
[postgresql] / src / include / c.h
1 /*-------------------------------------------------------------------------
2  *
3  * c.h
4  *        Fundamental C definitions.  This is included by every .c file in
5  *        PostgreSQL (via either postgres.h or postgres_fe.h, as appropriate).
6  *
7  *        Note that the definitions here are not intended to be exposed to clients
8  *        of the frontend interface libraries --- so we don't worry much about
9  *        polluting the namespace with lots of stuff...
10  *
11  *
12  * Portions Copyright (c) 1996-2006, PostgreSQL Global Development Group
13  * Portions Copyright (c) 1994, Regents of the University of California
14  *
15  * $PostgreSQL: pgsql/src/include/c.h,v 1.212 2006/10/03 03:59:22 momjian Exp $
16  *
17  *-------------------------------------------------------------------------
18  */
19 /*
20  *----------------------------------------------------------------
21  *       TABLE OF CONTENTS
22  *
23  *              When adding stuff to this file, please try to put stuff
24  *              into the relevant section, or add new sections as appropriate.
25  *
26  *        section       description
27  *        -------       ------------------------------------------------
28  *              0)              pg_config.h and standard system headers
29  *              1)              hacks to cope with non-ANSI C compilers
30  *              2)              bool, true, false, TRUE, FALSE, NULL
31  *              3)              standard system types
32  *              4)              IsValid macros for system types
33  *              5)              offsetof, lengthof, endof, alignment
34  *              6)              widely useful macros
35  *              7)              random stuff
36  *              8)              system-specific hacks
37  *
38  * NOTE: since this file is included by both frontend and backend modules, it's
39  * almost certainly wrong to put an "extern" declaration here.  typedefs and
40  * macros are the kind of thing that might go here.
41  *
42  *----------------------------------------------------------------
43  */
44 #ifndef C_H
45 #define C_H
46
47 /*
48  * We have to include stdlib.h here because it defines many of these macros
49  * on some platforms, and we only want our definitions used if stdlib.h doesn't
50  * have its own.  The same goes for stddef and stdarg if present.
51  */
52
53 #include "pg_config.h"
54 #include "pg_config_manual.h"   /* must be after pg_config.h */
55 #if !defined(WIN32) && !defined(__CYGWIN__) /* win32 will include further down */
56 #include "pg_config_os.h"               /* must be before any system header files */
57 #endif
58 #include "postgres_ext.h"
59 #include "pg_trace.h"
60
61 #if defined(__BORLANDC__) || (_MSC_VER > 1400)
62 #include <crtdefs.h>
63 #endif
64
65 #include <stdio.h>
66 #include <stdlib.h>
67 #include <string.h>
68 #include <stddef.h>
69 #include <stdarg.h>
70 #ifdef HAVE_STRINGS_H
71 #include <strings.h>
72 #endif
73 #include <sys/types.h>
74
75 #include <errno.h>
76 #if defined(WIN32) || defined(__CYGWIN__)
77 #include <fcntl.h>                              /* ensure O_BINARY is available */
78 #endif
79 #ifdef HAVE_SUPPORTDEFS_H
80 #include <SupportDefs.h>
81 #endif
82
83 #if defined(WIN32) || defined(__CYGWIN__)
84 /* We have to redefine some system functions after they are included above. */
85 #include "pg_config_os.h"
86 #endif
87
88 /* Must be before gettext() games below */
89 #include <locale.h>
90
91 #define _(x) gettext((x))
92
93 #ifdef ENABLE_NLS
94 #include <libintl.h>
95 #else
96 #define gettext(x) (x)
97 #endif
98
99 /*
100  *      Use this to mark strings to be translated by gettext, in places where
101  *      you don't want an actual function call to occur (eg, constant tables).
102  */
103 #define gettext_noop(x) (x)
104
105
106 /* ----------------------------------------------------------------
107  *                              Section 1: hacks to cope with non-ANSI C compilers
108  *
109  * type prefixes (const, signed, volatile, inline) are handled in pg_config.h.
110  * ----------------------------------------------------------------
111  */
112
113 /*
114  * CppAsString
115  *              Convert the argument to a string, using the C preprocessor.
116  * CppConcat
117  *              Concatenate two arguments together, using the C preprocessor.
118  *
119  * Note: the standard Autoconf macro AC_C_STRINGIZE actually only checks
120  * whether #identifier works, but if we have that we likely have ## too.
121  */
122 #if defined(HAVE_STRINGIZE)
123
124 #define CppAsString(identifier) #identifier
125 #define CppConcat(x, y)                 x##y
126 #else                                                   /* !HAVE_STRINGIZE */
127
128 #define CppAsString(identifier) "identifier"
129
130 /*
131  * CppIdentity -- On Reiser based cpp's this is used to concatenate
132  *              two tokens.  That is
133  *                              CppIdentity(A)B ==> AB
134  *              We renamed it to _private_CppIdentity because it should not
135  *              be referenced outside this file.  On other cpp's it
136  *              produces  A  B.
137  */
138 #define _priv_CppIdentity(x)x
139 #define CppConcat(x, y)                 _priv_CppIdentity(x)y
140 #endif   /* !HAVE_STRINGIZE */
141
142 /*
143  * dummyret is used to set return values in macros that use ?: to make
144  * assignments.  gcc wants these to be void, other compilers like char
145  */
146 #ifdef __GNUC__                                 /* GNU cc */
147 #define dummyret        void
148 #else
149 #define dummyret        char
150 #endif
151
152 #ifndef __GNUC__
153 #define __attribute__(_arg_)
154 #endif
155
156 /* ----------------------------------------------------------------
157  *                              Section 2:      bool, true, false, TRUE, FALSE, NULL
158  * ----------------------------------------------------------------
159  */
160
161 /*
162  * bool
163  *              Boolean value, either true or false.
164  *
165  * XXX for C++ compilers, we assume the compiler has a compatible
166  * built-in definition of bool.
167  */
168
169 #ifndef __cplusplus
170
171 #ifndef bool
172 typedef char bool;
173 #endif
174
175 #ifndef true
176 #define true    ((bool) 1)
177 #endif
178
179 #ifndef false
180 #define false   ((bool) 0)
181 #endif
182 #endif   /* not C++ */
183
184 typedef bool *BoolPtr;
185
186 #ifndef TRUE
187 #define TRUE    1
188 #endif
189
190 #ifndef FALSE
191 #define FALSE   0
192 #endif
193
194 /*
195  * NULL
196  *              Null pointer.
197  */
198 #ifndef NULL
199 #define NULL    ((void *) 0)
200 #endif
201
202
203 /* ----------------------------------------------------------------
204  *                              Section 3:      standard system types
205  * ----------------------------------------------------------------
206  */
207
208 /*
209  * Pointer
210  *              Variable holding address of any memory resident object.
211  *
212  *              XXX Pointer arithmetic is done with this, so it can't be void *
213  *              under "true" ANSI compilers.
214  */
215 typedef char *Pointer;
216
217 /*
218  * intN
219  *              Signed integer, EXACTLY N BITS IN SIZE,
220  *              used for numerical computations and the
221  *              frontend/backend protocol.
222  */
223 #ifndef HAVE_INT8
224 typedef signed char int8;               /* == 8 bits */
225 typedef signed short int16;             /* == 16 bits */
226 typedef signed int int32;               /* == 32 bits */
227 #endif   /* not HAVE_INT8 */
228
229 /*
230  * uintN
231  *              Unsigned integer, EXACTLY N BITS IN SIZE,
232  *              used for numerical computations and the
233  *              frontend/backend protocol.
234  */
235 #ifndef HAVE_UINT8
236 typedef unsigned char uint8;    /* == 8 bits */
237 typedef unsigned short uint16;  /* == 16 bits */
238 typedef unsigned int uint32;    /* == 32 bits */
239 #endif   /* not HAVE_UINT8 */
240
241 /*
242  * bitsN
243  *              Unit of bitwise operation, AT LEAST N BITS IN SIZE.
244  */
245 typedef uint8 bits8;                    /* >= 8 bits */
246 typedef uint16 bits16;                  /* >= 16 bits */
247 typedef uint32 bits32;                  /* >= 32 bits */
248
249 /*
250  * floatN
251  *              Floating point number, AT LEAST N BITS IN SIZE,
252  *              used for numerical computations.
253  *
254  *              Since sizeof(floatN) may be > sizeof(char *), always pass
255  *              floatN by reference.
256  *
257  * XXX: these typedefs are now deprecated in favor of float4 and float8.
258  * They will eventually go away.
259  */
260 typedef float float32data;
261 typedef double float64data;
262 typedef float *float32;
263 typedef double *float64;
264
265 /*
266  * 64-bit integers
267  */
268 #ifdef HAVE_LONG_INT_64
269 /* Plain "long int" fits, use it */
270
271 #ifndef HAVE_INT64
272 typedef long int int64;
273 #endif
274 #ifndef HAVE_UINT64
275 typedef unsigned long int uint64;
276 #endif
277 #elif defined(HAVE_LONG_LONG_INT_64)
278 /* We have working support for "long long int", use that */
279
280 #ifndef HAVE_INT64
281 typedef long long int int64;
282 #endif
283 #ifndef HAVE_UINT64
284 typedef unsigned long long int uint64;
285 #endif
286 #else                                                   /* not HAVE_LONG_INT_64 and not
287                                                                  * HAVE_LONG_LONG_INT_64 */
288
289 /* Won't actually work, but fall back to long int so that code compiles */
290 #ifndef HAVE_INT64
291 typedef long int int64;
292 #endif
293 #ifndef HAVE_UINT64
294 typedef unsigned long int uint64;
295 #endif
296
297 #define INT64_IS_BUSTED
298 #endif   /* not HAVE_LONG_INT_64 and not
299                                                                  * HAVE_LONG_LONG_INT_64 */
300
301 /* Decide if we need to decorate 64-bit constants */
302 #ifdef HAVE_LL_CONSTANTS
303 #define INT64CONST(x)  ((int64) x##LL)
304 #define UINT64CONST(x) ((uint64) x##ULL)
305 #else
306 #define INT64CONST(x)  ((int64) x)
307 #define UINT64CONST(x) ((uint64) x)
308 #endif
309
310
311 /* Select timestamp representation (float8 or int64) */
312 #if defined(USE_INTEGER_DATETIMES) && !defined(INT64_IS_BUSTED)
313 #define HAVE_INT64_TIMESTAMP
314 #endif
315
316 /* sig_atomic_t is required by ANSI C, but may be missing on old platforms */
317 #ifndef HAVE_SIG_ATOMIC_T
318 typedef int sig_atomic_t;
319 #endif
320
321 /*
322  * Size
323  *              Size of any memory resident object, as returned by sizeof.
324  */
325 typedef size_t Size;
326
327 /*
328  * Index
329  *              Index into any memory resident array.
330  *
331  * Note:
332  *              Indices are non negative.
333  */
334 typedef unsigned int Index;
335
336 /*
337  * Offset
338  *              Offset into any memory resident array.
339  *
340  * Note:
341  *              This differs from an Index in that an Index is always
342  *              non negative, whereas Offset may be negative.
343  */
344 typedef signed int Offset;
345
346 /*
347  * Common Postgres datatype names (as used in the catalogs)
348  */
349 typedef int16 int2;
350 typedef int32 int4;
351 typedef float float4;
352 typedef double float8;
353
354 /*
355  * Oid, RegProcedure, TransactionId, SubTransactionId, MultiXactId,
356  * CommandId
357  */
358
359 /* typedef Oid is in postgres_ext.h */
360
361 /*
362  * regproc is the type name used in the include/catalog headers, but
363  * RegProcedure is the preferred name in C code.
364  */
365 typedef Oid regproc;
366 typedef regproc RegProcedure;
367
368 typedef uint32 TransactionId;
369
370 typedef uint32 SubTransactionId;
371
372 #define InvalidSubTransactionId         ((SubTransactionId) 0)
373 #define TopSubTransactionId                     ((SubTransactionId) 1)
374
375 /* MultiXactId must be equivalent to TransactionId, to fit in t_xmax */
376 typedef TransactionId MultiXactId;
377
378 typedef uint32 MultiXactOffset;
379
380 typedef uint32 CommandId;
381
382 #define FirstCommandId  ((CommandId) 0)
383
384 /*
385  * Array indexing support
386  */
387 #define MAXDIM 6
388 typedef struct
389 {
390         int                     indx[MAXDIM];
391 } IntArray;
392
393 /* ----------------
394  *              Variable-length datatypes all share the 'struct varlena' header.
395  *
396  * NOTE: for TOASTable types, this is an oversimplification, since the value
397  * may be compressed or moved out-of-line.      However datatype-specific routines
398  * are mostly content to deal with de-TOASTed values only, and of course
399  * client-side routines should never see a TOASTed value.  See postgres.h for
400  * details of the TOASTed form.
401  * ----------------
402  */
403 struct varlena
404 {
405         int32           vl_len;
406         char            vl_dat[1];
407 };
408
409 #define VARHDRSZ                ((int32) sizeof(int32))
410
411 /*
412  * These widely-used datatypes are just a varlena header and the data bytes.
413  * There is no terminating null or anything like that --- the data length is
414  * always VARSIZE(ptr) - VARHDRSZ.
415  */
416 typedef struct varlena bytea;
417 typedef struct varlena text;
418 typedef struct varlena BpChar;  /* blank-padded char, ie SQL char(n) */
419 typedef struct varlena VarChar; /* var-length char, ie SQL varchar(n) */
420
421 /*
422  * Specialized array types.  These are physically laid out just the same
423  * as regular arrays (so that the regular array subscripting code works
424  * with them).  They exist as distinct types mostly for historical reasons:
425  * they have nonstandard I/O behavior which we don't want to change for fear
426  * of breaking applications that look at the system catalogs.  There is also
427  * an implementation issue for oidvector: it's part of the primary key for
428  * pg_proc, and we can't use the normal btree array support routines for that
429  * without circularity.
430  */
431 typedef struct
432 {
433         int32           size;                   /* these fields must match ArrayType! */
434         int                     ndim;                   /* always 1 for int2vector */
435         int32           dataoffset;             /* always 0 for int2vector */
436         Oid                     elemtype;
437         int                     dim1;
438         int                     lbound1;
439         int2            values[1];              /* VARIABLE LENGTH ARRAY */
440 } int2vector;                                   /* VARIABLE LENGTH STRUCT */
441
442 typedef struct
443 {
444         int32           size;                   /* these fields must match ArrayType! */
445         int                     ndim;                   /* always 1 for oidvector */
446         int32           dataoffset;             /* always 0 for oidvector */
447         Oid                     elemtype;
448         int                     dim1;
449         int                     lbound1;
450         Oid                     values[1];              /* VARIABLE LENGTH ARRAY */
451 } oidvector;                                    /* VARIABLE LENGTH STRUCT */
452
453 /*
454  * We want NameData to have length NAMEDATALEN and int alignment,
455  * because that's how the data type 'name' is defined in pg_type.
456  * Use a union to make sure the compiler agrees.  Note that NAMEDATALEN
457  * must be a multiple of sizeof(int), else sizeof(NameData) will probably
458  * not come out equal to NAMEDATALEN.
459  */
460 typedef union nameData
461 {
462         char            data[NAMEDATALEN];
463         int                     alignmentDummy;
464 } NameData;
465 typedef NameData *Name;
466
467 #define NameStr(name)   ((name).data)
468
469 /*
470  * Support macros for escaping strings.  escape_backslash should be TRUE
471  * if generating a non-standard-conforming string.  Prefixing a string
472  * with ESCAPE_STRING_SYNTAX guarantees it is non-standard-conforming.
473  * Beware of multiple evaluation of the "ch" argument!
474  */
475 #define SQL_STR_DOUBLE(ch, escape_backslash)    \
476         ((ch) == '\'' || ((ch) == '\\' && (escape_backslash)))
477
478 #define ESCAPE_STRING_SYNTAX    'E'
479
480 /* ----------------------------------------------------------------
481  *                              Section 4:      IsValid macros for system types
482  * ----------------------------------------------------------------
483  */
484 /*
485  * BoolIsValid
486  *              True iff bool is valid.
487  */
488 #define BoolIsValid(boolean)    ((boolean) == false || (boolean) == true)
489
490 /*
491  * PointerIsValid
492  *              True iff pointer is valid.
493  */
494 #define PointerIsValid(pointer) ((void*)(pointer) != NULL)
495
496 /*
497  * PointerIsAligned
498  *              True iff pointer is properly aligned to point to the given type.
499  */
500 #define PointerIsAligned(pointer, type) \
501                 (((long)(pointer) % (sizeof (type))) == 0)
502
503 #define OidIsValid(objectId)  ((bool) ((objectId) != InvalidOid))
504
505 #define RegProcedureIsValid(p)  OidIsValid(p)
506
507
508 /* ----------------------------------------------------------------
509  *                              Section 5:      offsetof, lengthof, endof, alignment
510  * ----------------------------------------------------------------
511  */
512 /*
513  * offsetof
514  *              Offset of a structure/union field within that structure/union.
515  *
516  *              XXX This is supposed to be part of stddef.h, but isn't on
517  *              some systems (like SunOS 4).
518  */
519 #ifndef offsetof
520 #define offsetof(type, field)   ((long) &((type *)0)->field)
521 #endif   /* offsetof */
522
523 /*
524  * lengthof
525  *              Number of elements in an array.
526  */
527 #define lengthof(array) (sizeof (array) / sizeof ((array)[0]))
528
529 /*
530  * endof
531  *              Address of the element one past the last in an array.
532  */
533 #define endof(array)    (&(array)[lengthof(array)])
534
535 /* ----------------
536  * Alignment macros: align a length or address appropriately for a given type.
537  *
538  * There used to be some incredibly crufty platform-dependent hackery here,
539  * but now we rely on the configure script to get the info for us. Much nicer.
540  *
541  * NOTE: TYPEALIGN will not work if ALIGNVAL is not a power of 2.
542  * That case seems extremely unlikely to occur in practice, however.
543  * ----------------
544  */
545
546 #define TYPEALIGN(ALIGNVAL,LEN)  \
547         (((long) (LEN) + ((ALIGNVAL) - 1)) & ~((long) ((ALIGNVAL) - 1)))
548
549 #define SHORTALIGN(LEN)                 TYPEALIGN(ALIGNOF_SHORT, (LEN))
550 #define INTALIGN(LEN)                   TYPEALIGN(ALIGNOF_INT, (LEN))
551 #define LONGALIGN(LEN)                  TYPEALIGN(ALIGNOF_LONG, (LEN))
552 #define DOUBLEALIGN(LEN)                TYPEALIGN(ALIGNOF_DOUBLE, (LEN))
553 #define MAXALIGN(LEN)                   TYPEALIGN(MAXIMUM_ALIGNOF, (LEN))
554 /* MAXALIGN covers only built-in types, not buffers */
555 #define BUFFERALIGN(LEN)                TYPEALIGN(ALIGNOF_BUFFER, (LEN))
556
557
558 /* ----------------------------------------------------------------
559  *                              Section 6:      widely useful macros
560  * ----------------------------------------------------------------
561  */
562 /*
563  * Max
564  *              Return the maximum of two numbers.
565  */
566 #define Max(x, y)               ((x) > (y) ? (x) : (y))
567
568 /*
569  * Min
570  *              Return the minimum of two numbers.
571  */
572 #define Min(x, y)               ((x) < (y) ? (x) : (y))
573
574 /*
575  * Abs
576  *              Return the absolute value of the argument.
577  */
578 #define Abs(x)                  ((x) >= 0 ? (x) : -(x))
579
580 /*
581  * StrNCpy
582  *      Like standard library function strncpy(), except that result string
583  *      is guaranteed to be null-terminated --- that is, at most N-1 bytes
584  *      of the source string will be kept.
585  *      Also, the macro returns no result (too hard to do that without
586  *      evaluating the arguments multiple times, which seems worse).
587  *
588  *      BTW: when you need to copy a non-null-terminated string (like a text
589  *      datum) and add a null, do not do it with StrNCpy(..., len+1).  That
590  *      might seem to work, but it fetches one byte more than there is in the
591  *      text object.  One fine day you'll have a SIGSEGV because there isn't
592  *      another byte before the end of memory.  Don't laugh, we've had real
593  *      live bug reports from real live users over exactly this mistake.
594  *      Do it honestly with "memcpy(dst,src,len); dst[len] = '\0';", instead.
595  */
596 #define StrNCpy(dst,src,len) \
597         do \
598         { \
599                 char * _dst = (dst); \
600                 Size _len = (len); \
601 \
602                 if (_len > 0) \
603                 { \
604                         strncpy(_dst, (src), _len); \
605                         _dst[_len-1] = '\0'; \
606                 } \
607         } while (0)
608
609
610 /* Get a bit mask of the bits set in non-long aligned addresses */
611 #define LONG_ALIGN_MASK (sizeof(long) - 1)
612
613 /*
614  * MemSet
615  *      Exactly the same as standard library function memset(), but considerably
616  *      faster for zeroing small word-aligned structures (such as parsetree nodes).
617  *      This has to be a macro because the main point is to avoid function-call
618  *      overhead.       However, we have also found that the loop is faster than
619  *      native libc memset() on some platforms, even those with assembler
620  *      memset() functions.  More research needs to be done, perhaps with
621  *      MEMSET_LOOP_LIMIT tests in configure.
622  */
623 #define MemSet(start, val, len) \
624         do \
625         { \
626                 /* must be void* because we don't know if it is integer aligned yet */ \
627                 void   *_vstart = (void *) (start); \
628                 int             _val = (val); \
629                 Size    _len = (len); \
630 \
631                 if ((((long) _vstart) & LONG_ALIGN_MASK) == 0 && \
632                         (_len & LONG_ALIGN_MASK) == 0 && \
633                         _val == 0 && \
634                         _len <= MEMSET_LOOP_LIMIT && \
635                         /* \
636                          *      If MEMSET_LOOP_LIMIT == 0, optimizer should find \
637                          *      the whole "if" false at compile time. \
638                          */ \
639                         MEMSET_LOOP_LIMIT != 0) \
640                 { \
641                         long *_start = (long *) _vstart; \
642                         long *_stop = (long *) ((char *) _start + _len); \
643                         while (_start < _stop) \
644                                 *_start++ = 0; \
645                 } \
646                 else \
647                         memset(_vstart, _val, _len); \
648         } while (0)
649
650 /*
651  * MemSetAligned is the same as MemSet except it omits the test to see if
652  * "start" is word-aligned.  This is okay to use if the caller knows a-priori
653  * that the pointer is suitably aligned (typically, because he just got it
654  * from palloc(), which always delivers a max-aligned pointer).
655  */
656 #define MemSetAligned(start, val, len) \
657         do \
658         { \
659                 long   *_start = (long *) (start); \
660                 int             _val = (val); \
661                 Size    _len = (len); \
662 \
663                 if ((_len & LONG_ALIGN_MASK) == 0 && \
664                         _val == 0 && \
665                         _len <= MEMSET_LOOP_LIMIT && \
666                         MEMSET_LOOP_LIMIT != 0) \
667                 { \
668                         long *_stop = (long *) ((char *) _start + _len); \
669                         while (_start < _stop) \
670                                 *_start++ = 0; \
671                 } \
672                 else \
673                         memset(_start, _val, _len); \
674         } while (0)
675
676
677 /*
678  * MemSetTest/MemSetLoop are a variant version that allow all the tests in
679  * MemSet to be done at compile time in cases where "val" and "len" are
680  * constants *and* we know the "start" pointer must be word-aligned.
681  * If MemSetTest succeeds, then it is okay to use MemSetLoop, otherwise use
682  * MemSetAligned.  Beware of multiple evaluations of the arguments when using
683  * this approach.
684  */
685 #define MemSetTest(val, len) \
686         ( ((len) & LONG_ALIGN_MASK) == 0 && \
687         (len) <= MEMSET_LOOP_LIMIT && \
688         MEMSET_LOOP_LIMIT != 0 && \
689         (val) == 0 )
690
691 #define MemSetLoop(start, val, len) \
692         do \
693         { \
694                 long * _start = (long *) (start); \
695                 long * _stop = (long *) ((char *) _start + (Size) (len)); \
696         \
697                 while (_start < _stop) \
698                         *_start++ = 0; \
699         } while (0)
700
701
702 /* ----------------------------------------------------------------
703  *                              Section 7:      random stuff
704  * ----------------------------------------------------------------
705  */
706
707 /* msb for char */
708 #define HIGHBIT                                 (0x80)
709 #define IS_HIGHBIT_SET(ch)              ((unsigned char)(ch) & HIGHBIT)
710
711 #define STATUS_OK                               (0)
712 #define STATUS_ERROR                    (-1)
713 #define STATUS_EOF                              (-2)
714 #define STATUS_FOUND                    (1)
715 #define STATUS_WAITING                  (2)
716
717
718 /* ----------------------------------------------------------------
719  *                              Section 8: system-specific hacks
720  *
721  *              This should be limited to things that absolutely have to be
722  *              included in every source file.  The port-specific header file
723  *              is usually a better place for this sort of thing.
724  * ----------------------------------------------------------------
725  */
726
727 /*
728  *      NOTE:  this is also used for opening text files.
729  *      WIN32 treats Control-Z as EOF in files opened in text mode.
730  *      Therefore, we open files in binary mode on Win32 so we can read
731  *      literal control-Z.      The other affect is that we see CRLF, but
732  *      that is OK because we can already handle those cleanly.
733  */
734 #if defined(WIN32) || defined(__CYGWIN__)
735 #define PG_BINARY       O_BINARY
736 #define PG_BINARY_R "rb"
737 #define PG_BINARY_W "wb"
738 #else
739 #define PG_BINARY       0
740 #define PG_BINARY_R "r"
741 #define PG_BINARY_W "w"
742 #endif
743
744 #if defined(sun) && defined(__sparc__) && !defined(__SVR4)
745 #include <unistd.h>
746 #endif
747
748 /* These are for things that are one way on Unix and another on NT */
749 #define NULL_DEV                "/dev/null"
750
751 /*
752  * Provide prototypes for routines not present in a particular machine's
753  * standard C library.
754  */
755
756 #if !HAVE_DECL_SNPRINTF
757 extern int
758 snprintf(char *str, size_t count, const char *fmt,...)
759 /* This extension allows gcc to check the format string */
760 __attribute__((format(printf, 3, 4)));
761 #endif
762
763 #if !HAVE_DECL_VSNPRINTF
764 extern int      vsnprintf(char *str, size_t count, const char *fmt, va_list args);
765 #endif
766
767 #if !defined(HAVE_MEMMOVE) && !defined(memmove)
768 #define memmove(d, s, c)                bcopy(s, d, c)
769 #endif
770
771 #ifndef DLLIMPORT
772 #define DLLIMPORT                               /* no special DLL markers on most ports */
773 #endif
774
775 /*
776  * The following is used as the arg list for signal handlers.  Any ports
777  * that take something other than an int argument should override this in
778  * their pg_config_os.h file.  Note that variable names are required
779  * because it is used in both the prototypes as well as the definitions.
780  * Note also the long name.  We expect that this won't collide with
781  * other names causing compiler warnings.
782  */
783
784 #ifndef SIGNAL_ARGS
785 #define SIGNAL_ARGS  int postgres_signal_arg
786 #endif
787
788 /*
789  * When there is no sigsetjmp, its functionality is provided by plain
790  * setjmp. Incidentally, nothing provides setjmp's functionality in
791  * that case.
792  */
793 #ifndef HAVE_SIGSETJMP
794 #define sigjmp_buf jmp_buf
795 #define sigsetjmp(x,y) setjmp(x)
796 #define siglongjmp longjmp
797 #endif
798
799 #if defined(HAVE_FDATASYNC) && !HAVE_DECL_FDATASYNC
800 extern int      fdatasync(int fildes);
801 #endif
802
803 /* If strtoq() exists, rename it to the more standard strtoll() */
804 #if defined(HAVE_LONG_LONG_INT_64) && !defined(HAVE_STRTOLL) && defined(HAVE_STRTOQ)
805 #define strtoll strtoq
806 #define HAVE_STRTOLL 1
807 #endif
808
809 /* If strtouq() exists, rename it to the more standard strtoull() */
810 #if defined(HAVE_LONG_LONG_INT_64) && !defined(HAVE_STRTOULL) && defined(HAVE_STRTOUQ)
811 #define strtoull strtouq
812 #define HAVE_STRTOULL 1
813 #endif
814
815 /* EXEC_BACKEND defines */
816 #ifdef EXEC_BACKEND
817 #define NON_EXEC_STATIC
818 #else
819 #define NON_EXEC_STATIC static
820 #endif
821
822 /* /port compatibility functions */
823 #include "port.h"
824
825 #endif   /* C_H */