1 /* vi: set sw=4 ts=4: */
3 * Mini uptime implementation for busybox
5 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
7 * Licensed under GPLv2, see file LICENSE in this source tree.
10 /* 2011 Pere Orga <gotrunks@gmail.com>
12 * Added FEATURE_UPTIME_UTMP_SUPPORT flag.
15 /* getopt not needed */
17 //config:config UPTIME
18 //config: bool "uptime"
20 //config: select PLATFORM_LINUX #sysinfo()
22 //config: uptime gives a one line display of the current time, how long
23 //config: the system has been running, how many users are currently logged
24 //config: on, and the system load averages for the past 1, 5, and 15 minutes.
26 //config:config FEATURE_UPTIME_UTMP_SUPPORT
27 //config: bool "Support for showing the number of users"
29 //config: depends on UPTIME && FEATURE_UTMP
31 //config: Makes uptime display the number of users currently logged on.
33 //usage:#define uptime_trivial_usage
35 //usage:#define uptime_full_usage "\n\n"
36 //usage: "Display the time since the last boot"
38 //usage:#define uptime_example_usage
40 //usage: " 1:55pm up 2:30, load average: 0.09, 0.04, 0.00\n"
44 # include <sys/sysinfo.h>
49 # define FSHIFT 16 /* nr of bits of precision */
51 #define FIXED_1 (1 << FSHIFT) /* 1.0 as fixed-point */
52 #define LOAD_INT(x) (unsigned)((x) >> FSHIFT)
53 #define LOAD_FRAC(x) LOAD_INT(((x) & (FIXED_1 - 1)) * 100)
56 int uptime_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
57 int uptime_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
59 unsigned updays, uphours, upminutes;
61 struct tm *current_time;
65 current_time = localtime(¤t_secs);
69 printf(" %02u:%02u:%02u up ",
70 current_time->tm_hour, current_time->tm_min, current_time->tm_sec);
71 updays = (unsigned) info.uptime / (unsigned)(60*60*24);
73 printf("%u day%s, ", updays, (updays != 1) ? "s" : "");
74 upminutes = (unsigned) info.uptime / (unsigned)60;
75 uphours = (upminutes / (unsigned)60) % (unsigned)24;
78 printf("%2u:%02u", uphours, upminutes);
80 printf("%u min", upminutes);
82 #if ENABLE_FEATURE_UPTIME_UTMP_SUPPORT
86 while ((ut = getutent()) != NULL) {
87 if ((ut->ut_type == USER_PROCESS) && (ut->ut_name[0] != '\0'))
90 printf(", %u users", users);
94 printf(", load average: %u.%02u, %u.%02u, %u.%02u\n",
95 LOAD_INT(info.loads[0]), LOAD_FRAC(info.loads[0]),
96 LOAD_INT(info.loads[1]), LOAD_FRAC(info.loads[1]),
97 LOAD_INT(info.loads[2]), LOAD_FRAC(info.loads[2]));