1184610Salfred/*
2184610Salfred * CDDL HEADER START
3184610Salfred *
4184610Salfred * The contents of this file are subject to the terms of the
5184610Salfred * Common Development and Distribution License (the "License").
6184610Salfred * You may not use this file except in compliance with the License.
7184610Salfred *
8184610Salfred * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9184610Salfred * or http://www.opensolaris.org/os/licensing.
10184610Salfred * See the License for the specific language governing permissions
11184610Salfred * and limitations under the License.
12184610Salfred *
13184610Salfred * When distributing Covered Code, include this CDDL HEADER in each
14184610Salfred * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15184610Salfred * If applicable, add the following below this CDDL HEADER, with the
16184610Salfred * fields enclosed by brackets "[]" replaced with your own identifying
17184610Salfred * information: Portions Copyright [yyyy] [name of copyright owner]
18184610Salfred *
19184610Salfred * CDDL HEADER END
20184610Salfred */
21184610Salfred
22184610Salfred/*
23184610Salfred * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
24184610Salfred * Use is subject to license terms.
25184610Salfred */
26184610Salfred
27205005Sthompsa#ifndef _SYS_FEATURE_TESTS_H
28205005Sthompsa#define	_SYS_FEATURE_TESTS_H
29194677Sthompsa
30194677Sthompsa#include <sys/ccompile.h>
31194677Sthompsa
32194677Sthompsa#ifdef	__cplusplus
33194677Sthompsaextern "C" {
34194677Sthompsa#endif
35194677Sthompsa
36194677Sthompsa/*
37194677Sthompsa * Values of _POSIX_C_SOURCE
38194677Sthompsa *
39194677Sthompsa *		undefined   not a POSIX compilation
40194677Sthompsa *		1	    POSIX.1-1990 compilation
41194677Sthompsa *		2	    POSIX.2-1992 compilation
42194677Sthompsa *		199309L	    POSIX.1b-1993 compilation (Real Time)
43194677Sthompsa *		199506L	    POSIX.1c-1995 compilation (POSIX Threads)
44194677Sthompsa *		200112L	    POSIX.1-2001 compilation (Austin Group Revision)
45194677Sthompsa */
46194677Sthompsa#if defined(_POSIX_SOURCE) && !defined(_POSIX_C_SOURCE)
47194677Sthompsa#define	_POSIX_C_SOURCE 1
48188942Sthompsa#endif
49194677Sthompsa
50184610Salfred/*
51194228Sthompsa * The feature test macros __XOPEN_OR_POSIX, _STRICT_STDC, and _STDC_C99
52184610Salfred * are Sun implementation specific macros created in order to compress
53188942Sthompsa * common standards specified feature test macros for easier reading.
54188942Sthompsa * These macros should not be used by the application developer as
55188942Sthompsa * unexpected results may occur. Instead, the user should reference
56188942Sthompsa * standards(5) for correct usage of the standards feature test macros.
57188942Sthompsa *
58188942Sthompsa * __XOPEN_OR_POSIX     Used in cases where a symbol is defined by both
59188942Sthompsa *                      X/Open or POSIX or in the negative, when neither
60184610Salfred *                      X/Open or POSIX defines a symbol.
61188942Sthompsa *
62188942Sthompsa * _STRICT_STDC         __STDC__ is specified by the C Standards and defined
63215649Sweongyo *                      by the compiler. For Sun compilers the value of
64229096Shselasky *                      __STDC__ is either 1, 0, or not defined based on the
65184610Salfred *                      compilation mode (see cc(1)). When the value of
66190754Sthompsa *                      __STDC__ is 1 and in the absence of any other feature
67184610Salfred *                      test macros, the namespace available to the application
68194228Sthompsa *                      is limited to only those symbols defined by the C
69194228Sthompsa *                      Standard. _STRICT_STDC provides a more readable means
70194228Sthompsa *                      of identifying symbols defined by the standard, or in
71229096Shselasky *                      the negative, symbols that are extensions to the C
72229096Shselasky *                      Standard. See additional comments for GNU C differences.
73229096Shselasky *
74184610Salfred * _STDC_C99            __STDC_VERSION__ is specified by the C standards and
75194228Sthompsa *                      defined by the compiler and indicates the version of
76184610Salfred *                      the C standard. A value of 199901L indicates a
77184610Salfred *                      compiler that complies with ISO/IEC 9899:1999, other-
78184610Salfred *                      wise known as the C99 standard.
79194677Sthompsa */
80194228Sthompsa
81184610Salfred#if defined(_XOPEN_SOURCE) || defined(_POSIX_C_SOURCE)
82248085Smarius#define	__XOPEN_OR_POSIX
83194228Sthompsa#endif
84184610Salfred
85184610Salfred/*
86184610Salfred * ISO/IEC 9899:1990 and it's revision, ISO/IEC 9899:1999 specify the
87195965Salfred * following predefined macro name:
88195965Salfred *
89242775Shselasky * __STDC__	The integer constant 1, intended to indicate a conforming
90229100Shselasky *		implementation.
91195965Salfred *
92232870Shselasky * Furthermore, a strictly conforming program shall use only those features
93232870Shselasky * of the language and library specified in these standards. A conforming
94232870Shselasky * implementation shall accept any strictly conforming program.
95232870Shselasky *
96232870Shselasky * Based on these requirements, Sun's C compiler defines __STDC__ to 1 for
97229100Shselasky * strictly conforming environments and __STDC__ to 0 for environments that
98229100Shselasky * use ANSI C semantics but allow extensions to the C standard. For non-ANSI
99232870Shselasky * C semantics, Sun's C compiler does not define __STDC__.
100232870Shselasky *
101229100Shselasky * The GNU C project interpretation is that __STDC__ should always be defined
102194228Sthompsa * to 1 for compilation modes that accept ANSI C syntax regardless of whether
103184610Salfred * or not extensions to the C standard are used. Violations of conforming
104194228Sthompsa * behavior are conditionally flagged as warnings via the use of the
105194228Sthompsa * -pedantic option. In addition to defining __STDC__ to 1, the GNU C
106194228Sthompsa * compiler also defines __STRICT_ANSI__ as a means of specifying strictly
107194228Sthompsa * conforming environments using the -ansi or -std=<standard> options.
108229096Shselasky *
109229096Shselasky * In the absence of any other compiler options, Sun and GNU set the value
110229096Shselasky * of __STDC__ as follows when using the following options:
111184610Salfred *
112184610Salfred *				Value of __STDC__  __STRICT_ANSI__
113184610Salfred *
114194228Sthompsa * cc -Xa (default)			0	      undefined
115184610Salfred * cc -Xt (transitional)		0             undefined
116194228Sthompsa * cc -Xc (strictly conforming)		1	      undefined
117184610Salfred * cc -Xs (K&R C)		    undefined	      undefined
118184610Salfred *
119184610Salfred * gcc (default)			1	      undefined
120213426Shselasky * gcc -ansi, -std={c89, c99,...)  	1              defined
121194228Sthompsa * gcc -traditional (K&R)	    undefined	      undefined
122194228Sthompsa *
123194228Sthompsa * The default compilation modes for Sun C compilers versus GNU C compilers
124213435Shselasky * results in a differing value for __STDC__ which results in a more
125213426Shselasky * restricted namespace when using Sun compilers. To allow both GNU and Sun
126213426Shselasky * interpretations to peacefully co-exist, we use the following Sun
127194228Sthompsa * implementation _STRICT_STDC_ macro:
128213426Shselasky */
129194228Sthompsa
130184610Salfred#if (__STDC__ - 0 == 1 && !defined(__GNUC__)) || \
131184610Salfred	(defined(__GNUC__) && defined(__STRICT_ANSI__))
132194228Sthompsa#define	_STRICT_STDC
133184610Salfred#else
134184610Salfred#undef	_STRICT_STDC
135184610Salfred#endif
136184610Salfred
137194228Sthompsa/*
138184610Salfred * Compiler complies with ISO/IEC 9899:1999
139184610Salfred */
140184610Salfred
141184610Salfred#if __STDC_VERSION__ - 0 >= 199901L
142184610Salfred#ifndef _STDC_C99
143213425Shselasky#define	_STDC_C99
144213425Shselasky#endif
145213425Shselasky#endif
146213425Shselasky
147213425Shselasky/*
148213425Shselasky * Large file interfaces:
149213425Shselasky *
150213425Shselasky *	_LARGEFILE_SOURCE
151213425Shselasky *		1		large file-related additions to POSIX
152213425Shselasky *				interfaces requested (fseeko, etc.)
153184610Salfred *	_LARGEFILE64_SOURCE
154194228Sthompsa *		1		transitional large-file-related interfaces
155184610Salfred *				requested (seek64, stat64, etc.)
156184610Salfred *
157194228Sthompsa * The corresponding announcement macros are respectively:
158184610Salfred *	_LFS_LARGEFILE
159192984Sthompsa *	_LFS64_LARGEFILE
160184610Salfred * (These are set in <unistd.h>.)
161184610Salfred *
162184610Salfred * Requesting _LARGEFILE64_SOURCE implies requesting _LARGEFILE_SOURCE as
163184610Salfred * well.
164199816Sthompsa *
165184610Salfred * The large file interfaces are made visible regardless of the initial values
166184610Salfred * of the feature test macros under certain circumstances:
167188907Sthompsa *    -	If no explicit standards-conforming environment is requested (neither
168195965Salfred *	of _POSIX_SOURCE nor _XOPEN_SOURCE is defined and the value of
169195965Salfred *	__STDC__ does not imply standards conformance).
170195965Salfred *    -	Extended system interfaces are explicitly requested (__EXTENSIONS__
171195965Salfred * 	is defined).
172188907Sthompsa *    -	Access to in-kernel interfaces is requested (_KERNEL or _KMEMUSER is
173196488Salfred *	defined).  (Note that this dependency is an artifact of the current
174196488Salfred *	kernel implementation and may change in future releases.)
175184610Salfred */
176184610Salfred#if	(!defined(_STRICT_STDC) && !defined(__XOPEN_OR_POSIX)) || \
177184610Salfred		defined(_KERNEL) || defined(_KMEMUSER) || \
178184610Salfred		defined(__EXTENSIONS__)
179194228Sthompsa#undef	_LARGEFILE64_SOURCE
180184610Salfred#define	_LARGEFILE64_SOURCE	1
181184610Salfred#endif
182194228Sthompsa#if	_LARGEFILE64_SOURCE - 0 == 1
183184610Salfred#undef	_LARGEFILE_SOURCE
184192984Sthompsa#define	_LARGEFILE_SOURCE	1
185184610Salfred#endif
186184610Salfred
187184610Salfred/*
188184610Salfred * Large file compilation environment control:
189184610Salfred *
190184610Salfred * The setting of _FILE_OFFSET_BITS controls the size of various file-related
191184610Salfred * types and governs the mapping between file-related source function symbol
192186730Salfred * names and the corresponding binary entry points.
193194228Sthompsa *
194186730Salfred * In the 32-bit environment, the default value is 32; if not set, set it to
195184610Salfred * the default here, to simplify tests in other headers.
196213425Shselasky *
197184610Salfred * In the 64-bit compilation environment, the only value allowed is 64.
198184824Sthompsa */
199229096Shselasky#if defined(_LP64)
200229096Shselasky#ifndef _FILE_OFFSET_BITS
201229096Shselasky#define	_FILE_OFFSET_BITS	64
202229096Shselasky#endif
203229096Shselasky#if	_FILE_OFFSET_BITS - 0 != 64
204184610Salfred#error	"invalid _FILE_OFFSET_BITS value specified"
205194228Sthompsa#endif
206184610Salfred#else	/* _LP64 */
207184610Salfred#ifndef	_FILE_OFFSET_BITS
208184824Sthompsa#define	_FILE_OFFSET_BITS	32
209184610Salfred#endif
210187174Sthompsa#if	_FILE_OFFSET_BITS - 0 != 32 && _FILE_OFFSET_BITS - 0 != 64
211187174Sthompsa#error	"invalid _FILE_OFFSET_BITS value specified"
212194228Sthompsa#endif
213194228Sthompsa#endif	/* _LP64 */
214187174Sthompsa
215184610Salfred/*
216184610Salfred * Use of _XOPEN_SOURCE
217194228Sthompsa *
218184610Salfred * The following X/Open specifications are supported:
219191400Sthompsa *
220191400Sthompsa * X/Open Portability Guide, Issue 3 (XPG3)
221194228Sthompsa * X/Open CAE Specification, Issue 4 (XPG4)
222191400Sthompsa * X/Open CAE Specification, Issue 4, Version 2 (XPG4v2)
223217558Shselasky * X/Open CAE Specification, Issue 5 (XPG5)
224215812Sweongyo * Open Group Technical Standard, Issue 6 (XPG6), also referred to as
225217558Shselasky *    IEEE Std. 1003.1-2001 and ISO/IEC 9945:2002.
226184610Salfred *
227184610Salfred * XPG4v2 is also referred to as UNIX 95 (SUS or SUSv1).
228184610Salfred * XPG5 is also referred to as UNIX 98 or the Single Unix Specification,
229184610Salfred *     Version 2 (SUSv2)
230229096Shselasky * XPG6 is the result of a merge of the X/Open and POSIX specifications
231229096Shselasky *     and as such is also referred to as IEEE Std. 1003.1-2001 in
232229096Shselasky *     addition to UNIX 03 and SUSv3.
233229096Shselasky *
234229096Shselasky * When writing a conforming X/Open application, as per the specification
235229096Shselasky * requirements, the appropriate feature test macros must be defined at
236229096Shselasky * compile time. These are as follows. For more info, see standards(5).
237229096Shselasky *
238229096Shselasky * Feature Test Macro				     Specification
239229096Shselasky * ------------------------------------------------  -------------
240229096Shselasky * _XOPEN_SOURCE                                         XPG3
241229096Shselasky * _XOPEN_SOURCE && _XOPEN_VERSION = 4                   XPG4
242229096Shselasky * _XOPEN_SOURCE && _XOPEN_SOURCE_EXTENDED = 1           XPG4v2
243229096Shselasky * _XOPEN_SOURCE = 500                                   XPG5
244229096Shselasky * _XOPEN_SOURCE = 600  (or POSIX_C_SOURCE=200112L)      XPG6
245229096Shselasky *
246229096Shselasky * In order to simplify the guards within the headers, the following
247232870Shselasky * implementation private test macros have been created. Applications
248232870Shselasky * must NOT use these private test macros as unexpected results will
249232870Shselasky * occur.
250232870Shselasky *
251232870Shselasky * Note that in general, the use of these private macros is cumulative.
252229096Shselasky * For example, the use of _XPG3 with no other restrictions on the X/Open
253229096Shselasky * namespace will make the symbols visible for XPG3 through XPG6
254229096Shselasky * compilation environments. The use of _XPG4_2 with no other X/Open
255229096Shselasky * namespace restrictions indicates that the symbols were introduced in
256229096Shselasky * XPG4v2 and are therefore visible for XPG4v2 through XPG6 compilation
257229096Shselasky * environments, but not for XPG3 or XPG4 compilation environments.
258229096Shselasky *
259229096Shselasky * _XPG3    X/Open Portability Guide, Issue 3 (XPG3)
260229096Shselasky * _XPG4    X/Open CAE Specification, Issue 4 (XPG4)
261229096Shselasky * _XPG4_2  X/Open CAE Specification, Issue 4, Version 2 (XPG4v2/UNIX 95/SUS)
262229096Shselasky * _XPG5    X/Open CAE Specification, Issue 5 (XPG5/UNIX 98/SUSv2)
263229096Shselasky * _XPG6    Open Group Technical Standard, Issue 6 (XPG6/UNIX 03/SUSv3)
264229096Shselasky */
265229096Shselasky
266229096Shselasky/* X/Open Portability Guide, Issue 3 */
267229096Shselasky#if defined(_XOPEN_SOURCE) && (_XOPEN_SOURCE - 0 < 500) && \
268229096Shselasky	(_XOPEN_VERSION - 0 < 4) && !defined(_XOPEN_SOURCE_EXTENDED)
269229096Shselasky#define	_XPG3
270229096Shselasky/* X/Open CAE Specification, Issue 4 */
271229096Shselasky#elif	(defined(_XOPEN_SOURCE) && _XOPEN_VERSION - 0 == 4)
272229096Shselasky#define	_XPG4
273229096Shselasky#define	_XPG3
274229096Shselasky/* X/Open CAE Specification, Issue 4, Version 2 */
275229096Shselasky#elif (defined(_XOPEN_SOURCE) && _XOPEN_SOURCE_EXTENDED - 0 == 1)
276229096Shselasky#define	_XPG4_2
277229096Shselasky#define	_XPG4
278229096Shselasky#define	_XPG3
279229096Shselasky/* X/Open CAE Specification, Issue 5 */
280229096Shselasky#elif	(_XOPEN_SOURCE - 0 == 500)
281259602Shselasky#define	_XPG5
282259602Shselasky#define	_XPG4_2
283259602Shselasky#define	_XPG4
284259602Shselasky#define	_XPG3
285259602Shselasky#undef	_POSIX_C_SOURCE
286259602Shselasky#define	_POSIX_C_SOURCE			199506L
287259602Shselasky/* Open Group Technical Standard , Issue 6 */
288259602Shselasky#elif	(_XOPEN_SOURCE - 0 == 600) || (_POSIX_C_SOURCE - 0 == 200112L)
289259602Shselasky#define	_XPG6
290259602Shselasky#define	_XPG5
291259602Shselasky#define	_XPG4_2
292259602Shselasky#define	_XPG4
293259602Shselasky#define	_XPG3
294259602Shselasky#undef	_POSIX_C_SOURCE
295259602Shselasky#define	_POSIX_C_SOURCE			200112L
296259602Shselasky#undef	_XOPEN_SOURCE
297259602Shselasky#define	_XOPEN_SOURCE			600
298259602Shselasky#endif
299259602Shselasky
300259602Shselasky/*
301259602Shselasky * _XOPEN_VERSION is defined by the X/Open specifications and is not
302259602Shselasky * normally defined by the application, except in the case of an XPG4
303229096Shselasky * application.  On the implementation side, _XOPEN_VERSION defined with
304229096Shselasky * the value of 3 indicates an XPG3 application. _XOPEN_VERSION defined
305229096Shselasky * with the value of 4 indicates an XPG4 or XPG4v2 (UNIX 95) application.
306229096Shselasky * _XOPEN_VERSION  defined with a value of 500 indicates an XPG5 (UNIX 98)
307229096Shselasky * application and with a value of 600 indicates an XPG6 (UNIX 03)
308229096Shselasky * application.  The appropriate version is determined by the use of the
309229096Shselasky * feature test macros described earlier.  The value of _XOPEN_VERSION
310229096Shselasky * defaults to 3 otherwise indicating support for XPG3 applications.
311229096Shselasky */
312229096Shselasky#ifndef _XOPEN_VERSION
313229096Shselasky#ifdef	_XPG6
314229096Shselasky#define	_XOPEN_VERSION 600
315229096Shselasky#elif defined(_XPG5)
316229096Shselasky#define	_XOPEN_VERSION 500
317229100Shselasky#elif	defined(_XPG4_2)
318229100Shselasky#define	_XOPEN_VERSION  4
319229096Shselasky#else
320229096Shselasky#define	_XOPEN_VERSION  3
321229096Shselasky#endif
322229100Shselasky#endif
323229100Shselasky
324229100Shselasky/*
325229100Shselasky * ANSI C and ISO 9899:1990 say the type long long doesn't exist in strictly
326229100Shselasky * conforming environments.  ISO 9899:1999 says it does.
327229096Shselasky *
328229096Shselasky * The presence of _LONGLONG_TYPE says "long long exists" which is therefore
329229100Shselasky * defined in all but strictly conforming environments that disallow it.
330229100Shselasky */
331229096Shselasky#if !defined(_STDC_C99) && defined(_STRICT_STDC) && !defined(__GNUC__)
332229096Shselasky/*
333229096Shselasky * Resist attempts to force the definition of long long in this case.
334229096Shselasky */
335194228Sthompsa#if defined(_LONGLONG_TYPE)
336184610Salfred#error	"No long long in strictly conforming ANSI C & 1990 ISO C environments"
337184610Salfred#endif
338184610Salfred#else
339184610Salfred#if !defined(_LONGLONG_TYPE)
340194228Sthompsa#define	_LONGLONG_TYPE
341184610Salfred#endif
342192984Sthompsa#endif
343192984Sthompsa
344184610Salfred/*
345192984Sthompsa * It is invalid to compile an XPG3, XPG4, XPG4v2, or XPG5 application
346184610Salfred * using c99.  The same is true for POSIX.1-1990, POSIX.2-1992, POSIX.1b,
347184610Salfred * and POSIX.1c applications. Likewise, it is invalid to compile an XPG6
348229096Shselasky * or a POSIX.1-2001 application with anything other than a c99 or later
349229096Shselasky * compiler.  Therefore, we force an error in both cases.
350229096Shselasky */
351267350Shselasky#if defined(_STDC_C99) && (defined(__XOPEN_OR_POSIX) && !defined(_XPG6))
352267350Shselasky#error "Compiler or options invalid for pre-UNIX 03 X/Open applications \
353267350Shselasky	and pre-2001 POSIX applications"
354267350Shselasky#elif !defined(_STDC_C99) && \
355267350Shselasky	(defined(__XOPEN_OR_POSIX) && defined(_XPG6))
356184610Salfred#error "Compiler or options invalid; UNIX 03 and POSIX.1-2001 applications \
357267350Shselasky	require the use of c99"
358267350Shselasky#endif
359184610Salfred
360184610Salfred/*
361184610Salfred * The following macro defines a value for the ISO C99 restrict
362184610Salfred * keyword so that _RESTRICT_KYWD resolves to "restrict" if
363184610Salfred * an ISO C99 compiler is used and "" (null string) if any other
364184610Salfred * compiler is used. This allows for the use of single prototype
365184610Salfred * declarations regardless of compiler version.
366184610Salfred */
367196488Salfred#if (defined(__STDC__) && defined(_STDC_C99)) && !defined(__cplusplus)
368205005Sthompsa#define	_RESTRICT_KYWD	restrict
369196488Salfred#else
370196488Salfred#define	_RESTRICT_KYWD
371196488Salfred#endif
372196488Salfred
373196488Salfred/*
374196488Salfred * The following macro indicates header support for the ANSI C++
375196488Salfred * standard.  The ISO/IEC designation for this is ISO/IEC FDIS 14882.
376205005Sthompsa */
377196488Salfred#define	_ISO_CPP_14882_1998
378184824Sthompsa
379184610Salfred/*
380208013Sthompsa * The following macro indicates header support for the C99 standard,
381184610Salfred * ISO/IEC 9899:1999, Programming Languages - C.
382186730Salfred */
383186730Salfred#define	_ISO_C_9899_1999
384194228Sthompsa
385208013Sthompsa/*
386196488Salfred * The following macro indicates header support for DTrace. The value is an
387196403Sjhb * integer that corresponds to the major version number for DTrace.
388184824Sthompsa */
389184610Salfred#define	_DTRACE_VERSION	1
390213425Shselasky
391184610Salfred#ifdef	__cplusplus
392184610Salfred}
393184610Salfred#endif
394194228Sthompsa
395184610Salfred#endif	/* _SYS_FEATURE_TESTS_H */
396184610Salfred