]> granicus.if.org Git - procps-ng/commitdiff
w: Use POSIX <utmpx.h> functions where available
authorA. Wilcox <AWilcox@Wilcox-Tech.com>
Sat, 16 Jun 2018 07:26:53 +0000 (02:26 -0500)
committerCraig Small <csmall@dropbear.xyz>
Tue, 22 Dec 2020 03:59:15 +0000 (14:59 +1100)
<utmp.h> has been deprecated since 2001 in favour of <utmpx.h>.

On glibc systems, utmp is just an alias to utmpx, so there is no
functional change using one over the other.

However, on the musl libc, a library (utmps) can be used to provide
utmpx functionality - but not utmp.  This means that procps either
doesn't work properly (`w` shows nothing under musl with default no-op
implementation), or fails to build (utmps provides utmpx.h but no
utmp.h).

This commit will use utmpx.h where available, which allows `w` to work
correctly with utmps and has no change on glibc systems.

configure.ac
w.c

index f29fc0646679232803f2c6254dbfe3c6d7eea509..ebd94c27534a4a5a38e4d1cf5e21366e9382b2c4 100644 (file)
@@ -51,7 +51,7 @@ else
 fi
 # Checks for header files.
 AC_HEADER_MAJOR
-AC_CHECK_HEADERS([arpa/inet.h fcntl.h float.h langinfo.h libintl.h limits.h locale.h netinet/in.h stdint.h stdio_ext.h stdlib.h string.h sys/file.h sys/ioctl.h sys/param.h sys/time.h termios.h unistd.h utmp.h values.h wchar.h wctype.h])
+AC_CHECK_HEADERS([arpa/inet.h fcntl.h float.h langinfo.h libintl.h limits.h locale.h netinet/in.h stdint.h stdio_ext.h stdlib.h string.h sys/file.h sys/ioctl.h sys/param.h sys/time.h termios.h unistd.h utmp.h utmpx.h values.h wchar.h wctype.h])
 
 # Checks for typedefs, structures, and compiler characteristics.
 AC_CHECK_HEADER_STDBOOL
diff --git a/w.c b/w.c
index 35710a3a6758aefe0cae9f14acdb1c0ed24707ff..9d07ac9ec558ecfd704661e7c11dcc6323d25581 100644 (file)
--- a/w.c
+++ b/w.c
@@ -23,6 +23,7 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
  */
 
+#include "config.h"
 #include "c.h"
 #include "fileutils.h"
 #include "nls.h"
 #include <termios.h>
 #include <time.h>
 #include <unistd.h>
-#include <utmp.h>
+#ifdef HAVE_UTMPX_H
+#      include <utmpx.h>
+#else
+#      include <utmp.h>
+#endif
 #include <arpa/inet.h>
 
 static int ignoreuser = 0;     /* for '-u' */
 static int oldstyle = 0;       /* for '-o' */
 static proc_t **procs;         /* our snapshot of the process table */
 
+#ifdef HAVE_UTMPX_H
+typedef struct utmpx utmp_t;
+#else
 typedef struct utmp utmp_t;
+#endif
+
+#if !defined(UT_HOSTSIZE) || defined(__UT_HOSTSIZE)
+#      define UT_HOSTSIZE __UT_HOSTSIZE
+#      define UT_LINESIZE __UT_LINESIZE
+#      define UT_NAMESIZE __UT_NAMESIZE
+#endif
 
 #ifdef W_SHOWFROM
 # define FROM_STRING "on"
@@ -412,7 +427,11 @@ static void showinfo(utmp_t * u, int formtype, int maxcmd, int from,
                printf("%-*.*s%-9.8s", userlen + 1, userlen, uname, u->ut_line);
                if (from)
                        print_from(u, ip_addresses, fromlen);
+#ifdef HAVE_UTMPX_H
+               print_logintime(u->ut_tv.tv_sec, stdout);
+#else
                print_logintime(u->ut_time, stdout);
+#endif
                if (*u->ut_line == ':')
                        /* idle unknown for xdm logins */
                        printf(" ?xdm? ");
@@ -604,11 +623,19 @@ int main(int argc, char **argv)
                        printf(_("   IDLE WHAT\n"));
        }
 
+#ifdef HAVE_UTMPX_H
+       setutxent();
+#else
        utmpname(UTMP_FILE);
        setutent();
+#endif
        if (user) {
                for (;;) {
+#ifdef HAVE_UTMPX_H
+                       u = getutxent();
+#else
                        u = getutent();
+#endif
                        if (unlikely(!u))
                                break;
                        if (u->ut_type != USER_PROCESS)
@@ -619,7 +646,11 @@ int main(int argc, char **argv)
                }
        } else {
                for (;;) {
+#ifdef HAVE_UTMPX_H
+                       u = getutxent();
+#else
                        u = getutent();
+#endif
                        if (unlikely(!u))
                                break;
                        if (u->ut_type != USER_PROCESS)
@@ -629,7 +660,11 @@ int main(int argc, char **argv)
                                         fromlen, ip_addresses);
                }
        }
+#ifdef HAVE_UTMPX_H
+       endutxent();
+#else
        endutent();
+#endif
 
        return EXIT_SUCCESS;
 }