]> eyrie.org Git - kerberos/krb5-strength.git/blob - portable/system.h
Declare fast forward from 3.1-2
[kerberos/krb5-strength.git] / portable / system.h
1 /*
2  * Standard system includes and portability adjustments.
3  *
4  * Declarations of routines and variables in the C library.  Including this
5  * file is the equivalent of including all of the following headers,
6  * portably:
7  *
8  *     #include <inttypes.h>
9  *     #include <limits.h>
10  *     #include <stdarg.h>
11  *     #include <stdbool.h>
12  *     #include <stddef.h>
13  *     #include <stdio.h>
14  *     #include <stdlib.h>
15  *     #include <stdint.h>
16  *     #include <string.h>
17  *     #include <strings.h>
18  *     #include <sys/types.h>
19  *     #include <unistd.h>
20  *
21  * Missing functions are provided via #define or prototyped if available from
22  * the portable helper library.  Also provides some standard #defines.
23  *
24  * The canonical version of this file is maintained in the rra-c-util package,
25  * which can be found at <https://www.eyrie.org/~eagle/software/rra-c-util/>.
26  *
27  * Written by Russ Allbery <eagle@eyrie.org>
28  * Copyright 2014, 2016, 2018, 2020 Russ Allbery <eagle@eyrie.org>
29  * Copyright 2006-2011, 2013-2014
30  *     The Board of Trustees of the Leland Stanford Junior University
31  *
32  * Copying and distribution of this file, with or without modification, are
33  * permitted in any medium without royalty provided the copyright notice and
34  * this notice are preserved.  This file is offered as-is, without any
35  * warranty.
36  *
37  * SPDX-License-Identifier: FSFAP
38  */
39
40 #ifndef PORTABLE_SYSTEM_H
41 #define PORTABLE_SYSTEM_H 1
42
43 /* Make sure we have our configuration information. */
44 #include <config.h>
45
46 /* BEGIN_DECL and __attribute__. */
47 #include <portable/macros.h>
48
49 /* A set of standard ANSI C headers.  We don't care about pre-ANSI systems. */
50 #if HAVE_INTTYPES_H
51 #    include <inttypes.h>
52 #endif
53 #include <limits.h>
54 #include <stdarg.h>
55 #include <stddef.h>
56 #if HAVE_STDINT_H
57 #    include <stdint.h>
58 #endif
59 #include <stdio.h>
60 #include <stdlib.h>
61 #include <string.h>
62 #if HAVE_STRINGS_H
63 #    include <strings.h>
64 #endif
65 #include <sys/types.h>
66 #if HAVE_UNISTD_H
67 #    include <unistd.h>
68 #endif
69
70 /* SCO OpenServer gets int32_t from here. */
71 #if HAVE_SYS_BITYPES_H
72 #    include <sys/bitypes.h>
73 #endif
74
75 /* Get the bool type. */
76 #include <portable/stdbool.h>
77
78 /* Windows provides snprintf under a different name. */
79 #ifdef _WIN32
80 #    define snprintf _snprintf
81 #endif
82
83 /* Windows does not define ssize_t. */
84 #ifndef HAVE_SSIZE_T
85 typedef ptrdiff_t ssize_t;
86 #endif
87
88 /*
89  * POSIX requires that these be defined in <unistd.h>.  If one of them has
90  * been defined, all the rest almost certainly have.
91  */
92 #ifndef STDIN_FILENO
93 #    define STDIN_FILENO  0
94 #    define STDOUT_FILENO 1
95 #    define STDERR_FILENO 2
96 #endif
97
98 /*
99  * C99 requires va_copy.  Older versions of GCC provide __va_copy.  Per the
100  * Autoconf manual, memcpy is a generally portable fallback.
101  */
102 #ifndef va_copy
103 #    ifdef __va_copy
104 #        define va_copy(d, s) __va_copy((d), (s))
105 #    else
106 #        define va_copy(d, s) memcpy(&(d), &(s), sizeof(va_list))
107 #    endif
108 #endif
109
110 /*
111  * If explicit_bzero is not available, fall back on memset.  This does NOT
112  * provide any of the security guarantees of explicit_bzero and will probably
113  * be optimized away by the compiler.  It just ensures that code will compile
114  * and function on systems without explicit_bzero.
115  */
116 #if !HAVE_EXPLICIT_BZERO
117 #    define explicit_bzero(s, n) memset((s), 0, (n))
118 #endif
119
120 BEGIN_DECLS
121
122 /* Default to a hidden visibility for all portability functions. */
123 #pragma GCC visibility push(hidden)
124
125 /*
126  * Provide prototypes for functions not declared in system headers.  Use the
127  * HAVE_DECL macros for those functions that may be prototyped but implemented
128  * incorrectly or implemented without a prototype.
129  */
130 #if !HAVE_ASPRINTF
131 extern int asprintf(char **, const char *, ...)
132     __attribute__((__format__(printf, 2, 3)));
133 extern int vasprintf(char **, const char *, va_list)
134     __attribute__((__format__(printf, 2, 0)));
135 #endif
136 #if !HAVE_DECL_SNPRINTF
137 extern int snprintf(char *, size_t, const char *, ...)
138     __attribute__((__format__(printf, 3, 4)));
139 #endif
140 #if !HAVE_DECL_VSNPRINTF
141 extern int vsnprintf(char *, size_t, const char *, va_list)
142     __attribute__((__format__(printf, 3, 0)));
143 #endif
144 #if !HAVE_MKSTEMP
145 extern int mkstemp(char *);
146 #endif
147 #if !HAVE_REALLOCARRAY
148 extern void *reallocarray(void *, size_t, size_t);
149 #endif
150 #if !HAVE_STRNDUP
151 extern char *strndup(const char *, size_t);
152 #endif
153
154 /* Undo default visibility change. */
155 #pragma GCC visibility pop
156
157 END_DECLS
158
159 #endif /* !PORTABLE_SYSTEM_H */