| 1 |
/*********************************************************************** |
| 2 |
* Code listing from "Advanced Linux Programming," by CodeSourcery LLC * |
| 3 |
* Copyright (C) 2001 by New Riders Publishing * |
| 4 |
* See COPYRIGHT for license information. * |
| 5 |
***********************************************************************/ |
| 6 |
|
| 7 |
#include <stdio.h> |
| 8 |
|
| 9 |
/* Summarize a duration of time to standard output. TIME is the |
| 10 |
amount of time, in seconds, and LABEL is a short descriptive label. */ |
| 11 |
|
| 12 |
void print_time (char* label, long time) |
| 13 |
{ |
| 14 |
/* Conversion constants. */ |
| 15 |
const long minute = 60; |
| 16 |
const long hour = minute * 60; |
| 17 |
const long day = hour * 24; |
| 18 |
/* Produce output. */ |
| 19 |
printf ("%s: %ld days, %ld:%02ld:%02ld\n", label, time / day, |
| 20 |
(time % day) / hour, (time % hour) / minute, time % minute); |
| 21 |
} |
| 22 |
|
| 23 |
int main () |
| 24 |
{ |
| 25 |
FILE* fp; |
| 26 |
double uptime, idle_time; |
| 27 |
/* Read the system uptime and accumulated idle time from /proc/uptime. */ |
| 28 |
fp = fopen ("/proc/uptime", "r"); |
| 29 |
fscanf (fp, "%lf %lf\n", &uptime, &idle_time); |
| 30 |
fclose (fp); |
| 31 |
/* Summarize it. */ |
| 32 |
print_time ("uptime ", (long) uptime); |
| 33 |
print_time ("idle time", (long) idle_time); |
| 34 |
return 0; |
| 35 |
} |