]> granicus.if.org Git - postgresql/blob - src/include/c.h
Fix misuse of StrNCpy to copy and add null to non-null-terminated data.
[postgresql] / src / include / c.h
1 /*-------------------------------------------------------------------------
2  *
3  * c.h
4  *        Fundamental C definitions.  This is included by every .c file in
5  *        postgres.
6  *
7  *
8  * Portions Copyright (c) 1996-2000, PostgreSQL, Inc
9  * Portions Copyright (c) 1994, Regents of the University of California
10  *
11  * $Id: c.h,v 1.76 2000/07/07 21:12:47 tgl Exp $
12  *
13  *-------------------------------------------------------------------------
14  */
15 /*
16  *       TABLE OF CONTENTS
17  *
18  *              When adding stuff to this file, please try to put stuff
19  *              into the relevant section, or add new sections as appropriate.
20  *
21  *        section       description
22  *        -------       ------------------------------------------------
23  *              1)              bool, true, false, TRUE, FALSE, NULL
24  *              2)              non-ansi C definitions:
25  *                              type prefixes: const, signed, volatile, inline
26  *                              cpp magic macros
27  *              3)              standard system types
28  *              4)              datum type
29  *              5)              IsValid macros for system types
30  *              6)              offsetof, lengthof, endof
31  *              7)              exception handling definitions, Assert, Trap, etc macros
32  *              8)              Min, Max, Abs, StrNCpy macros
33  *              9)              externs
34  *              10)             Berkeley-specific defs
35  *              11)             system-specific hacks
36  *
37  * ----------------------------------------------------------------
38  */
39 #ifndef C_H
40 #define C_H
41
42 /* We have to include stdlib.h here because it defines many of these macros
43    on some platforms, and we only want our definitions used if stdlib.h doesn't
44    have its own.  The same goes for stddef and stdarg if present.
45 */
46
47 #include "config.h"
48
49
50 #include <stdio.h>
51 #include <stdlib.h>
52 #include <string.h>
53 #include <sys/fcntl.h>
54 #ifdef STDC_HEADERS
55 #include <stddef.h>
56 #include <stdarg.h>
57 #endif
58
59 #ifdef __CYGWIN32__
60 #include <errno.h>
61 #endif
62
63 /* ----------------------------------------------------------------
64  *                              Section 1:      bool, true, false, TRUE, FALSE, NULL
65  * ----------------------------------------------------------------
66  */
67 /*
68  * bool
69  *              Boolean value, either true or false.
70  *
71  */
72 #ifndef __cplusplus
73 #ifndef bool
74 typedef char bool;
75
76 #endif   /* ndef bool */
77 #endif   /* not C++ */
78 #ifndef true
79 #define true    ((bool) 1)
80 #endif
81 #ifndef false
82 #define false   ((bool) 0)
83 #endif
84 typedef bool *BoolPtr;
85
86 #ifndef TRUE
87 #define TRUE    1
88 #endif   /* TRUE */
89
90 #ifndef FALSE
91 #define FALSE   0
92 #endif   /* FALSE */
93
94 /*
95  * NULL
96  *              Null pointer.
97  */
98 #ifndef NULL
99 #define NULL    ((void *) 0)
100 #endif   /* !defined(NULL) */
101
102 /* ----------------------------------------------------------------
103  *                              Section 2: non-ansi C definitions:
104  *
105  *                              type prefixes: const, signed, volatile, inline
106  *                              cpp magic macros
107  * ----------------------------------------------------------------
108  */
109
110 /*
111  * CppAsString
112  *              Convert the argument to a string, using the C preprocessor.
113  * CppConcat
114  *              Concatenate two arguments together, using the C preprocessor.
115  *
116  * Note: the standard Autoconf macro AC_C_STRINGIZE actually only checks
117  * whether #identifier works, but if we have that we likely have ## too.
118  */
119 #if defined(HAVE_STRINGIZE)
120
121 #define CppAsString(identifier) #identifier
122 #define CppConcat(x, y)                 x##y
123
124 #else                                                   /* !HAVE_STRINGIZE */
125
126 #define CppAsString(identifier) "identifier"
127
128 /*
129  * CppIdentity -- On Reiser based cpp's this is used to concatenate
130  *              two tokens.  That is
131  *                              CppIdentity(A)B ==> AB
132  *              We renamed it to _private_CppIdentity because it should not
133  *              be referenced outside this file.  On other cpp's it
134  *              produces  A  B.
135  */
136 #define _priv_CppIdentity(x)x
137 #define CppConcat(x, y)                 _priv_CppIdentity(x)y
138
139 #endif   /* !HAVE_STRINGIZE */
140
141 /*
142  * dummyret is used to set return values in macros that use ?: to make
143  * assignments.  gcc wants these to be void, other compilers like char
144  */
145 #ifdef __GNUC__                                 /* GNU cc */
146 #define dummyret        void
147 #else
148 #define dummyret        char
149 #endif
150
151 /* ----------------------------------------------------------------
152  *                              Section 3:      standard system types
153  * ----------------------------------------------------------------
154  */
155
156 /*
157  * Pointer
158  *              Variable holding address of any memory resident object.
159  *
160  *              XXX Pointer arithmetic is done with this, so it can't be void *
161  *              under "true" ANSI compilers.
162  */
163 typedef char *Pointer;
164
165 /*
166  * intN
167  *              Signed integer, EXACTLY N BITS IN SIZE,
168  *              used for numerical computations and the
169  *              frontend/backend protocol.
170  */
171 typedef signed char int8;               /* == 8 bits */
172 typedef signed short int16;             /* == 16 bits */
173 typedef signed int int32;               /* == 32 bits */
174
175 /*
176  * uintN
177  *              Unsigned integer, EXACTLY N BITS IN SIZE,
178  *              used for numerical computations and the
179  *              frontend/backend protocol.
180  */
181 typedef unsigned char uint8;    /* == 8 bits */
182 typedef unsigned short uint16;  /* == 16 bits */
183 typedef unsigned int uint32;    /* == 32 bits */
184
185 /*
186  * floatN
187  *              Floating point number, AT LEAST N BITS IN SIZE,
188  *              used for numerical computations.
189  *
190  *              Since sizeof(floatN) may be > sizeof(char *), always pass
191  *              floatN by reference.
192  */
193 typedef float float32data;
194 typedef double float64data;
195 typedef float *float32;
196 typedef double *float64;
197
198 /*
199  * boolN
200  *              Boolean value, AT LEAST N BITS IN SIZE.
201  */
202 typedef uint8 bool8;                    /* >= 8 bits */
203 typedef uint16 bool16;                  /* >= 16 bits */
204 typedef uint32 bool32;                  /* >= 32 bits */
205
206 /*
207  * bitsN
208  *              Unit of bitwise operation, AT LEAST N BITS IN SIZE.
209  */
210 typedef uint8 bits8;                    /* >= 8 bits */
211 typedef uint16 bits16;                  /* >= 16 bits */
212 typedef uint32 bits32;                  /* >= 32 bits */
213
214 /*
215  * wordN
216  *              Unit of storage, AT LEAST N BITS IN SIZE,
217  *              used to fetch/store data.
218  */
219 typedef uint8 word8;                    /* >= 8 bits */
220 typedef uint16 word16;                  /* >= 16 bits */
221 typedef uint32 word32;                  /* >= 32 bits */
222
223 /*
224  * Size
225  *              Size of any memory resident object, as returned by sizeof.
226  */
227 typedef size_t Size;
228
229 /*
230  * Index
231  *              Index into any memory resident array.
232  *
233  * Note:
234  *              Indices are non negative.
235  */
236 typedef unsigned int Index;
237
238 #define MAXDIM 6
239 typedef struct
240 {
241         int                     indx[MAXDIM];
242 } IntArray;
243
244 /*
245  * Offset
246  *              Offset into any memory resident array.
247  *
248  * Note:
249  *              This differs from an Index in that an Index is always
250  *              non negative, whereas Offset may be negative.
251  */
252 typedef signed int Offset;
253
254 /*
255  * Common Postgres datatypes.
256  */
257 typedef int16 int2;
258 typedef int32 int4;
259 typedef float float4;
260 typedef double float8;
261
262 #ifdef HAVE_LONG_INT_64
263 /* Plain "long int" fits, use it */
264 typedef long int int64;
265 #else
266 #ifdef HAVE_LONG_LONG_INT_64
267 /* We have working support for "long long int", use that */
268 typedef long long int int64;
269 #else
270 /* Won't actually work, but fall back to long int so that code compiles */
271 typedef long int int64;
272 #define INT64_IS_BUSTED
273 #endif
274 #endif
275
276 /* ----------------------------------------------------------------
277  *                              Section 4:      datum type + support macros
278  * ----------------------------------------------------------------
279  */
280 /*
281  * datum.h
282  *              POSTGRES abstract data type datum representation definitions.
283  *
284  * Note:
285  *
286  * Port Notes:
287  *      Postgres makes the following assumption about machines:
288  *
289  *      sizeof(Datum) == sizeof(long) >= sizeof(void *) >= 4
290  *
291  *      Postgres also assumes that
292  *
293  *      sizeof(char) == 1
294  *
295  *      and that
296  *
297  *      sizeof(short) == 2
298  *
299  *      If your machine meets these requirements, Datums should also be checked
300  *      to see if the positioning is correct.
301  */
302
303 typedef unsigned long Datum;    /* XXX sizeof(long) >= sizeof(void *) */
304 typedef Datum *DatumPtr;
305
306 #define GET_1_BYTE(datum)       (((Datum) (datum)) & 0x000000ff)
307 #define GET_2_BYTES(datum)      (((Datum) (datum)) & 0x0000ffff)
308 #define GET_4_BYTES(datum)      (((Datum) (datum)) & 0xffffffff)
309 #define SET_1_BYTE(value)       (((Datum) (value)) & 0x000000ff)
310 #define SET_2_BYTES(value)      (((Datum) (value)) & 0x0000ffff)
311 #define SET_4_BYTES(value)      (((Datum) (value)) & 0xffffffff)
312
313 /*
314  * DatumGetBool
315  *              Returns boolean value of a datum.
316  *
317  * Note: any nonzero value will be considered TRUE.
318  */
319
320 #define DatumGetBool(X) ((bool) (((Datum) (X)) != 0))
321
322 /*
323  * BoolGetDatum
324  *              Returns datum representation for a boolean.
325  *
326  * Note: any nonzero value will be considered TRUE.
327  */
328
329 #define BoolGetDatum(X) ((Datum) ((X) ? 1 : 0))
330
331 /*
332  * DatumGetChar
333  *              Returns character value of a datum.
334  */
335
336 #define DatumGetChar(X) ((char) GET_1_BYTE(X))
337
338 /*
339  * CharGetDatum
340  *              Returns datum representation for a character.
341  */
342
343 #define CharGetDatum(X) ((Datum) SET_1_BYTE(X))
344
345 /*
346  * Int8GetDatum
347  *              Returns datum representation for an 8-bit integer.
348  */
349
350 #define Int8GetDatum(X) ((Datum) SET_1_BYTE(X))
351
352 /*
353  * DatumGetUInt8
354  *              Returns 8-bit unsigned integer value of a datum.
355  */
356
357 #define DatumGetUInt8(X) ((uint8) GET_1_BYTE(X))
358
359 /*
360  * UInt8GetDatum
361  *              Returns datum representation for an 8-bit unsigned integer.
362  */
363
364 #define UInt8GetDatum(X) ((Datum) SET_1_BYTE(X))
365
366 /*
367  * DatumGetInt16
368  *              Returns 16-bit integer value of a datum.
369  */
370
371 #define DatumGetInt16(X) ((int16) GET_2_BYTES(X))
372
373 /*
374  * Int16GetDatum
375  *              Returns datum representation for a 16-bit integer.
376  */
377
378 #define Int16GetDatum(X) ((Datum) SET_2_BYTES(X))
379
380 /*
381  * DatumGetUInt16
382  *              Returns 16-bit unsigned integer value of a datum.
383  */
384
385 #define DatumGetUInt16(X) ((uint16) GET_2_BYTES(X))
386
387 /*
388  * UInt16GetDatum
389  *              Returns datum representation for a 16-bit unsigned integer.
390  */
391
392 #define UInt16GetDatum(X) ((Datum) SET_2_BYTES(X))
393
394 /*
395  * DatumGetInt32
396  *              Returns 32-bit integer value of a datum.
397  */
398
399 #define DatumGetInt32(X) ((int32) GET_4_BYTES(X))
400
401 /*
402  * Int32GetDatum
403  *              Returns datum representation for a 32-bit integer.
404  */
405
406 #define Int32GetDatum(X) ((Datum) SET_4_BYTES(X))
407
408 /*
409  * DatumGetUInt32
410  *              Returns 32-bit unsigned integer value of a datum.
411  */
412
413 #define DatumGetUInt32(X) ((uint32) GET_4_BYTES(X))
414
415 /*
416  * UInt32GetDatum
417  *              Returns datum representation for a 32-bit unsigned integer.
418  */
419
420 #define UInt32GetDatum(X) ((Datum) SET_4_BYTES(X))
421
422 /*
423  * DatumGetObjectId
424  *              Returns object identifier value of a datum.
425  */
426
427 #define DatumGetObjectId(X) ((Oid) GET_4_BYTES(X))
428
429 /*
430  * ObjectIdGetDatum
431  *              Returns datum representation for an object identifier.
432  */
433
434 #define ObjectIdGetDatum(X) ((Datum) SET_4_BYTES(X))
435
436 /*
437  * DatumGetPointer
438  *              Returns pointer value of a datum.
439  */
440
441 #define DatumGetPointer(X) ((Pointer) (X))
442
443 /*
444  * PointerGetDatum
445  *              Returns datum representation for a pointer.
446  */
447
448 #define PointerGetDatum(X) ((Datum) (X))
449
450 /*
451  * DatumGetCString
452  *              Returns C string (null-terminated string) value of a datum.
453  *
454  * Note: C string is not a full-fledged Postgres type at present,
455  * but type input functions use this conversion for their inputs.
456  */
457
458 #define DatumGetCString(X) ((char *) DatumGetPointer(X))
459
460 /*
461  * CStringGetDatum
462  *              Returns datum representation for a C string (null-terminated string).
463  *
464  * Note: C string is not a full-fledged Postgres type at present,
465  * but type output functions use this conversion for their outputs.
466  * Note: CString is pass-by-reference; caller must ensure the pointed-to
467  * value has adequate lifetime.
468  */
469
470 #define CStringGetDatum(X) PointerGetDatum(X)
471
472 /*
473  * DatumGetName
474  *              Returns name value of a datum.
475  */
476
477 #define DatumGetName(X) ((Name) DatumGetPointer(X))
478
479 /*
480  * NameGetDatum
481  *              Returns datum representation for a name.
482  *
483  * Note: Name is pass-by-reference; caller must ensure the pointed-to
484  * value has adequate lifetime.
485  */
486
487 #define NameGetDatum(X) PointerGetDatum(X)
488
489 /*
490  * DatumGetInt64
491  *              Returns 64-bit integer value of a datum.
492  *
493  * Note: this macro hides the fact that int64 is currently a
494  * pass-by-reference type.  Someday it may be pass-by-value,
495  * at least on some platforms.
496  */
497
498 #define DatumGetInt64(X) (* ((int64 *) DatumGetPointer(X)))
499
500 /*
501  * Int64GetDatum
502  *              Returns datum representation for a 64-bit integer.
503  *
504  * Note: this routine returns a reference to palloc'd space.
505  */
506
507 extern Datum Int64GetDatum(int64 X);
508
509 /*
510  * DatumGetFloat4
511  *              Returns 4-byte floating point value of a datum.
512  *
513  * Note: this macro hides the fact that float4 is currently a
514  * pass-by-reference type.  Someday it may be pass-by-value.
515  */
516
517 #define DatumGetFloat4(X) (* ((float4 *) DatumGetPointer(X)))
518
519 /*
520  * Float4GetDatum
521  *              Returns datum representation for a 4-byte floating point number.
522  *
523  * Note: this routine returns a reference to palloc'd space.
524  */
525
526 extern Datum Float4GetDatum(float4 X);
527
528 /*
529  * DatumGetFloat8
530  *              Returns 8-byte floating point value of a datum.
531  *
532  * Note: this macro hides the fact that float8 is currently a
533  * pass-by-reference type.  Someday it may be pass-by-value,
534  * at least on some platforms.
535  */
536
537 #define DatumGetFloat8(X) (* ((float8 *) DatumGetPointer(X)))
538
539 /*
540  * Float8GetDatum
541  *              Returns datum representation for an 8-byte floating point number.
542  *
543  * Note: this routine returns a reference to palloc'd space.
544  */
545
546 extern Datum Float8GetDatum(float8 X);
547
548
549 /*
550  * DatumGetFloat32
551  *              Returns 32-bit floating point value of a datum.
552  *              This is really a pointer, of course.
553  *
554  * XXX: this macro is now deprecated in favor of DatumGetFloat4.
555  * It will eventually go away.
556  */
557
558 #define DatumGetFloat32(X) ((float32) DatumGetPointer(X))
559
560 /*
561  * Float32GetDatum
562  *              Returns datum representation for a 32-bit floating point number.
563  *              This is really a pointer, of course.
564  *
565  * XXX: this macro is now deprecated in favor of Float4GetDatum.
566  * It will eventually go away.
567  */
568
569 #define Float32GetDatum(X) PointerGetDatum(X)
570
571 /*
572  * DatumGetFloat64
573  *              Returns 64-bit floating point value of a datum.
574  *              This is really a pointer, of course.
575  *
576  * XXX: this macro is now deprecated in favor of DatumGetFloat8.
577  * It will eventually go away.
578  */
579
580 #define DatumGetFloat64(X) ((float64) DatumGetPointer(X))
581
582 /*
583  * Float64GetDatum
584  *              Returns datum representation for a 64-bit floating point number.
585  *              This is really a pointer, of course.
586  *
587  * XXX: this macro is now deprecated in favor of Float8GetDatum.
588  * It will eventually go away.
589  */
590
591 #define Float64GetDatum(X) PointerGetDatum(X)
592
593 /* ----------------------------------------------------------------
594  *                              Section 5:      IsValid macros for system types
595  * ----------------------------------------------------------------
596  */
597 /*
598  * BoolIsValid
599  *              True iff bool is valid.
600  */
601 #define BoolIsValid(boolean)    ((boolean) == false || (boolean) == true)
602
603 /*
604  * PointerIsValid
605  *              True iff pointer is valid.
606  */
607 #define PointerIsValid(pointer) ((void*)(pointer) != NULL)
608
609 /*
610  * PointerIsAligned
611  *              True iff pointer is properly aligned to point to the given type.
612  */
613 #define PointerIsAligned(pointer, type) \
614                 (((long)(pointer) % (sizeof (type))) == 0)
615
616 /* ----------------------------------------------------------------
617  *                              Section 6:      offsetof, lengthof, endof
618  * ----------------------------------------------------------------
619  */
620 /*
621  * offsetof
622  *              Offset of a structure/union field within that structure/union.
623  *
624  *              XXX This is supposed to be part of stddef.h, but isn't on
625  *              some systems (like SunOS 4).
626  */
627 #ifndef offsetof
628 #define offsetof(type, field)   ((long) &((type *)0)->field)
629 #endif   /* offsetof */
630
631 /*
632  * lengthof
633  *              Number of elements in an array.
634  */
635 #define lengthof(array) (sizeof (array) / sizeof ((array)[0]))
636
637 /*
638  * endof
639  *              Address of the element one past the last in an array.
640  */
641 #define endof(array)    (&array[lengthof(array)])
642
643 /* ----------------------------------------------------------------
644  *                              Section 7:      exception handling definitions
645  *                                                      Assert, Trap, etc macros
646  * ----------------------------------------------------------------
647  */
648 /*
649  * Exception Handling definitions
650  */
651
652 typedef char *ExcMessage;
653 typedef struct Exception
654 {
655         ExcMessage      message;
656 } Exception;
657
658 /*
659  * USE_ASSERT_CHECKING, if defined, turns on all the assertions.
660  * - plai  9/5/90
661  *
662  * It should _NOT_ be defined in releases or in benchmark copies
663  */
664
665 /*
666  * Trap
667  *              Generates an exception if the given condition is true.
668  *
669  */
670 #define Trap(condition, exception) \
671                 do { \
672                         if ((assert_enabled) && (condition)) \
673                                 ExceptionalCondition(CppAsString(condition), &(exception), \
674                                                 (char*)NULL, __FILE__, __LINE__); \
675                 } while (0)
676
677 /*
678  *      TrapMacro is the same as Trap but it's intended for use in macros:
679  *
680  *              #define foo(x) (AssertM(x != 0) && bar(x))
681  *
682  *      Isn't CPP fun?
683  */
684 #define TrapMacro(condition, exception) \
685         ((bool) ((! assert_enabled) || ! (condition) || \
686                          (ExceptionalCondition(CppAsString(condition), \
687                                                                   &(exception), \
688                                                                   (char*) NULL, __FILE__, __LINE__))))
689
690 #ifndef USE_ASSERT_CHECKING
691 #define Assert(condition)
692 #define AssertMacro(condition)  ((void)true)
693 #define AssertArg(condition)
694 #define AssertState(condition)
695 #define assert_enabled 0
696 #else
697 #define Assert(condition) \
698                 Trap(!(condition), FailedAssertion)
699
700 #define AssertMacro(condition) \
701                 ((void) TrapMacro(!(condition), FailedAssertion))
702
703 #define AssertArg(condition) \
704                 Trap(!(condition), BadArg)
705
706 #define AssertState(condition) \
707                 Trap(!(condition), BadState)
708
709 extern int      assert_enabled;
710
711 #endif   /* USE_ASSERT_CHECKING */
712
713 /*
714  * LogTrap
715  *              Generates an exception with a message if the given condition is true.
716  *
717  */
718 #define LogTrap(condition, exception, printArgs) \
719                 do { \
720                         if ((assert_enabled) && (condition)) \
721                                 ExceptionalCondition(CppAsString(condition), &(exception), \
722                                                 vararg_format printArgs, __FILE__, __LINE__); \
723                 } while (0)
724
725 /*
726  *      LogTrapMacro is the same as LogTrap but it's intended for use in macros:
727  *
728  *              #define foo(x) (LogAssertMacro(x != 0, "yow!") && bar(x))
729  */
730 #define LogTrapMacro(condition, exception, printArgs) \
731         ((bool) ((! assert_enabled) || ! (condition) || \
732                          (ExceptionalCondition(CppAsString(condition), \
733                                                                    &(exception), \
734                                                                    vararg_format printArgs, __FILE__, __LINE__))))
735
736 #ifndef USE_ASSERT_CHECKING
737 #define LogAssert(condition, printArgs)
738 #define LogAssertMacro(condition, printArgs) true
739 #define LogAssertArg(condition, printArgs)
740 #define LogAssertState(condition, printArgs)
741 #else
742 #define LogAssert(condition, printArgs) \
743                 LogTrap(!(condition), FailedAssertion, printArgs)
744
745 #define LogAssertMacro(condition, printArgs) \
746                 LogTrapMacro(!(condition), FailedAssertion, printArgs)
747
748 #define LogAssertArg(condition, printArgs) \
749                 LogTrap(!(condition), BadArg, printArgs)
750
751 #define LogAssertState(condition, printArgs) \
752                 LogTrap(!(condition), BadState, printArgs)
753
754 #ifdef ASSERT_CHECKING_TEST
755 extern int      assertTest(int val);
756
757 #endif
758 #endif   /* USE_ASSERT_CHECKING */
759
760 /* ----------------------------------------------------------------
761  *                              Section 8:      Min, Max, Abs macros
762  * ----------------------------------------------------------------
763  */
764 /*
765  * Max
766  *              Return the maximum of two numbers.
767  */
768 #define Max(x, y)               ((x) > (y) ? (x) : (y))
769
770 /*
771  * Min
772  *              Return the minimum of two numbers.
773  */
774 #define Min(x, y)               ((x) < (y) ? (x) : (y))
775
776 /*
777  * Abs
778  *              Return the absolute value of the argument.
779  */
780 #define Abs(x)                  ((x) >= 0 ? (x) : -(x))
781
782 /*
783  * StrNCpy
784  *      Like standard library function strncpy(), except that result string
785  *      is guaranteed to be null-terminated --- that is, at most N-1 bytes
786  *      of the source string will be kept.
787  *      Also, the macro returns no result (too hard to do that without
788  *      evaluating the arguments multiple times, which seems worse).
789  *
790  *      BTW: when you need to copy a non-null-terminated string (like a text
791  *      datum) and add a null, do not do it with StrNCpy(..., len+1).  That
792  *      might seem to work, but it fetches one byte more than there is in the
793  *      text object.  One fine day you'll have a SIGSEGV because there isn't
794  *      another byte before the end of memory.  Don't laugh, we've had real
795  *      live bug reports from real live users over exactly this mistake.
796  *      Do it honestly with "memcpy(dst,src,len); dst[len] = '\0';", instead.
797  */
798 #define StrNCpy(dst,src,len) \
799         do \
800         { \
801                 char * _dst = (dst); \
802                 Size _len = (len); \
803 \
804                 if (_len > 0) \
805                 { \
806                         strncpy(_dst, (src), _len); \
807                         _dst[_len-1] = '\0'; \
808                 } \
809         } while (0)
810
811
812 /* Get a bit mask of the bits set in non-int32 aligned addresses */
813 #define INT_ALIGN_MASK (sizeof(int32) - 1)
814
815 /*
816  * MemSet
817  *      Exactly the same as standard library function memset(), but considerably
818  *      faster for zeroing small word-aligned structures (such as parsetree nodes).
819  *      This has to be a macro because the main point is to avoid function-call
820  *      overhead.
821  *
822  *      We got the 64 number by testing this against the stock memset() on
823  *      BSD/OS 3.0. Larger values were slower.  bjm 1997/09/11
824  *
825  *      I think the crossover point could be a good deal higher for
826  *      most platforms, actually.  tgl 2000-03-19
827  */
828 #define MemSet(start, val, len) \
829         do \
830         { \
831                 int32 * _start = (int32 *) (start); \
832                 int             _val = (val); \
833                 Size    _len = (len); \
834 \
835                 if ((((long) _start) & INT_ALIGN_MASK) == 0 && \
836                         (_len & INT_ALIGN_MASK) == 0 && \
837                         _val == 0 && \
838                         _len <= MEMSET_LOOP_LIMIT) \
839                 { \
840                         int32 * _stop = (int32 *) ((char *) _start + _len); \
841                         while (_start < _stop) \
842                                 *_start++ = 0; \
843                 } \
844                 else \
845                         memset((char *) _start, _val, _len); \
846         } while (0)
847
848 #define MEMSET_LOOP_LIMIT  64
849
850
851 /* ----------------------------------------------------------------
852  *                              Section 9: externs
853  * ----------------------------------------------------------------
854  */
855
856 extern Exception FailedAssertion;
857 extern Exception BadArg;
858 extern Exception BadState;
859
860 /* in utils/error/assert.c */
861 extern int ExceptionalCondition(char *conditionName,
862                                          Exception *exceptionP, char *details,
863                                          char *fileName, int lineNumber);
864
865
866 /* ----------------
867  *              vararg_format is used by assert and the exception handling stuff
868  * ----------------
869  */
870 extern char *vararg_format(const char *fmt,...);
871
872
873
874 /* ----------------------------------------------------------------
875  *                              Section 10: berkeley-specific configuration
876  *
877  * this section contains settings which are only relevant to the UC Berkeley
878  * sites.  Other sites can ignore this
879  * ----------------------------------------------------------------
880  */
881
882 /* ----------------
883  *              storage managers
884  *
885  *              These are experimental and are not supported in the code that
886  *              we distribute to other sites.
887  * ----------------
888  */
889 #ifdef NOT_USED
890 #define STABLE_MEMORY_STORAGE
891 #endif
892
893
894
895 /* ----------------------------------------------------------------
896  *                              Section 11: system-specific hacks
897  *
898  *              This should be limited to things that absolutely have to be
899  *              included in every source file.  The changes should be factored
900  *              into a separate file so that changes to one port don't require
901  *              changes to c.h (and everyone recompiling their whole system).
902  * ----------------------------------------------------------------
903  */
904
905 #ifdef __CYGWIN32__
906 #define PG_BINARY       O_BINARY
907 #define PG_BINARY_R     "rb"
908 #define PG_BINARY_W     "wb"
909 #else
910 #define PG_BINARY       0
911 #define PG_BINARY_R     "r"
912 #define PG_BINARY_W     "w"
913 #endif
914
915 #if defined(sun) && defined(__sparc__) && !defined(__SVR4)
916 #define memmove(d, s, l)                bcopy(s, d, l)
917 #include <unistd.h>
918 #include <varargs.h>
919 #endif
920
921 /* These are for things that are one way on Unix and another on NT */
922 #define NULL_DEV                "/dev/null"
923 #define SEP_CHAR                '/'
924
925 /* defines for dynamic linking on Win32 platform */
926 #ifdef __CYGWIN32__
927 #if __GNUC__ && ! defined (__declspec)
928 #error You need egcs 1.1 or newer for compiling!
929 #endif
930 #ifdef BUILDING_DLL
931 #define DLLIMPORT __declspec (dllexport)
932 #else                                                   /* not BUILDING_DLL */
933 #define DLLIMPORT __declspec (dllimport)
934 #endif
935 #else                                                   /* not CYGWIN */
936 #define DLLIMPORT
937 #endif
938
939 /* Provide prototypes for routines not present in a particular machine's
940  * standard C library.  It'd be better to put these in config.h, but
941  * in config.h we haven't yet included anything that defines size_t...
942  */
943
944 #ifndef HAVE_SNPRINTF_DECL
945 extern int      snprintf(char *str, size_t count, const char *fmt,...);
946
947 #endif
948
949 #ifndef HAVE_VSNPRINTF_DECL
950 extern int      vsnprintf(char *str, size_t count, const char *fmt, va_list args);
951
952 #endif
953
954 #ifndef HAVE_MEMMOVE
955 #include <regex/utils.h>
956 #endif
957
958 /* ----------------
959  *              end of c.h
960  * ----------------
961  */
962 #endif   /* C_H */