# Disable default inclusion of <stdio.h> by setting stdio option to empty
set stdio

# Flag systems that omit necessary definitions such as u_long
# when _POSIX_SOURCE or _XOPEN_SOURCE are defined.
# Affected: Mac OS X, UnixWare.
typ u_long

# In FreeBSD, definitions like _POSIX_SOURCE and such are used to *limit*
# functionality to known API; they don't enable anything. The general intent in
# BSD is to enable everything by default. So we look for that here, but stay
# careful that we don't get fooled by presence of FreeBSD that underpins some
# subsystems in Mac OS X; there are other Apple-specific portability hacks
# elsewhere we should not interfere with.
if tst note{ FreeBSD or DragonFly BSD }end compile{
		#include <sys/param.h>
		#if (!defined(__FreeBSD__) && !defined(__DragonFly__)) || defined(__APPLE__) || defined(__MACH__)
		#error not one of the listed systems
		#endif
	}end {
	}
elif tst note{ SunOS (Solaris, illumos) }end compile{
		#if !__sun
		#error dark
		#endif
	}end {
		#define _XPG7		1
		#define	_XPG6		1
		#define	_XPG5		1
		#define _XPG4		1
		#define	__EXTENSIONS__	1
		#define	_XOPEN_SOURCE	9900
	}
elif tst note{ _ALL_SOURCE & _POSIX_SOURCE & _POSIX_C_SOURCE & _XOPEN_SOURCE & _GNU_SOURCE & _DARWIN_C_SOURCE & __EXTENSIONS__ works }end compile{
		#define _ALL_SOURCE	1
		#define _POSIX_SOURCE	1
		#define _POSIX_C_SOURCE	21000101L
		#define _XOPEN_SOURCE	9900
		#define _GNU_SOURCE	1
		#define _DARWIN_C_SOURCE 1
		#define __EXTENSIONS__	1
		#include <sys/types.h>
		#include <sys/stat.h>
		#include <stdlib.h>
		#include <unistd.h>
		#include <fcntl.h>
		#if _typ_u_long
		u_long _test_dummy_;
		#endif
	}end {
		#ifndef _ALL_SOURCE
		#define _ALL_SOURCE	1
		#endif
		#ifndef _POSIX_SOURCE
		#define _POSIX_SOURCE	1
		#endif
		#ifndef _POSIX_C_SOURCE
		#define _POSIX_C_SOURCE	21000101L
		#endif
		#ifndef _XOPEN_SOURCE
		#define _XOPEN_SOURCE	9900
		#endif
		#ifndef _GNU_SOURCE
		#define _GNU_SOURCE	1
		#endif
		#ifndef _DARWIN_C_SOURCE
		#define _DARWIN_C_SOURCE 1
		#endif
		#ifndef __EXTENSIONS__
		#define __EXTENSIONS__	1
		#endif
	}
elif tst note{ _ALL_SOURCE & _POSIX_SOURCE & _XOPEN_SOURCE & _GNU_SOURCE & _DARWIN_C_SOURCE & __EXTENSIONS__ works }end compile{
		#define _ALL_SOURCE	1
		#define _POSIX_SOURCE	1
		#define _XOPEN_SOURCE	9900
		#define _GNU_SOURCE	1
		#define _DARWIN_C_SOURCE 1
		#define __EXTENSIONS__	1
		#include <sys/types.h>
		#include <sys/stat.h>
		#include <stdlib.h>
		#include <unistd.h>
		#include <fcntl.h>
		#if _typ_u_long
		u_long _test_dummy;
		#endif
	}end {
		#ifndef _ALL_SOURCE
		#define _ALL_SOURCE	1
		#endif
		#ifndef _POSIX_SOURCE
		#define _POSIX_SOURCE	1
		#endif
		#ifndef _XOPEN_SOURCE
		#define _XOPEN_SOURCE	9900
		#endif
		#ifndef _GNU_SOURCE
		#define _GNU_SOURCE	1
		#endif
		#ifndef _DARWIN_C_SOURCE
		#define _DARWIN_C_SOURCE 1
		#endif
		#ifndef __EXTENSIONS__
		#define __EXTENSIONS__	1
		#endif
	}
