]> git.ipfire.org Git - thirdparty/glibc.git/blobdiff - login/login.c
[BZ #40]
[thirdparty/glibc.git] / login / login.c
index 7cbe8b603e2e7a53913da964fbf3d448386447a8..6efa7627f432cbd3162b4905df8fd9e56cf262d1 100644 (file)
@@ -1,29 +1,31 @@
-/* Copyright (C) 1996 Free Software Foundation, Inc.
+/* Copyright (C) 1996, 1997, 1998, 2002 Free Software Foundation, Inc.
    This file is part of the GNU C Library.
    Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996.
 
    The GNU C Library is free software; you can redistribute it and/or
-   modify it under the terms of the GNU Library General Public License as
-   published by the Free Software Foundation; either version 2 of the
-   License, or (at your option) any later version.
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
 
    The GNU C Library 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
-   Library General Public License for more details.
+   Lesser General Public License for more details.
 
-   You should have received a copy of the GNU Library General Public
-   License along with the GNU C Library; see the file COPYING.LIB.  If not,
-   write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
-   Boston, MA 02111-1307, USA.  */
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, write to the Free
+   Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
+   02111-1307 USA.  */
 
+#include <assert.h>
 #include <errno.h>
 #include <limits.h>
 #include <string.h>
 #include <unistd.h>
 #include <stdlib.h>
 #include <utmp.h>
-\f
+
+
 /* Return the result of ttyname in the buffer pointed to by TTY, which should
    be of length BUF_LEN.  If it is too long to fit in this buffer, a
    sufficiently long buffer is allocated using malloc, and returned in TTY.
@@ -42,7 +44,7 @@ tty_name (int fd, char **tty, size_t buf_len)
        {
          rv = ttyname_r (fd, buf, buf_len);
 
-         if (rv < 0 || memchr (buf, '\0', buf_len))
+         if (rv != 0 || memchr (buf, '\0', buf_len))
            /* We either got an error, or we succeeded and the
               returned name fit in the buffer.  */
            break;
@@ -65,6 +67,7 @@ tty_name (int fd, char **tty, size_t buf_len)
          __set_errno (ENOMEM);
          break;
        }
+      buf = new_buf;
     }
 
   if (rv == 0)
@@ -87,7 +90,6 @@ login (const struct utmp *ut)
   int found_tty;
   const char *ttyp;
   struct utmp copy = *ut;
-  struct utmp utbuf;
 
   /* Fill in those fields we supply.  */
 #if _HAVE_UT_TYPE - 0
@@ -106,23 +108,22 @@ login (const struct utmp *ut)
 
   if (found_tty >= 0)
     {
-      /* We only want to insert the name of the tty without path.  */
-      ttyp = basename (tty);
+      /* We only want to insert the name of the tty without path.
+        But take care of name like /dev/pts/3.  */
+      if (strncmp (tty, "/dev/", 5) == 0)
+       ttyp = tty + 5;         /* Skip the "/dev/".  */
+      else
+       ttyp = basename (tty);
 
       /* Position to record for this tty.  */
       strncpy (copy.ut_line, ttyp, UT_LINESIZE);
 
       /* Tell that we want to use the UTMP file.  */
-      if (utmpname (_PATH_UTMP) != 0)
+      if (utmpname (_PATH_UTMP) == 0)
        {
-         struct utmp *old;
-
          /* Open UTMP file.  */
          setutent ();
 
-         /* Read the record.  */
-         getutline_r (&copy, &utbuf, &old);
-
          /* Write the entry.  */
          pututline (&copy);
 
@@ -133,22 +134,11 @@ login (const struct utmp *ut)
       if (tty != _tty)
        free (tty);             /* Free buffer malloced by tty_name.  */
     }
+  else
+    /* We provide a default value so that the output does not contain
+       an random bytes in this field.  */
+    strncpy (copy.ut_line, "???", UT_LINESIZE);
 
   /* Update the WTMP file.  Here we have to add a new entry.  */
-  if (utmpname (_PATH_WTMP) != 0)
-    {
-      struct utmp *up;
-
-      /* Open the WTMP file.  */
-      setutent ();
-
-      /* Position at end of file.  */
-      while (! getutent_r (&utbuf, &up));
-
-      /* Write the new entry.  */
-      pututline (&copy);
-
-      /* Close WTMP file.  */
-      endutent ();
-    }
+  updwtmp (_PATH_WTMP, &copy);
 }