X-Git-Url: http://erislabs.net/gitweb/?a=blobdiff_plain;f=lib%2Fgetlogin_r.c;h=bb89f6e6028467d8c221518cbd86bf0813c5e34a;hb=20229ba96d971f97ff03ab74eeb4e7de1acccf98;hp=5683504c7d777ba0b459d3821e43d78c3760a2a3;hpb=118ba24d049832db7affcd30bed4209cb37b2b30;p=gnulib.git diff --git a/lib/getlogin_r.c b/lib/getlogin_r.c index 5683504c7..bb89f6e60 100644 --- a/lib/getlogin_r.c +++ b/lib/getlogin_r.c @@ -1,6 +1,6 @@ /* Provide a working getlogin_r for systems which lack it. - Copyright (C) 2005 Free Software Foundation, Inc. + Copyright (C) 2005, 2006, 2007 Free Software Foundation, Inc. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -20,40 +20,37 @@ #include -#include "getlogin_r.h" +/* Specification. */ +#include #include #include -#if HAVE_UNISTD_H -# include -#endif - #if !HAVE_DECL_GETLOGIN char *getlogin (void); #endif -/* See getlogin_r.h for documentation. */ +/* See unistd_.h for documentation. */ int getlogin_r (char *name, size_t size) { char *n; - int save_errno = errno; + size_t nlen; 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; + + /* A system function like getlogin_r is never supposed to set errno + to zero, so make sure errno is nonzero here. ENOENT is a + reasonable errno value if getlogin returns NULL. */ + if (!errno) + errno = ENOENT; + + if (!n) + return errno; + nlen = strlen (n); + if (size <= nlen) + return ERANGE; + memcpy (name, n, nlen + 1); + return 0; }