bug-coreutils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

readutmp patch for hosts without UTMP_NAME_FUNCTION


From: Paul Eggert
Subject: readutmp patch for hosts without UTMP_NAME_FUNCTION
Date: Wed, 30 Mar 2005 02:47:47 -0500
User-agent: Gnus/5.1006 (Gnus v5.10.6) Emacs/21.4 (gnu/linux)

The previous patch I installed was incomplete: it didn't handle
ancient hosts without any UTMP_NAME_FUNCTION.  I installed the
following to fix this.  While I was at it, I noticed that the code
won't work if the utmp file is not a regular file, so I fixed that as
well.

2005-03-30  Paul Eggert  <address@hidden>

        * lib/readutmp.c (read_utmp) [!defined UTMP_NAME_FUNCTION]: Add
        support for options.  Don't assume utmp file is a regular file,
        so don't trust its st_size.
        * m4/readutmp.m4 (gl_READUTMP): Bring back the requirement for
        gl_FUNC_FREE.     

Index: lib/readutmp.c
===================================================================
RCS file: /fetish/cu/lib/readutmp.c,v
retrieving revision 1.24
diff -p -u -r1.24 readutmp.c
--- lib/readutmp.c      30 Mar 2005 05:18:37 -0000      1.24
+++ lib/readutmp.c      30 Mar 2005 07:34:22 -0000
@@ -118,55 +118,39 @@ read_utmp (const char *filename, size_t 
 #else
 
 int
-read_utmp (const char *filename, size_t *n_entries, STRUCT_UTMP **utmp_buf)
+read_utmp (const char *filename, size_t *n_entries, STRUCT_UTMP **utmp_buf,
+          int options)
 {
-  FILE *utmp;
-  struct stat file_stats;
-  size_t n_read;
-  size_t size;
-  size_t i;
-  size_t n_desired;
-  STRUCT_UTMP *buf;
+  size_t n_read = 0;
+  size_t n_alloc = 0;
+  STRUCT_UTMP *utmp = NULL;
+  int saved_errno;
+  FILE *f = fopen (filename, "r");
 
-  utmp = fopen (filename, "r");
-  if (utmp == NULL)
+  if (! f)
     return -1;
 
-  if (fstat (fileno (utmp), &file_stats) != 0)
+  for (;;)
     {
-      int e = errno;
-      fclose (utmp);
-      errno = e;
-      return -1;
-    }
-  if (! (0 <= file_stats.st_size && file_stats.st_size <= SIZE_MAX))
-    xalloc_die ();
-  size = file_stats.st_size;
-  buf = xmalloc (size);
-  n_read = fread (buf, sizeof *buf, size / sizeof *buf, utmp);
-  if (ferror (utmp))
-    {
-      int e = errno;
-      free (buf);
-      fclose (utmp);
-      errno = e;
-      return -1;
+      if (n_read == n_alloc)
+       utmp = x2nrealloc (utmp, &n_alloc, sizeof *utmp);
+      if (fread (&utmp[n_read], sizeof utmp[n_read], 1, f) == 0)
+       break;
+      n_read += desirable_utmp_entry (&utmp[n_read], options);
     }
-  if (fclose (utmp) != 0)
+
+  saved_errno = ferror (f) ? errno : 0;
+  if (fclose (f) != 0)
+    saved_errno = errno;
+  if (saved_errno != 0)
     {
-      int e = errno;
-      free (buf);
-      errno = e;
+      free (utmp);
+      errno = saved_errno;
       return -1;
     }
 
-  n_desired = 0;
-  for (i = 0; i < n_read; i++)
-    if (desirable_utmp_entry (&utmp[i], options))
-      utmp[n_desired++] = utmp[i];
-
-  *n_entries = n_desired;
-  *utmp_buf = buf;
+  *n_entries = n_read;
+  *utmp_buf = utmp;
 
   return 0;
 }
Index: m4/readutmp.m4
===================================================================
RCS file: /fetish/cu/m4/readutmp.m4,v
retrieving revision 1.7
diff -p -u -r1.7 readutmp.m4
--- m4/readutmp.m4      30 Mar 2005 05:20:25 -0000      1.7
+++ m4/readutmp.m4      30 Mar 2005 07:34:22 -0000
@@ -1,4 +1,4 @@
-# readutmp.m4 serial 8
+# readutmp.m4 serial 9
 dnl Copyright (C) 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
 dnl This file is free software; the Free Software Foundation
 dnl gives unlimited permission to copy and/or distribute it,
@@ -9,8 +9,9 @@ AC_DEFUN([gl_READUTMP],
   AC_LIBSOURCES([readutmp.c, readutmp.h])
   AC_LIBOBJ([readutmp])
 
-  dnl Prerequisites of lib/readutmp.h.
+  dnl Prerequisites of lib/readutmp.h and lib/readutmp.c.
   AC_REQUIRE([AC_C_INLINE])
+  AC_REQUIRE([gl_FUNC_FREE])
   AC_CHECK_HEADERS_ONCE(utmp.h utmpx.h)
   AC_CHECK_FUNCS_ONCE(utmpname utmpxname)
   AC_CHECK_DECLS(getutent,,,[




reply via email to

[Prev in Thread] Current Thread [Next in Thread]