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 <http://www.eyrie.org/~eagle/software/rra-c-util/>.
26  *
27  * Written by Russ Allbery <eagle@eyrie.org>
28  *
29  * The authors hereby relinquish any claim to any copyright that they may have
30  * in this work, whether granted under contract or by operation of law or
31  * international treaty, and hereby commit to the public, at large, that they
32  * shall not, at any time in the future, seek to enforce any copyright in this
33  * work against any person or entity, or prevent any person or entity from
34  * copying, publishing, distributing or creating derivative works of this
35  * work.
36  */
37 
38 #ifndef PORTABLE_SYSTEM_H
39 #define PORTABLE_SYSTEM_H 1
40 
41 /* Make sure we have our configuration information. */
42 #include <config.h>
43 
44 /* BEGIN_DECL and __attribute__. */
45 #include <portable/macros.h>
46 
47 /* A set of standard ANSI C headers.  We don't care about pre-ANSI systems. */
48 #if HAVE_INTTYPES_H
49 # include <inttypes.h>
50 #endif
51 #include <limits.h>
52 #include <stdarg.h>
53 #include <stddef.h>
54 #if HAVE_STDINT_H
55 # include <stdint.h>
56 #endif
57 #include <stdio.h>
58 #include <stdlib.h>
59 #include <string.h>
60 #if HAVE_STRINGS_H
61 # include <strings.h>
62 #endif
63 #include <sys/types.h>
64 #if HAVE_UNISTD_H
65 # include <unistd.h>
66 #endif
67 
68 /* SCO OpenServer gets int32_t from here. */
69 #if HAVE_SYS_BITYPES_H
70 # include <sys/bitypes.h>
71 #endif
72 
73 /* Get the bool type. */
74 #include <portable/stdbool.h>
75 
76 /* Windows provides snprintf under a different name. */
77 #ifdef _WIN32
78 # define snprintf _snprintf
79 #endif
80 
81 /* Define sig_atomic_t if it's not available in signal.h. */
82 #ifndef HAVE_SIG_ATOMIC_T
83 #endif
84 
85 /* Windows does not define ssize_t. */
86 #ifndef HAVE_SSIZE_T
87 typedef ptrdiff_t ssize_t;
88 #endif
89 
90 /*
91  * POSIX requires that these be defined in <unistd.h>.  If one of them has
92  * been defined, all the rest almost certainly have.
93  */
94 #ifndef STDIN_FILENO
95 # define STDIN_FILENO  0
96 # define STDOUT_FILENO 1
97 # define STDERR_FILENO 2
98 #endif
99 
100 /*
101  * C99 requires va_copy.  Older versions of GCC provide __va_copy.  Per the
102  * Autoconf manual, memcpy is a generally portable fallback.
103  */
104 #ifndef va_copy
105 # ifdef __va_copy
106 #  define va_copy(d, s) __va_copy((d), (s))
107 # else
108 #  define va_copy(d, s) memcpy(&(d), &(s), sizeof(va_list))
109 # endif
110 #endif
111 
112 BEGIN_DECLS
113 
114 /* Default to a hidden visibility for all portability functions. */
115 #pragma GCC visibility push(hidden)
116 
117 /*
118  * Provide prototypes for functions not declared in system headers.  Use the
119  * HAVE_DECL macros for those functions that may be prototyped but implemented
120  * incorrectly or implemented without a prototype.
121  */
122 #if !HAVE_ASPRINTF
123 extern int asprintf(char **, const char *, ...)
124     __attribute__((__format__(printf, 2, 3)));
125 extern int vasprintf(char **, const char *, va_list);
126 #endif
127 #if !HAVE_DECL_SNPRINTF
128 extern int snprintf(char *, size_t, const char *, ...)
129     __attribute__((__format__(printf, 3, 4)));
130 #endif
131 #if !HAVE_DECL_VSNPRINTF
132 extern int vsnprintf(char *, size_t, const char *, va_list);
133 #endif
134 #if !HAVE_MKSTEMP
135 extern int mkstemp(char *);
136 #endif
137 #if !HAVE_REALLOCARRAY
138 extern void *reallocarray(void *, size_t, size_t);
139 #endif
140 #if !HAVE_STRLCAT
141 extern size_t strlcat(char *, const char *, size_t);
142 #endif
143 #if !HAVE_STRLCPY
144 extern size_t strlcpy(char *, const char *, size_t);
145 #endif
146 #if !HAVE_STRNDUP
147 extern char *strndup(const char *, size_t);
148 #endif
149 
150 /* Undo default visibility change. */
151 #pragma GCC visibility pop
152 
153 END_DECLS
154 
155 #endif /* !PORTABLE_SYSTEM_H */
156