Re: oddness from getgrnam()

1999-04-17 Thread Miquel van Smoorenburg
In article [EMAIL PROTECTED],
Shaleh  [EMAIL PROTECTED] wrote:
I was testing a program and to see how it handled an invalid group I did:

gr = getgrnam(bob);

Now obviously this failed.  However the string from perror() states:

Could not find file or directory


The manual page for getgrnam(2) states the ENOMEM is the only valid
errno getgrnam() can return.

So that should be:

gr = getgrnam(bob);
if (gr == NULL) {
if (errno == ENOMEM)
perror(getgrnam);
else
fprintf(stderr, getgrnam: bob not found\n);
}

Why is this?  Seems like a counter intuitive error.
(Yes I know UNIX is sometimes like that, but this one is odd)

Basically in non-system calls (not section 2 of the manual) you cannot
use errno generically. It's just a numeric value which in a lot of
cases only makes sense with system calls.

getgrnam() probably set it to ENOENT, which is the most sensible
thing to do, but the correspondig string doesn't make much sense
for a non-file related call.

Mike.
-- 
Indifference will certainly be the downfall of mankind, but who cares?


oddness from getgrnam()

1999-04-16 Thread Shaleh
I was testing a program and to see how it handled an invalid group I did:

gr = getgrnam(bob);

Now obviously this failed.  However the string from perror() states:

Could not find file or directory

Why is this?  Seems like a counter intuitive error.

(Yes I know UNIX is sometimes like that, but this one is odd)


Re: oddness from getgrnam()

1999-04-16 Thread E.L. Meijer \(Eric\)
 
 I was testing a program and to see how it handled an invalid group I did:
 
 gr = getgrnam(bob);
 
 Now obviously this failed.  However the string from perror() states:
 
 Could not find file or directory
 
 Why is this?  Seems like a counter intuitive error.

I think the error is not set by getgrnam().  If I run


#include stdio.h
#include stdlib.h
#include grp.h

int main(void)
{
  struct group* gr;
  perror(before);
  gr = getgrnam(tgakem);
  perror(after);

  return 0;
}


it always prints

before: Success
after: No such file or directory

It makes no difference if tgakem exists (which it happens to do) or
if it doesn't.  My guess is that getgrnam tries to look for group
descriptions in several files, of which at least one does not exist.

If I run the above program with strace (strace ./prg) and grep for
`open', I get

open(/etc/ld.so.preload, O_RDONLY)= -1 ENOENT (No such file or directory)
open(/etc/ld.so.cache, O_RDONLY)  = 3
open(/lib/libc.so.6, O_RDONLY)= 3
open(/etc/nsswitch.conf, O_RDONLY)= 3
open(/etc/ld.so.cache, O_RDONLY)  = 3
open(/lib/libnss_compat.so.1, O_RDONLY) = 3
open(/lib/libnsl.so.1, O_RDONLY)  = 3
open(/lib/libnss_files.so.1, O_RDONLY) = 3
open(/etc/group, O_RDONLY)= 3

As you can see the first file could not be opened.  I checked and it
indeed does not exist on my system, so that will have triggered the
error message.  The getgrnam function returns NULL on failure, and
apparently does not manipulate errno.

HTH,
Eric

-- 
 E.L. Meijer ([EMAIL PROTECTED])
 Eindhoven Univ. of Technology
 Lab. for Catalysis and Inorg. Chem. (SKA)