From 85a0e49c334bfc826d13d2d82cecc9da2511d795 Mon Sep 17 00:00:00 2001 From: sin Date: Tue, 8 Oct 2013 16:15:08 +0100 Subject: [PATCH] Use utmpx instead of utmp If your utmp files are generated with programs linked against glibc then musl-libc might not be able to parse the records correctly. This is because the fields `ut_session' and `ut_tv' must be the same size when compiled in x86 and x86_64. This is a requirement by glibc that musl does not fulfil for good reasons. --- uptime.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/uptime.c b/uptime.c index af4266c..74c8da6 100644 --- a/uptime.c +++ b/uptime.c @@ -4,7 +4,7 @@ #include #include #include -#include +#include #include "util.h" static void @@ -16,13 +16,14 @@ usage(void) int main(int argc, char *argv[]) { - struct utmp usr; + struct utmpx utx; FILE *ufp; struct sysinfo info; time_t tmptime; struct tm *now; unsigned int days, hours, minutes; int nusers = 0; + size_t n; ARGBEGIN { default: @@ -47,14 +48,15 @@ main(int argc, char *argv[]) printf("%d min, ", minutes); if ((ufp = fopen("/var/run/utmp", "r"))) { - while(fread(&usr, sizeof(usr), 1, ufp) == 1) { - if (!*usr.ut_name || !*usr.ut_line || - usr.ut_line[0] == '~') + while ((n = fread(&utx, sizeof(utx), 1, ufp)) > 0) { + if (!utx.ut_user[0]) continue; - if (strcmp(usr.ut_name, "LOGIN") == 0) + if (utx.ut_type != USER_PROCESS) continue; nusers++; } + if (ferror(ufp)) + eprintf("/var/run/utmp: read error:"); fclose(ufp); printf(" %d user%s, ", nusers, nusers > 1 ? "s" : ""); }