static void print_uptime (int n) { register STRUCT_UTMP *this = utmp_contents; register int entries = 0; time_t boot_time = 0; time_t time_now; time_t uptime = 0; int updays; int uphours; int upmins; struct tm *tmn; double avg[3]; int loads; #ifdef HAVE_PROC_UPTIME FILE *fp; double upsecs; fp = fopen ("/proc/uptime", "r"); if (fp != NULL) { char buf[BUFSIZ]; int res; fgets(buf, BUFSIZ, fp); res = sscanf (buf, "%lf", &upsecs); if (res == 1) uptime = (time_t) upsecs; fclose (fp); } #endif /* HAVE_PROC_UPTIME */ /* Loop through all the utmp entries we just read and count up the valid ones, also in the process possibly gleaning boottime. */ while (n--) { if (this->ut_name[0] #ifdef USER_PROCESS && this->ut_type == USER_PROCESS #endif ) { ++entries; } /* If BOOT_MSG is defined, we can get boottime from utmp. This avoids possibly needing special privs to read /dev/kmem. */ #ifdef BOOT_MSG # if HAVE_PROC_UPTIME if (uptime == 0) # endif /* HAVE_PROC_UPTIME */ if (!strcmp (this->ut_line, BOOT_MSG)) boot_time = UT_TIME_MEMBER (this); #endif /* BOOT_MSG */ ++this; } time_now = time (0); #if defined HAVE_PROC_UPTIME if (uptime == 0) #endif { if (boot_time == 0) error (1, errno, _("couldn't get boot time")); uptime = time_now - boot_time; } updays = uptime / 86400; uphours = (uptime - (updays * 86400)) / 3600; upmins = (uptime - (updays * 86400) - (uphours * 3600)) / 60; tmn = localtime (&time_now); printf (_(" %2d:%02d%s up "), ((tmn->tm_hour % 12) == 0 ? 12 : tmn->tm_hour % 12), tmn->tm_min, (tmn->tm_hour < 12 ? _("am") : _("pm"))); if (updays > 0) printf ("%d %s,", updays, (updays == 1 ? _("day") : _("days"))); printf (" %2d:%02d, %d %s", uphours, upmins, entries, (entries == 1) ? _("user") : _("users")); #if defined (HAVE_GETLOADAVG) || defined (C_GETLOADAVG) loads = getloadavg (avg, 3); #else loads = -1; #endif if (loads == -1) putchar ('\n'); else { if (loads > 0) printf (_(", load average: %.2f"), avg[0]); if (loads > 1) printf (", %.2f", avg[1]); if (loads > 2) printf (", %.2f", avg[2]); if (loads > 0) putchar ('\n'); } }
void wtmpxrawdump (const char *wtmpfile, const char *user) { STRUCT_UTMP *utp; struct in_addr addr; char *addr_string, *time_string; if (access (wtmpfile, R_OK)) die (errno, "cannot access the file"); /* 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 (wtmpfile); SET_UTMP_ENT (); while ((utp = GET_UTMP_ENT ()) != NULL) { if (user && strncmp (UT_USER (utp), user, sizeof (UT_USER (utp)))) continue; /* FIXME: missing support for IPv6 */ #ifdef HAVE_UTP_UT_ADDR_V6 addr.s_addr = utp->ut_addr_v6[0]; #endif addr_string = inet_ntoa (addr); time_string = timetostr (UT_TIME_MEMBER (utp)); switch (utp->ut_type) { default: /* Note: also catch EMPTY/UT_UNKNOWN values */ printf ("%-9s", "NONE"); break; #ifdef RUN_LVL /* Undefined on AIX if _ALL_SOURCE is false */ case RUN_LVL: printf ("%-9s", "RUNLEVEL"); break; #endif case BOOT_TIME: printf ("%-9s", "REBOOT"); break; case OLD_TIME: case NEW_TIME: /* FIXME */ break; case INIT_PROCESS: printf ("%-9s", "INIT"); break; case LOGIN_PROCESS: printf ("%-9s", "LOGIN"); break; case USER_PROCESS: printf ("%-9.*s", sizeof (UT_USER (utp)), UT_USER (utp)); break; case DEAD_PROCESS: printf ("%-9s", "DEAD"); break; #ifdef ACCOUNTING /* Undefined on AIX if _ALL_SOURCE is false */ case ACCOUNTING: printf ("%-9s", "ACCOUNT"); break; #endif } /* pid */ UT_PID (utp) ? printf ("[%05d]", UT_PID (utp)) : printf ("[%5s]", "-"); /* line id host addr date&time */ printf (" [%-12.*s] [%-4.*s] [%-19.*s] [%-15.15s] [%-19.19s]\n", sizeof (utp->ut_line), utp->ut_line, sizeof (utp->ut_id), utp->ut_id, sizeof (utp->ut_host), utp->ut_host, addr_string, time_string); } END_UTMP_ENT (); }