blob: 1a7da46a361769371c04ffc8e8f33d5fd4872abc [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 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02007 * Licensed under GPLv2, see file LICENSE in this source tree.
Erik Andersen65fc1c72000-03-05 08:16:03 +00008 */
9
10/* This version of uptime doesn't display the number of users on the system,
11 * since busybox init doesn't mess with utmp. For folks using utmp that are
12 * just dying to have # of users reported, feel free to write it as some type
Eric Andersenbdfd0d72001-10-24 05:00:29 +000013 * of CONFIG_FEATURE_UTMP_SUPPORT #define
Erik Andersen65fc1c72000-03-05 08:16:03 +000014 */
15
Mark Whitley827e45c2001-03-09 23:59:51 +000016/* getopt not needed */
17
Pere Orga5bc8c002011-04-11 03:29:49 +020018//usage:#define uptime_trivial_usage
19//usage: ""
20//usage:#define uptime_full_usage "\n\n"
21//usage: "Display the time since the last boot"
22//usage:
23//usage:#define uptime_example_usage
24//usage: "$ uptime\n"
25//usage: " 1:55pm up 2:30, load average: 0.09, 0.04, 0.00\n"
26
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000027#include "libbb.h"
Erik Andersen65fc1c72000-03-05 08:16:03 +000028
Mike Frysinger038b0762005-08-27 18:18:06 +000029#ifndef FSHIFT
30# define FSHIFT 16 /* nr of bits of precision */
31#endif
Erik Andersen65fc1c72000-03-05 08:16:03 +000032#define FIXED_1 (1<<FSHIFT) /* 1.0 as fixed-point */
33#define LOAD_INT(x) ((x) >> FSHIFT)
34#define LOAD_FRAC(x) LOAD_INT(((x) & (FIXED_1-1)) * 100)
35
Eric Andersen10dc9d42000-06-26 10:45:52 +000036
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000037int uptime_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000038int uptime_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
Erik Andersen65fc1c72000-03-05 08:16:03 +000039{
40 int updays, uphours, upminutes;
41 struct sysinfo info;
42 struct tm *current_time;
43 time_t current_secs;
44
45 time(&current_secs);
46 current_time = localtime(&current_secs);
47
48 sysinfo(&info);
49
Eric Andersenba9b22a2004-04-06 11:10:50 +000050 printf(" %02d:%02d:%02d up ",
51 current_time->tm_hour, current_time->tm_min, current_time->tm_sec);
Erik Andersen65fc1c72000-03-05 08:16:03 +000052 updays = (int) info.uptime / (60*60*24);
53 if (updays)
Matt Kraai12f417e2001-01-18 02:57:08 +000054 printf("%d day%s, ", updays, (updays != 1) ? "s" : "");
Erik Andersen65fc1c72000-03-05 08:16:03 +000055 upminutes = (int) info.uptime / 60;
56 uphours = (upminutes / 60) % 24;
57 upminutes %= 60;
Denis Vlasenko51742f42007-04-12 00:32:05 +000058 if (uphours)
Matt Kraai12f417e2001-01-18 02:57:08 +000059 printf("%2d:%02d, ", uphours, upminutes);
Erik Andersen65fc1c72000-03-05 08:16:03 +000060 else
Matt Kraai12f417e2001-01-18 02:57:08 +000061 printf("%d min, ", upminutes);
Erik Andersen65fc1c72000-03-05 08:16:03 +000062
Eric Andersenc7bda1c2004-03-15 08:29:22 +000063 printf("load average: %ld.%02ld, %ld.%02ld, %ld.%02ld\n",
64 LOAD_INT(info.loads[0]), LOAD_FRAC(info.loads[0]),
65 LOAD_INT(info.loads[1]), LOAD_FRAC(info.loads[1]),
Erik Andersen65fc1c72000-03-05 08:16:03 +000066 LOAD_INT(info.loads[2]), LOAD_FRAC(info.loads[2]));
67
Matt Kraai3e856ce2000-12-01 02:55:13 +000068 return EXIT_SUCCESS;
Erik Andersen65fc1c72000-03-05 08:16:03 +000069}