diff --git a/src/util.c b/src/util.c index 3574b9d9..e9b5ca14 100644 --- a/src/util.c +++ b/src/util.c @@ -23,8 +23,11 @@ #include "config.h" #include /* isprint() */ +#include #include #include +#include /* sysinfo() */ +#include /* strlcat() */ #include "util.h" int screen_rows = 24; @@ -54,6 +57,57 @@ static int isallowed(int ch) return isprint(ch); } +/* Seconds since boot, from sysinfo() */ +long jiffies(void) +{ + struct sysinfo si; + + if (!sysinfo(&si)) + return si.uptime; + + return 0; +} + +char *uptime(long secs, char *buf, size_t len) +{ + long mins, hours, days, years; + char y[11] = "", d[9] = "", h[9] = "", m[9] = "", s[9] = ""; + + if (!buf) { + errno = EINVAL; + return NULL; + } + + years = secs / 31556926; + secs = secs % 31556926; + days = secs / 86400; + secs = secs % 86400; + hours = secs / 3600; + secs = secs % 3600; + mins = secs / 60; + secs = secs % 60; + + if (years) + snprintf(y, sizeof(y), "%ld year", years); + if (days) + snprintf(d, sizeof(d), "%ld day", days); + if (hours) + snprintf(h, sizeof(h), "%ld hour", hours); + if (mins) + snprintf(m, sizeof(m), "%ld min", mins); + if (secs) + snprintf(s, sizeof(s), "%ld sec", secs); + + snprintf(buf, len, "%s%s%s%s%s%s%s%s%s", + y, years ? " " : "", + d, days ? " " : "", + h, hours ? " " : "", + m, mins ? " " : "", + s); + + return buf; +} + /* Sanitize user input, make sure to NUL terminate. */ char *sanitize(char *arg, size_t len) { diff --git a/src/util.h b/src/util.h index a6bf77d6..952f410e 100644 --- a/src/util.h +++ b/src/util.h @@ -40,6 +40,10 @@ extern char *prognm; char *progname (char *arg0); void do_sleep (unsigned int sec); + +long jiffies (void); +char *uptime (long secs, char *buf, size_t len); + char *sanitize (char *arg, size_t len); void screen_init (void);