1#!/bin/sh -e
2#
3#    uptime: condensed uptime of the machine
4#
5#    Copyright (C) 2009 Raphaël Pinson.
6#    Copyright (C) 2009 Canonical Ltd.
7#    Copyright (C) 2011-2014 Dustin Kirkland
8#
9#    Authors: Raphaël Pinson <raphink@ubuntu.com>
10#             Dustin Kirkland <kirkland@byobu.org>
11#
12#    This program is free software: you can redistribute it and/or modify
13#    it under the terms of the GNU General Public License as published by
14#    the Free Software Foundation, version 3 of the License.
15#
16#    This program is distributed in the hope that it will be useful,
17#    but WITHOUT ANY WARRANTY; without even the implied warranty of
18#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19#    GNU General Public License for more details.
20#
21#    You should have received a copy of the GNU General Public License
22#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
23
24__uptime_detail() {
25	uptime
26}
27
28__uptime() {
29	local u= idle= str=
30	if [ -r /proc/uptime ]; then
31		read u idle < /proc/uptime
32		u=${u%.*}
33	elif [ $(uname) = "FreeBSD" ]; then
34		u=$(sysctl -n kern.boottime | sed -En 's:.*sec = ([[:digit:]]+),.*:\1:p')
35		u=$(($(date +%s) - $u))
36	elif [ -x /usr/sbin/sysctl ]; then
37		# MacOS support
38		u=$(/usr/sbin/sysctl -n kern.boottime | cut -f4 -d' ' | cut -d',' -f1)
39		u=$(($(date +%s) - $u))
40	fi
41	if [ "$u" ]; then
42		if [ "$u" -gt 86400 ]; then
43			str="$(($u / 86400))d$((($u % 86400) / 3600))h"
44		elif [ "$u" -gt 3600 ]; then
45			str="$(($u / 3600))h$((($u % 3600) / 60))m"
46		elif [ "$u" -gt 60 ]; then
47			str="$(($u / 60))m"
48		else
49			str="${u}s"
50		fi
51	else
52		# Last ditch hack
53		str=$(uptime | sed -e "s/.* up *//" -e "s/ *days, */d/" -e "s/:/h/" -e "s/,.*/m/")
54	fi
55	[ -n "$str" ] || return
56	color w b; printf "%s" "${str}"; color --
57}
58
59# vi: syntax=sh ts=4 noexpandtab
60