Mercurial > hg > toybox
comparison toys/uptime.c @ 466:9f1e089262cb
Adding free and uptime
author | Elie De Brauwer <eliedebrauwer@gmail.com> |
---|---|
date | Mon, 13 Feb 2012 17:15:49 +0100 |
parents | |
children | 77f590d2cad9 |
comparison
equal
deleted
inserted
replaced
465:ab6c0adfcc10 | 466:9f1e089262cb |
---|---|
1 /* vi: set sw=4 ts=4: | |
2 * | |
3 * uptime.c - Tell how long the system has been running. | |
4 * | |
5 * Copyright 2012 Elie De Brauwer <eliedebrauwer@gmail.com> | |
6 * | |
7 * Not in SUSv3. | |
8 | |
9 USE_UPTIME(NEWTOY(uptime, NULL, TOYFLAG_USR|TOYFLAG_BIN)) | |
10 | |
11 config UPTIME | |
12 bool "uptime" | |
13 default y | |
14 help | |
15 usage: uptime | |
16 | |
17 Tell how long the system has been running and the system load | |
18 averages for the past 1, 5 and 15 minutes. | |
19 */ | |
20 | |
21 #include "toys.h" | |
22 #include <sys/sysinfo.h> | |
23 #include <time.h> | |
24 | |
25 void uptime_main(void) | |
26 { | |
27 struct sysinfo info; | |
28 time_t tmptime; | |
29 struct tm * now; | |
30 unsigned int days, hours, minutes; | |
31 | |
32 // Obtain the data we need. | |
33 sysinfo(&info); | |
34 time(&tmptime); | |
35 now = localtime(&tmptime); | |
36 | |
37 // Time | |
38 printf(" %02d:%02d:%02d up ", now->tm_hour, now->tm_min, now->tm_sec); | |
39 // Uptime | |
40 info.uptime /= 60; | |
41 minutes = info.uptime%60; | |
42 info.uptime /= 60; | |
43 hours = info.uptime%24; | |
44 days = info.uptime/24; | |
45 if (days) printf("%d day%s, ", days, (days!=1)?"s":""); | |
46 if (hours) | |
47 printf("%2d:%02d, ", hours, minutes); | |
48 else | |
49 printf("%d min, ", minutes); | |
50 | |
51 printf(" load average: %.02f %.02f %.02f\n", info.loads[0]/65536.0, | |
52 info.loads[1]/65536.0, info.loads[2]/65536.0); | |
53 | |
54 } |