For Programmers: Free Programming Magazines  


Home > Archive > Unix Programming > July 2004 > getpwnam problem









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author getpwnam problem
ultra

2004-07-21, 3:58 pm

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.


Aaron Isotton

2004-07-21, 3:58 pm

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
ultra

2004-07-21, 3:58 pm

>
> 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


But the problem is I always pass the same user(exist in the system) to
getpwnam, sometimes it works but sometimes it fails with NULL. Any idea?


Jim Hollenback

2004-07-21, 3:58 pm

ultra (ultraman@rogers_NOSPAM.com) wrote:
: >
: > 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");
: > }
: >

: But the problem is I always pass the same user(exist in the system) to
: getpwnam, sometimes it works but sometimes it fails with NULL. Any idea?

what is the errno when it fails?

--
Jim Hollenback
jholly@cup.hp.com
my opinion.
Dragan Cvetkovic

2004-07-21, 3:58 pm

jholly@cup.hp.com (Jim Hollenback) writes:

> ultra (ultraman@rogers_NOSPAM.com) wrote:
> : But the problem is I always pass the same user(exist in the system) to
> : getpwnam, sometimes it works but sometimes it fails with NULL. Any idea?
>
> what is the errno when it fails?


Apparently EINVAL. Here is the original message:

> 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.
>


--
Dragan Cvetkovic,

To be or not to be is true. G. Boole No it isn't. L. E. J. Brouwer

!!! Sender/From address is bogus. Use reply-to one !!!
ultra

2004-07-21, 3:58 pm

> what is the errno when it fails?
>
> --
> Jim Hollenback
> jholly@cup.hp.com
> my opinion.


it didn't set the errno when it failed...


Dragan Cvetkovic

2004-07-21, 3:58 pm

"ultra" <ultraman@rogers_NOSPAM.com> writes:

>
> it didn't set the errno when it failed...
>
>


Huh? What do you mean it didn't set the errno? Here is your original
message:

> I have a program that call getpwnam to get the user info, but sometimes
> the call returned NULL with errno EINVAL.





--
Dragan Cvetkovic,

To be or not to be is true. G. Boole No it isn't. L. E. J. Brouwer

!!! Sender/From address is bogus. Use reply-to one !!!
ultra

2004-07-21, 3:58 pm

ya.. I didn't errno=0 before the call at the begining. Once I set errno=0
before the call, it is still errno=0 after the call fail...

"Dragan Cvetkovic" <me@privacy.net> wrote in message
news:lmu0w1qjtq.fsf@privacy.net...
> "ultra" <ultraman@rogers_NOSPAM.com> writes:
>
>
> Huh? What do you mean it didn't set the errno? Here is your original
> message:
>
>
>
>
>
> --
> Dragan Cvetkovic,
>
> To be or not to be is true. G. Boole No it isn't. L. E. J. Brouwer
>
> !!! Sender/From address is bogus. Use reply-to one !!!



Chuck Dillon

2004-07-22, 3:58 pm

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.
>
>


Is the program threaded? Try getpwnam_r?

-- ced

--
Chuck Dillon
Senior Software Engineer
NimbleGen Systems Inc.
Aaron Isotton

2004-07-23, 8:57 am

ultra wrote:
> ya.. I didn't errno=0 before the call at the begining. Once I set errno=0
> before the call, it is still errno=0 after the call fail...


In this case, the system thinks that the user doesn't exist. Maybe
there's some problem with your NIS configuration.

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







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com