aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2018-08-03 19:53:49 +0200
committerDenys Vlasenko <vda.linux@googlemail.com>2018-08-03 19:53:49 +0200
commit3f2e963768cd9ceb52988db2848ecf0b930f3f44 (patch)
tree506f302c8ee22d261e69f2c449cc9c5d7f6b7362
parent277e00ed12264f7ae1d17f2cbc8be40d3dc0655d (diff)
downloadbusybox-w32-3f2e963768cd9ceb52988db2848ecf0b930f3f44.tar.gz
busybox-w32-3f2e963768cd9ceb52988db2848ecf0b930f3f44.tar.bz2
busybox-w32-3f2e963768cd9ceb52988db2848ecf0b930f3f44.zip
uptime: add -s support
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r--procps/uptime.c30
1 files changed, 24 insertions, 6 deletions
diff --git a/procps/uptime.c b/procps/uptime.c
index b0ee8391b..3262f41b4 100644
--- a/procps/uptime.c
+++ b/procps/uptime.c
@@ -45,7 +45,6 @@
45# include <sys/sysinfo.h> 45# include <sys/sysinfo.h>
46#endif 46#endif
47 47
48
49#ifndef FSHIFT 48#ifndef FSHIFT
50# define FSHIFT 16 /* nr of bits of precision */ 49# define FSHIFT 16 /* nr of bits of precision */
51#endif 50#endif
@@ -53,29 +52,48 @@
53#define LOAD_INT(x) (unsigned)((x) >> FSHIFT) 52#define LOAD_INT(x) (unsigned)((x) >> FSHIFT)
54#define LOAD_FRAC(x) LOAD_INT(((x) & (FIXED_1 - 1)) * 100) 53#define LOAD_FRAC(x) LOAD_INT(((x) & (FIXED_1 - 1)) * 100)
55 54
56
57int uptime_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; 55int uptime_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
58int uptime_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) 56int uptime_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
59{ 57{
60 unsigned updays, uphours, upminutes; 58 unsigned updays, uphours, upminutes;
59 unsigned opts;
61 struct sysinfo info; 60 struct sysinfo info;
62 struct tm *current_time; 61 struct tm *current_time;
63 time_t current_secs; 62 time_t current_secs;
64 63
64 opts = getopt32(argv, "s");
65
65 time(&current_secs); 66 time(&current_secs);
67 sysinfo(&info);
68
69 if (opts) // -s
70 current_secs -= info.uptime;
71
66 current_time = localtime(&current_secs); 72 current_time = localtime(&current_secs);
67 73
68 sysinfo(&info); 74 if (opts) { // -s
75 printf("%04u-%02u-%02u %02u:%02u:%02u\n",
76 current_time->tm_year + 1900, current_time->tm_mon + 1, current_time->tm_mday,
77 current_time->tm_hour, current_time->tm_min, current_time->tm_sec
78 );
79 /* The above way of calculating boot time is wobbly,
80 * info.uptime has only 1 second precision, which makes
81 * "uptime -s" wander +- one second.
82 * /proc/uptime may be better, it has 0.01s precision.
83 */
84 return EXIT_SUCCESS;
85 }
69 86
70 printf(" %02u:%02u:%02u up ", 87 printf(" %02u:%02u:%02u up ",
71 current_time->tm_hour, current_time->tm_min, current_time->tm_sec); 88 current_time->tm_hour, current_time->tm_min, current_time->tm_sec
89 );
72 updays = (unsigned) info.uptime / (unsigned)(60*60*24); 90 updays = (unsigned) info.uptime / (unsigned)(60*60*24);
73 if (updays) 91 if (updays != 0)
74 printf("%u day%s, ", updays, (updays != 1) ? "s" : ""); 92 printf("%u day%s, ", updays, (updays != 1) ? "s" : "");
75 upminutes = (unsigned) info.uptime / (unsigned)60; 93 upminutes = (unsigned) info.uptime / (unsigned)60;
76 uphours = (upminutes / (unsigned)60) % (unsigned)24; 94 uphours = (upminutes / (unsigned)60) % (unsigned)24;
77 upminutes %= 60; 95 upminutes %= 60;
78 if (uphours) 96 if (uphours != 0)
79 printf("%2u:%02u", uphours, upminutes); 97 printf("%2u:%02u", uphours, upminutes);
80 else 98 else
81 printf("%u min", upminutes); 99 printf("%u min", upminutes);