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 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | * General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 20 | * |
| 21 | */ |
| 22 | |
| 23 | /* This version of uptime doesn't display the number of users on the system, |
| 24 | * since busybox init doesn't mess with utmp. For folks using utmp that are |
| 25 | * 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] | 26 | * of CONFIG_FEATURE_UTMP_SUPPORT #define |
Erik Andersen | 65fc1c7 | 2000-03-05 08:16:03 +0000 | [diff] [blame] | 27 | */ |
| 28 | |
Mark Whitley | 827e45c | 2001-03-09 23:59:51 +0000 | [diff] [blame] | 29 | /* getopt not needed */ |
| 30 | |
Erik Andersen | 65fc1c7 | 2000-03-05 08:16:03 +0000 | [diff] [blame] | 31 | |
Erik Andersen | 65fc1c7 | 2000-03-05 08:16:03 +0000 | [diff] [blame] | 32 | #include <stdio.h> |
| 33 | #include <time.h> |
Eric Andersen | 10dc9d4 | 2000-06-26 10:45:52 +0000 | [diff] [blame] | 34 | #include <errno.h> |
Eric Andersen | ed3ef50 | 2001-01-27 08:24:39 +0000 | [diff] [blame] | 35 | #include <stdlib.h> |
Eric Andersen | cbe31da | 2001-02-20 06:14:08 +0000 | [diff] [blame] | 36 | #include "busybox.h" |
Erik Andersen | 65fc1c7 | 2000-03-05 08:16:03 +0000 | [diff] [blame] | 37 | |
Mike Frysinger | 038b076 | 2005-08-27 18:18:06 +0000 | [diff] [blame] | 38 | #ifndef FSHIFT |
| 39 | # define FSHIFT 16 /* nr of bits of precision */ |
| 40 | #endif |
Erik Andersen | 65fc1c7 | 2000-03-05 08:16:03 +0000 | [diff] [blame] | 41 | #define FIXED_1 (1<<FSHIFT) /* 1.0 as fixed-point */ |
| 42 | #define LOAD_INT(x) ((x) >> FSHIFT) |
| 43 | #define LOAD_FRAC(x) LOAD_INT(((x) & (FIXED_1-1)) * 100) |
| 44 | |
Eric Andersen | 10dc9d4 | 2000-06-26 10:45:52 +0000 | [diff] [blame] | 45 | |
Rob Landley | dfba741 | 2006-03-06 20:47:33 +0000 | [diff] [blame] | 46 | int uptime_main(int argc, char **argv) |
Erik Andersen | 65fc1c7 | 2000-03-05 08:16:03 +0000 | [diff] [blame] | 47 | { |
| 48 | int updays, uphours, upminutes; |
| 49 | struct sysinfo info; |
| 50 | struct tm *current_time; |
| 51 | time_t current_secs; |
| 52 | |
| 53 | time(¤t_secs); |
| 54 | current_time = localtime(¤t_secs); |
| 55 | |
| 56 | sysinfo(&info); |
| 57 | |
Eric Andersen | ba9b22a | 2004-04-06 11:10:50 +0000 | [diff] [blame] | 58 | printf(" %02d:%02d:%02d up ", |
| 59 | current_time->tm_hour, current_time->tm_min, current_time->tm_sec); |
Erik Andersen | 65fc1c7 | 2000-03-05 08:16:03 +0000 | [diff] [blame] | 60 | updays = (int) info.uptime / (60*60*24); |
| 61 | if (updays) |
Matt Kraai | 12f417e | 2001-01-18 02:57:08 +0000 | [diff] [blame] | 62 | printf("%d day%s, ", updays, (updays != 1) ? "s" : ""); |
Erik Andersen | 65fc1c7 | 2000-03-05 08:16:03 +0000 | [diff] [blame] | 63 | upminutes = (int) info.uptime / 60; |
| 64 | uphours = (upminutes / 60) % 24; |
| 65 | upminutes %= 60; |
| 66 | if(uphours) |
Matt Kraai | 12f417e | 2001-01-18 02:57:08 +0000 | [diff] [blame] | 67 | printf("%2d:%02d, ", uphours, upminutes); |
Erik Andersen | 65fc1c7 | 2000-03-05 08:16:03 +0000 | [diff] [blame] | 68 | else |
Matt Kraai | 12f417e | 2001-01-18 02:57:08 +0000 | [diff] [blame] | 69 | printf("%d min, ", upminutes); |
Erik Andersen | 65fc1c7 | 2000-03-05 08:16:03 +0000 | [diff] [blame] | 70 | |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 71 | printf("load average: %ld.%02ld, %ld.%02ld, %ld.%02ld\n", |
| 72 | LOAD_INT(info.loads[0]), LOAD_FRAC(info.loads[0]), |
| 73 | LOAD_INT(info.loads[1]), LOAD_FRAC(info.loads[1]), |
Erik Andersen | 65fc1c7 | 2000-03-05 08:16:03 +0000 | [diff] [blame] | 74 | LOAD_INT(info.loads[2]), LOAD_FRAC(info.loads[2])); |
| 75 | |
Matt Kraai | 3e856ce | 2000-12-01 02:55:13 +0000 | [diff] [blame] | 76 | return EXIT_SUCCESS; |
Erik Andersen | 65fc1c7 | 2000-03-05 08:16:03 +0000 | [diff] [blame] | 77 | } |