Big cleanup in config help and description
[oweals/busybox.git] / procps / uptime.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Mini uptime implementation for busybox
4  *
5  * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
6  *
7  * Licensed under GPLv2, see file LICENSE in this source tree.
8  */
9
10 /* 2011         Pere Orga <gotrunks@gmail.com>
11  *
12  * Added FEATURE_UPTIME_UTMP_SUPPORT flag.
13  */
14 //config:config UPTIME
15 //config:       bool "uptime"
16 //config:       default y
17 //config:       select PLATFORM_LINUX #sysinfo()
18 //config:       help
19 //config:         uptime gives a one line display of the current time, how long
20 //config:         the system has been running, how many users are currently logged
21 //config:         on, and the system load averages for the past 1, 5, and 15 minutes.
22 //config:
23 //config:config FEATURE_UPTIME_UTMP_SUPPORT
24 //config:       bool "Show the number of users"
25 //config:       default y
26 //config:       depends on UPTIME && FEATURE_UTMP
27 //config:       help
28 //config:         Display the number of users currently logged on.
29
30 //applet:IF_UPTIME(APPLET(uptime, BB_DIR_USR_BIN, BB_SUID_DROP))
31
32 //kbuild:lib-$(CONFIG_UPTIME) += uptime.o
33
34 //usage:#define uptime_trivial_usage
35 //usage:       ""
36 //usage:#define uptime_full_usage "\n\n"
37 //usage:       "Display the time since the last boot"
38 //usage:
39 //usage:#define uptime_example_usage
40 //usage:       "$ uptime\n"
41 //usage:       "  1:55pm  up  2:30, load average: 0.09, 0.04, 0.00\n"
42
43 #include "libbb.h"
44 #ifdef __linux__
45 # include <sys/sysinfo.h>
46 #endif
47
48
49 #ifndef FSHIFT
50 # define FSHIFT 16              /* nr of bits of precision */
51 #endif
52 #define FIXED_1      (1 << FSHIFT)     /* 1.0 as fixed-point */
53 #define LOAD_INT(x)  (unsigned)((x) >> FSHIFT)
54 #define LOAD_FRAC(x) LOAD_INT(((x) & (FIXED_1 - 1)) * 100)
55
56
57 int uptime_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
58 int uptime_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
59 {
60         unsigned updays, uphours, upminutes;
61         struct sysinfo info;
62         struct tm *current_time;
63         time_t current_secs;
64
65         time(&current_secs);
66         current_time = localtime(&current_secs);
67
68         sysinfo(&info);
69
70         printf(" %02u:%02u:%02u up ",
71                         current_time->tm_hour, current_time->tm_min, current_time->tm_sec);
72         updays = (unsigned) info.uptime / (unsigned)(60*60*24);
73         if (updays)
74                 printf("%u day%s, ", updays, (updays != 1) ? "s" : "");
75         upminutes = (unsigned) info.uptime / (unsigned)60;
76         uphours = (upminutes / (unsigned)60) % (unsigned)24;
77         upminutes %= 60;
78         if (uphours)
79                 printf("%2u:%02u", uphours, upminutes);
80         else
81                 printf("%u min", upminutes);
82
83 #if ENABLE_FEATURE_UPTIME_UTMP_SUPPORT
84         {
85                 struct utmpx *ut;
86                 unsigned users = 0;
87                 while ((ut = getutxent()) != NULL) {
88                         if ((ut->ut_type == USER_PROCESS) && (ut->ut_user[0] != '\0'))
89                                 users++;
90                 }
91                 printf(",  %u users", users);
92         }
93 #endif
94
95         printf(",  load average: %u.%02u, %u.%02u, %u.%02u\n",
96                         LOAD_INT(info.loads[0]), LOAD_FRAC(info.loads[0]),
97                         LOAD_INT(info.loads[1]), LOAD_FRAC(info.loads[1]),
98                         LOAD_INT(info.loads[2]), LOAD_FRAC(info.loads[2]));
99
100         return EXIT_SUCCESS;
101 }