annotate toys/uptime.c @ 562:4d802d438983

Match uint64_t with PRIu64 to avoid warnings on 64 bit builds.
author Rob Landley <rob@landley.net>
date Sat, 14 Apr 2012 21:27:00 -0500
parents 77f590d2cad9
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
466
9f1e089262cb Adding free and uptime
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
1 /* vi: set sw=4 ts=4:
9f1e089262cb Adding free and uptime
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
2 *
9f1e089262cb Adding free and uptime
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
3 * uptime.c - Tell how long the system has been running.
9f1e089262cb Adding free and uptime
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
4 *
9f1e089262cb Adding free and uptime
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
5 * Copyright 2012 Elie De Brauwer <eliedebrauwer@gmail.com>
9f1e089262cb Adding free and uptime
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
6 *
468
77f590d2cad9 Move includes into toys.h, more xprintf()
Rob Landley <rob@landley.net>
parents: 466
diff changeset
7 * Not in SUSv4.
466
9f1e089262cb Adding free and uptime
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
8
9f1e089262cb Adding free and uptime
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
9 USE_UPTIME(NEWTOY(uptime, NULL, TOYFLAG_USR|TOYFLAG_BIN))
9f1e089262cb Adding free and uptime
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
10
9f1e089262cb Adding free and uptime
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
11 config UPTIME
9f1e089262cb Adding free and uptime
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
12 bool "uptime"
9f1e089262cb Adding free and uptime
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
13 default y
9f1e089262cb Adding free and uptime
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
14 help
9f1e089262cb Adding free and uptime
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
15 usage: uptime
9f1e089262cb Adding free and uptime
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
16
9f1e089262cb Adding free and uptime
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
17 Tell how long the system has been running and the system load
9f1e089262cb Adding free and uptime
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
18 averages for the past 1, 5 and 15 minutes.
9f1e089262cb Adding free and uptime
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
19 */
9f1e089262cb Adding free and uptime
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
20
9f1e089262cb Adding free and uptime
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
21 #include "toys.h"
9f1e089262cb Adding free and uptime
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
22
9f1e089262cb Adding free and uptime
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
23 void uptime_main(void)
9f1e089262cb Adding free and uptime
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
24 {
9f1e089262cb Adding free and uptime
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
25 struct sysinfo info;
9f1e089262cb Adding free and uptime
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
26 time_t tmptime;
9f1e089262cb Adding free and uptime
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
27 struct tm * now;
9f1e089262cb Adding free and uptime
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
28 unsigned int days, hours, minutes;
9f1e089262cb Adding free and uptime
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
29
9f1e089262cb Adding free and uptime
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
30 // Obtain the data we need.
9f1e089262cb Adding free and uptime
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
31 sysinfo(&info);
9f1e089262cb Adding free and uptime
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
32 time(&tmptime);
9f1e089262cb Adding free and uptime
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
33 now = localtime(&tmptime);
9f1e089262cb Adding free and uptime
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
34
9f1e089262cb Adding free and uptime
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
35 // Time
468
77f590d2cad9 Move includes into toys.h, more xprintf()
Rob Landley <rob@landley.net>
parents: 466
diff changeset
36 xprintf(" %02d:%02d:%02d up ", now->tm_hour, now->tm_min, now->tm_sec);
466
9f1e089262cb Adding free and uptime
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
37 // Uptime
9f1e089262cb Adding free and uptime
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
38 info.uptime /= 60;
9f1e089262cb Adding free and uptime
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
39 minutes = info.uptime%60;
9f1e089262cb Adding free and uptime
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
40 info.uptime /= 60;
9f1e089262cb Adding free and uptime
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
41 hours = info.uptime%24;
9f1e089262cb Adding free and uptime
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
42 days = info.uptime/24;
468
77f590d2cad9 Move includes into toys.h, more xprintf()
Rob Landley <rob@landley.net>
parents: 466
diff changeset
43 if (days) xprintf("%d day%s, ", days, (days!=1)?"s":"");
466
9f1e089262cb Adding free and uptime
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
44 if (hours)
468
77f590d2cad9 Move includes into toys.h, more xprintf()
Rob Landley <rob@landley.net>
parents: 466
diff changeset
45 xprintf("%2d:%02d, ", hours, minutes);
466
9f1e089262cb Adding free and uptime
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
46 else
9f1e089262cb Adding free and uptime
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
47 printf("%d min, ", minutes);
9f1e089262cb Adding free and uptime
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
48
9f1e089262cb Adding free and uptime
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
49 printf(" load average: %.02f %.02f %.02f\n", info.loads[0]/65536.0,
9f1e089262cb Adding free and uptime
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
50 info.loads[1]/65536.0, info.loads[2]/65536.0);
9f1e089262cb Adding free and uptime
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
51
9f1e089262cb Adding free and uptime
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
52 }