getlogin_r: Work around portability problem on OSF/1.
authorBruno Haible <bruno@clisp.org>
Wed, 22 Dec 2010 18:25:34 +0000 (19:25 +0100)
committerBruno Haible <bruno@clisp.org>
Wed, 22 Dec 2010 18:25:34 +0000 (19:25 +0100)
* m4/getlogin_r.m4 (gl_FUNC_GETLOGIN_R): Detect the OSF/1 problem.
* lib/unistd.in.h (getlogin_r): Replace if REPLACE_GETLOGIN_R is set.
* lib/getlogin_r.c (getlogin_r): When getlogin_r exists, invoke it and
test for a truncated result.
* m4/unistd_h.m4 (gl_UNISTD_H_DEFAULTS): Initialize REPLACE_GETLOGIN_R.
* modules/unistd (Makefile.am): Substitute REPLACE_GETLOGIN_R.
* modules/getlogin_r (Depends-on): Add memchr.
* doc/posix-functions/getlogin_r.texi: Mention the OSF/1 problem.

ChangeLog
doc/posix-functions/getlogin_r.texi
lib/getlogin_r.c
lib/unistd.in.h
m4/getlogin_r.m4
m4/unistd_h.m4
modules/getlogin_r
modules/unistd

index db2c7ad..80d4552 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,17 @@
 2010-12-22  Bruno Haible  <bruno@clisp.org>
 
+       getlogin_r: Work around portability problem on OSF/1.
+       * m4/getlogin_r.m4 (gl_FUNC_GETLOGIN_R): Detect the OSF/1 problem.
+       * lib/unistd.in.h (getlogin_r): Replace if REPLACE_GETLOGIN_R is set.
+       * lib/getlogin_r.c (getlogin_r): When getlogin_r exists, invoke it and
+       test for a truncated result.
+       * m4/unistd_h.m4 (gl_UNISTD_H_DEFAULTS): Initialize REPLACE_GETLOGIN_R.
+       * modules/unistd (Makefile.am): Substitute REPLACE_GETLOGIN_R.
+       * modules/getlogin_r (Depends-on): Add memchr.
+       * doc/posix-functions/getlogin_r.texi: Mention the OSF/1 problem.
+
+2010-12-22  Bruno Haible  <bruno@clisp.org>
+
        ptsname: Avoid test failure on OSF/1 5.1.
        * modules/ptsname-tests (Depends-on): Add 'same-inode'.
        * tests/test-ptsname.c: Include <sys/stat.h>, same-inode.h.
index c162b7d..66692ce 100644 (file)
@@ -18,6 +18,10 @@ HP-UX 11.
 @item
 This function has an incompatible declaration on some platforms:
 Solaris 11 2010-11 (when @code{_POSIX_PTHREAD_SEMANTICS} is not defined).
+@item
+This function returns a truncated result, instead of failing with error code
+@code{ERANGE}, when the buffer is not large enough, on some platforms:
+OSF/1 5.1.
 @end itemize
 
 Portability problems not fixed by Gnulib:
index c1e7435..27b8404 100644 (file)
@@ -1,6 +1,6 @@
 /* Provide a working getlogin_r for systems which lack it.
 
-   Copyright (C) 2005-2007, 2009-2010 Free Software Foundation, Inc.
+   Copyright (C) 2005-2007, 2010 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
@@ -39,6 +39,7 @@ extern char *getlogin (void);
 int
 getlogin_r (char *name, size_t size)
 {
+#undef getlogin_r
 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
   /* Native Windows platform.  */
   DWORD sz;
@@ -59,6 +60,14 @@ getlogin_r (char *name, size_t size)
         return ENOENT;
     }
   return 0;
+#elif HAVE_GETLOGIN_R
+  /* Platform with a getlogin_r() function.  */
+  int ret = getlogin_r (name, size);
+
+  if (ret == 0 && memchr (name, '\0', size) == NULL)
+    /* name contains a truncated result.  */
+    return ERANGE;
+  return ret;
 #else
   /* Platform with a getlogin() function.  */
   char *n;
