# HG changeset patch # User Bruno Haible # Date 1263050270 -3600 # Node ID e61123383caf43f2b674197684e05528d0c68cb4 # Parent 62e2caeb3995c034f57cef8c56e4cda0e648bc63 New module 'getlogin'. diff --git a/ChangeLog b/ChangeLog --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,17 @@ +2010-01-09 Bruno Haible + + 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 . + 2010-01-09 Bruno Haible getlogin_r: Support for native Windows. diff --git a/doc/posix-functions/getlogin.texi b/doc/posix-functions/getlogin.texi --- a/doc/posix-functions/getlogin.texi +++ b/doc/posix-functions/getlogin.texi @@ -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 -@end itemize - -Portability problems not fixed by Gnulib: -@itemize @item This function is missing on some platforms: mingw. @end itemize + +Portability problems not fixed by Gnulib: +@itemize +@end itemize diff --git a/lib/getlogin.c b/lib/getlogin.c new file mode 100644 --- /dev/null +++ b/lib/getlogin.c @@ -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 . */ + +/* Written by Bruno Haible, 2010. */ + +#include + +/* Specification. */ +#include + +#if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__ +# define WIN32_LEAN_AND_MEAN +# include +#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; +} diff --git a/lib/unistd.in.h b/lib/unistd.in.h --- a/lib/unistd.in.h +++ b/lib/unistd.in.h @@ -476,6 +476,29 @@ #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 . + + 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 --- /dev/null +++ b/m4/getlogin.m4 @@ -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 +]) diff --git a/m4/unistd_h.m4 b/m4/unistd_h.m4 --- a/m4/unistd_h.m4 +++ b/m4/unistd_h.m4 @@ -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 @@ 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 @@ 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 --- /dev/null +++ b/modules/getlogin @@ -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: + + +License: +LGPLv2+ + +Maintainer: +all diff --git a/modules/unistd b/modules/unistd --- a/modules/unistd +++ b/modules/unistd @@ -42,6 +42,7 @@ -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 @@ -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' \