blob: c1e7af3a5ed8318034b0774d87ac190f8c39eb1f [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 *
Bernhard Reutner-Fischere15d7572006-06-02 20:56:16 +00007 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
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
Bernhard Reutner-Fischere15d7572006-06-02 20:56:16 +000018#include "busybox.h"
Erik Andersen65fc1c72000-03-05 08:16:03 +000019#include <stdio.h>
20#include <time.h>
Eric Andersen10dc9d42000-06-26 10:45:52 +000021#include <errno.h>
Eric Andersened3ef502001-01-27 08:24:39 +000022#include <stdlib.h>
Erik Andersen65fc1c72000-03-05 08:16:03 +000023
Mike Frysinger038b0762005-08-27 18:18:06 +000024#ifndef FSHIFT
25# define FSHIFT 16 /* nr of bits of precision */
26#endif
Erik Andersen65fc1c72000-03-05 08:16:03 +000027#define FIXED_1 (1<<FSHIFT) /* 1.0 as fixed-point */
28#define LOAD_INT(x) ((x) >> FSHIFT)
29#define LOAD_FRAC(x) LOAD_INT(((x) & (FIXED_1-1)) * 100)
30
Eric Andersen10dc9d42000-06-26 10:45:52 +000031
Rob Landleydfba7412006-03-06 20:47:33 +000032int uptime_main(int argc, char **argv)
Erik Andersen65fc1c72000-03-05 08:16:03 +000033{
34 int updays, uphours, upminutes;
35 struct sysinfo info;
36 struct tm *current_time;
37 time_t current_secs;
38
39 time(&current_secs);
40 current_time = localtime(&current_secs);
41
42 sysinfo(&info);
43
Eric Andersenba9b22a2004-04-06 11:10:50 +000044 printf(" %02d:%02d:%02d up ",
45 current_time->tm_hour, current_time->tm_min, current_time->tm_sec);
Erik Andersen65fc1c72000-03-05 08:16:03 +000046 updays = (int) info.uptime / (60*60*24);
47 if (updays)
Matt Kraai12f417e2001-01-18 02:57:08 +000048 printf("%d day%s, ", updays, (updays != 1) ? "s" : "");
Erik Andersen65fc1c72000-03-05 08:16:03 +000049 upminutes = (int) info.uptime / 60;
50 uphours = (upminutes / 60) % 24;
51 upminutes %= 60;
52 if(uphours)
Matt Kraai12f417e2001-01-18 02:57:08 +000053 printf("%2d:%02d, ", uphours, upminutes);
Erik Andersen65fc1c72000-03-05 08:16:03 +000054 else
Matt Kraai12f417e2001-01-18 02:57:08 +000055 printf("%d min, ", upminutes);
Erik Andersen65fc1c72000-03-05 08:16:03 +000056
Eric Andersenc7bda1c2004-03-15 08:29:22 +000057 printf("load average: %ld.%02ld, %ld.%02ld, %ld.%02ld\n",
58 LOAD_INT(info.loads[0]), LOAD_FRAC(info.loads[0]),
59 LOAD_INT(info.loads[1]), LOAD_FRAC(info.loads[1]),
Erik Andersen65fc1c72000-03-05 08:16:03 +000060 LOAD_INT(info.loads[2]), LOAD_FRAC(info.loads[2]));
61
Matt Kraai3e856ce2000-12-01 02:55:13 +000062 return EXIT_SUCCESS;
Erik Andersen65fc1c72000-03-05 08:16:03 +000063}