View Single Post

   
  #2 (permalink)  
Old 01-16-2008, 07:15 PM
Aaron Isotton
 
Posts: n/a
Default Re: getpwnam problem

ultra wrote:
> Hi there,
> I have a program that call getpwnam to get the user info, but sometimes
> the call returned NULL with errno EINVAL. I always pass the same user to the
> call. Anyone know what is the reason? I'm running HP11.00 and NIS.
>
>


getpwnam() returns NULL if:

a) the user doesn't exits
b) an error occurred

When an error occurred, errno is set appropriately. When the user
doesn't exist, errno is NOT set. Thus you want to use some code as follows:

struct passwd *pw;

/* we reset errno so we can tell whether an error happens in getpwnam()
* or not */
errno = 0;
pw = getpwnam("some_user");
if (!pw) {
if (errno) fprintf(stderr, "error: %s\n", strerror(errno));
else fprintf(stderr, "no such user\n");
}

Greetings,
Aaron
--
Aaron Isotton | http://www.isotton.com/
You know it's Monday when you wake up and it's Tuesday. -- Garfield
Reply With Quote