_POSIX_C_SOURCE and S_IFMT macros

Tijl Coosemans tijl at FreeBSD.org
Mon Jan 25 19:42:31 UTC 2021


On Mon, 25 Jan 2021 01:04:25 -0800 Xin Li via freebsd-standards
<freebsd-standards at freebsd.org> wrote:
> 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)
>                                               ^
> [...]

S_IFMT is marked as an XSI extension so _XOPEN_SOURCE needs to be
defined to use it.  POSIX programs are supposed to use the equivalent
function-like macros, so in this case S_ISREG(st.st_mode) == 0.

https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/sys_stat.h.html


More information about the freebsd-standards mailing list