Skip to content

Instantly share code, notes, and snippets.

@skreuzer
Created December 23, 2009 15:32
Show Gist options
  • Save skreuzer/262576 to your computer and use it in GitHub Desktop.
Save skreuzer/262576 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <time.h>
#include <sys/param.h>
#include <sys/sysctl.h>
int
main()
{
time_t uptime;
time_t now;
size_t size;
int mib[2];
struct timeval boottime;
time(&now);
size = sizeof(boottime);
mib[0] = CTL_KERN;
mib[1] = KERN_BOOTTIME;
if (sysctl(mib, 2, &boottime, &size, NULL, 0) != -1) {
uptime = now - boottime.tv_sec;
printf("%d\n", uptime);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment