Date: Mon, 25 Jan 2021 20:42:25 +0100 From: =?UTF-8?B?VMSzbA==?= Coosemans <tijl@FreeBSD.org> To: Xin Li via freebsd-standards <freebsd-standards@freebsd.org> Cc: d@delphij.net, Jilles Tjoelker <jilles@FreeBSD.org>, Ed Maste <emaste@FreeBSD.org>, Xin Li <delphij@delphij.net>, Marshall Kirk McKusick <mckusick@freebsd.org> Subject: Re: _POSIX_C_SOURCE and S_IFMT macros Message-ID: <20210125204225.1a9e38d3@FreeBSD.org> In-Reply-To: <69977864-0a68-909d-8ecb-1d72c0ec0ef9@delphij.net> References: <69977864-0a68-909d-8ecb-1d72c0ec0ef9@delphij.net>
next in thread | previous in thread | raw e-mail | index | archive | help
On Mon, 25 Jan 2021 01:04:25 -0800 Xin Li via freebsd-standards <freebsd-standards@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
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20210125204225.1a9e38d3>