*: work around sysinfo.h versus linux/*.h problems
[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 /* This version of uptime doesn't display the number of users on the system,
11  * since busybox init doesn't mess with utmp.  For folks using utmp that are
12  * just dying to have # of users reported, feel free to write it as some type
13  * of CONFIG_FEATURE_UTMP_SUPPORT #define
14  */
15
16 /* getopt not needed */
17
18 //usage:#define uptime_trivial_usage
19 //usage:       ""
20 //usage:#define uptime_full_usage "\n\n"
21 //usage:       "Display the time since the last boot"
22 //usage:
23 //usage:#define uptime_example_usage
24 //usage:       "$ uptime\n"
25 //usage:       "  1:55pm  up  2:30, load average: 0.09, 0.04, 0.00\n"
26
27 #include "libbb.h"
28 #ifdef __linux__
29 # include <sys/sysinfo.h>
30 #endif
31
32
33 #ifndef FSHIFT
34 # define FSHIFT 16              /* nr of bits of precision */
35 #endif
36 #define FIXED_1         (1<<FSHIFT)     /* 1.0 as fixed-point */
37 #define LOAD_INT(x) ((x) >> FSHIFT)
38 #define LOAD_FRAC(x) LOAD_INT(((x) & (FIXED_1-1)) * 100)
39
40
41 int uptime_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
42 int uptime_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
43 {
44         int updays, uphours, upminutes;
45         struct sysinfo info;
46         struct tm *current_time;
47         time_t current_secs;
48
49         time(&current_secs);
50         current_time = localtime(&current_secs);
51
52         sysinfo(&info);
53
54         printf(" %02d:%02d:%02d up ",
55                         current_time->tm_hour, current_time->tm_min, current_time->tm_sec);
56         updays = (int) info.uptime / (60*60*24);
57         if (updays)
58                 printf("%d day%s, ", updays, (updays != 1) ? "s" : "");
59         upminutes = (int) info.uptime / 60;
60         uphours = (upminutes / 60) % 24;
61         upminutes %= 60;
62         if (uphours)
63                 printf("%2d:%02d, ", uphours, upminutes);
64         else
65                 printf("%d min, ", upminutes);
66
67         printf("load average: %ld.%02ld, %ld.%02ld, %ld.%02ld\n",
68                         LOAD_INT(info.loads[0]), LOAD_FRAC(info.loads[0]),
69                         LOAD_INT(info.loads[1]), LOAD_FRAC(info.loads[1]),
70                         LOAD_INT(info.loads[2]), LOAD_FRAC(info.loads[2]));
71
72         return EXIT_SUCCESS;
73 }