/*  Program to print out the current system uptime in days, as a decimal.
 *  
 *  Author: Eric Carlson, carl0240@tc.umn.edu
 *  
 *  This software is supplied 'as is', with no warranty. 
 *  You are free to use and/or redistribute this source, modified or unmodified,
 *  in any form.
 */

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#include <sys/types.h>
#include <sys/sysctl.h>

#define SECSPERDAY 86400

int main(int argc, char* argv[]) {
  struct timeval boottime;
  time_t uptime, now;
  size_t size;
  int mib[2];
  
  time(&now); //put current secs since epoc into 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 = now - boottime.tv_sec;
    printf("%f\n", uptime/((float)SECSPERDAY));
  }
  else
  {
    printf("0.0\n");
  }
  exit(0);
}
