This is a discussion on getpwnam problem within the HP-UX Operating System forums, part of the Unix Operating Systems category; --> Hi there, I have a program that call getpwnam to get the user info, but sometimes the call returned ...
| |||||||
| Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| |||
| 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 |
| |||
| > > 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? |
| |||
| 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. |
| |||
| 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 !!! |
| |||
| > what is the errno when it fails? > > -- > Jim Hollenback > jholly@cup.hp.com > my opinion. it didn't set the errno when it failed... |
| |||
| "ultra" <ultraman@rogers_NOSPAM.com> writes: >> what is the errno when it fails? > > 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 !!! |
| |||
| 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: > > >> what is the errno when it fails? > > > > 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 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. |
| ||||
| 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 |