Philip Guenther wrote: > On Mon, May 6, 2013 at 6:06 PM, Friedrich Locke > <friedrich.lo...@gmail.com> wrote: >> I am in need to write a simple program to return the passwd entry for a >> given uid number. >> >> Here you have it: >> >> #include <sys/types.h> >> #include <errno.h> >> #include <pwd.h> >> #include <stdio.h> >> >> int >> main(int argc, char **argv) >> { >> struct passwd *p; >> int e; >> >> e = errno, errno = 0; >> p = getpwuid(0); >> if (errno) { > > This isn't right. To test for whether getpwuid() found an entry for > the UID, test whether its return value is not NULL. If it found the > UID it'll return non-NULL. If it didn't find the UID but didn't hit > any error (it could read the passwd file, etc), then it will return > NULL and not change errno. Only if it didn't find it because of an > error will it set errno. >
To be clear, are you sure about this ? The way I read the man page on my OpenBSD 5.2 system, as well as on the www.openbsd.org web site, errno has no specific meaning when getpwuid returns. It only tells you whether it succeeded or not, it doesn't say it sets errno, nor does it provide a clear way to determine why the function didn't succeed. (however, if you're right the man page may be lacking)