I wonder about fgetpwent()'s memory ownership.  Its man page says it can fail with "ENOMEM Insufficient memory to allocate passwd structure" but says nothing about deallocation; the getpwent() man page has the same error and explicitly says to not call free() on the returned pointer, so surely the same applies here.  Yet, if the memory is allocated, its release must be done by later fgetpwent() calls, until one of them returns NULL and all memory has been released.  The getpwent man page also says the pointer may be to static memory and may be tromped by later calls, so we can't simply record the correct answer and then read on to the end of file, to ensure we get to that NULL return; furthermore, if this function can ever be called by more than one thread, a thread may see false information due to another calling between the first finding its entry and using it.  The fgetpwent_r() manpage says fgetpwent() actually does use a static buffer; but reveals that some of the fields in the struct passwd we're returning are flexible-sized strings, so either it has another static buffer from which to allocate those or it's using malloc() for them.

Perhaps it would be better to use fgetpwent_r() instead.