blob: 7e0735c444eba5c09b6924657c48f02fc9fb2b78 [file] [log] [blame]
Erik Andersen65fc1c72000-03-05 08:16:03 +00001/* vi: set sw=4 ts=4: */
2/*
3 * Mini uptime implementation for busybox
4 *
Eric Andersenc7bda1c2004-03-15 08:29:22 +00005 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
Erik Andersen65fc1c72000-03-05 08:16:03 +00006 *
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 Andersenbdfd0d72001-10-24 05:00:29 +000026 * of CONFIG_FEATURE_UTMP_SUPPORT #define
Erik Andersen65fc1c72000-03-05 08:16:03 +000027 */
28
Mark Whitley827e45c2001-03-09 23:59:51 +000029/* getopt not needed */
30
Erik Andersen65fc1c72000-03-05 08:16:03 +000031
Erik Andersen65fc1c72000-03-05 08:16:03 +000032#include <stdio.h>
33#include <time.h>
Eric Andersen10dc9d42000-06-26 10:45:52 +000034#include <errno.h>
Eric Andersened3ef502001-01-27 08:24:39 +000035#include <stdlib.h>
Eric Andersencbe31da2001-02-20 06:14:08 +000036#include "busybox.h"
Erik Andersen65fc1c72000-03-05 08:16:03 +000037
Mark Whitley59ab0252001-01-23 22:30:04 +000038static const int FSHIFT = 16; /* nr of bits of precision */
Erik Andersen65fc1c72000-03-05 08:16:03 +000039#define FIXED_1 (1<<FSHIFT) /* 1.0 as fixed-point */
40#define LOAD_INT(x) ((x) >> FSHIFT)
41#define LOAD_FRAC(x) LOAD_INT(((x) & (FIXED_1-1)) * 100)
42
Eric Andersen10dc9d42000-06-26 10:45:52 +000043
Erik Andersen65fc1c72000-03-05 08:16:03 +000044extern int uptime_main(int argc, char **argv)
45{
46 int updays, uphours, upminutes;
47 struct sysinfo info;
48 struct tm *current_time;
49 time_t current_secs;
50
51 time(&current_secs);
52 current_time = localtime(&current_secs);
53
54 sysinfo(&info);
55
Eric Andersenc7bda1c2004-03-15 08:29:22 +000056 printf(" %2d:%02d%s up ",
57 current_time->tm_hour%12 ? current_time->tm_hour%12 : 12,
Erik Andersen65fc1c72000-03-05 08:16:03 +000058 current_time->tm_min, current_time->tm_hour > 11 ? "pm" : "am");
59 updays = (int) info.uptime / (60*60*24);
60 if (updays)
Matt Kraai12f417e2001-01-18 02:57:08 +000061 printf("%d day%s, ", updays, (updays != 1) ? "s" : "");
Erik Andersen65fc1c72000-03-05 08:16:03 +000062 upminutes = (int) info.uptime / 60;
63 uphours = (upminutes / 60) % 24;
64 upminutes %= 60;
65 if(uphours)
Matt Kraai12f417e2001-01-18 02:57:08 +000066 printf("%2d:%02d, ", uphours, upminutes);
Erik Andersen65fc1c72000-03-05 08:16:03 +000067 else
Matt Kraai12f417e2001-01-18 02:57:08 +000068 printf("%d min, ", upminutes);
Erik Andersen65fc1c72000-03-05 08:16:03 +000069
Eric Andersenc7bda1c2004-03-15 08:29:22 +000070 printf("load average: %ld.%02ld, %ld.%02ld, %ld.%02ld\n",
71 LOAD_INT(info.loads[0]), LOAD_FRAC(info.loads[0]),
72 LOAD_INT(info.loads[1]), LOAD_FRAC(info.loads[1]),
Erik Andersen65fc1c72000-03-05 08:16:03 +000073 LOAD_INT(info.loads[2]), LOAD_FRAC(info.loads[2]));
74
Matt Kraai3e856ce2000-12-01 02:55:13 +000075 return EXIT_SUCCESS;
Erik Andersen65fc1c72000-03-05 08:16:03 +000076}