C program to print system uptime
#include <stdio.h>
void print_time (long time)
{
/* Conversion constants.
*/
const long minute = 60;
const long hour = minute
* 60;
const long day = hour *
24;
/* Produce output.
*/
printf ("Uptime:
%ld days, %ld:%02ld:%02ld\n", time / day,
(time % day) / hour, (time % hour)
/ minute, time % minute);
}
int main ()
{
FILE* fp;
double uptime,
idle_time;
/* Read the system
uptime and accumulated idle time from /proc/uptime. */
fp = fopen
("/proc/uptime", "r");
fscanf (fp,
"%lf\n", &uptime);
fclose (fp);
/* Summarize it.
*/
print_time ((long)
uptime);
return 0;
}