New module 'getlogin'.
authorBruno Haible <bruno@clisp.org>
Sat, 9 Jan 2010 15:17:50 +0000 (16:17 +0100)
committerBruno Haible <bruno@clisp.org>
Sat, 9 Jan 2010 15:17:50 +0000 (16:17 +0100)
ChangeLog
doc/posix-functions/getlogin.texi
lib/getlogin.c [new file with mode: 0644]
lib/unistd.in.h
m4/getlogin.m4 [new file with mode: 0644]
m4/unistd_h.m4
modules/getlogin [new file with mode: 0644]
modules/unistd

index 8f22273..fa51e14 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,19 @@
 2010-01-09  Bruno Haible  <bruno@clisp.org>
 
+       New module 'getlogin'.
+       * lib/unistd.in.h (getlogin): New declaration.
+       * lib/getlogin.c: New file.
+       * m4/getlogin.m4: New file.
+       * m4/unistd_h.m4 (gl_UNISTD_H_DEFAULTS): Initialize GNULIB_GETLOGIN,
+       HAVE_GETLOGIN.
+       * modules/unistd (Makefile.am): Substitute GNULIB_GETLOGIN,
+       HAVE_GETLOGIN.
+       * modules/getlogin: New file.
+       * doc/posix-functions/getlogin.texi: Mention the new module.
+       Reported by John W. Eaton <jwe@gnu.org>.
+
+2010-01-09  Bruno Haible  <bruno@clisp.org>
+
        getlogin_r: Support for native Windows.
        * lib/getlogin_r.c: Include <windows.h>
        (getlogin_r): Implement for native Windows.
