Erik Andersen | 65fc1c7 | 2000-03-05 08:16:03 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * Mini uptime implementation for busybox |
| 4 | * |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 5 | * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org> |
Erik Andersen | 65fc1c7 | 2000-03-05 08:16:03 +0000 | [diff] [blame] | 6 | * |
Rob Landley | e9a7a62 | 2006-09-22 02:52:41 +0000 | [diff] [blame] | 7 | * Licensed under the GPL version 2, see the file LICENSE in this tarball. |
Erik Andersen | 65fc1c7 | 2000-03-05 08:16:03 +0000 | [diff] [blame] | 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 |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 13 | * of CONFIG_FEATURE_UTMP_SUPPORT #define |
Erik Andersen | 65fc1c7 | 2000-03-05 08:16:03 +0000 | [diff] [blame] | 14 | */ |
| 15 | |
Mark Whitley | 827e45c | 2001-03-09 23:59:51 +0000 | [diff] [blame] | 16 | /* getopt not needed */ |
| 17 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 18 | #include "libbb.h" |
Erik Andersen | 65fc1c7 | 2000-03-05 08:16:03 +0000 | [diff] [blame] | 19 | |
Mike Frysinger | 038b076 | 2005-08-27 18:18:06 +0000 | [diff] [blame] | 20 | #ifndef FSHIFT |
| 21 | # define FSHIFT 16 /* nr of bits of precision */ |
| 22 | #endif |
Erik Andersen | 65fc1c7 | 2000-03-05 08:16:03 +0000 | [diff] [blame] | 23 | #define FIXED_1 (1<<FSHIFT) /* 1.0 as fixed-point */ |
| 24 | #define LOAD_INT(x) ((x) >> FSHIFT) |
| 25 | #define LOAD_FRAC(x) LOAD_INT(((x) & (FIXED_1-1)) * 100) |
| 26 | |
Eric Andersen | 10dc9d4 | 2000-06-26 10:45:52 +0000 | [diff] [blame] | 27 | |
Denis Vlasenko | 06af216 | 2007-02-03 17:28:39 +0000 | [diff] [blame] | 28 | int uptime_main(int argc, char **argv); |
Rob Landley | dfba741 | 2006-03-06 20:47:33 +0000 | [diff] [blame] | 29 | int uptime_main(int argc, char **argv) |
Erik Andersen | 65fc1c7 | 2000-03-05 08:16:03 +0000 | [diff] [blame] | 30 | { |
| 31 | int updays, uphours, upminutes; |
| 32 | struct sysinfo info; |
| 33 | struct tm *current_time; |
| 34 | time_t current_secs; |
| 35 | |
| 36 | time(¤t_secs); |
| 37 | current_time = localtime(¤t_secs); |
| 38 | |
| 39 | sysinfo(&info); |
| 40 | |
Eric Andersen | ba9b22a | 2004-04-06 11:10:50 +0000 | [diff] [blame] | 41 | printf(" %02d:%02d:%02d up ", |
| 42 | current_time->tm_hour, current_time->tm_min, current_time->tm_sec); |
Erik Andersen | 65fc1c7 | 2000-03-05 08:16:03 +0000 | [diff] [blame] | 43 | updays = (int) info.uptime / (60*60*24); |
| 44 | if (updays) |
Matt Kraai | 12f417e | 2001-01-18 02:57:08 +0000 | [diff] [blame] | 45 | printf("%d day%s, ", updays, (updays != 1) ? "s" : ""); |
Erik Andersen | 65fc1c7 | 2000-03-05 08:16:03 +0000 | [diff] [blame] | 46 | upminutes = (int) info.uptime / 60; |
| 47 | uphours = (upminutes / 60) % 24; |
| 48 | upminutes %= 60; |
Denis Vlasenko | 51742f4 | 2007-04-12 00:32:05 +0000 | [diff] [blame] | 49 | if (uphours) |
Matt Kraai | 12f417e | 2001-01-18 02:57:08 +0000 | [diff] [blame] | 50 | printf("%2d:%02d, ", uphours, upminutes); |
Erik Andersen | 65fc1c7 | 2000-03-05 08:16:03 +0000 | [diff] [blame] | 51 | else |
Matt Kraai | 12f417e | 2001-01-18 02:57:08 +0000 | [diff] [blame] | 52 | printf("%d min, ", upminutes); |
Erik Andersen | 65fc1c7 | 2000-03-05 08:16:03 +0000 | [diff] [blame] | 53 | |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 54 | printf("load average: %ld.%02ld, %ld.%02ld, %ld.%02ld\n", |
| 55 | LOAD_INT(info.loads[0]), LOAD_FRAC(info.loads[0]), |
| 56 | LOAD_INT(info.loads[1]), LOAD_FRAC(info.loads[1]), |
Erik Andersen | 65fc1c7 | 2000-03-05 08:16:03 +0000 | [diff] [blame] | 57 | LOAD_INT(info.loads[2]), LOAD_FRAC(info.loads[2])); |
| 58 | |
Matt Kraai | 3e856ce | 2000-12-01 02:55:13 +0000 | [diff] [blame] | 59 | return EXIT_SUCCESS; |
Erik Andersen | 65fc1c7 | 2000-03-05 08:16:03 +0000 | [diff] [blame] | 60 | } |