-/* Read the utmp file FILENAME into *UTMP_BUF, set *N_ENTRIES to the
- number of entries read, and return zero. If there is any error,
- return non-zero and don't modify the parameters. */
+/* Is the utmp entry U desired by the user who asked for OPTIONS? */
+
+static inline bool
+desirable_utmp_entry (STRUCT_UTMP const *u, int options)
+{
+ return ! (options & READ_UTMP_CHECK_PIDS
+ && IS_USER_PROCESS (u)
+ && (UT_PID (u) <= 0
+ || (kill (UT_PID (u), 0) < 0 && errno == ESRCH)));
+}
+
+/* Read the utmp entries corresponding to file FILE into freshly-
+ malloc'd storage, set *UTMP_BUF to that pointer, set *N_ENTRIES to
+ the number of entries, and return zero. If there is any error,
+ return -1, setting errno, and don't modify the parameters.
+ If OPTIONS & READ_UTMP_CHECK_PIDS is nonzero, omit entries whose
+ process-IDs do not currently exist. */
+
+#ifdef UTMP_NAME_FUNCTION
+
+int
+read_utmp (char const *file, size_t *n_entries, STRUCT_UTMP **utmp_buf,
+ int options)
+{
+ size_t n_read = 0;
+ size_t n_alloc = 0;
+ STRUCT_UTMP *utmp = NULL;
+ STRUCT_UTMP *u;
+
+ /* Ignore the return value for now.
+ Solaris' utmpname returns 1 upon success -- which is contrary
+ to what the GNU libc version does. In addition, older GNU libc
+ versions are actually void. */
+ UTMP_NAME_FUNCTION (file);
+
+ SET_UTMP_ENT ();
+
+ while ((u = GET_UTMP_ENT ()) != NULL)
+ if (desirable_utmp_entry (u, options))
+ {
+ if (n_read == n_alloc)
+ utmp = x2nrealloc (utmp, &n_alloc, sizeof *utmp);
+
+ utmp[n_read++] = *u;
+ }
+
+ END_UTMP_ENT ();
+
+ *n_entries = n_read;
+ *utmp_buf = utmp;
+
+ return 0;
+}
+
+#else