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