_POSIX_C_SOURCE and S_IFMT macros

Xin Li delphij at delphij.net
Mon Jan 25 09:04:30 UTC 2021


Hi,

I recently discovered [1] a difference between FreeBSD's handling of
_POSIX_C_SOURCE and what I have observe on Linux and macOS systems,
basically, if a program defines _POSIX_C_SOURCE to 200809L, but did not
define _XOPEN_SOURCE, we will not define S_IFMT when sys/stat.h is included.

It appears that the glibc version of sys/stat.h won't define it either,
but their fcntl.h would define it by having _POSIX_C_SOURCE >= 200809L
to define _USE_XOPEN2K8 and expose it.

A quick glance on my Macbook suggests that they were defining the S_IFMT
macros unconditionally.

Based on my research, hiding these macros started in revision 40491 of
CSRG repository (
https://svnweb.freebsd.org/csrg?view=revision&revision=40491 ); NetBSD
chose to make the macro visible when _XOPEN_SOURCE in revision 1.34 and
expanded the visibility scope to also cover _NETBSD_SOURCE.

Do we need to hide these with 200809L when _XOPEN_SOURCE is not defined?
 If I'm reading the standards correctly, I think we are required to
define these in modern POSIX revisions?



[1] When building pigz (https://github.com/madler/pigz), I got:

cc  -O3 -Wall -Wextra -Wno-unknown-pragmas -Wcast-qual -c pigz.c -o pigz.o
pigz.c:3817:47: error: use of undeclared identifier 'S_IFMT'
    if (stat(from, &st) != 0 || (st.st_mode & S_IFMT) != S_IFREG)
                                              ^
[...]


-------------- next part --------------
A non-text attachment was scrubbed...
Name: OpenPGP_signature
Type: application/pgp-signature
Size: 840 bytes
Desc: OpenPGP digital signature
URL: <http://lists.freebsd.org/pipermail/freebsd-standards/attachments/20210125/1436bec6/attachment.sig>


More information about the freebsd-standards mailing list