From db63b11c64c6f4daa366df65647798034badd8d3 Mon Sep 17 00:00:00 2001 From: Alan Cox Date: Sat, 23 May 2015 13:53:29 +0100 Subject: [PATCH] uptime: add an initial version of uptime Now the load average code can be debugged.. --- Applications/util/Makefile | 1 + Applications/util/uptime.c | 59 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 Applications/util/uptime.c diff --git a/Applications/util/Makefile b/Applications/util/Makefile index 24a0ad99..5897a334 100644 --- a/Applications/util/Makefile +++ b/Applications/util/Makefile @@ -92,6 +92,7 @@ SRCS = banner.c \ uniq.c \ uud.c \ uue.c \ + uptime.c \ wc.c \ which.c \ who.c \ diff --git a/Applications/util/uptime.c b/Applications/util/uptime.c new file mode 100644 index 00000000..866b3e39 --- /dev/null +++ b/Applications/util/uptime.c @@ -0,0 +1,59 @@ +#include +#include +#include +#include +#include + +static int count_users(void) +{ + int ct = 0; + struct utmp *entry; + setutent(); + while(entry = getutent()) + if (entry->ut_type == USER_PROCESS) + ct++; + endutent(); + return ct; +} + +int main(int argc, char *argv[]) +{ + static struct { + struct _uzisysinfoblk i; + char buf[128]; + } uts; + static struct timespec res; + int bytes = _uname(&uts.i, sizeof(uts)); + time_t t; + uint32_t days, hours, mins; + struct tm *tm; + int u; + + /* Get the clocks we want */ + clock_gettime(CLOCK_MONOTONIC, &res); + time(&t); + tm = localtime(&t); + printf(" %2d:%02d:%02d up ", + tm->tm_hour, tm->tm_min, tm->tm_sec); + mins = res.tv_sec / 60; + hours = mins / 60; + days = hours / 24; + + mins %= 60; + hours %= 24; + + if (days == 1) + printf("1 day, "); + else if (days > 1) + printf("%d days, ", days); + printf("%02d:%02d, ", hours, mins); + + u = count_users(); + printf("%d user%s, ", u, u != 1 ? "s" : ""); + + printf("load average: %d %d %d\n", + uts.i.loadavg[0], + uts.i.loadavg[1], + uts.i.loadavg[2]); +} + -- 2.34.1