readutmp: Make 'struct utmpx32' usable by other code.
authorBruno Haible <bruno@clisp.org>
Fri, 11 Aug 2023 14:09:22 +0000 (16:09 +0200)
committerBruno Haible <bruno@clisp.org>
Fri, 11 Aug 2023 20:03:10 +0000 (22:03 +0200)
* lib/readutmp.h (struct utmpx32): Moved to here from lib/readutmp.c.
(UTMP_STRUCT_NAME): Define as utmpx32 if needed.
* lib/readutmp.c (read_utmp_from_file): Simply use UTMP_STRUCT_NAME.

ChangeLog
lib/readutmp.c
lib/readutmp.h

index 526e5cb4be1847451231d68c6b3b00012bffbb06..6836ca6308d1e1e27d2290256b95345ccb8c88c3 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,10 @@
+2023-08-11  Bruno Haible  <bruno@clisp.org>
+
+       readutmp: Make 'struct utmpx32' usable by other code.
+       * lib/readutmp.h (struct utmpx32): Moved to here from lib/readutmp.c.
+       (UTMP_STRUCT_NAME): Define as utmpx32 if needed.
+       * lib/readutmp.c (read_utmp_from_file): Simply use UTMP_STRUCT_NAME.
+
 2023-08-11  Bruno Haible  <bruno@clisp.org>
 
        readutmp tests: Fix link error.
index 40cee0e58cc042b77da38b6cc5af53774e484d2e..00dbbcea483caaf79ed20a61b468973d71740cfe 100644 (file)
@@ -383,40 +383,7 @@ read_utmp_from_file (char const *file, idx_t *n_entries, STRUCT_UTMP **utmp_buf,
 
   while ((entry = GET_UTMP_ENT ()) != NULL)
     {
-#   if __GLIBC__ && _TIME_BITS == 64
-      /* This is a near-copy of glibc's struct utmpx, which stops working
-         after the year 2038.  Unlike the glibc version, struct utmpx32
-         describes the file format even if time_t is 64 bits.  */
-      struct utmpx32
-      {
-        short int ut_type;               /* Type of login.  */
-        pid_t ut_pid;                    /* Process ID of login process.  */
-        char ut_line[UT_LINE_SIZE];      /* Devicename.  */
-        char ut_id[UT_ID_SIZE];          /* Inittab ID.  */
-        char ut_user[UT_USER_SIZE];      /* Username.  */
-        char ut_host[UT_HOST_SIZE];      /* Hostname for remote login. */
-        struct __exit_status ut_exit;    /* Exit status of a process marked
-                                            as DEAD_PROCESS.  */
-        /* The fields ut_session and ut_tv must be the same size when compiled
-           32- and 64-bit.  This allows files and shared memory to be shared
-           between 32- and 64-bit applications.  */
-        int ut_session;                  /* Session ID, used for windowing.  */
-        struct
-        {
-          /* Seconds.  Unsigned not signed, as glibc did not exist before 1970,
-             and if the format is still in use after 2038 its timestamps
-             will surely have the sign bit on.  This hack stops working
-             at 2106-02-07 06:28:16 UTC.  */
-          unsigned int tv_sec;
-          int tv_usec;                   /* Microseconds.  */
-        } ut_tv;                         /* Time entry was made.  */
-        int ut_addr_v6[4];               /* Internet address of remote host.  */
-        char ut_reserved[20];            /* Reserved for future use.  */
-      };
-      struct utmpx32 const *ut = (struct utmpx32 const *) entry;
-#   else
       struct UTMP_STRUCT_NAME const *ut = (struct UTMP_STRUCT_NAME const *) entry;
-#   endif
 
       struct timespec ts =
         #if (HAVE_UTMPX_H ? 1 : HAVE_STRUCT_UTMP_UT_TV)
index 70cd3801dbff4abc953515d49014b7c117c97d9d..1cf588d265bf4fb8e8a23f61e59ca1112a0c2723 100644 (file)
@@ -132,7 +132,40 @@ enum { UT_HOST_SIZE = -1 };
    ⎣ ut_ss        struct sockaddr_storage    NetBSD, Minix
  */
 
-# define UTMP_STRUCT_NAME utmpx
+# if __GLIBC__ && _TIME_BITS == 64
+/* This is a near-copy of glibc's struct utmpx, which stops working
+   after the year 2038.  Unlike the glibc version, struct utmpx32
+   describes the file format even if time_t is 64 bits.  */
+struct utmpx32
+{
+  short int ut_type;               /* Type of login.  */
+  pid_t ut_pid;                    /* Process ID of login process.  */
+  char ut_line[__UT_LINESIZE];     /* Devicename.  */
+  char ut_id[4];                   /* Inittab ID.  */
+  char ut_user[__UT_USERSIZE];     /* Username.  */
+  char ut_host[__UT_HOSTSIZE];     /* Hostname for remote login. */
+  struct __exit_status ut_exit;    /* Exit status of a process marked
+                                      as DEAD_PROCESS.  */
+  /* The fields ut_session and ut_tv must be the same size when compiled
+     32- and 64-bit.  This allows files and shared memory to be shared
+     between 32- and 64-bit applications.  */
+  int ut_session;                  /* Session ID, used for windowing.  */
+  struct
+  {
+    /* Seconds.  Unsigned not signed, as glibc did not exist before 1970,
+       and if the format is still in use after 2038 its timestamps
+       will surely have the sign bit on.  This hack stops working
+       at 2106-02-07 06:28:16 UTC.  */
+    unsigned int tv_sec;
+    int tv_usec;                   /* Microseconds.  */
+  } ut_tv;                         /* Time entry was made.  */
+  int ut_addr_v6[4];               /* Internet address of remote host.  */
+  char ut_reserved[20];            /* Reserved for future use.  */
+};
+#  define UTMP_STRUCT_NAME utmpx32
+# else
+#  define UTMP_STRUCT_NAME utmpx
+# endif
 # define SET_UTMP_ENT setutxent
 # define GET_UTMP_ENT getutxent
 # define END_UTMP_ENT endutxent