blob: 6758d959ec81829e3547f65d84902251252da65f [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 Andersen8ec10a92001-01-27 09:33:39 +00005 * Copyright (C) 1999,2000,2001 by Lineo, inc.
Erik Andersen65fc1c72000-03-05 08:16:03 +00006 * 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
Mark Whitleyf1b0c772001-01-23 22:31:10 +000027 * of BB_FEATURE_UTMP_SUPPORT #define
Erik Andersen65fc1c72000-03-05 08:16:03 +000028 */
29
Mark Whitley827e45c2001-03-09 23:59:51 +000030/* getopt not needed */
31
Erik Andersen65fc1c72000-03-05 08:16:03 +000032
Erik Andersen65fc1c72000-03-05 08:16:03 +000033#include <stdio.h>
34#include <time.h>
Eric Andersen10dc9d42000-06-26 10:45:52 +000035#include <errno.h>
Eric Andersened3ef502001-01-27 08:24:39 +000036#include <stdlib.h>
Eric Andersencbe31da2001-02-20 06:14:08 +000037#include "busybox.h"
Erik Andersen65fc1c72000-03-05 08:16:03 +000038
Mark Whitley59ab0252001-01-23 22:30:04 +000039static const int FSHIFT = 16; /* nr of bits of precision */
Erik Andersen65fc1c72000-03-05 08:16:03 +000040#define FIXED_1 (1<<FSHIFT) /* 1.0 as fixed-point */
41#define LOAD_INT(x) ((x) >> FSHIFT)
42#define LOAD_FRAC(x) LOAD_INT(((x) & (FIXED_1-1)) * 100)
43
Eric Andersen10dc9d42000-06-26 10:45:52 +000044
Erik Andersen65fc1c72000-03-05 08:16:03 +000045extern int uptime_main(int argc, char **argv)
46{
47 int updays, uphours, upminutes;
48 struct sysinfo info;
49 struct tm *current_time;
50 time_t current_secs;
51
52 time(&current_secs);
53 current_time = localtime(&current_secs);
54
55 sysinfo(&info);
56
Matt Kraai12f417e2001-01-18 02:57:08 +000057 printf(" %2d:%02d%s up ",
Erik Andersen65fc1c72000-03-05 08:16:03 +000058 current_time->tm_hour%12 ? current_time->tm_hour%12 : 12,
59 current_time->tm_min, current_time->tm_hour > 11 ? "pm" : "am");
60 updays = (int) info.uptime / (60*60*24);
61 if (updays)
Matt Kraai12f417e2001-01-18 02:57:08 +000062 printf("%d day%s, ", updays, (updays != 1) ? "s" : "");
Erik Andersen65fc1c72000-03-05 08:16:03 +000063 upminutes = (int) info.uptime / 60;
64 uphours = (upminutes / 60) % 24;
65 upminutes %= 60;
66 if(uphours)
Matt Kraai12f417e2001-01-18 02:57:08 +000067 printf("%2d:%02d, ", uphours, upminutes);
Erik Andersen65fc1c72000-03-05 08:16:03 +000068 else
Matt Kraai12f417e2001-01-18 02:57:08 +000069 printf("%d min, ", upminutes);
Erik Andersen65fc1c72000-03-05 08:16:03 +000070
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]),
74 LOAD_INT(info.loads[2]), LOAD_FRAC(info.loads[2]));
75
Matt Kraai3e856ce2000-12-01 02:55:13 +000076 return EXIT_SUCCESS;
Erik Andersen65fc1c72000-03-05 08:16:03 +000077}