The Code has been compiled on a Redhat Linux System and have been tested on SuSe.
---- The Code -------
#include
#include
#include
int main (int argc, char *argv[]) {
char *file = "/proc/uptime";
char *line[1024];
long time;
int uptime;
int days,hours,mins;
FILE *stream;
if( (stream = fopen(file,"r")) == NULL) {
printf("Error opening file %s\n", file);
exit(1);
}
/* need the first entry so need not recurse here on fscanf */
fscanf(stream,"%s",line);
/*convert the string to something useful */
time = atoi(&line);
/* Do a little stupid computation */
mins = time/60;
hours = mins/60;
uptime = hours/24;
days = uptime;
hours = hours-(days*24);
mins = mins-((hours*60)+(days*24*60));
printf("%d Days %d Hours %d mins Uptime\n",days,hours,mins);
fclose(stream);
return 0;
}
|