uptime: add an initial version of uptime
authorAlan Cox <alan@linux.intel.com>
Sat, 23 May 2015 12:53:29 +0000 (13:53 +0100)
committerAlan Cox <alan@linux.intel.com>
Sat, 23 May 2015 12:53:29 +0000 (13:53 +0100)
Now the load average code can be debugged..

Applications/util/Makefile
Applications/util/uptime.c [new file with mode: 0644]

index 24a0ad9..5897a33 100644 (file)
@@ -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 (file)
index 0000000..866b3e3
--- /dev/null
@@ -0,0 +1,59 @@
+#include <stdio.h>
+#include <unistd.h>
+#include <syscalls.h>
+#include <time.h>
+#include <utmp.h>
+
+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]);
+}
+