blob: 7784850ae73c0221b89195ee8963eb60b112931b [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
Mike Frysinger038b0762005-08-27 18:18:06 +000038#ifndef FSHIFT
39# define FSHIFT 16 /* nr of bits of precision */
40#endif
Erik Andersen65fc1c72000-03-05 08:16:03 +000041#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 Andersen10dc9d42000-06-26 10:45:52 +000045
Rob Landleydfba7412006-03-06 20:47:33 +000046int uptime_main(int argc, char **argv)
Erik Andersen65fc1c72000-03-05 08:16:03 +000047{
48 int updays, uphours, upminutes;
49 struct sysinfo info;
50 struct tm *current_time;
51 time_t current_secs;
52
53 time(&current_secs);
54 current_time = localtime(&current_secs);
55
56 sysinfo(&info);
57
Eric Andersenba9b22a2004-04-06 11:10:50 +000058 printf(" %02d:%02d:%02d up ",
59 current_time->tm_hour, current_time->tm_min, current_time->tm_sec);
Erik Andersen65fc1c72000-03-05 08:16:03 +000060 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
Eric Andersenc7bda1c2004-03-15 08:29:22 +000071 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 Andersen65fc1c72000-03-05 08:16:03 +000074 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}