* lib/getlogin_r.c, lib/getlogin_r.h, m4/getlogin_r.m4,
authorDerek R. Price <derek@ximbiot.com>
Wed, 25 May 2005 14:21:20 +0000 (14:21 +0000)
committerDerek R. Price <derek@ximbiot.com>
Wed, 25 May 2005 14:21:20 +0000 (14:21 +0000)
modules/getlogin_r: New files.

ChangeLog
lib/getlogin_r.c [new file with mode: 0644]
lib/getlogin_r.h [new file with mode: 0644]
m4/getlogin_r.m4 [new file with mode: 0644]
modules/getlogin_r [new file with mode: 0644]

index 40eb5a4..7d6aca4 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+2005-05-25  Derek Price  <derek@ximbiot.com>
+           Paul Eggert  <eggert@cs.ucla.edu>
+
+       * lib/getlogin_r.c, lib/getlogin_r.h, m4/getlogin_r.m4,
+       modules/getlogin_r: New files.
+
 2005-05-18  Derek Price  <derek@ximbiot.com>
 
        * modules/minmax (Files): Add m4/minmax.m4.
diff --git a/lib/getlogin_r.c b/lib/getlogin_r.c
new file mode 100644 (file)
index 0000000..5683504
--- /dev/null
@@ -0,0 +1,59 @@
+/* Provide a working getlogin_r for systems which lack it.
+
+   Copyright (C) 2005 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 2, 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, write to the Free Software Foundation,
+   Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
+
+/* written by Paul Eggert and Derek Price */
+
+#include <config.h>
+
+#include "getlogin_r.h"
+
+#include <errno.h>
+#include <string.h>
+
+#if HAVE_UNISTD_H
+# include <unistd.h>
+#endif
+
+#if !HAVE_DECL_GETLOGIN
+char *getlogin (void);
+#endif
+
+/* See getlogin_r.h for documentation.  */
+int
+getlogin_r (char *name, size_t size)
+{
+  char *n;
+  int save_errno = errno;
+
+  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;
+}
diff --git a/lib/getlogin_r.h b/lib/getlogin_r.h
new file mode 100644 (file)
index 0000000..d6f31be
--- /dev/null
@@ -0,0 +1,37 @@
+/* getlogin_r declaration
+
+   Copyright (C) 2005 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 2, 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, write to the Free Software Foundation,
+   Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
+
+/* Written by Paul Eggert and Derek Price.  */
+
+#if HAVE_UNISTD_H
+# include <unistd.h>
+#endif
+
+#if !HAVE_DECL_GETLOGIN_R
+/* Copies the user's login name to NAME.
+   The array pointed to by NAME has room for SIZE bytes.
+
+   Returns 0 if successful.  Upon error, an error number is returned, or -1 in
+   the case that the login name cannot be found but no specific error is
+   provided by getlogin (getlogin returned NULL and did not set errno - this
+   case is hopefully rare but is left open by the POSIX spec).
+
+   See <http://www.opengroup.org/onlinepubs/009695399/functions/getlogin.html>.
+ */
+int getlogin_r (char *name, size_t size);
+#endif
diff --git a/m4/getlogin_r.m4 b/m4/getlogin_r.m4
new file mode 100644 (file)
index 0000000..b1d9c59
--- /dev/null
@@ -0,0 +1,33 @@
+#serial 1
+
+# Copyright (C) 2005 Free Software Foundation, Inc.
+#
+# This file is free software; the Free Software Foundation
+# gives unlimited permission to copy and/or distribute it,
+# with or without modifications, as long as this notice is preserved.
+
+dnl From Derek Price
+dnl
+dnl Provide getlogin_r when the system lacks it.
+dnl
+
+AC_DEFUN([gl_GETLOGIN_R_SUBSTITUTE],
+[
+  gl_PREREQ_GETLOGIN_R
+  AC_LIBSOURCE([getlogin_r.h])
+  AC_LIBOBJ([getlogin_r])
+])
+
+AC_DEFUN([gl_GETLOGIN_R],
+[
+  AC_REPLACE_FUNCS([getlogin_r])
+  if test $ac_cv_func_strcasecmp = no; then
+    gl_GETLOGIN_R_SUBSTITUTE
+  fi
+])
+
+AC_DEFUN([gl_PREREQ_GETLOGIN_R],
+[
+  AC_CHECK_HEADERS_ONCE([unistd.h])
+  AC_CHECK_DECLS_ONCE([getlogin getlogin_r])
+])
diff --git a/modules/getlogin_r b/modules/getlogin_r
new file mode 100644 (file)
index 0000000..7a6f651
--- /dev/null
@@ -0,0 +1,23 @@
+Description:
+Get user name to a buffer allocated by the caller.
+
+Files:
+lib/getlogin_r.h
+lib/getlogin_r.c
+m4/getlogin_r.m4
+
+Depends-on:
+
+configure.ac:
+gl_GETLOGIN_R
+
+Makefile.am:
+
+Include:
+"getlogin_r.h"
+
+License:
+LGPL
+
+Maintainer:
+all