[PATCH] linedit, deluser: use POSIX getpwent instead of getpwent_r

Rich Felker dalias at libc.org
Sat Feb 7 17:54:55 UTC 2015


On Sat, Feb 07, 2015 at 09:49:19AM -0800, Isaac Dunham wrote:
> On Thu, Feb 05, 2015 at 03:52:24PM -0500, Rich Felker wrote:
> > On Thu, Feb 05, 2015 at 09:42:08PM +0100, Denys Vlasenko wrote:
> > > struct passwd *getpwent()
> > > {
> > >         static char *line;
> > >         static struct passwd pw;
> > >         size_t size=0;
> > >         if (!f) f = fopen("/etc/passwd", "rbe");
> > >         if (!f) return 0;
> > >         return __getpwent_a(f, &pw, &line, &size);
> > > }
> > > 
> > > I would prefer that even "struct passwd" is malloced...
> > 
> > I don't think it would make much practical difference. It could be
> > changed though.
> > 
> > > But more importantly, bbox can't optimize only for musl.
> > > Other libc'es  may have static line buffers there.
> > > 
> > > And musl will eventually be forced to implement getpwent_r()
> > > if it wants to be usable for more packages... so...
> > 
> > getpwent_r makes no sense; the _r functions are for thread-safe
> > versions of their corresponding legacy functions, but getpwent_r has
> > inherent global state -- the iterator. Whoever made it just wasn't
> > thinking. To make a correct interface like this the caller would need
> > to have an iterator object to pass to the function, but I can't see
> > much merit in inventing a new interface for this.
> 
> Besides having hidden global state, the man page notes:
>  Other systems  use  the prototype
>   struct passwd *getpwent_r(struct passwd *pwd, char *buf, int buflen);
>  or, better,
>   int getpwent_r(struct passwd *pwd, char *buf, int buflen, FILE **pw_fp);
> 
> In other words, according to the manpage, getpwent_r() is decidedly
> unportable.
> 
> Per my investigations, Dragonfly/Net/FreeBSD seem to use the same
> prototype as glibc; apparently Solaris uses the first alternate prototype;
> and the last mentioned seems to be a reference to Tru64, which
> uses pw_fp to keep track of its position instead of an iterator.
> 
> OpenBSD and MirBSD do not implement getpwent_r, as far as I can tell.

It should be noted here that multiple conflicting historical
definitions of a nonstandard interface are one of the big exclusion
criteria musl goes by.

Rich


More information about the busybox mailing list