Date: Sat, 19 Dec 1998 20:58:04 +1100 (EST) From: Andy Farkas <andyf@speednet.com.au> To: freebsd-current@FreeBSD.ORG Subject: an addition to top(1) Message-ID: <Pine.BSF.4.05.9812192048090.15643-100000@backup.zippynet.iol.net.au>
next in thread | raw e-mail | index | archive | help
I've always thought that top(1) should display the system uptime. Here's a patch. The code was cut straight out of /usr/src/usr.bin/w/w.c Merry Christmas! # cd /usr/src/contrib/top # rcsdiff -r1.1 -u display.c =================================================================== RCS file: display.c,v retrieving revision 1.1 diff -u -r1.1 display.c --- display.c 1998/12/18 02:23:39 1.1 +++ display.c 1998/12/19 09:12:03 @@ -30,6 +30,10 @@ #include <ctype.h> #include <time.h> +#include <sys/time.h> +#include <sys/types.h> +#include <sys/sysctl.h> + #include "screen.h" /* interface to screen package */ #include "layout.h" /* defines for screen position layout */ #include "display.h" @@ -240,11 +244,51 @@ } } +struct timeval boottime; +time_t now; +time_t uptime; + i_timeofday(tod) time_t *tod; { + int days, hrs, i, mins, secs; + int mib[2]; + size_t size; + + (void)time(&now); + + /* + * Print how long system has been up. + * (Found by looking getting "boottime" from the kernel) + */ + mib[0] = CTL_KERN; + mib[1] = KERN_BOOTTIME; + size = sizeof(boottime); + if (sysctl(mib, 2, &boottime, &size, NULL, 0) != -1 && + boottime.tv_sec != 0) { + uptime = now - boottime.tv_sec; + uptime += 30; + days = uptime / 86400; + uptime %= 86400; + hrs = uptime / 3600; + uptime %= 3600; + mins = uptime / 60; + secs = uptime % 60; + + if (smart_terminal) + { + Move_to((screen_width - 24) - (days > 9 ? 1 : 0), 0); + } + else + { + fputs(" ", stdout); + } + + printf(" up %d+%02d:%02d:%02d", days, hrs, mins, secs); + } + /* * Display the current time. * "ctime" always returns a string that looks like this: -- :{ andyf@speednet.com.au Andy Farkas System Administrator Speed Internet Services http://www.speednet.com.au/ To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-current" in the body of the message
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?Pine.BSF.4.05.9812192048090.15643-100000>