elif tst note{ _POSIX_SOURCE & _POSIX_C_SOURCE & _XOPEN_SOURCE & _GNU_SOURCE & _DARWIN_C_SOURCE & __EXTENSIONS__ works }end compile{
		#define _POSIX_SOURCE	1
		#define _POSIX_C_SOURCE	21000101L
		#define _XOPEN_SOURCE	9900
		#define _GNU_SOURCE	1
		#define _DARWIN_C_SOURCE 1
		#define __EXTENSIONS__	1
		#include <sys/types.h>
		#include <sys/stat.h>
		#include <stdlib.h>
		#include <unistd.h>
		#include <fcntl.h>
		#if _typ_u_long
		u_long _test_dummy;
		#endif
	}end {
		#ifndef _POSIX_SOURCE
		#define _POSIX_SOURCE	1
		#endif
		#ifndef _POSIX_C_SOURCE
		#define _POSIX_C_SOURCE	21000101L
		#endif
		#ifndef _XOPEN_SOURCE
		#define _XOPEN_SOURCE	9900
		#endif
		#ifndef _GNU_SOURCE
		#define _GNU_SOURCE	1
		#endif
		#ifndef _DARWIN_C_SOURCE
		#define _DARWIN_C_SOURCE 1
		#endif
		#ifndef __EXTENSIONS__
		#define __EXTENSIONS__	1
		#endif
	}
elif tst note{ _POSIX_SOURCE & _XOPEN_SOURCE & __EXTENSIONS__ works }end compile{
		#define _POSIX_SOURCE	1
		#define _XOPEN_SOURCE	1
		#define __EXTENSIONS__	1
		#include <sys/types.h>
		#include <sys/stat.h>
		#include <stdlib.h>
		#include <unistd.h>
		#include <fcntl.h>
		#if _typ_u_long
		u_long _test_dummy;
		#endif
	}end {
		#ifndef _POSIX_SOURCE
		#define _POSIX_SOURCE	1
		#endif
		#ifndef _XOPEN_SOURCE
		#define _XOPEN_SOURCE	1
		#endif
		#ifndef __EXTENSIONS__
		#define __EXTENSIONS__	1
		#endif
	}
elif tst note{ _XOPEN_SOURCE & __EXTENSIONS__ works }end compile{
		#define _XOPEN_SOURCE	1
		#define __EXTENSIONS__	1
		#include <sys/types.h>
		#include <sys/stat.h>
		#include <stdlib.h>
		#include <unistd.h>
		#include <fcntl.h>
		#if _typ_u_long
		u_long _test_dummy;
		#endif
	}end {
		#ifndef _XOPEN_SOURCE
		#define _XOPEN_SOURCE	1
		#endif
		#ifndef __EXTENSIONS__
		#define __EXTENSIONS__	1
		#endif
	}
elif tst note{ _XOPEN_SOURCE works }end compile{
		#define _XOPEN_SOURCE	1
		#include <sys/types.h>
		#include <sys/stat.h>
		#include <stdlib.h>
		#include <unistd.h>
		#include <fcntl.h>
		#if _typ_u_long
		u_long _test_dummy;
		#endif
	}end {
		#ifndef _XOPEN_SOURCE
		#define _XOPEN_SOURCE	1
		#endif
	}
else tst note{ __EXTENSIONS__ works }end compile{
		#define __EXTENSIONS__	1
		#include <sys/types.h>
		#include <sys/stat.h>
		#include <fcntl.h>
		#if _typ_u_long
		u_long _test_dummy;
		#endif
	}end {
		#ifndef __EXTENSIONS__
		#define __EXTENSIONS__	1
		#endif
	}
endif

if tst -D_ISOC99_SOURCE -lm note{ _ISOC99_SOURCE plays nice }end link{
		#include <sys/types.h>
		#include <sys/stat.h>
		#include <stdlib.h>
		#include <unistd.h>
		#include <math.h>
		int main() { return signbit(-0.0); }
	}end {
		#ifndef _ISOC99_SOURCE
		#define _ISOC99_SOURCE	1
		#endif
	}
endif
