Implement standard library feature macros.

This commit is contained in:
Jonas 'Sortie' Termansen 2014-04-02 21:35:20 +02:00
parent d2cea190a8
commit fbbb33287b
11 changed files with 183 additions and 69 deletions

View File

@ -107,7 +107,7 @@ int scandir(const char*, struct dirent***, int (*)(const struct dirent*),
int versionsort(const struct dirent**, const struct dirent**);
int versionsort_r(const struct dirent**, const struct dirent**, void*);
#if defined(_SORTIX_SOURCE)
#if __USE_SORTIX
void dregister(DIR* dir);
void dunregister(DIR* dir);
DIR* dnewdir(void);

View File

@ -31,10 +31,8 @@ __BEGIN_DECLS
void gnu_error(int status, int errnum, const char* format, ...)
__attribute__((__format__(__printf__, 3, 4)));
#if __SORTIX_STDLIB_REDIRECTS
void error(int status, int errnum, const char* format, ...) __asm__ ("gnu_error")
__attribute__((__format__(__printf__, 3, 4)));
#endif
__END_DECLS

View File

@ -1,6 +1,6 @@
/*******************************************************************************
Copyright(C) Jonas 'Sortie' Termansen 2011, 2012, 2013.
Copyright(C) Jonas 'Sortie' Termansen 2011, 2012, 2013, 2014.
This file is part of the Sortix C Library.
@ -49,24 +49,158 @@
((gcc_major) < __GNUC__))
/* Sortix system components implicitly use the native API. */
#if defined(__is_sortix_system_component)
#if defined(__is_sortix_system_component) && !defined(_SORTIX_SOURCE)
#define _SORTIX_SOURCE 1
#endif
/* By default, assume the source is compiled using the native API. */
#if !defined(_SORTIX_SOURCE) && \
!defined(_POSIX_SOURCE) && !defined(_POSIX_C_SOURCE) && \
!defined(_BSD_SOURCE) && \
!defined(_SVID_SOURCE) && \
!defined(_XOPEN_SOURCE) && !defined(_XOPEN_SOURCE_EXTENDED) && \
!defined(_GNU_SOURCE) && \
1
/* These macros are used internally by the standard library to decide which
symbols are provided by the standard library headers. They are always defined
and have a value of zero if the feature is not activated. */
#undef __USE_C
#undef __USE_POSIX
#undef __USE_XOPEN
#undef __USE_SORTIX
/* Determine if all symbols should be visible in an effort to be as compatible
as possible, even if the Sortix API doesn't have them in these places. */
#if defined(_ALL_SOURCE)
#if !defined(_C11_SOURCE)
#define _C11_SOURCE
#endif
#if !defined(_POSIX_C_SOURCE)
#define _POSIX_C_SOURCE 200809L
#endif
#if !defined(_XOPEN_SOURCE)
#define _XOPEN_SOURCE 700
#endif
#if !defined(_SORTIX_SOURCE)
#define _SORTIX_SOURCE 1
#define __IMPLICIT_SORTIX_SOURCE
#endif
#endif
/* Default to the native API if no other base feature macros is defined. */
#if !defined(__STRICT_ANSI__) && \
!defined(_ANSI_SOURCE) && \
!defined(_ISOC99_SOURCE) && \
!defined(_ISOC11_SOURCE) && \
!defined(_XOPEN_SOURCE) && \
!defined(_POSIX_SOURCE) && \
!defined(_POSIX_C_SOURCE) && \
!defined(_ALL_SOURCE) && \
!defined(_SORTIX_SOURCE) && \
!defined(_DEFAULT_SOURCE)
#define _DEFAULT_SOURCE 1
#endif
/* Use the Sortix API if the default API was requested. */
#if defined(_DEFAULT_SOURCE) && !defined(_SORTIX_SOURCE)
#define _SORTIX_SOURCE 1
#define __IMPLICIT_SORTIX_SOURCE
#endif
/* Particular XSI revisions imply certain POSIX revisions. */
#if defined(_XOPEN_SOURCE)
#if 700 <= _XOPEN_SOURCE - 0
#undef _POSIX_C_SOURCE
#define _POSIX_C_SOURCE 200809L
#elif 600 <= _XOPEN_SOURCE - 0
#undef _POSIX_C_SOURCE
#define _POSIX_C_SOURCE 200112L
#elif 520 <= _XOPEN_SOURCE - 0
#undef _POSIX_C_SOURCE
#define _POSIX_C_SOURCE 199506L
#elif 500 <= _XOPEN_SOURCE - 0
#undef _POSIX_C_SOURCE
#define _POSIX_C_SOURCE 199506L
#endif
#endif
/* C89 is always visible. */
#define __USE_C 1989
/* Determine if C90 Amendment 1:1995 is visible. */
#if defined(__STDC_VERSION__) && 199409L <= __STDC_VERSION__
#undef __USE_C
#define __USE_C 1995
#endif
/* Determine if C99 is visible. */
#if defined(_ISOC99_SOURCE) || \
(defined(__STDC_VERSION__) && 199901L <= __STDC_VERSION__) || \
(defined(__cplusplus) && 201103L <= __cplusplus)
#undef __USE_C
#define __USE_C 1999
#endif
/* Determine if C11 is visible. */
#if defined(_ISOC11_SOURCE) || \
(defined(__STDC_VERSION__) && 201112L <= __STDC_VERSION__)
#undef __USE_C
#define __USE_C 2011
#endif
/* Determine which revision of XSI is used. */
#if defined(_XOPEN_SOURCE)
#if 700 <= _XOPEN_SOURCE - 0
#define __USE_XOPEN 700
#elif 600 <= _XOPEN_SOURCE - 0
#define __USE_XOPEN 600
#elif 520 <= _XOPEN_SOURCE - 0
#define __USE_XOPEN 520
#elif 500 <= _XOPEN_SOURCE - 0
#define __USE_XOPEN 500
#elif 1 == _XOPEN_SOURCE_EXTENDED - 0
#define __USE_XOPEN 420
#elif 4 <= _XOPEN_VERSION - 0
#define __USE_XOPEN 400
#else
#define __USE_XOPEN 300
#endif
#else
#define __USE_XOPEN 0
#endif
/* Determine which revision of POSIX is used. */
#ifdef _POSIX_C_SOURCE
#if 200809L <= _POSIX_C_SOURCE - 0
#define __USE_POSIX 200809L
#elif 200112L <= _POSIX_C_SOURCE - 0
#define __USE_POSIX 200112L
#elif 199506L <= _POSIX_C_SOURCE - 0
#define __USE_POSIX 199506L
#elif 199309L <= _POSIX_C_SOURCE - 0
#define __USE_POSIX 199309L
#elif 2 <= _POSIX_C_SOURCE - 0
#define __USE_POSIX 199209L
#else
#define __USE_POSIX 199009L
#endif
#elif defined(_POSIX_SOURCE)
#define __USE_POSIX 198808L
#else
#define __USE_POSIX 0
#endif
/* Upgrade the visible C revision in accordance with the used POSIX revision. */
#if 200112L <= __USE_POSIX && __USE_C < 1999
#undef __USE_C
#define __USE_C 1999
#elif 0 < __USE_POSIX && __USE_C < 1990
#undef __USE_C
#define __USE_C 1990
#endif
/* Determine whether the Sortix API is visible. */
#if defined(_SORTIX_SOURCE)
#define __USE_SORTIX 1
#else
#define __USE_SORTIX 0
#endif
/* Whether to override some "standard" functions with Sortix alternatives. */
#if !defined(__SORTIX_STDLIB_REDIRECTS)
#if defined(_SORTIX_SOURCE)
#if __USE_SORTIX && !defined(__IMPLICIT_SORTIX_SOURCE)
#define __SORTIX_STDLIB_REDIRECTS 1
#else
#define __SORTIX_STDLIB_REDIRECTS 0
@ -86,8 +220,10 @@
/* Provide the full <stdint.h> in all system components. */
#if defined(__is_sortix_system_component)
#undef __STDC_CONSTANT_MACROS
#undef __STDC_FORMAT_MACROS
#undef __STDC_LIMIT_MACROS
#define __STDC_CONSTANT_MACROS
#define __STDC_FORMAT_MACROS
#define __STDC_LIMIT_MACROS
#endif

View File

@ -69,7 +69,7 @@ struct lconv
#define LC_NUM_CATEGORIES LC_ALL
const char* sortix_setlocale(int category, const char* locale);
#if defined(_SORTIX_SOURCE) && __SORTIX_STDLIB_REDIRECTS
#if __USE_SORTIX && __SORTIX_STDLIB_REDIRECTS
const char* setlocale(int category, const char* locale) __asm__ ("sortix_setlocale");
#else
char* setlocale(int category, const char* locale);

View File

@ -214,7 +214,7 @@ char* ctermid(char* s);
FILE* open_memstream(char** bufp, size_t* sizep);
#endif
#if defined(_SORTIX_SOURCE)
#if __USE_SORTIX
#define FILE_MODE_READ (1 << 0)
#define FILE_MODE_WRITE (1 << 1)
@ -264,7 +264,7 @@ int fpipe(FILE* pipes[2]);
int fparsemode(const char*);
#endif
#if defined(_SORTIX_SOURCE) || defined(_WANT_SORTIX_VPRINTF_CALLBACK)
#if __USE_SORTIX
size_t vprintf_callback(size_t (*callback)(void*, const char*, size_t),
void* user,
const char* __restrict format,

View File

@ -64,6 +64,7 @@ char* stpncpy(char* __restrict, const char* __restrict, size_t);
int strcasecmp(const char* a, const char* b);
char* strcat(char* __restrict, const char* __restrict);
char* strchr(const char*, int);
char* strchrnul(const char* str, int c);
int strcmp(const char*, const char*);
int strcoll(const char*, const char*);
int strcoll_l(const char*, const char*, locale_t);
@ -90,16 +91,12 @@ int strverscmp(const char*, const char*);
size_t strxfrm(char* __restrict, const char* __restrict, size_t);
size_t strxfrm_l(char* __restrict, const char* __restrict, size_t, locale_t);
#if defined(_SORTIX_SOURCE) || defined(_GNU_SOURCE)
char* strchrnul(const char* str, int c);
#endif
#if defined(_SORTIX_SOURCE)
#if __USE_SORTIX
const char* sortix_strerror(int errnum);
const char* sortix_strerror_l(int, locale_t);
const char* sortix_strsignal(int signum);
#endif
#if defined(_SOURCE_SOURCE) && __SORTIX_STDLIB_REDIRECTS
#if __USE_SORTIX && __SORTIX_STDLIB_REDIRECTS
const char* strerror(int errnum) __asm__ ("sortix_strerror");
const char* strerror_l(int, locale_t) __asm__ ("sortix_strerror_l");
const char* strsignal(int signum) __asm__ ("sortix_strsignal");

View File

@ -88,7 +88,7 @@ struct tm
__END_DECLS
#include <sortix/timespec.h>
#include <sortix/itimerspec.h>
#if defined(_SORTIX_SOURCE)
#if __USE_SORTIX
#include <sortix/tmns.h>
#endif
__BEGIN_DECLS
@ -144,7 +144,7 @@ void tzset(void);
/* TODO: This is some _MISC_SOURCE thing according to GNU, but I like it. */
time_t timegm(struct tm*);
#if defined(_SORTIX_SOURCE)
#if __USE_SORTIX
int clock_gettimeres(clockid_t, struct timespec*, struct timespec*);
int clock_settimeres(clockid_t, const struct timespec*, const struct timespec*);
int timens(struct tmns* tmns);

View File

@ -33,7 +33,7 @@
#include <sys/__/types.h>
#include <__/stdint.h>
#if defined(_SORTIX_SOURCE)
#if __USE_SORTIX
#include <stdarg.h>
#include <stdint.h>
#include <sortix/exit.h>
@ -50,8 +50,6 @@ __END_DECLS
#include <sortix/seek.h>
#include <sortix/unistd.h>
#define _SORTIX_ALWAYS_SBRK
__BEGIN_DECLS
/* Currently just say we support the newest POSIX. */
@ -379,6 +377,7 @@ ssize_t readlink(const char* __restrict, char* __restrict, size_t);
ssize_t readlinkat(int, const char* __restrict, char* __restrict, size_t);
ssize_t read(int, void*, size_t);
int rmdir(const char*);
void* sbrk(__intptr_t increment);
int setegid(gid_t);
int seteuid(uid_t);
int setgid(gid_t);
@ -399,7 +398,7 @@ int unlinkat(int, const char*, int);
int unlink(const char*);
ssize_t write(int, const void*, size_t);
#if defined(_SORTIX_SOURCE)
#if __USE_SORTIX
int alarmns(const struct timespec* delay, struct timespec* odelay);
int execvpe(const char*, char* const [], char* const []);
int exit_thread(int, int, const struct exit_thread*);
@ -410,12 +409,9 @@ pid_t tfork(int flags, struct tfork* regs);
size_t writeall(int fd, const void* buf, size_t count);
size_t writeleast(int fd, const void* buf, size_t least, size_t max);
#endif
#if defined(_SORTIX_SOURCE) || defined(_SORTIX_ALWAYS_SBRK)
void* sbrk(__intptr_t increment);
#endif
/* For compatibility with POSIX, declare getopt(3) here. */
#if !defined(_SORTIX_SOURCE)
#if __USE_POSIX
/* These declarations are repeated in <getopt.h>. */
#ifndef __getopt_unistd_shared_declared
#define __getopt_unistd_shared_declared

View File

@ -51,13 +51,13 @@ int feholdexcept(fenv_t *);
int fesetenv(const fenv_t *);
int feupdateenv(const fenv_t *);
#if defined(_NETBSD_SOURCE) || defined(_GNU_SOURCE) || defined(_SORTIX_SOURCE)
#if __USE_SORTIX
int feenableexcept(int mask);
int fedisableexcept(int mask);
int fegetexcept(void);
#endif /* _NETBDS_SOURCE || _GNU_SOURCE */
#endif /* __USE_SORTIX */
__END_DECLS

View File

@ -55,7 +55,7 @@ __BEGIN_DECLS
#define HUGE_VAL __builtin_huge_val()
/* C99 macros */
#if __ISO_C_VISIBLE >= 1999 || 1 /* TODO: HACK: Visibility */
#if __USE_SORTIX || 1999 <= __USE_C
#define FP_ILOGB0 (-__INT_MAX)
#define FP_ILOGBNAN __INT_MAX
@ -106,10 +106,10 @@ __BEGIN_DECLS
typedef __double_t double_t;
typedef __float_t float_t;
#endif /* __ISO_C_VISIBLE >= 1999 */
#endif /* __USE_SORTIX || 1999 <= __USE_C */
/* XOPEN/SVID macros */
#if __BSD_VISIBLE || __XSI_VISIBLE || 1 /* TODO: HACK: Support visibility! */
#if __USE_SORTIX || __USE_XOPEN
#define M_E 2.7182818284590452354 /* e */
#define M_LOG2E 1.4426950408889634074 /* log 2e */
#define M_LOG10E 0.43429448190325182765 /* log 10e */
@ -128,11 +128,11 @@ typedef __float_t float_t;
#define MAXFLOAT ((float)3.40282346638528860e+38)
extern int signgam;
#endif /* __BSD_VISIBLE || __XSI_VISIBLE */
#endif /* __USE_SORTIX || __USE_XOPEN */
/* Various extensions inherited from NetBSD libm. Perhaps we should get rid of
some of them or make them private to libm itself, or just rename them. */
#if defined(_NETBSD_SOURCE) || defined(_SORTIX_SOURCE) || 1 /* TODO: HACK: Visibility */
#if __USE_SORTIX
enum fdversion {fdlibm_ieee = -1, fdlibm_svid, fdlibm_xopen, fdlibm_posix};
#define _LIB_VERSION_TYPE enum fdversion
@ -179,7 +179,7 @@ struct exception
#define TLOSS 5
#define PLOSS 6
#endif /* _NETBSD_SOURCE */
#endif /* __USE_SORTIX */
/*
* Most of these functions depend on the rounding mode and have the side
@ -219,7 +219,7 @@ double fabs(double);
double floor(double);
double fmod(double, double);
#if defined(_XOPEN_SOURCE) || defined(_NETBSD_SOURCE) || 1 /* TODO: HACK: Visibility */
#if __USE_SORTIX || __USE_XOPEN
double erf(double);
double erfc(double);
double gamma(double);
@ -232,8 +232,9 @@ double lgamma(double);
double y0(double);
double y1(double);
double yn(int, double);
#endif /* __USE_SORTIX || __USE_XOPEN */
#if (_XOPEN_SOURCE - 0) >= 500 || defined(_NETBSD_SOURCE) || 1 /* TODO: HACK: Visibility */
#if __USE_SORTIX || 500 <= __USE_XOPEN
double acosh(double);
double asinh(double);
double atanh(double);
@ -246,18 +247,12 @@ double nextafter(double, double);
double remainder(double, double);
double rint(double);
double scalb(double, double);
#endif /* (_XOPEN_SOURCE - 0) >= 500 || defined(_NETBSD_SOURCE)*/
#endif /* _XOPEN_SOURCE || _NETBSD_SOURCE */
#endif /* __USE_SORTIX || 500 <= __USE_XOPEN */
/*
* ISO C99
*/
#if !defined(_ANSI_SOURCE) && !defined(_POSIX_C_SOURCE) && \
!defined(_XOPEN_SOURCE) || \
((__STDC_VERSION__ - 0) >= 199901L) || \
((_POSIX_C_SOURCE - 0) >= 200112L) || \
((_XOPEN_SOURCE - 0) >= 600) || \
defined(_ISOC99_SOURCE) || defined(_NETBSD_SOURCE) || 1 /* TODO: HACK: Visibility */
#if __USE_SORTIX || 1999 <= __USE_C
/* 7.12.3.1 int fpclassify(real-floating x) */
#define fpclassify(__x) __fpmacro_unary_floating(fpclassify, __x)
@ -379,21 +374,15 @@ long double fdiml(long double, long double);
long double fmaxl(long double, long double);
long double fminl(long double, long double);
#endif /* !_ANSI_SOURCE && ... */
#if !defined(_ANSI_SOURCE) && !defined(_POSIX_C_SOURCE) || \
!defined(_XOPEN_SOURCE) || \
((__STDC_VERSION__ - 0) >= 199901L) || \
((_POSIX_C_SOURCE - 0) >= 200112L) || \
defined(_ISOC99_SOURCE) || defined(_NETBSD_SOURCE) || 1 /* TODO: HACK: Visibility */
/* 7.12.3.3 int isinf(real-floating x) */
#define isinf(__x) __fpmacro_unary_floating(isinf, __x)
/* 7.12.3.4 int isnan(real-floating x) */
#define isnan(__x) __fpmacro_unary_floating(isnan, __x)
#endif /* !_ANSI_SOURCE && ... */
#if defined(_NETBSD_SOURCE) || 1 /* TODO: HACK: Visibility */
#endif /* __USE_SORTIX || 1999 <= __USE_C */
#if __USE_SORTIX
#ifndef __cplusplus
int matherr(struct exception*);
#endif
@ -414,19 +403,19 @@ double scalbn(double, int);
*/
double drem(double, double);
#endif /* _NETBSD_SOURCE */
#endif /* __USE_SORTIX */
#if defined(_NETBSD_SOURCE) || defined(_REENTRANT) || 1 /* TODO: HACK: Visibility */
#if __USE_SORTIX /* or _REENTRANT */
/*
* Reentrant version of gamma & lgamma; passes signgam back by reference
* as the second argument; user must allocate space for signgam.
*/
double gamma_r(double, int*);
double lgamma_r(double, int*);
#endif /* _NETBSD_SOURCE || _REENTRANT */
#endif /* __USE_SORTIX */
#if defined(_NETBSD_SOURCE) || 1 /* TODO: HACK: Visibility */
#if __USE_SORTIX
/* float versions of ANSI/POSIX functions */
@ -452,9 +441,9 @@ float significandf(float);
* float versions of BSD math library entry points
*/
float dremf(float, float);
#endif /* _NETBSD_SOURCE */
#endif /* __USE_SORTIX */
#if defined(_NETBSD_SOURCE) || defined(_REENTRANT) || 1 /* TODO: HACK: Visibility */
#if __USE_SORTIX /* or _REENTRANT */
/*
* Float versions of reentrant version of gamma & lgamma; passes
* signgam back by reference as the second argument; user must
@ -462,7 +451,7 @@ float dremf(float, float);
*/
float gammaf_r(float, int*);
float lgammaf_r(float, int*);
#endif /* !... || _REENTRANT */
#endif /* __USE_SORTIX */
/*
* Library implementation

View File

@ -20,8 +20,6 @@
*******************************************************************************/
#define _SORTIX_SOURCE
#include <sys/ioctl.h>
#include <ctype.h>