Am 07.01.2019 um 19:40 hat Richard W.M. Jones geschrieben: > On Mon, Jan 07, 2019 at 06:50:53PM +0100, Max Reitz wrote: > [...] > > I don't particularly care how we fix this, but it breaks the nbdkit > tests on FreeBSD so I am keen to fix it one way or another. > > > And if optreset not being available for glibc is the only issue, I'd say > > adding it as a weak global variable would work without #ifdefs. > > The weak global variable doesn't make the code "#ifdef free". I tried > a patch like this: > > +int optreset __attribute__((weak)); > > ... > > static int command(...) > { > ... > optind = 0; > + optreset = 1; > ... > } > > but that still doesn't work on FreeBSD. > > You have to set optind=1 apparently. So if we want to set optreset=1 > we still end up with #ifdef __FreeBSD__. The final patch will > end up looking something like: > > static int command(...) > { > ... > +#ifdef __FreeBSD__ > + optind = 1; > + optreset = 1; > +#else > optind = 0; > +#endif > ... > }
Unconditionally setting optind = 1 looks fine. I would, however, quote a different part of the glibc man page (in addition or instead of the paragraph you already quoted): The variable optind is the index of the next element to be processed in argv. The system initializes this value to 1. The caller can reset it to 1 to restart scanning of the same argv, or when scanning a new argument vector. This makes it pretty clear that optind = 1 is fine for our case with glibc. The FreeBSD man page still suggests that we need optreset = 1, so I suppose we'd end up with something like: ... optind = 1; #ifdef __FreeBSD__ optreset = 1; #endif ... I think I slightly prefer the #ifdef to a weak variable, but that's another option. Kevin