Skip to content

Commit

Permalink
clar: stop requiring POSIX.1-2008
Browse files Browse the repository at this point in the history
We use a couple of functions not part of the ISO C standard, but instead
part of POSIX.1-2008. There are ancient platforms like SunOS that do not
support this standard, but which _do_ have the required functions hidden
away behind some other macros.

The feature test macros are a bit of a mess. The following functions are
what we require as non-standard extensions:

   strdup():
       _XOPEN_SOURCE >= 500
           || /* Since glibc 2.12: */ _POSIX_C_SOURCE >= 200809L
           || /* glibc <= 2.19: */ _BSD_SOURCE || _SVID_SOURCE

    snprintf(), vsnprintf():
        _BSD_SOURCE || _XOPEN_SOURCE >= 500 || _ISOC99_SOURCE || _POSIX_C_SOURCE >= 200112L;
        or cc -std=c99

    mkdtemp():
        _BSD_SOURCE
        || /* Since glibc 2.10: */
        (_POSIX_C_SOURCE >= 200809L || _XOPEN_SOURCE >= 700)

   lstat():
       /* Since glibc 2.20 */ _DEFAULT_SOURCE
           || _XOPEN_SOURCE >= 500
           || /* Since glibc 2.10: */ _POSIX_C_SOURCE >= 200112L
           || /* glibc 2.19 and earlier */ _BSD_SOURCE

A few observations:

  - We cannot set _POSIX_C_SOURCE to 200809L because it will cause
    errors on platforms that do not support POSIX.1-2008.

  - We cannot easily set _XOPEN_SOURCE because on SunOS, we would have
    to set it conditionally depending on the current C standard used by
    the compiler.

What is common to all though is _BSD_SOURCE, even though it has been
deprecated in glibc 2.20. Quoting feature-test-macros(7):

  Since glibc 2.20, this macro is deprecated.  It now has the same
  effect as defining _DEFAULT_SOURCE, but generates a compile-time
  warning (unless _DEFAULT_SOURCE is also defined).  Use _DEFAULT_SOURCE
  instead.  To allow code that requires _BSD_SOURCE in glibc 2.19 and
  earlier and _DEFAULT_SOURCE in glibc 2.20 and later to compile without
  warnings, define both _BSD_SOURCE and _DEFAULT_SOURCE.

So even though _BSD_SOURCE is deprecated, we can set it in tandem with
_DEFAULT_SOURCE. Furthermore, _DEFAULT_SOURCE is defined to be roughly
equivalent to "-D_BSD_SOURCE -D_SVID_SOURCE -D_POSIX_C_SOURCE=200809",
which should thus enables all features required by us.

Replace our use of _POSIX_C_SOURCE with _BSD_SOURCE plus _DEFAULT_SOURCE
to make things work on SunOS.
  • Loading branch information
pks-t committed Oct 14, 2024
1 parent 6925d1d commit b9563a2
Showing 1 changed file with 2 additions and 1 deletion.
3 changes: 2 additions & 1 deletion clar.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
* For full terms see the included COPYING file.
*/

#define _BSD_SOURCE
#define _DARWIN_C_SOURCE
#define _POSIX_C_SOURCE 200809L
#define _DEFAULT_SOURCE

#include <errno.h>
#include <setjmp.h>
Expand Down

0 comments on commit b9563a2

Please sign in to comment.