index 40ac378..1dd06bf 100644 (file)
@@ -712,13 +712,22 @@ _GL_WARN_ON_USE (getlogin, "getlogin is unportable - "
      ${LOGNAME-$USER}        on Unix platforms,
      $USERNAME               on native Windows platforms.
  */
-# if !@HAVE_DECL_GETLOGIN_R@
+# if @REPLACE_GETLOGIN_R@
+#  if !(defined __cplusplus && defined GNULIB_NAMESPACE)
+#   define getlogin_r rpl_getlogin_r
+#  endif
+_GL_FUNCDECL_RPL (getlogin_r, int, (char *name, size_t size)
+                                   _GL_ARG_NONNULL ((1)));
+_GL_CXXALIAS_RPL (getlogin_r, int, (char *name, size_t size));
+# else
+#  if !@HAVE_DECL_GETLOGIN_R@
 _GL_FUNCDECL_SYS (getlogin_r, int, (char *name, size_t size)
                                    _GL_ARG_NONNULL ((1)));
-# endif
+#  endif
 /* Need to cast, because on Solaris 10 systems, the second argument is
                                                      int size.  */
 _GL_CXXALIAS_SYS_CAST (getlogin_r, int, (char *name, size_t size));
+# endif
 _GL_CXXALIASWARN (getlogin_r);
 #elif defined GNULIB_POSIXCHECK
 # undef getlogin_r
index 077ef81..9cda9a4 100644 (file)
@@ -1,4 +1,4 @@
-#serial 8
+#serial 9
 
 # Copyright (C) 2005-2007, 2009-2010 Free Software Foundation, Inc.
 #
@@ -27,6 +27,56 @@ AC_DEFUN([gl_FUNC_GETLOGIN_R],
 
   AC_CHECK_FUNCS_ONCE([getlogin_r])
   if test $ac_cv_func_getlogin_r = no; then
+    HAVE_GETLOGIN_R=0
+  else
+    HAVE_GETLOGIN_R=1
+    dnl On OSF/1 5.1, getlogin_r returns a truncated result if the buffer is
+    dnl not large enough.
+    AC_REQUIRE([AC_CANONICAL_HOST])
+    AC_CACHE_CHECK([whether getlogin_r works with small buffers],
+      [gl_cv_func_getlogin_r_works],
+      [
+        dnl Initial guess, used when cross-compiling.
+changequote(,)dnl
+        case "$host_os" in
+                # Guess no on OSF/1.
+          osf*) gl_cv_func_getlogin_r_works="guessing no" ;;
+                # Guess yes otherwise.
+          *)    gl_cv_func_getlogin_r_works="guessing yes" ;;
+        esac
+changequote([,])dnl
+        AC_RUN_IFELSE(
+          [AC_LANG_SOURCE([[
+#include <stddef.h>
+#include <unistd.h>
+#if !HAVE_DECL_GETLOGIN_R
+extern int getlogin_r (char *, size_t);
+#endif
+int
+main (void)
+{
+  int result = 0;
+  char buf[100];
+
+  if (getlogin_r (buf, 0) == 0)
+    result |= 16;
+  if (getlogin_r (buf, 1) == 0)
+    result |= 17;
+  return result;
+}]])],
+          [gl_cv_func_getlogin_r_works=yes],
+          [case $? in
+             16 | 17) gl_cv_func_getlogin_r_works=no ;;
+           esac
+          ],
+          [:])
+      ])
+    case "$gl_cv_func_getlogin_r_works" in
+      *yes) ;;
+      *) REPLACE_GETLOGIN_R=1 ;;
+    esac
+  fi
+  if test $HAVE_GETLOGIN_R = 0 || test $REPLACE_GETLOGIN_R = 1; then
     AC_LIBOBJ([getlogin_r])
     gl_PREREQ_GETLOGIN_R
   fi
index 27d96ff..bd34773 100644 (file)
@@ -1,4 +1,4 @@
-# unistd_h.m4 serial 51
+# unistd_h.m4 serial 52
 dnl Copyright (C) 2006-2010 Free Software Foundation, Inc.
 dnl This file is free software; the Free Software Foundation
 dnl gives unlimited permission to copy and/or distribute it,
@@ -140,6 +140,7 @@ AC_DEFUN([gl_UNISTD_H_DEFAULTS],
   REPLACE_FCHOWNAT=0;     AC_SUBST([REPLACE_FCHOWNAT])
   REPLACE_GETCWD=0;       AC_SUBST([REPLACE_GETCWD])
   REPLACE_GETDOMAINNAME=0; AC_SUBST([REPLACE_GETDOMAINNAME])
+  REPLACE_GETLOGIN_R=0;   AC_SUBST([REPLACE_GETLOGIN_R])
   REPLACE_GETGROUPS=0;    AC_SUBST([REPLACE_GETGROUPS])
   REPLACE_GETPAGESIZE=0;  AC_SUBST([REPLACE_GETPAGESIZE])
   REPLACE_LCHOWN=0;       AC_SUBST([REPLACE_LCHOWN])
index 13c287f..1d5c20b 100644 (file)
@@ -8,6 +8,7 @@ m4/getlogin_r.m4
 Depends-on:
 extensions
 unistd
+memchr
 
 configure.ac:
 gl_FUNC_GETLOGIN_R
index 536e64c..df0870e 100644 (file)
@@ -114,6 +114,7 @@ unistd.h: unistd.in.h $(CXXDEFS_H) $(ARG_NONNULL_H) $(WARN_ON_USE_H)
              -e 's|@''REPLACE_FCHOWNAT''@|$(REPLACE_FCHOWNAT)|g' \
              -e 's|@''REPLACE_GETCWD''@|$(REPLACE_GETCWD)|g' \
              -e 's|@''REPLACE_GETDOMAINNAME''@|$(REPLACE_GETDOMAINNAME)|g' \
+             -e 's|@''REPLACE_GETLOGIN_R''@|$(REPLACE_GETLOGIN_R)|g' \
              -e 's|@''REPLACE_GETGROUPS''@|$(REPLACE_GETGROUPS)|g' \
              -e 's|@''REPLACE_GETPAGESIZE''@|$(REPLACE_GETPAGESIZE)|g' \
              -e 's|@''REPLACE_LCHOWN''@|$(REPLACE_LCHOWN)|g' \