Re: [PATCH 3/3] tools/nolibc: simplify mode handling in open() and openat()

From: Willy Tarreau

Date: Mon Apr 27 2026 - 13:50:07 EST


Hi Thomas!

On Mon, Apr 27, 2026 at 07:28:22PM +0200, Thomas Weißschuh wrote:
> On 2026-04-19 22:16:01+0200, Thomas Weißschuh wrote:
> > On 2026-04-19 18:08:07+0200, Willy Tarreau wrote:
> > > On Sun, Apr 19, 2026 at 05:29:05PM +0200, Thomas Weißschuh wrote:
> > > The macro passes the "mode" argument to _open() when it's present, and when
> > > it's absent, it passes the next one in the _OPT_ARG() macro, which is zero.
> > >
> > > E.g.:
> > >
> > > #include <stdio.h>
> > >
> > > #define _OPT_ARG(a0, a1, ...) a1
> > > #define open(path, flags, mode...) _open(path, flags, _OPT_ARG(0, ##mode, 0))
> > >
> > > int _open(const char *path, int flags, int mode)
> > > {
> > > return printf("path=%s flags=%d mode=%d\n", path, flags, mode);
> > > }
> >
> > This is exactly what I tried to do. But ,## only works for the
> > non-first argument and I didn't think of adding a dummy argument, so I
> > ended up with __VA_OPT__.
> >
> > As open() is defined to be a function, I used the same name for both the
> > vararg macro and the underlying function. I slightly prefer that, but if
> > you object, let's rename it to _open().
> >
> > > int main(void)
> > > {
> > > open("file1", 12);
> > > open("file2", 34, 56);
> > > return 0;
> > > }
> > >
> > > $ ./a.out
> > > path=file1 flags=12 mode=0
> > > path=file2 flags=34 mode=56
> > >
> > > And this one works even on very old compilers (gcc-3.4 successfully tested).
> >
> > Let's use that.
>
> Sashiko rightfully complains[0] that '#define open()' will also apply to
> struct member functions called open(). So I'm holding off on this patch
> for now.

Ah indeed, that's a good point, I hadn't thought about these ones. And
yes, this can easily happen.

Willy