/* ################################################################# my_uptime, Dennis Helligsoe, dh@hovklan.com ------------------------------------------- 1.0 2006-02-18 : Initial release WHAT IT IS? ----------- "my_uptime" is a FreeBSD uptime program, that reports the uptime on a FreeBSD server. The standard "uptime" on a FreeBSD is really not that database friendly, so it is quite hard to parse the output into a such! ;-) The output is given as seconds, minutes, hours and days. Example - if the current uptime is exacly 1 day: 86400 1440 24 1 INPUT: ------ none OUTPUT: ------- seconds minutes hours days HOW TO COMPILE SOURCE CODE: --------------------------- gcc my_uptime.c -o /usr/local/sbin/my_uptime ################################################################# */ #include #include #include int main(int argc, char *argv[]) { unsigned long uptime_secs; float uptime_mins; float uptime_hours; float uptime_days; time_t now; struct timeval boottime; size_t size; int mib[2]; time(&now); 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_secs = now - boottime.tv_sec; uptime_mins = uptime_secs; uptime_mins /= 60; uptime_hours = uptime_mins; uptime_hours /= 60; uptime_days = uptime_hours; uptime_days /= 24; } printf("%lu %4.2f %4.2f %4.2f\n", uptime_secs, uptime_mins, uptime_hours, uptime_days); }