[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [bug-gnulib] New getlogin_r module
From: |
Paul Eggert |
Subject: |
Re: [bug-gnulib] New getlogin_r module |
Date: |
Wed, 25 May 2005 12:18:09 -0700 |
User-agent: |
Gnus/5.1006 (Gnus v5.10.6) Emacs/21.4 (gnu/linux) |
Derek Price <address@hidden> writes:
> 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.
But glibc getlogin_r doesn't always set errno to the returned value,
and POSIX doesn't promise it, so such code is already broken.
> 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.
We should return a valid errno value, since POSIX requires that
we return an error number, and -1 is not an error number. Returning
ENOENT is fine.
A couple of other glitches I just noticed. getlogin_r.h uses size_t
without necessarily defining it. We shouldn't include <config.h>
without checking for HAVE_CONFIG_H.
I installed this change:
Index: lib/getlogin_r.h
===================================================================
RCS file: /cvsroot/gnulib/gnulib/lib/getlogin_r.h,v
retrieving revision 1.1
diff -p -u -r1.1 getlogin_r.h
--- lib/getlogin_r.h 25 May 2005 14:21:20 -0000 1.1
+++ lib/getlogin_r.h 25 May 2005 19:12:57 -0000
@@ -33,5 +33,6 @@
See <http://www.opengroup.org/onlinepubs/009695399/functions/getlogin.html>.
*/
+# include <stddef.h>
int getlogin_r (char *name, size_t size);
#endif
Index: lib/getlogin_r.c
===================================================================
RCS file: /cvsroot/gnulib/gnulib/lib/getlogin_r.c,v
retrieving revision 1.1
diff -p -u -r1.1 getlogin_r.c
--- lib/getlogin_r.c 25 May 2005 14:21:20 -0000 1.1
+++ lib/getlogin_r.c 25 May 2005 19:12:57 -0000
@@ -18,7 +18,9 @@
/* written by Paul Eggert and Derek Price */
-#include <config.h>
+#if HAVE_CONFIG_H
+# include <config.h>
+#endif
#include "getlogin_r.h"
@@ -38,22 +40,15 @@ 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;
+ if (!n)
+ return errno ? errno : ENOENT;
+ nlen = strlen (n);
+ if (size <= nlen)
+ return ERANGE;
+ memcpy (name, n, nlen + 1);
+ return 0;
}
- [bug-gnulib] Re: New getlogin_r module, (continued)