Bruno Haible wrote:

>Would you mind adding a bit of documentation? Something like
>

I've added the following to getlogin_r.h:

/* Copies the user's login name to NAME.
   The array pointed to by NAME has room for SIZE bytes.
         
   Returns 0 if successful.  Upon error, an error number is returned, or
-1 in
   the case that the login name cannot be found but no specific error is
   provided by getlogin (getlogin returned NULL and did not set errno - this
   case is hopefully rare but is left open by the POSIX spec).

   See
<http://www.opengroup.org/onlinepubs/009695399/functions/getlogin.html>.
 */


>The return value is incorrect, and the getlogin () return conventions are
>ignored.
>
>http://www.opengroup.org/onlinepubs/009695399/functions/getlogin.html
>says:
>
>  "Upon successful completion, getlogin() shall return a pointer to the login
>   name or a null pointer if the user's login name cannot be found.
>   Otherwise, it shall return a null pointer and set errno to indicate the
>   error."
>
>Which means, you need to set errno = 0 before calling getlogin(), to
>distinguish two of the three cases.
>
>And
>
>  "If successful, the getlogin_r() function shall return zero; otherwise,
>   an error number shall be returned to indicate the error."
>
>So, instead of "errno = ERANGE; return -1; " you need to do "return ERANGE;".
>  
>

This is what I installed.  It sets *and* returns errno since my local
man pages make it sound like errno will be set by getlogin_r and others
may already have code which checks errno and not getlogin_r's return
value.  I would note that the opengroup POSIX spec you reference does
not define an error return code for getlogin_r for the case where "the
user's login name cannot be found".  I chose -1 arbitrarily.  Glancing
at the code in glibc-2.3.5, it looks like it also sets and returns
errno, though they return ENOENT as an extension to the POSIX spec when
the login name cannot be found.

/* See getlogin_r.h for documentation.  */
int
getlogin_r (char *name, size_t size)
{
  char *n;
  int save_errno = errno;

  errno = 0;
  n = getlogin ();
  if (n)
    {
      size_t nlen = strlen (n);
      if (nlen < size)
        {
          memcpy (name, n, nlen + 1);
          return 0;
        }
      errno = ERANGE;
    }

  if (errno) return errno;
  errno = save_errno;
  return -1;
}


2005-05-25  Derek Price  <[EMAIL PROTECTED]>
            Paul Eggert  <[EMAIL PROTECTED]>

        * lib/getlogin_r.c, lib/getlogin_r.h, m4/getlogin_r.m4,
        modules/getlogin_r: New files.

Regards,

Derek



_______________________________________________
bug-gnulib mailing list
[email protected]
http://lists.gnu.org/mailman/listinfo/bug-gnulib

Reply via email to