index 1c422c0..b7aebed 100644 (file)
@@ -4,15 +4,15 @@
 
 POSIX specification: @url{http://www.opengroup.org/onlinepubs/9699919799/functions/getlogin.html}
 
-Gnulib module: ---
+Gnulib module: getlogin
 
 Portability problems fixed by Gnulib:
 @itemize
+@item
+This function is missing on some platforms:
+mingw.
 @end itemize
 
 Portability problems not fixed by Gnulib:
 @itemize
-@item
-This function is missing on some platforms:
-mingw.
 @end itemize
diff --git a/lib/getlogin.c b/lib/getlogin.c
new file mode 100644 (file)
index 0000000..6c79de6
--- /dev/null
@@ -0,0 +1,41 @@
+/* Provide a working getlogin for systems which lack it.
+
+   Copyright (C) 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
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+/* Written by Bruno Haible, 2010.  */
+
+#include <config.h>
+
+/* Specification.  */
+#include <unistd.h>
+
+#if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
+# define WIN32_LEAN_AND_MEAN
+# include <windows.h>
+#endif
+
+char *
+getlogin (void)
+{
+#if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
+  static char login_name[1024];
+  DWORD sz = sizeof (login_name);
+
+  if (GetUserName (login_name, &sz))
+    return login_name;
+#endif
+  return NULL;
+}
index 3e22d8d..a353b0c 100644 (file)
@@ -476,6 +476,29 @@ extern int gethostname(char *name, size_t len) _GL_ARG_NONNULL ((1));
 #endif
 
 
+#if @GNULIB_GETLOGIN@
+/* Returns the user's login name, or NULL if it cannot be found.  Upon error,
+   returns NULL with errno set.
+
+   See <http://www.opengroup.org/susv3xsh/getlogin.html>.
+
+   Most programs don't need to use this function, because the information is
+   available through environment variables:
+     ${LOGNAME-$USER}        on Unix platforms,
+     $USERNAME               on native Windows platforms.
+ */
+# if !@HAVE_GETLOGIN@
+extern char *getlogin (void);
+# endif
+#elif defined GNULIB_POSIXCHECK
+# undef getlogin
+# define getlogin() \
+    (GL_LINK_WARNING ("getlogin is unportable - " \
+                      "use gnulib module getlogin for portability"), \
+     getlogin ())
+#endif
+
+
 #if @GNULIB_GETLOGIN_R@
 /* Copies the user's login name to NAME.
    The array pointed to by NAME has room for SIZE bytes.
diff --git a/m4/getlogin.m4 b/m4/getlogin.m4
new file mode 100644 (file)
index 0000000..e47967d
--- /dev/null
@@ -0,0 +1,15 @@
+# getlogin.m4 serial 1
+dnl Copyright (C) 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,
+dnl with or without modifications, as long as this notice is preserved.
+
+AC_DEFUN([gl_GETLOGIN],
+[
+  AC_REQUIRE([gl_UNISTD_H_DEFAULTS])
+  AC_CHECK_FUNCS_ONCE([getlogin])
+  if test $ac_cv_func_getlogin = no; then
+    AC_LIBOBJ([getlogin])
+    HAVE_GETLOGIN=0
+  fi
+])
index ff522b0..f3074ea 100644 (file)
@@ -1,4 +1,4 @@
-# unistd_h.m4 serial 36
+# unistd_h.m4 serial 37
 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,
@@ -48,6 +48,7 @@ AC_DEFUN([gl_UNISTD_H_DEFAULTS],
   GNULIB_GETDTABLESIZE=0;    AC_SUBST([GNULIB_GETDTABLESIZE])
   GNULIB_GETGROUPS=0;        AC_SUBST([GNULIB_GETGROUPS])
   GNULIB_GETHOSTNAME=0;      AC_SUBST([GNULIB_GETHOSTNAME])
+  GNULIB_GETLOGIN=0;         AC_SUBST([GNULIB_GETLOGIN])
   GNULIB_GETLOGIN_R=0;       AC_SUBST([GNULIB_GETLOGIN_R])
   GNULIB_GETPAGESIZE=0;      AC_SUBST([GNULIB_GETPAGESIZE])
   GNULIB_GETUSERSHELL=0;     AC_SUBST([GNULIB_GETUSERSHELL])
@@ -82,6 +83,7 @@ AC_DEFUN([gl_UNISTD_H_DEFAULTS],
   HAVE_GETDTABLESIZE=1;   AC_SUBST([HAVE_GETDTABLESIZE])
   HAVE_GETGROUPS=1;       AC_SUBST([HAVE_GETGROUPS])
   HAVE_GETHOSTNAME=1;     AC_SUBST([HAVE_GETHOSTNAME])
+  HAVE_GETLOGIN=1;        AC_SUBST([HAVE_GETLOGIN])
   HAVE_GETPAGESIZE=1;     AC_SUBST([HAVE_GETPAGESIZE])
   HAVE_GETUSERSHELL=1;    AC_SUBST([HAVE_GETUSERSHELL])
   HAVE_LCHOWN=1;          AC_SUBST([HAVE_LCHOWN])
diff --git a/modules/getlogin b/modules/getlogin
new file mode 100644 (file)
index 0000000..1e0c77a
--- /dev/null
@@ -0,0 +1,24 @@
+Description:
+getlogin() function: Get user name.
+
+Files:
+lib/getlogin.c
+m4/getlogin.m4
+
+Depends-on:
+unistd
+
+configure.ac:
+gl_GETLOGIN
+gl_UNISTD_MODULE_INDICATOR([getlogin])
+
+Makefile.am:
+
+Include:
+<unistd.h>
+
+License:
+LGPLv2+
+
+Maintainer:
+all
index 3ee8f31..f557837 100644 (file)
@@ -42,6 +42,7 @@ unistd.h: unistd.in.h $(LINK_WARNING_H) $(ARG_NONNULL_H)
              -e 's|@''GNULIB_GETDTABLESIZE''@|$(GNULIB_GETDTABLESIZE)|g' \
              -e 's|@''GNULIB_GETGROUPS''@|$(GNULIB_GETGROUPS)|g' \
              -e 's|@''GNULIB_GETHOSTNAME''@|$(GNULIB_GETHOSTNAME)|g' \
+             -e 's|@''GNULIB_GETLOGIN''@|$(GNULIB_GETLOGIN)|g' \
              -e 's|@''GNULIB_GETLOGIN_R''@|$(GNULIB_GETLOGIN_R)|g' \
              -e 's|@''GNULIB_GETPAGESIZE''@|$(GNULIB_GETPAGESIZE)|g' \
              -e 's|@''GNULIB_GETUSERSHELL''@|$(GNULIB_GETUSERSHELL)|g' \
@@ -75,6 +76,7 @@ unistd.h: unistd.in.h $(LINK_WARNING_H) $(ARG_NONNULL_H)
              -e 's|@''HAVE_GETDTABLESIZE''@|$(HAVE_GETDTABLESIZE)|g' \
              -e 's|@''HAVE_GETGROUPS''@|$(HAVE_GETGROUPS)|g' \
              -e 's|@''HAVE_GETHOSTNAME''@|$(HAVE_GETHOSTNAME)|g' \
+             -e 's|@''HAVE_GETLOGIN''@|$(HAVE_GETLOGIN)|g' \
              -e 's|@''HAVE_GETPAGESIZE''@|$(HAVE_GETPAGESIZE)|g' \
              -e 's|@''HAVE_GETUSERSHELL''@|$(HAVE_GETUSERSHELL)|g' \
              -e 's|@''HAVE_LCHOWN''@|$(HAVE_LCHOWN)|g' \